dmBridge PHP API
UNLVHighlightMySQLDataStore.php
00001 <?php
00002 #
00003 # UNLVHighlight: a dmBridge module for highlighting objects
00004 #
00005 # Copyright © 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 UNLVHighlightMySQLDataStore extends DMMySQLDataStore
00014 implements UNLVHighlightDataStore {
00015 
00016    private static $instance;
00017 
00023    public static function getHighlightedObjectSQL() {
00024       return "SELECT alias, ptr, datetime FROM highlighted_object
00025          WHERE datetime <= NOW()
00026          ORDER BY datetime DESC
00027          LIMIT 1";
00028    }
00029 
00033    public static function getHighlightedObjectsSQL() {
00034       return "SELECT alias, ptr, datetime FROM highlighted_object
00035          ORDER BY datetime";
00036    }
00037 
00043    public static function getSetupSQL() {
00044       return array(
00045          "CREATE TABLE IF NOT EXISTS highlighted_object (
00046             id int(10) unsigned NOT NULL auto_increment,
00047             alias varchar(30) NOT NULL,
00048             ptr int(10) unsigned NOT NULL,
00049             datetime datetime NOT NULL,
00050             PRIMARY KEY (id)
00051          );"
00052       );
00053    }
00054 
00060    public static function getUpgradeSQL() {
00061       return array(
00062          // currently empty because there is no previous version from which
00063          // to upgrade.
00064       );
00065    }
00066 
00071    public static function getInstance() {
00072       if (!self::$instance instanceof self) {
00073          self::$instance = new self;
00074       }
00075       return self::$instance;
00076    }
00077 
00082    public function addHighlightedObject(DMObject $obj, DMDateTime $datetime) {
00083       $sql = UNLVHighlightGenericDataStore::addHighlightedObjectSQL();
00084       $params = array(
00085          ':alias' => $obj->getCollection()->getAlias(),
00086          ':ptr' => $obj->getPtr(),
00087          ':datetime' => $datetime->asMySQLDateTime()
00088       );
00089       $this->write($sql, $params);
00090    }
00091 
00095    function deleteHighlightedObjectsOnDate(DMDateTime $date) {
00096       $sql = UNLVHighlightGenericDataStore::getDeleteObjectsSQL();
00097       $params = array(
00098          ':datetime' => $date->asMySQLDateTime()
00099       );
00100       $this->write($sql, $params);
00101    }
00102 
00106    public function getHighlightedObject() {
00107       $sql = self::getHighlightedObjectSQL();
00108       $result = $this->read($sql, array());
00109 
00110       if (count($result)) {
00111          $collection = DMCollectionFactory::getCollection(
00112                $result[0]['alias']);
00113          $obj = DMObjectFactory::getObject($collection, $result[0]['ptr']);
00114          $h_obj = new UNLVHighlightedObject(new DMDateTime($result[0]['datetime']));
00115          $obj->addAssociatedModel($h_obj);
00116          return $obj;
00117       }
00118       return null;
00119    }
00120 
00124    public function getHighlightedObjects() {
00125       $sql = self::getHighlightedObjectsSQL();
00126       $result = $this->read($sql, array());
00127 
00128       $objects = array();
00129       foreach ($result as $row) {
00130          try {
00131             $collection = DMCollectionFactory::getCollection($row['alias']);
00132             $obj = DMObjectFactory::getObject($collection, $row['ptr']);
00133 
00134             $h_obj = new UNLVHighlightedObject(new DMDateTime($row['datetime']));
00135             $obj->addAssociatedModel($h_obj);
00136 
00137             $objects[] = $obj;
00138          } catch (DMUnavailableModelException $e) {
00139             // nothing we can do; skip
00140          }
00141       }
00142       return $objects;
00143    }
00144 
00145 }
 All Data Structures Functions Variables