dmBridge PHP API
DMCPCommentController.php
00001 <?php
00002 #
00003 # dmBridge: a data access framework for CONTENTdm(R)
00004 #
00005 # Copyright © 2009, 2010, 2011 Board of Regents of the Nevada System of Higher
00006 # Education, on behalf of the University of Nevada, Las Vegas
00007 #
00008 
00013 class DMCPCommentController extends DMCPAdminController {
00014 
00015    public function configure() {
00016       $this->preFlightCheck();
00017 
00018       $this->setForm(new DMCPCommentSettingsForm());
00019 
00020       if (DMHTTPRequest::getCurrent()->getMethod() == DMHTTPMethod::POST) {
00021          $this->handleForm();
00022       }
00023 
00024       $cq = new DMCommentQuery(DMDataStoreFactory::getDataStore());
00025       $cq->setApproved(-1);
00026       $cq->getSearchResults();
00027       $this->tpl_vars['are_unapproved_comments'] = $cq->getNumResults();
00028       $this->tpl_vars['enable'] = $this->getForm()
00029             ->getFieldByName('enable')->getHTMLTag();
00030       $this->tpl_vars['moderation'] = $this->getForm()
00031             ->getFieldByName('moderation')->getHTMLTag();
00032       $this->tpl_vars['notification'] = $this->getForm()
00033             ->getFieldByName('notification')->getHTMLTag();
00034       $this->tpl_vars['notification_email'] = $this->getForm()
00035             ->getFieldByName('notification_email')->getHTMLTag();
00036 
00037       $this->renderTemplate("/templates/comment/configure.html.php");
00038       die;
00039    }
00040 
00041    public function search() {
00042       $this->preFlightCheck();
00043       $req = DMHTTPRequest::getCurrent();
00044       $ds = DMDataStoreFactory::getDataStore();
00045 
00046       $term = $req->getURI()->getQueryValue("q");
00047       $alias = $req->getURI()->getQueryValue("alias");
00048       $alias = DMCollection::getSanitizedAlias($alias);
00049       $results = array();
00050       $num_results = 0;
00051       if ($term && $alias) {
00052          $pred = new DMQueryPredicate();
00053          $pred->setString($term);
00054          $cq = new DMCommentQuery($ds);
00055          $cq->setApproved(0);
00056          try {
00057             if ($alias != "/dmdefault") {
00058                $col = DMCollectionFactory::getCollection($alias);
00059                $cq->addCollection($col);
00060                $this->tpl_vars['collection'] = $col;
00061             }
00062             $cq->setNumResultsPerPage(1000);
00063             $cq->addPredicate($pred);
00064             $results = $cq->getSearchResults();
00065             $num_results = $cq->getNumResults();
00066          } catch (DMUnavailableModelException $e) {
00067             // invalid collection supplied; abort the search
00068          }
00069       }
00070 
00071       if ($req->getMethod() == DMHTTPMethod::POST) {
00072          switch (substr($req->getRepresentation()->getFormValue("action"), 0, 6)) {
00073          case "Update":
00074             $flash = new DMFlash(
00075                   DMLocalizedString::getString("NO_COMMENT_SELECTED"), false);
00076             $rep = $req->getRepresentation();
00077             if (is_array($rep->getFormValue("id"))) {
00078                $comments = array();
00079                foreach ($rep->getFormValue("id") as $id) {
00080                   if ($id <> round($id) || $id < 0 || $id > 1000000) {
00081                      continue;
00082                   }
00083                   $comments[] = $ds->loadComment((int) $id);
00084                }
00085                $this->approveAndUpdateComments($comments);
00086                $flash = new DMFlash(
00087                   DMLocalizedString::getString("COMMENT_UPDATED"), true);
00088             }
00089             $req->getSession()->setFlash($flash);
00090             DMHTTPRequest::reload();
00091             die;
00092          case "Delete":
00093             $flash = new DMFlash(
00094                   DMLocalizedString::getString("NO_COMMENT_SELECTED"), false);
00095             $rep = $req->getRepresentation();
00096             if (is_array($rep->getFormValue("id"))) {
00097                foreach ($rep->getFormValue("id") as $id) {
00098                   if ($id <> round($id) || $id < 0 || $id > 1000000) {
00099                      continue;
00100                   }
00101                   $comment = $ds->loadComment((int) $id);
00102                   DMDataStoreFactory::getDataStore()->deleteComment($comment);
00103                }
00104                $flash = new DMFlash(
00105                   DMLocalizedString::getString("COMMENT_DELETED"), true);
00106             }
00107             $req->getSession()->setFlash($flash);
00108             DMHTTPRequest::reload();
00109             die;
00110          }
00111       }
00112 
00113       $this->tpl_vars['term'] = DMString::websafe(substr($term, 0, 50));
00114       $this->tpl_vars['results'] = $results;
00115       $this->tpl_vars['num_results'] = $num_results;
00116 
00117       $this->renderTemplate("/templates/comment/search.html.php");
00118       die;
00119    }
00120 
00121    public function moderate() {
00122       $this->preFlightCheck();
00123 
00124       if (DMHTTPRequest::getCurrent()->getMethod() == DMHTTPMethod::POST) {
00125          $rep = DMHTTPRequest::getCurrent()->getRepresentation();
00126          if (is_array($rep->getFormValue("id"))) {
00127             $comments = array();
00128             $ds = DMDataStoreFactory::getDataStore();
00129             foreach ($rep->getFormValue("id") as $id) {
00130                $comments[] = $ds->loadComment((int) $id);
00131             }
00132             try {
00133                switch (strtolower(
00134                      substr($rep->getFormValue("moderate"), 0, 6))) {
00135                case 'approv':
00136                   $this->approveAndUpdateComments($comments);
00137                   break;
00138                case 'reject':
00139                   foreach ($comments as $c) {
00140                      $ds->deleteComment($c);
00141                   }
00142                   break;
00143                }
00144             } catch (DMDataStoreException $e) {
00145                DMHTTPRequest::getCurrent()->getSession()->setFlash(
00146                      new DMFlash($e->getMessage(), false));
00147             }
00148          } else {
00149             DMHTTPRequest::getCurrent()->getSession()->setFlash(new DMFlash(
00150                DMLocalizedString::getString("NO_COMMENT_SELECTED"), false));
00151          }
00152       }
00153 
00154       try {
00155          $cq = new DMCommentQuery(DMDataStoreFactory::getDataStore());
00156          $cq->setApproved(-1);
00157          $cq->setSortFields(array("posted_at" => "desc"));
00158          $this->tpl_vars['comments'] = $cq->getSearchResults();
00159       } catch (DMException $e) {
00160          $this->tpl_vars['comments'] = array();
00161       }
00162 
00163       $this->renderTemplate("/templates/comment/moderate.html.php");
00164       die;
00165    }
00166 
00167    public function statistics() {
00168       $this->preFlightCheck();
00169 
00170       $this->tpl_vars['collections'] = DMCollection::getAuthorized();
00171       $ds = DMDataStoreFactory::getDataStore();
00172       $cq = new DMCommentQuery($ds);
00173       $cq->setApproved(1);
00174       $cq->getSearchResults();
00175       $this->tpl_vars['most_commented_objects']
00176             = DMDataStoreFactory::getDataStore()
00177             ->getObjectsWithMostComments(50);
00178       $this->tpl_vars['num_comments'] = $cq->getNumResults();
00179       $this->tpl_vars['num_objects_with_approved_comments']
00180             = DMDataStoreFactory::getDataStore()
00181             ->getNumObjectsWithApprovedComments();
00182 
00183       $this->renderTemplate("/templates/comment/statistics.html.php");
00184       die;
00185    }
00186 
00187    private function approveAndUpdateComments(array $comments) {
00188       foreach ($comments as $c) {
00189          if (!$c instanceof DMComment) {
00190             continue;
00191          }
00192          $rep = DMHTTPRequest::getCurrent()->getRepresentation();
00193          $updated_authors = $rep->getFormValue("author");
00194          $updated_emails = $rep->getFormValue("email");
00195          $updated_values = $rep->getFormValue("body");
00196          $c->setApproved(true);
00197          $c->setName($updated_authors[$c->getID()]);
00198          $c->setEmail($updated_emails[$c->getID()]);
00199          $c->setValue($updated_values[$c->getID()]);
00200          DMDataStoreFactory::getDataStore()->saveComment($c);
00201       }
00202       DMHTTPRequest::getCurrent()->getSession()->setFlash(new DMFlash(
00203             DMLocalizedString::getString("COMMENT_APPROVED"), true));
00204    }
00205 
00206 }
 All Data Structures Functions Variables