dmBridge PHP API
DMTEFavoriteController.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 
00015 class DMTEFavoriteController extends DMAbstractController {
00016 
00017    public function create() {
00018       $rep = $this->getHTTPRequest()->getRepresentation();
00019       try {
00020          if (is_array($rep->getFormValue("data"))) {
00021             $this->createMultiple();
00022          } else {
00023             $this->createSingle();
00024          }
00025          $this->getSession()->setFlash(new DMFlash(
00026                DMLocalizedString::getString("OBJ_ADDED_TO_FAVORITES"),
00027                true));
00028       } catch (DMException $e) {
00029          header("HTTP/1.1 400 Bad Request");
00030          $this->getSession()->setFlash(new DMFlash(
00031                DMLocalizedString::getString("NONEXISTENT_OBJECT"),
00032                false));
00033       }
00034 /* @todo Get rid of this?
00035       $params = ($rep->getFormValue("params"))
00036             ? $rep->getFormValue("params") : "";
00037       DMHTTPRequest::redirectToParams($params);
00038       die; */
00039    }
00040 
00041    private function createSingle() {
00042       $rep = $this->getHTTPRequest()->getRepresentation();
00043       $obj = DMObjectFactory::getObject(
00044          DMCollectionFactory::getCollection(
00045                DMCollection::getSanitizedAlias(
00046                      $rep->getFormValue("alias"))),
00047             (int) $rep->getFormValue("ptr"));
00048       $this->getSession()->addFavorite($obj);
00049    }
00050 
00051    private function createMultiple() {
00052       $rep = $this->getHTTPRequest()->getRepresentation();
00053       foreach ($rep->getFormValue("data") as $data) {
00054          $tmp = explode(DMAbstractForm::ALIAS_PTR_SEPARATOR, $data);
00055          $alias = DMCollection::getSanitizedAlias($tmp[0]);
00056          $ptr = (int) $tmp[1];
00057          $obj = DMObjectFactory::getObject(
00058             DMCollectionFactory::getCollection($alias), $ptr);
00059          $this->getSession()->addFavorite($obj);
00060       }
00061    }
00062 
00063    public function delete() {
00064       $rep = $this->getHTTPRequest()->getRepresentation();
00065       $data = $rep->getFormValue("data");
00066       $objects = array();
00067       for ($i = 0; $i < count($data); $i++) {
00068          $tmp = explode('|', $data[$i]);
00069          $objects[] = DMObjectFactory::getObject(
00070                   DMCollectionFactory::getCollection(
00071                         DMCollection::getSanitizedAlias($tmp[0])),
00072                   (int) $tmp[1]);
00073       }
00074       $this->getSession()->deleteFavorites($objects);
00075 
00076       $this->getSession()->setFlash(new DMFlash(
00077                DMLocalizedString::getString("OBJ_REMOVED_FROM_FAVORITES"),
00078                true));
00079 /* @todo Get rid of this?
00080       $params = ($rep->getFormValue("params"))
00081             ? $rep->getFormValue("params") : "";
00082       DMHTTPRequest::redirectToParams($params);
00083       die; */
00084    }
00085 
00086    public function index($alias) {
00087       if ($this->getHTTPRequest()->getMethod() == DMHTTPMethod::POST) {
00088          $rep = $this->getHTTPRequest()->getRepresentation();
00089          if ($rep->getFormValue("method") == "DELETE") {
00090             $this->delete();
00091          } else {
00092             $this->create();
00093          }
00094       }
00095       $this->listAll($alias);
00096    }
00097 
00098    private function listAll($alias) {
00099       $this->getSession()->unsetQuery();
00100 
00101       $alias = DMCollection::getSanitizedAlias($alias);
00102       if (strlen($alias) < 2) {
00103          $alias = "/dmdefault";
00104       }
00105       $collection = DMCollectionFactory::getCollection($alias);
00106       if (!$collection) {
00107          throw new DMUnavailableModelException(
00108                DMLocalizedString::getString("NONEXISTENT_COLLECTION"));
00109       }
00110 
00111       // restrict favorites from unauthorized collections from appearing
00112       $objects = array();
00113       foreach ($this->getSession()->getAccessibleFavorites($this->getTemplateSet()) as $obj) {
00114          $objects[] = $obj;
00115       }
00116 
00117       $path = "/templates/favorite/index.html.php";
00118       $tpl = $this->getTemplateSet()->getTemplateAtPathname($path);
00119       global $view;
00120       $view = new DMFavoritesView($tpl, $this->getSession(),
00121             $collection,
00122             new DMFavoriteQuery($this->getSession(), $this->getTemplateSet()));
00123 
00124       // store results view in session to facilitate "back to results" link
00125       $this->getSession()->setResultsView($view);
00126    }
00127 
00128 }
00129 
 All Data Structures Functions Variables