dmBridge PHP API
DMCPTagController.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 DMCPTagController extends DMCPAdminController {
00014 
00015    public function configure() {
00016       $this->preFlightCheck();
00017 
00018       $form = new DMCPTagSettingsForm();
00019       $this->setForm($form);
00020 
00021       if (DMHTTPRequest::getCurrent()->getMethod() == DMHTTPMethod::POST) {
00022          $this->handleForm();
00023       }
00024 
00025       $ds = DMDataStoreFactory::getDataStore();
00026       $tq = new DMTagQuery($ds);
00027       $tq->setApproved(-1);
00028       $tq->getSearchResultsAsObjects();
00029 
00030       $this->tpl_vars['enable']
00031             = $form->getFieldByName("enable")->getHTMLTag();
00032       $this->tpl_vars['moderation']
00033             = $form->getFieldByName("moderation")->getHTMLTag();
00034       $this->tpl_vars['num_unapproved_tags'] = $tq->getNumResults();
00035 
00036       $this->renderTemplate("/templates/tag/edit.html.php");
00037       die;
00038    }
00039 
00040    public function moderate() {
00041       $this->preFlightCheck();
00042 
00043       if (DMHTTPRequest::getCurrent()->getMethod() == DMHTTPMethod::POST) {
00044          $rep = DMHTTPRequest::getCurrent()->getRepresentation();
00045          if (is_array($rep->getFormValue("id"))) {
00046             $tags = array();
00047             foreach ($rep->getFormValue("id") as $id) {
00048                $tags[] = DMDataStoreFactory::getDataStore()->loadTag($id);
00049             }
00050             try {
00051                switch (strtolower(
00052                      substr($rep->getFormValue("moderate"), 0, 6))) {
00053                case "approv":
00054                   $this->approveTags($tags);
00055                   break;
00056                case "reject":
00057                   $this->deleteTags($tags);
00058                   break;
00059                }
00060             } catch (DMDataStoreException $e) {
00061                DMHTTPRequest::getCurrent()->getSession()->setFlash(
00062                      new DMFlash($e->getMessage(), false));
00063                return;
00064             }
00065             DMHTTPRequest::reload();
00066             die;
00067          } else {
00068             DMHTTPRequest::getCurrent()->getSession()->setFlash(
00069                   new DMFlash(
00070                         DMLocalizedString::getString("NO_TAG_SELECTED"),
00071                         false));
00072          }
00073       }
00074 
00075       $this->tpl_vars['moderation_is_enabled']
00076             = DMConfigXML::getInstance()->isTagModerationEnabled();
00077       try {
00078          $tq = new DMTagQuery(DMDataStoreFactory::getDataStore());
00079          $tq->setApproved(-1);
00080          $this->tpl_vars['unapproved_tags'] = $tq->getSearchResultsAsObjects();
00081       } catch (DMException $e) {
00082          $this->tpl_vars['unapproved_tags'] = array();
00083       }
00084 
00085       $this->renderTemplate("/templates/tag/moderate.html.php");
00086       die;
00087    }
00088 
00089    public function statistics() {
00090       $this->preFlightCheck();
00091 
00092       $this->tpl_vars['all_collections'] = DMCollection::getAuthorized();
00093       $this->tpl_vars['most_tagged'] = DMDataStoreFactory::getDataStore()
00094             ->getObjectsWithMostTags(20);
00095       $this->tpl_vars['num_with_approved_tags']
00096             = DMDataStoreFactory::getDataStore()
00097             ->getNumObjectsWithApprovedTags();
00098 
00099       $this->renderTemplate("/templates/tag/statistics.html.php");
00100       die;
00101    }
00102 
00103    public function update() {
00104       $this->preFlightCheck();
00105       $this->index();
00106       $this->handleForm();
00107    }
00108 
00109    private function approveTag(DMTag $t) {
00110       $t->setApproved(true);
00111       $result = DMDataStoreFactory::getDataStore()->saveTag($t);
00112       if ($result) {
00113          DMHTTPRequest::getCurrent()->getSession()->setFlash(
00114                new DMFlash(DMLocalizedString::getString("TAG_APPROVED"),
00115                      true));
00116       }
00117    }
00118 
00119    private function approveTags() {
00120       $rep = DMHTTPRequest::getCurrent()->getRepresentation();
00121       if (!is_array($rep->getFormValue("id"))) {
00122          return false;
00123       }
00124       foreach ($rep->getFormValue("id") as $id) {
00125          if ($id <> round($id) or $id < 0) {
00126             continue;
00127          }
00128          $t = DMDataStoreFactory::getDataStore()->loadTag((int) $id);
00129          $this->approveTag($t);
00130       }
00131    }
00132 
00133    private function deleteTag(DMTag $t) {
00134       $result = DMDataStoreFactory::getDataStore()->deleteTag($t);
00135       if ($result) {
00136          DMHTTPRequest::getCurrent()->getSession()->setFlash(new DMFlash(
00137                DMLocalizedString::getString("TAG_DELETED"), true));
00138       }
00139    }
00140 
00141    private function deleteTags() {
00142       $rep = DMHTTPRequest::getCurrent()->getRepresentation();
00143       if (!is_array($rep->getFormValue("id"))) {
00144          return false;
00145       }
00146       foreach ($rep->getFormValue("id") as $id) {
00147          if ($id <> round($id) or $id < 0) {
00148             continue;
00149          }
00150          $t = DMDataStoreFactory::getDataStore()->loadTag((int) $id);
00151          $this->deleteTag($t);
00152       }
00153    }
00154 
00155    public function index() {
00156       $this->preFlightCheck();
00157 
00158       $ds = DMDataStoreFactory::getDataStore();
00159 
00160       if (DMHTTPRequest::getCurrent()->getMethod() == DMHTTPMethod::POST) {
00161          $flash = new DMFlash(
00162                DMLocalizedString::getString("NO_TAG_SELECTED"), false);
00163          $rep = DMHTTPRequest::getCurrent()->getRepresentation();
00164          if (is_array($rep->getFormValue("values"))) {
00165             foreach ($rep->getFormValue("values") as $value) {
00166                DMDataStoreFactory::getDataStore()->deleteAllTagsWithValue($value);
00167             }
00168             $flash = new DMFlash(
00169                DMLocalizedString::getString("TAG_DELETED"), true);
00170          }
00171          DMHTTPRequest::getCurrent()->getSession()->setFlash($flash);
00172          DMHTTPRequest::reload();
00173          die;
00174       }
00175 
00176       $tq = new DMTagQuery($ds);
00177       $tq->setSortByFrequency(true);
00178       // @todo add pagination
00179       $tq->setNumResultsPerPage(100000);
00180       $this->tpl_vars['tags'] = $tq->getSearchResultsAsCounts();
00181       $this->tpl_vars['num_approved_tags']
00182             = $tq->getNumResults();
00183 
00184       $this->renderTemplate("/templates/tag/index.html.php");
00185       die;
00186    }
00187 
00188 }
 All Data Structures Functions Variables