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          natcasesort(self::$authorized);
00070       }
00071       return self::$authorized;
00072    }
00073 
00078    public static final function exists($alias) {
00079       foreach (dmGetCollectionList() as $c) {
00080          if ($c['alias'] == $alias) {
00081             return true;
00082          }
00083       }
00084       return false;
00085    }
00086 
00090    public static function getSanitizedAlias($alias) {
00091       return "/" . DMString::paranoid(trim($alias, "/"), array("-", "_"));
00092    }
00093 
00102    public function __construct($alias) {
00103       $this->setAlias($alias);
00104       $this->load();
00105    }
00106 
00113    public function __toString() {
00114       return $this->getAlias();
00115    }
00116 
00121    public function equals($obj) {
00122       return (is_object($obj) && $obj instanceof DMCollection
00123             && $this->getAlias() == $obj->getAlias());
00124    }
00125 
00130    public function getAlias() {
00131       return $this->alias;
00132    }
00133 
00139    private function setAlias($alias) {
00140       // @todo externalize this validation
00141       if (strlen($alias) < 1) {
00142          throw new DMUnavailableModelException(
00143             DMLocalizedString::getString("NO_ALIAS_SUPPLIED"));
00144       }
00145       else if (strlen($alias) < 2 or strlen($alias) > 30) {
00146          throw new DMUnavailableModelException(
00147             DMLocalizedString::getString("INVALID_ALIAS"));
00148       }
00149       $this->alias = $alias;
00150    }
00151 
00155    public function hasCustomObjectViewPrefs() {
00156       return $this->has_custom_object_view_prefs;
00157    }
00158 
00162    public function setHasCustomObjectViewPrefs($bool) {
00163       $this->has_custom_object_view_prefs = (bool) $bool;
00164    }
00165 
00169    public function hasCustomResultsViewPrefs() {
00170       return $this->has_custom_results_view_prefs;
00171    }
00172 
00176    public function setHasCustomResultsViewPrefs($bool) {
00177       $this->has_custom_results_view_prefs = (bool) $bool;
00178    }
00179 
00183    public function hasCustomSearchViewPrefs() {
00184       return $this->has_custom_search_view_prefs;
00185    }
00186 
00190    public function setHasCustomSearchViewPrefs($bool) {
00191       $this->has_custom_search_view_prefs = (bool) $bool;
00192    }
00193 
00197    public function getDateSearchBeginYear() {
00198       return $this->date_search_begin_year;
00199    }
00200 
00204    public function getDateSearchEndYear() {
00205       return $this->date_search_end_year;
00206    }
00207 
00214    public function setDateSearchRange($begin_year, $end_year) {
00215       if ($begin_year > $end_year) {
00216          throw new DMIllegalArgumentException(
00217                DMLocalizedString::getString("INVALID_DATE_RANGE"));
00218       }
00219       $this->date_search_begin_year = $begin_year;
00220       $this->date_search_end_year = $end_year;
00221    }
00222 
00226    public function unsetDateSearchRange() {
00227       $this->date_search_begin_year = null;
00228       $this->date_search_end_year = null;
00229    }
00230 
00238    public function getDateSearchField() {
00239       foreach ($this->getFields() as $f) {
00240          if ($f->getType() == "DATE") {
00241             return $f;
00242          }
00243       }
00244    }
00245 
00249    public function getDescription() {
00250       return $this->description;
00251    }
00252 
00256    public function setDescription($desc) {
00257       $this->description = (string) $desc;
00258    }
00259 
00264    public function isDefault() {
00265       return ($this->getAlias() == "/dmdefault");
00266    }
00267 
00271    public function addFacet(DMFacetTerm $term) {
00272       $this->facets[] = $term;
00273    }
00274 
00278    public function getFacets() {
00279       return $this->facets;
00280    }
00281 
00285    public function unsetFacets() {
00286       $this->facets = array();
00287    }
00288 
00292    public function addField(DMDCElement $element) {
00293       $this->fields[] = $element;
00294    }
00295 
00305    public function getField($nick) {
00306       if (count($this->getFields()) < 1) {
00307          $this->instantiateFields();
00308       }
00309       foreach ($this->getFields() as $field) {
00310          if ($field->getNick() == $nick) {
00311             return $field;
00312          }
00313       }
00314       return null;
00315    }
00316 
00327    public function getFields() {
00328       return $this->fields;
00329    }
00330 
00334    public function unsetFields() {
00335       $this->fields = array();
00336    }
00337 
00341    public function addGridViewField(DMDCElement $field) {
00342       $this->grid_view_fields[] = $field;
00343    }
00344 
00348    public function getGridViewFields() {
00349       return $this->grid_view_fields;
00350    }
00351 
00355    public function unsetGridViewFields() {
00356       $this->grid_view_fields = array();
00357    }
00358 
00365    public function getImage512URI() {
00366       return $this->image_uri_512;
00367    }
00368 
00374    public function setImage512URI(DMURI $uri) {
00375       $this->image_uri_512 = $uri;
00376    }
00377 
00378    private function load() {
00379       if ($this->has_been_loaded) {
00380          return;
00381       }
00382       if ($this->getAlias() == "/dmdefault") {
00383          foreach (DMDCElement::getAll() as $f) {
00384             $this->addField($f);
00385          }
00386       } else {
00387          $cdm_defs = dmGetCollectionFieldInfo($this->getAlias());
00388          if (!is_array($cdm_defs)) {
00389             throw new DMUnavailableModelException(
00390                DMLocalizedString::getString("INSTANTIATE_NONEXISTENT_COLLECTION"));
00391          }
00392          foreach ($cdm_defs as $a) {
00393             $f = new DMDCElement($a['nick']);
00394             $f->setName($a['name']);
00395             $f->setControlled(($a['vocab'] > 0));
00396             $f->setCollection($this);
00397             $f->setSearchable(($a['search'] > 0));
00398             $f->setHidden(($a['hide'] > 0));
00399             $f->setDCNick($a['dc']);
00400             $f->setType($a['type']);
00401             $this->fields[] = $f;
00402          }
00403       }
00404       DMConfigXML::getInstance()->loadCollection($this);
00405       $this->has_been_loaded = true;
00406    }
00407 
00419    public function getName() {
00420       if (empty($this->name)) {
00421          foreach (dmGetCollectionList() as $c) {
00422             if ($c['alias'] == $this->getAlias()) {
00423                $this->name = $c['name'];
00424             }
00425          }
00426          if (empty($this->name)) {
00427             $this->name = DMLocalizedString::getString(
00428                   "DEFAULT_COLLECTION_NAME");
00429          }
00430       }
00431       return $this->name;
00432    }
00433 
00438    public function getNumObjects() {
00439       $qt = new DMQueryPredicate();
00440       $qt->setMode("any");
00441 
00442       $query = new DMObjectQuery();
00443       $query->setPredicates(array($qt));
00444       $query->setCollections(array($this));
00445       $query->setPage(1);
00446       $query->setNumResultsPerPage(1);
00447 
00448       $query->getSearchResults();
00449       return $query->getNumResults();
00450    }
00451 
00455    public function getOverviewURI() {
00456       return $this->overview_uri;
00457    }
00458 
00462    public function setOverviewURI(DMURI $uri) {
00463       $this->overview_uri = $uri;
00464    }
00465 
00470    public function isRedirectingReferenceURLs() {
00471       return $this->is_redirecting_reference_urls;
00472    }
00473 
00477    public function setRedirectingReferenceURLs($bool) {
00478       $this->is_redirecting_reference_urls = ($bool);
00479    }
00480 
00489    public function getResultsViewFields() {
00490       return $this->grid_view_fields;
00491    }
00492 
00497    public function getTemplateSet() {
00498       if ($this->template_set_id) {
00499          return new DMTemplateSet($this->template_set_id);
00500       }
00501       return null;
00502    }
00503 
00507    public function setTemplateSetID($id) {
00508       $this->setUsingDefaultTemplateSet(false);
00509       $this->template_set_id = $id;
00510    }
00511 
00515    public function getURI() {
00516       return DMInternalURI::getResourceURI(
00517             DMBridgeComponent::HTTPAPI,
00518             "collections" . $this->getAlias());
00519    }
00520 
00524    public function isUsingDefaultTemplateSet() {
00525       return $this->template_set_is_default;
00526    }
00527 
00531    public function setUsingDefaultTemplateSet($bool) {
00532       if ($bool) {
00533          $this->template_set_id = null;
00534       }
00535       $this->template_set_is_default = ($bool);
00536    }
00537 
00541    public function addViewerDefinition(DMObjectViewerDefinition $vd) {
00542       $this->viewer_defs[] = $vd;
00543    }
00544 
00545 
00550    public function getViewerDefinitionForMediaType(DMMediaType $type) {
00551       foreach ($this->viewer_defs as $def) {
00552          if ($def->getMediaType()->equals($type)) {
00553             return $def;
00554          }
00555       }
00556       return null;
00557    }
00558 
00562    public function getViewerDefinitions() {
00563       return $this->viewer_defs;
00564    }
00565 
00569    public function unsetViewerDefinitions() {
00570       $this->viewer_defs = array();
00571    }
00572 
00573 }
00574 
 All Data Structures Functions Variables