dmBridge PHP API
DMSession.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 
00017 final class DMSession {
00018 
00020    private $favorites = array();
00021 
00022    public function __construct() {
00023       $this->populateFavorites();
00024    }
00025 
00032    public function addFavorite(DMObject $obj) {
00033       // check for duplicates
00034       foreach ($this->favorites as $fav) {
00035          if ($fav->equals($obj)) {
00036             return;
00037          }
00038       }
00039 
00040       if (!array_key_exists('BUF', $_COOKIE)) {
00041          $_COOKIE['BUF'] = "";
00042       }
00043       $buf = &$_COOKIE['BUF'];
00044       $objcookie = sprintf("%s<%d>",
00045             $obj->getCollection()->getAlias(),
00046             $obj->getPtr());
00047       if (strpos($buf, $objcookie) === false) {
00048          $buf .= $objcookie;
00049          if (!headers_sent()) {
00050             setcookie('BUF', $buf, null, '/');
00051          }
00052       }
00053       $this->favorites[] = $obj;
00054    }
00055 
00064    public function getAccessibleFavorites(DMTemplateSet $template_set) {
00065       $objects = array();
00066       foreach ($this->getAllFavorites() as $obj) {
00067          if ($template_set
00068                ->isAuthorizedToViewCollection($obj->getCollection())) {
00069             $objects[] = $obj;
00070          }
00071       }
00072       return $objects;
00073    }
00074 
00082    public function getAllFavorites() {
00083       return $this->favorites;
00084    }
00085 
00094    public function deleteFavorites(array $objects) {
00095       if (array_key_exists("BUF", $_COOKIE)) {
00096          $buf = $_COOKIE['BUF'];
00097          foreach ($objects as &$obj) {
00098             // delete it from the cookie
00099             $buf = str_replace(
00100                sprintf("%s<%d>",
00101                      $obj->getCollection()->getAlias(),
00102                      $obj->getPtr()),
00103                   "", $buf);
00104             // delete it from the instance
00105             for ($i = 0; $i < count($this->favorites); $i++) {
00106                if ($obj->equals($this->favorites[$i])) {
00107                   unset($this->favorites[$i]);
00108                   break;
00109                }
00110             }
00111             // delete it from the passed-in array
00112             $obj = null;
00113          }
00114          if (!headers_sent()) {
00115             setcookie("BUF", $buf, null, "/");
00116          }
00117       }
00118    }
00119 
00123    private function populateFavorites() {
00124       if (count($this->getAllFavorites()) < 1) {
00125          if (array_key_exists('BUF', $_COOKIE)) {
00126             $pairs = explode('>', $_COOKIE['BUF']);
00127             array_pop($pairs);
00128             foreach ($pairs as $pair) {
00129                $tmp = explode('<', $pair);
00130                $this->favorites[] = DMObjectFactory::getObject(
00131                      DMCollectionFactory::getCollection(
00132                            DMCollection::getSanitizedAlias($tmp[0])),
00133                      $tmp[1]);
00134             }
00135          }
00136       }
00137    }
00138 
00150    public function getFlash() {
00151       if (array_key_exists('flash', $_SESSION)) {
00152          return ($_SESSION['flash'] instanceof DMFlash)
00153             ? $_SESSION['flash'] : null;
00154       }
00155    }
00156 
00164    public function setFlash(DMFlash $flash) {
00165       $_SESSION['flash'] = $flash;
00166    }
00167 
00176    public function unsetFlash() {
00177       $_SESSION['flash'] = null;
00178    }
00179 
00186    public function hasCommentedOnObject(DMObject $obj) {
00187       if (!array_key_exists('has_commented', $_SESSION)) {
00188          return false;
00189       }
00190       if (!is_array($_SESSION['has_commented'])) {
00191          return false;
00192       }
00193       foreach ($_SESSION['has_commented'] as $arr) {
00194          if ($arr['alias'] == $obj->getCollection()->getAlias()
00195                && $arr['ptr'] == $obj->getPtr()) {
00196             return true;
00197          }
00198       }
00199       return false;
00200    }
00201 
00208    public function addHasCommented(DMObject $obj) {
00209       if (self::hasCommentedOnObject($obj)) {
00210          return;
00211       }
00212       if (!array_key_exists('has_commented', $_SESSION)
00213             || !is_array($_SESSION['has_commented'])) {
00214          $_SESSION['has_commented'] = array();
00215       }
00216       // if $obj is a child object, add all siblings as well as the parent
00217       if ($obj->isChild()) {
00218          $objects = $obj->getParent()->getChildren();
00219          $objects[] = $obj->getParent();
00220       } else if ($obj->isCompound()) {
00221          // if it's a compound object, add all children
00222          $objects = $obj->getChildren();
00223          $objects[] = $obj;
00224       } else { // it's a lonely DMObject
00225          $objects = array($obj);
00226       }
00227       foreach ($objects as $o) {
00228          $_SESSION['has_commented'][] = array(
00229             'alias' => $o->getCollection()->getAlias(),
00230             'ptr' => $o->getPtr()
00231          );
00232       }
00233    }
00234 
00240    public function hasRated() {
00241       return (array_key_exists("has_rated", $_SESSION)
00242             && is_array($_SESSION['has_rated']))
00243             ? $_SESSION['has_rated'] : array();
00244    }
00245 
00251    public function hasRatedObject(DMObject $obj) {
00252       if (array_key_exists('has_rated', $_SESSION)) {
00253          if (is_array($_SESSION['has_rated'])) {
00254             foreach ($_SESSION['has_rated'] as $arr) {
00255                if ($arr['alias'] == $obj->getCollection()->getAlias()
00256                && $arr['ptr'] == $obj->getPtr()) {
00257                   return true;
00258                }
00259             }
00260          }
00261       }
00262       return false;
00263    }
00264 
00270    public function addHasRated(DMObject $obj) {
00271       if (!self::hasRatedObject($obj)) {
00272          if (!array_key_exists("has_rated", $_SESSION)
00273          || !is_array($_SESSION['has_rated'])) {
00274             $_SESSION['has_rated'] = array();
00275          }
00276          $_SESSION['has_rated'][] = array(
00277             'alias' => $obj->getCollection()->getAlias(),
00278             'ptr' => $obj->getPtr()
00279          );
00280       }
00281    }
00282 
00287    public function getQuery() {
00288       return (array_key_exists('query', $_SESSION)
00289             && $_SESSION['query'] instanceof DMQuery)
00290             ? $_SESSION['query'] : null;
00291    }
00292 
00298    public function setQuery(DMQuery $q) {
00299       $_SESSION['query'] = $q;
00300    }
00301 
00306    public function unsetQuery() {
00307       $_SESSION['query'] = null;
00308    }
00309 
00317    public function addRecentlyViewedObject(DMObject $obj) {
00318       $k = "recently_viewed_objects";
00319       if (!array_key_exists($k, $_SESSION) || !is_array($_SESSION[$k])) {
00320          $_SESSION[$k] = array();
00321       }
00322       if (count($_SESSION[$k]) > 0
00323             && $obj->equals($_SESSION[$k][count($_SESSION[$k]) - 1])) {
00324          return;
00325       }
00326       $_SESSION[$k][] = $obj;
00327    }
00328 
00335    public function getRecentlyViewedObjects($limit) {
00336       $k = "recently_viewed_objects";
00337       $objects = array();
00338       if (array_key_exists($k, $_SESSION) && is_array($_SESSION[$k])) {
00339          foreach ($_SESSION[$k] as $obj) {
00340             if (count($objects) >= $limit) {
00341                break;
00342             }
00343             foreach ($objects as $obj2) {
00344                if ($obj2->equals($obj)) {
00345                   continue(2);
00346                }
00347             }
00348             $objects[] = $obj;
00349          }
00350       }
00351       return $objects;
00352    }
00353 
00358    public function getResultsView() {
00359       return array_key_exists("results_view", $_SESSION)
00360          ? $_SESSION['results_view'] : null;
00361    }
00362 
00368    public function setResultsView(DMResultsView $view) {
00369       $_SESSION['results_view'] = $view;
00370    }
00371 
00376    public function unsetResultsView() {
00377       $_SESSION['results_view'] = null;
00378    }
00379 
00384    public function getSearchInput() {
00385       return (array_key_exists("search_input", $_SESSION))
00386          ? $_SESSION['search_input'] : null;
00387    }
00388 
00394    public function setSearchInput(DMInput $input) {
00395       $_SESSION['search_input'] = $input;
00396    }
00397 
00402    public function unsetSearchInput() {
00403       $_SESSION['search_input'] = null;
00404    }
00405 
00410    public function getUser() {
00411       return (array_key_exists("user", $_SESSION))
00412             ? $_SESSION['user'] : null;
00413    }
00414 
00419    public function setUser(DMUser $user) {
00420       $_SESSION['user'] = $user;
00421    }
00422 
00426    public function unsetUser() {
00427       $_SESSION['user'] = null;
00428    }
00429 
00430 }
00431 
 All Data Structures Functions Variables