dmBridge PHP API
DMCollection.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 DMCollection extends DMModel implements DMURIAddressable {
00016 
00018    private static $authorized = array();
00019 
00021    private $alias;
00023    private $date_search_begin_year;
00025    private $date_search_end_year;
00027    private $description;
00029    private $facets = array();
00031    private $fields = array();
00033    private $grid_view_fields = array();
00035    private $has_been_loaded = false;
00037    private $has_custom_object_view_prefs = false;
00039    private $has_custom_results_view_prefs = false;
00041    private $has_custom_search_view_prefs = false;
00043    private $image_uri_512;
00045    private $is_redirecting_reference_urls;
00047    private $name;
00049    private $template_set_is_default = false;
00051    private $overview_uri;
00053    private $template_set_id;
00055    private $viewer_defs = array();
00056 
00064    public static function getAuthorized() {
00065       if (count(self::$authorized) < 1) {
00066          foreach (dmGetCollectionList() as $c) {
00067             self::$authorized[] = DMCollectionFactory::getCollection($c['alias']);
00068          }
00069          usort(self::$authorized,
00070                array("DMCollection", "compareCollectionNames"));
00071       }
00072       return self::$authorized;
00073    }
00074 
00078    private static function compareCollectionNames(DMCollection $a,
00079          DMCollection $b) {
00080       return strcasecmp($a->getName(), $b->getName());
00081    }
00082 
00087    public static final function exists($alias) {
00088       foreach (dmGetCollectionList() as $c) {
00089          if ($c['alias'] == $alias) {
00090             return true;
00091          }
00092       }
00093       return false;
00094    }
00095 
00099    public static function getSanitizedAlias($alias) {
00100       return "/" . DMString::paranoid(trim($alias, "/"), array("-", "_"));
00101    }
00102 
00111    public function __construct($alias) {
00112       $this->setAlias($alias);
00113       $this->load();
00114    }
00115 
00122    public function __toString() {
00123       return $this->getAlias();
00124    }
00125 
00130    public function equals($obj) {
00131       return (is_object($obj) && $obj instanceof DMCollection
00132             && $this->getAlias() == $obj->getAlias());
00133    }
00134 
00139    public function getAlias() {
00140       return $this->alias;
00141    }
00142 
00148    private function setAlias($alias) {
00149       // @todo externalize this validation
00150       if (strlen($alias) < 1) {
00151          throw new DMUnavailableModelException(
00152             DMLocalizedString::getString("NO_ALIAS_SUPPLIED"));
00153       }
00154       else if (strlen($alias) < 2 or strlen($alias) > 30) {
00155          throw new DMUnavailableModelException(
00156             DMLocalizedString::getString("INVALID_ALIAS"));
00157       }
00158       $this->alias = $alias;
00159    }
00160 
00164    public function hasCustomObjectViewPrefs() {
00165       return $this->has_custom_object_view_prefs;
00166    }
00167 
00171    public function setHasCustomObjectViewPrefs($bool) {
00172       $this->has_custom_object_view_prefs = (bool) $bool;
00173    }
00174 
00178    public function hasCustomResultsViewPrefs() {
00179       return $this->has_custom_results_view_prefs;
00180    }
00181 
00185    public function setHasCustomResultsViewPrefs($bool) {
00186       $this->has_custom_results_view_prefs = (bool) $bool;
00187    }
00188 
00192    public function hasCustomSearchViewPrefs() {
00193       return $this->has_custom_search_view_prefs;
00194    }
00195 
00199    public function setHasCustomSearchViewPrefs($bool) {
00200       $this->has_custom_search_view_prefs = (bool) $bool;
00201    }
00202 
00206    public function getDateSearchBeginYear() {
00207       return $this->date_search_begin_year;
00208    }
00209 
00213    public function getDateSearchEndYear() {
00214       return $this->date_search_end_year;
00215    }
00216 
00223    public function setDateSearchRange($begin_year, $end_year) {
00224       if ($begin_year > $end_year) {
00225          throw new DMIllegalArgumentException(
00226                DMLocalizedString::getString("INVALID_DATE_RANGE"));
00227       }
00228       $this->date_search_begin_year = $begin_year;
00229       $this->date_search_end_year = $end_year;
00230    }
00231 
00235    public function unsetDateSearchRange() {
00236       $this->date_search_begin_year = null;
00237       $this->date_search_end_year = null;
00238    }
00239 
00247    public function getDateSearchField() {
00248       foreach ($this->getFields() as $f) {
00249          if ($f->getType() == "DATE") {
00250             return $f;
00251          }
00252       }
00253    }
00254 
00258    public function getDescription() {
00259       return $this->description;
00260    }
00261 
00265    public function setDescription($desc) {
00266       $this->description = (string) $desc;
00267    }
00268 
00273    public function isDefault() {
00274       return ($this->getAlias() == "/dmdefault");
00275    }
00276 
00280    public function addFacet(DMFacetTerm $term) {
00281       $this->facets[] = $term;
00282    }
00283 
00287    public function getFacets() {
00288       return $this->facets;
00289    }
00290 
00294    public function unsetFacets() {
00295       $this->facets = array();
00296    }
00297 
00301    public function addField(DMDCElement $element) {
00302       $this->fields[] = $element;
00303    }
00304 
00314    public function getField($nick) {
00315       if (count($this->getFields()) < 1) {
00316          $this->instantiateFields();
00317       }
00318       foreach ($this->getFields() as $field) {
00319          if ($field->getNick() == $nick) {
00320             return $field;
00321          }
00322       }
00323       return null;
00324    }
00325 
00336    public function getFields() {
00337       return $this->fields;
00338    }
00339 
00343    public function unsetFields() {
00344       $this->fields = array();
00345    }
00346 
00350    public function addGridViewField(DMDCElement $field) {
00351       $this->grid_view_fields[] = $field;
00352    }
00353 
00357    public function getGridViewFields() {
00358       return $this->grid_view_fields;
00359    }
00360 
00364    public function unsetGridViewFields() {
00365       $this->grid_view_fields = array();
00366    }
00367 
00374    public function getImage512URI() {
00375       return $this->image_uri_512;
00376    }
00377 
00383    public function setImage512URI(DMURI $uri) {
00384       $this->image_uri_512 = $uri;
00385    }
00386 
00387    private function load() {
00388       if ($this->has_been_loaded) {
00389          return;
00390       }
00391       if ($this->getAlias() == "/dmdefault") {
00392          foreach (DMDCElement::getAll() as $f) {
00393             $this->addField($f);
00394          }
00395       } else {
00396          $cdm_defs = dmGetCollectionFieldInfo($this->getAlias());
00397          if (!is_array($cdm_defs)) {
00398             throw new DMUnavailableModelException(
00399                DMLocalizedString::getString("INSTANTIATE_NONEXISTENT_COLLECTION"));
00400          }
00401          foreach ($cdm_defs as $a) {
00402             $f = new DMDCElement($a['nick']);
00403             $f->setName($a['name']);
00404             $f->setControlled(($a['vocab'] > 0));
00405             $f->setCollection($this);
00406             $f->setSearchable(($a['search'] > 0));
00407             $f->setHidden(($a['hide'] > 0));
00408             $f->setDCNick($a['dc']);
00409             $f->setType($a['type']);
00410             $this->fields[] = $f;
00411          }
00412       }
00413       DMConfigXML::getInstance()->loadCollection($this);
00414       $this->has_been_loaded = true;
00415    }
00416 
00428    public function getName() {
00429       if (empty($this->name)) {
00430          foreach (dmGetCollectionList() as $c) {
00431             if ($c['alias'] == $this->getAlias()) {
00432                $this->name = $c['name'];
00433             }
00434          }
00435          if (empty($this->name)) {
00436             $this->name = DMLocalizedString::getString(
00437                   "DEFAULT_COLLECTION_NAME");
00438          }
00439       }
00440       return $this->name;
00441    }
00442 
00447    public function getNumObjects() {
00448       $aliases = array($this->getAlias());
00449       $terms = array();
00450       $fields = array('title');
00451       $sort_fields = array();
00452       $limit = 1;
00453       $start = 1;
00454       $num_results = 0;
00455       dmQuery($aliases, $terms, $fields, $sort_fields, $limit,
00456          $start, $num_results, 1);
00457       return $num_results;
00458    }
00459 
00463    public function getOverviewURI() {
00464       return $this->overview_uri;
00465    }
00466 
00470    public function setOverviewURI(DMURI $uri) {
00471       $this->overview_uri = $uri;
00472    }
00473 
00478    public function isRedirectingReferenceURLs() {
00479       return $this->is_redirecting_reference_urls;
00480    }
00481 
00485    public function setRedirectingReferenceURLs($bool) {
00486       $this->is_redirecting_reference_urls = ($bool);
00487    }
00488 
00497    public function getResultsViewFields() {
00498       return $this->grid_view_fields;
00499    }
00500 
00505    public function getTemplateSet() {
00506       if ($this->template_set_id) {
00507          return new DMTemplateSet($this->template_set_id);
00508       }
00509       return null;
00510    }
00511 
00515    public function setTemplateSetID($id) {
00516       $this->setUsingDefaultTemplateSet(false);
00517       $this->template_set_id = $id;
00518    }
00519 
00523    public function getURI() {
00524       return DMInternalURI::getResourceURI(
00525             DMBridgeComponent::HTTPAPI,
00526             "collections" . $this->getAlias());
00527    }
00528 
00532    public function isUsingDefaultTemplateSet() {
00533       return $this->template_set_is_default;
00534    }
00535 
00539    public function setUsingDefaultTemplateSet($bool) {
00540       if ($bool) {
00541          $this->template_set_id = null;
00542       }
00543       $this->template_set_is_default = ($bool);
00544    }
00545 
00549    public function addViewerDefinition(DMObjectViewerDefinition $vd) {
00550       $this->viewer_defs[] = $vd;
00551    }
00552 
00553 
00558    public function getViewerDefinitionForMediaType(DMMediaType $type) {
00559       foreach ($this->viewer_defs as $def) {
00560          if ($def->getMediaType()->equals($type)) {
00561             return $def;
00562          }
00563       }
00564       return null;
00565    }
00566 
00570    public function getViewerDefinitions() {
00571       return $this->viewer_defs;
00572    }
00573 
00577    public function unsetViewerDefinitions() {
00578       $this->viewer_defs = array();
00579    }
00580 
00581 }
00582 
 All Data Structures Functions Variables