dmBridge PHP API
DMConfigXML.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 final class DMConfigXML {
00016 
00020    private static $instances = array();
00021 
00023    private $config_xml_pathname;
00025    private $data_dir;
00027    private $dxml;
00028 
00032    private static function getDefaultXmlPathname() {
00033       return dirname(__FILE__) . "/../../data/config.xml";
00034    }
00035 
00040    public static function getInstance($xml_file_pathname = null) {
00041       if (!$xml_file_pathname) {
00042          $xml_file_pathname = self::getDefaultXmlPathname();
00043       }
00044       $xml_file_pathname = realpath($xml_file_pathname);
00045       if (!array_key_exists($xml_file_pathname, self::$instances)) {
00046          self::$instances[$xml_file_pathname] = new DMConfigXML(
00047                $xml_file_pathname);
00048       }
00049       return self::$instances[$xml_file_pathname];
00050    }
00051 
00056    public static function destroyInstance(DMConfigXML $instance) {
00057       foreach (self::$instances as $pathname => $inst) {
00058          if ($inst == $instance) {
00059             unset(self::$instances[$pathname]);
00060             break;
00061          }
00062       }
00063    }
00064 
00068    protected function __construct($xml_file_pathname) {
00069       $this->config_xml_pathname = $xml_file_pathname;
00070    }
00071 
00072    public function __clone() {
00073       //trigger_error("Cannot clone a Singleton");
00074    }
00075 
00076    public function __wakeup() {
00077       //trigger_error("Cannot deserialize a Singleton");
00078    }
00079 
00084    public function getAllTemplateSets() {
00085       $sets = array();
00086       $xp = new DOMXPath($this->getDomDocument());
00087       foreach ($xp->query('templates/set/@id') as $node) {
00088          $sets[] = new DMTemplateSet($node->nodeValue);
00089       }
00090       sort($sets);
00091       return $sets;
00092    }
00093 
00100    public function loadCollection(DMCollection $col) {
00101       // overview URI
00102       $uri = $this->getCollectionPropertyValue($col, "/overviewURI");
00103       if ($uri) {
00104          $uri = new DMURI($uri);
00105          $col->setOverviewURI($uri);
00106       }
00107 
00108       // image URI
00109       $uri = $this->getCollectionPropertyValue($col, "/image512URI");
00110       if ($uri) {
00111          $uri = new DMURI($uri);
00112          $col->setImage512URI($uri);
00113       }
00114 
00115       // description
00116       $desc = $this->getCollectionPropertyValue($col, '/description');
00117       $col->setDescription($desc);
00118 
00119       // grid view
00120       $result = $this->getCollectionPropertyValues($col, "gridView/field",
00121             false);
00122       $col->setHasCustomResultsViewPrefs($result);
00123 
00124       $result = $this->getCollectionPropertyValues($col, "gridView/field");
00125       foreach ($result as $node) {
00126          $f = $col->getField($node->getAttribute('nick'));
00127          if (!$f instanceof DMDCElement) {
00128             $f = new DMDCElement($node->getAttribute('nick'));
00129          }
00130          $col->addGridViewField($f);
00131       }
00132 
00133       // facets
00134       $result = $this->getCollectionPropertyValues($col,
00135             "/resultsView/facets/field", false);
00136       $col->setHasCustomResultsViewPrefs($result);
00137 
00138       $result = $this->getCollectionPropertyValues($col,
00139             "/resultsView/facets/field");
00140       foreach ($result as $node) {
00141          $field = new DMDCElement($node->getAttribute('nick'));
00142          foreach ($col->getFields() as $f) {
00143             if ($f->getNick() == $node->getAttribute('nick')) {
00144                $field->setName($f->getName());
00145             }
00146          }
00147          $col->addFacet(new DMFacetTerm($field));
00148       }
00149 
00150       // search view
00151       $result1 = $this->getCollectionPropertyValue(
00152             $col, "/searchView/dateSearch/beginYear", false);
00153       $result2 = $this->getCollectionPropertyValue(
00154             $col, "/searchView/dateSearch/endYear", false);
00155       $col->setHasCustomSearchViewPrefs($result1 || $result2);
00156 
00157       $col->setDateSearchRange(
00158          $this->getCollectionPropertyValue(
00159                $col, "/searchView/dateSearch/beginYear"),
00160          $this->getCollectionPropertyValue(
00161                $col, "/searchView/dateSearch/endYear"));
00162 
00163       // reference url redirection
00164       $col->setRedirectingReferenceURLs(
00165          (bool) $this
00166             ->getCollectionPropertyValue($col, "/redirectReferenceURLs"));
00167 
00168       // field preferences
00169       $result = $this->getCollectionPropertyValues($col,
00170             "/resultsView/fields/field", false);
00171       $col->setHasCustomResultsViewPrefs($result);
00172 
00173       $result = $this->getCollectionPropertyValues($col,
00174             "/resultsView/fields/field");
00175       foreach ($result as $xf) {
00176          foreach ($col->getFields() as $cf) {
00177             $cf->setCollection($col);
00178             if ($xf->getAttribute('nick') == $cf->getNick()) {
00179                $cf->setSortable($xf->getAttribute('isSortable'));
00180                $cf->setDefaultSort($xf->getAttribute('isDefaultSort'));
00181                $cf->setMaxWords((int) $xf->getAttribute('maxWords'));
00182             }
00183          }
00184       }
00185 
00186       // template set
00187       switch ($this->getCollectionPropertyValue($col,
00188             "/templateSets/templateSet/@use", false)) {
00189       case "id":
00190          $col->setTemplateSetID(
00191             $this->getCollectionPropertyValue($col,
00192                   "/templateSets/templateSet/@id"));
00193          break;
00194       default:
00195          $col->setUsingDefaultTemplateSet(true);
00196          break;
00197       }
00198 
00199       // viewer definitions
00200       $result = $this->getCollectionPropertyValues($col, "/objectView/media",
00201             false);
00202       $col->setHasCustomObjectViewPrefs($result);
00203       
00204       $result = $this->getCollectionPropertyValues($col, "/objectView/media");
00205       foreach ($result as $node) {
00206          $vd = new DMObjectViewerDefinition(
00207                DMMediaType::getTypeForString($node->getAttribute("type")));
00208          $vd->setClass(
00209             $node->getElementsByTagName("class")->item(0)->nodeValue);
00210          $filesize = $node->getElementsByTagName("maxFileSizeK");
00211          $vd->setMaxFileSize(
00212             $filesize->length ? $filesize->item(0)->nodeValue * 1000 : null);
00213          $vd->setWidth(
00214             $node->getElementsByTagName("maxWidth")->item(0)->nodeValue);
00215          $vd->setHeight(
00216             $node->getElementsByTagName("maxHeight")->item(0)->nodeValue);
00217          $col->addViewerDefinition($vd);
00218       }
00219    }
00220 
00226    public function saveCollection(DMCollection $col) {
00227       // delete old collection node if it exists
00228       $xpath = sprintf("collections/collection[@alias = '%s']",
00229          $col->getAlias());
00230       $this->removeNode($xpath);
00231 
00232       // get collections node
00233       $parent_node = $this->getNode("collections");
00234       $col_node = $this->getDomDocument()->createElement("collection");
00235       $col_node->setAttribute("alias", $col->getAlias());
00236       $parent_node->appendChild($col_node);
00237 
00238       // overview URI
00239       if ($col->getAlias() != "/dmdefault") {
00240          $node = $this->getDomDocument()->createElement("overviewURI",
00241             DMString::xmlentities($col->getOverviewURI()));
00242          $col_node->appendChild($node);
00243       }
00244 
00245       // image URI
00246       if ($col->getAlias() != "/dmdefault") {
00247          $node = $this->getDomDocument()->createElement("image512URI",
00248             DMString::xmlentities($col->getImage512URI()));
00249          $col_node->appendChild($node);
00250       }
00251 
00252       // description
00253       if ($col->getAlias() != "/dmdefault") {
00254          $node = $this->getDomDocument()->createElement("description",
00255             DMString::xmlentities($col->getDescription()));
00256          $col_node->appendChild($node);
00257       }
00258 
00259       // redirect reference URLs
00260       if ($col->getAlias() != "/dmdefault") {
00261          $node = $this->getDomDocument()->createElement("redirectReferenceURLs",
00262             (int) $col->isRedirectingReferenceURLs());
00263          $col_node->appendChild($node);
00264       }
00265 
00266       // search view
00267       if ($col->getDateSearchBeginYear() || $col->getDateSearchEndYear()) {
00268          $sv_node = $this->getDomDocument()->createElement("searchView");
00269          $ds_node = $this->getDomDocument()->createElement("dateSearch");
00270          $sv_node->appendChild($ds_node);
00271          $col_node->appendChild($sv_node);
00272 
00273          // date search begin year
00274          $year_node = $this->getDomDocument()->createElement("beginYear",
00275             $col->getDateSearchBeginYear());
00276          $ds_node->appendChild($year_node);
00277 
00278          // date search end year
00279          $year_node = $this->getDomDocument()->createElement("endYear",
00280             $col->getDateSearchEndYear());
00281          $ds_node->appendChild($year_node);
00282       }
00283 
00284       // grid view
00285       if (count($col->getGridViewFields())) {
00286          $gv_node = $this->getDomDocument()->createElement("gridView");
00287          $col_node->appendChild($gv_node);
00288          foreach ($col->getGridViewFields() as $f) {
00289             if ($f instanceof DMDCElement) {
00290                if (strlen($f->getNick()) < 1) {
00291                   continue;
00292                }
00293                $fnode = $this->getDomDocument()->createElement("field");
00294                $fnode->setAttribute("nick", $f->getNick());
00295                $gv_node->appendChild($fnode);
00296             }
00297          }
00298       }
00299 
00300       // template sets
00301       $ts_node = $this->getDomDocument()->createElement("templateSets");
00302       $col_node->appendChild($ts_node);
00303 
00304       $d_node = $this->getDomDocument()->createElement("templateSet");
00305       $ts_node->appendChild($d_node);
00306       if ($col->getTemplateSet() instanceof DMTemplateSet
00307             && $col->getTemplateSet()->getID()) {
00308          $d_node->setAttribute("use", "id");
00309          $d_node->setAttribute("id", $col->getTemplateSet()->getID());
00310       } else {
00311          $d_node->setAttribute("use", "default");
00312       }
00313 
00314       // results view
00315       $rv_node = $this->getDomDocument()->createElement("resultsView");
00316       $col_node->appendChild($rv_node);
00317 
00318       // fields
00319       $fi_node = $this->getDomDocument()->createElement("fields");
00320       $rv_node->appendChild($fi_node);
00321       $added_fields = 0;
00322       foreach ($col->getFields() as $f) {
00323          if (strlen($f->getNick()) < 1) {
00324             continue;
00325          }
00326          if ($f->getNick() == "index" || $f->getNick() == "thumb") {
00327             continue;
00328          }
00329          // only save fields that are sortable, default sort, and/or have max
00330          // words
00331          if (!$f->isSortable() && !$f->isDefaultSort() && !$f->getMaxWords()) {
00332             continue;
00333          }
00334          $fnode = $this->getDomDocument()->createElement("field");
00335          $fnode->setAttribute("nick", $f->getNick());
00336          $fnode->setAttribute("isSortable", (int) $f->isSortable());
00337          $fnode->setAttribute("isDefaultSort", (int) $f->isDefaultSort());
00338          $fnode->setAttribute("maxWords", (int) $f->getMaxWords());
00339          $fi_node->appendChild($fnode);
00340          $added_fields++;
00341       }
00342       if ($added_fields) {
00343          $rv_node->appendChild($fi_node);
00344       }
00345 
00346       // facets
00347       $fa_node = $this->getDomDocument()->createElement("facets");
00348       $rv_node->appendChild($fa_node);
00349       $added_fields = 0;
00350       foreach ($col->getFacets() as $f) {
00351          $fnode = $this->getDomDocument()->createElement("field");
00352          $fnode->setAttribute("nick", $f->getField()->getNick());
00353          $fa_node->appendChild($fnode);
00354          $added_fields++;
00355       }
00356       if ($added_fields) {
00357          $rv_node->appendChild($fa_node);
00358       }
00359 
00360       // viewer definitions
00361       $added_fields = 0;
00362       $ov_node = $this->getDomDocument()->createElement("objectView");
00363       foreach ($col->getViewerDefinitions() as $d) {
00364          $media_node = $this->getDomDocument()->createElement("media");
00365          $media_node->setAttribute("type", $d->getMediaType());
00366          $class_node = $this->getDomDocument()->createElement("class",
00367             $d->getClass());
00368          $max_size_node = $this->getDomDocument()->createElement("maxFileSizeK",
00369             $d->getMaxFileSize() / 1000);
00370          $width_node = $this->getDomDocument()->createElement("maxWidth",
00371             $d->getWidth());
00372          $height_node = $this->getDomDocument()->createElement("maxHeight",
00373             $d->getHeight());
00374          $media_node->appendChild($class_node);
00375          $media_node->appendChild($max_size_node);
00376          $media_node->appendChild($width_node);
00377          $media_node->appendChild($height_node);
00378          $ov_node->appendChild($media_node);
00379          $added_fields++;
00380       }
00381       if ($added_fields) {
00382          $col_node->appendChild($ov_node);
00383       }
00384 
00385       $this->save();
00386    }
00387 
00392    public function isCommentingEnabled() {
00393       return (bool) $this->getNodeValue("comments/@enabled");
00394    }
00395 
00400    public function setCommentingEnabled($bool) {
00401       $this->setNodeValue('comments/@enabled', (int) $bool);
00402    }
00403 
00408    public function isCommentModerationEnabled() {
00409       return (bool) $this->getNodeValue("comments/moderation/@enabled");
00410    }
00411 
00415    public function setCommentModerationEnabled($bool) {
00416       $this->setNodeValue('comments/moderation/@enabled', (int) $bool);
00417    }
00418 
00423    public function getCommentNotificationEmail() {
00424       return $this->getNodeValue("comments/email");
00425    }
00426 
00430    public function setCommentNotificationEmail($str) {
00431       $this->setNodeValue('comments/email', $str);
00432    }
00433 
00438    public function isCommentNotificationEnabled() {
00439       return (bool) $this->getNodeValue("comments/email/@enabled");
00440    }
00441 
00445    public function setCommentNotificationEnabled($bool) {
00446       $this->setNodeValue("comments/email/@enabled", (int) $bool);
00447    }
00448 
00454    public function getDataDir() {
00455       $root = DMConfigIni::getInstance()->getString("dmbridge.data_path");
00456       if (!$this->data_dir) {
00457          if ($root) {
00458             $this->data_dir = str_replace("\\", "/", $root);
00459          } else {
00460             $this->data_dir = realpath(dirname(__FILE__) . "/../../data");
00461          }
00462       }
00463       return $this->data_dir;
00464    }
00465 
00470    public function getFeedCopyright() {
00471       return $this->getNodeValue("feed/copyright");
00472    }
00473 
00477    public function setFeedCopyright($str) {
00478       $this->setNodeValue('feed/copyright', $str);
00479    }
00480 
00485    public function getFeedLanguage() {
00486       return $this->getNodeValue("feed/language");
00487    }
00488 
00492    public function setFeedLanguage($str) {
00493       $this->setNodeValue('feed/language', $str);
00494    }
00495 
00500    public function getFeedManagingEditorEmail() {
00501       return $this->getNodeValue("feed/managingEditor/email");
00502    }
00503 
00507    public function setFeedManagingEditorEmail($str) {
00508       $this->setNodeValue('feed/managingEditor/email', $str);
00509    }
00510 
00515    public function getFeedManagingEditorName() {
00516       return $this->getNodeValue("feed/managingEditor/name");
00517    }
00518 
00522    public function setFeedManagingEditorName($str) {
00523       $this->setNodeValue('feed/managingEditor/name', $str);
00524    }
00525 
00530    public function getFeedTitle() {
00531       return $this->getNodeValue("feed/title");
00532    }
00533 
00537    public function setFeedTitle($str) {
00538       $this->setNodeValue("feed/title", $str);
00539    }
00540 
00545    public function getFeedSubtitle() {
00546       return $this->getNodeValue("feed/subtitle");
00547    }
00548 
00552    public function setFeedSubtitle($str) {
00553       $this->setNodeValue("feed/subtitle", $str);
00554    }
00555 
00560    public function getFeedWebMasterEmail() {
00561       return $this->getNodeValue("feed/webMaster/email");
00562    }
00563 
00567    public function setFeedWebMasterEmail($str) {
00568       $this->setNodeValue('feed/webMaster/email', $str);
00569    }
00570 
00575    public function getFeedWebMasterName() {
00576       return $this->getNodeValue("feed/webMaster/name");
00577    }
00578 
00582    public function setFeedWebMasterName($str) {
00583       $this->setNodeValue('feed/webMaster/name', $str);
00584    }
00585 
00589    public function getFullPath() {
00590       return $this->config_xml_pathname;
00591    }
00592 
00597    public function getInstitutionName() {
00598       return $this->getNodeValue("institution/name");
00599    }
00600 
00605    public function setInstitutionName($str) {
00606       $this->setNodeValue("institution/name", $str);
00607    }
00608 
00613    public function isLoggingEnabled() {
00614       return (bool) $this->getNodeValue("logging/@enabled");
00615    }
00616 
00617    private function getNextAvailableTemplateSetID() {
00618       $id = 1;
00619       $xp = new DOMXPath($this->getDomDocument());
00620       foreach ($xp->query('//templates/set/@id') as $c) {
00621          $id = ((int) $c->nodeValue > $id) ? (int) $c->nodeValue : $id;
00622       }
00623       return $id + 1;
00624    }
00625 
00631    public function isModuleEnabled($module) {
00632       if ($module instanceof DMBridgeModule) {
00633          $module = $module->getName();
00634       }
00635       $xpath = sprintf("//modules/module[name = '%s']/@enabled", $module);
00636       return (bool) $this->getNodeValue($xpath);
00637    }
00638 
00648    public function isModuleOfSameNameInstalled(DMBridgeModule $module) {
00649       $xpath = sprintf("//modules/module[name = '%s']",
00650             $module->getName());
00651       return ($this->getNodeList($xpath)->length);
00652    }
00653 
00663    public function isModuleOfSameNameAndVersionInstalled(
00664          DMBridgeModule $module) {
00665       $xpath = sprintf("//modules/module[name = '%s' and version = '%s']",
00666             $module->getName(), $module->getVersion());
00667       return ($this->getNodeList($xpath)->length);
00668    }
00669 
00675    public function setModuleEnabled(DMBridgeModule $module, $enabled) {
00676       // delete old module node if it exists
00677       $xpath = sprintf("modules/module[name = '%s' and version = '%s']",
00678          $module->getName(), $module->getVersion());
00679       $this->removeNode($xpath);
00680 
00681       // create new module node
00682       $module_node = $this->getDomDocument()->createElement("module");
00683       $module_node->setAttribute("enabled", (int) $enabled);
00684       $name_node = $this->getDomDocument()->createElement("name",
00685             $module->getName());
00686       $version_node = $this->getDomDocument()->createElement("version",
00687             $module->getVersion());
00688 
00689       $module_node->appendChild($name_node);
00690       $module_node->appendChild($version_node);
00691       $this->getDomDocument()->getElementsByTagName("modules")->item(0)
00692             ->appendChild($module_node);
00693    }
00694 
00699    public function isRatingEnabled() {
00700       return (bool) $this->getNodeValue("rating/@enabled");
00701    }
00702 
00707    public function setRatingEnabled($bool) {
00708       $this->setNodeValue("rating/@enabled", (int) $bool);
00709    }
00710 
00715    public function isTaggingEnabled() {
00716       return (bool) $this->getNodeValue("tagging/@enabled");
00717    }
00718 
00723    public function setTaggingEnabled($bool) {
00724       $this->setNodeValue("tagging/@enabled", (int) $bool);
00725    }
00726 
00731    public function isTagModerationEnabled() {
00732       return (bool) $this->getNodeValue("tagging/@moderated");
00733    }
00734 
00738    public function setTagModerationEnabled($bool) {
00739       $this->setNodeValue("tagging/@moderated", (int) $bool);
00740    }
00741 
00747    public function deleteTemplateSet(DMTemplateSet $set) {
00748       // first check to make sure no collections are using it.
00749       $normal_xpath = "//collection/templateSets/templateSet/@id";
00750       $dxp = new DOMXPath($this->getDomDocument());
00751 
00752       $result = $dxp->query($normal_xpath);
00753       $in_use = false;
00754       foreach ($result as $node) {
00755          if ($node->value == $set->getID()) {
00756             $in_use = true;
00757          }
00758       }
00759 
00760       if ($in_use) {
00761          throw new DMConstraintException(
00762                DMLocalizedString::getString("TPL_IN_USE"));
00763       }
00764 
00765       // delete template node
00766       if ($set->getID()) {
00767          $xpath = sprintf("templates/set[@id = '%d']", $set->getID());
00768          $this->removeNode($xpath);
00769       }
00770       $this->save();
00771    }
00772 
00781    public function loadTemplateSet(DMTemplateSet $set) {
00782       $xpath = sprintf("//templates/set[@id = '%d']", $set->getID());
00783       $result = $this->getNode($xpath);
00784       if (!$result instanceof DOMNode) {
00785          throw new DMUnavailableModelException(
00786             DMLocalizedString::getString("INVALID_TPL_SET"));
00787       }
00788       $set->setName($result->getAttribute("name"));
00789       $alias = null;
00790       if ($result->getElementsByTagName("set")->item(0) instanceof DOMNode) {
00791          $alias = $result->getElementsByTagName("set")->item(0)
00792                ->getAttribute("default");
00793       }
00794 
00795       $xpath = sprintf("//templates/set[@id = '%d']/collections/@default",
00796          $set->getID());
00797       $alias = $this->getNodeValue($xpath);
00798       try {
00799          $col = DMCollectionFactory::getCollection($alias);
00800       } catch (DMUnavailableModelException $e) {
00801          $collections = DMCollection::getAuthorized();
00802          $col = $collections[0];
00803       }
00804       $set->setDefaultCollection($col);
00805 
00806       // results view fields
00807       $result = $this->getTemplateSetPropertyValues(
00808             "/resultsView/gridView/field", $set);
00809       foreach ($result as $node) {
00810          $set->addGridViewField(
00811                new DMDCElement($node->getAttribute('nick')));
00812       }
00813 
00814       // tile view columns
00815       $result = $this->getTemplateSetPropertyValue(
00816             "/resultsView/tileView/columns", $set);
00817       $set->setNumTileViewColumns((int) $result);
00818 
00819       // results per page
00820       $result = $this->getTemplateSetPropertyValue(
00821             "/resultsView/resultsPerPage", $set);
00822       $set->setNumResultsPerPage($result);
00823 
00824       // authorized collections
00825       $xpath = sprintf(
00826          "//templates/set[@id = '%d']/collections/allowed/collection/@alias",
00827          DMString::xmlentities($set->getID()));
00828       $dxp = new DOMXPath($this->getDomDocument());
00829       foreach ($dxp->query($xpath) as $alias) {
00830          $set->addAuthorizedCollection(
00831                DMCollectionFactory::getCollection($alias->value));
00832       }
00833 
00834    }
00835 
00842    public function saveTemplateSet(DMTemplateSet $set) {
00843       // delete old template node if it exists
00844       if ($set->getID()) {
00845          $xpath = sprintf("templates/set[@id = '%d']", $set->getID());
00846          $this->removeNode($xpath);
00847       } else { // find next id
00848          $set->setID($this->getNextAvailableTemplateSetID());
00849       }
00850 
00851       // make sure name is unique
00852       $xpath = sprintf("templates/set[@id != '%d' and @name = '%s']",
00853          $set->getID(),
00854          DMString::xmlentities($set->getName())
00855       );
00856       $dxp = new DOMXPath($this->getDomDocument());
00857       if ($dxp->query($xpath)->length) {
00858          throw new DMException(
00859                DMLocalizedString::getString("TPL_NAME_EXISTS"));
00860       }
00861 
00862       // get templates node
00863       $tpl_node = $this->getNode("templates");
00864       $set_node = $this->getDomDocument()->createElement("set");
00865       $set_node->setAttribute("id", $set->getID());
00866       $set_node->setAttribute("name", $set->getName());
00867       $tpl_node->appendChild($set_node);
00868 
00869       // default collection
00870       $col_node = $this->getDomDocument()->createElement("collections");
00871       $col_node->setAttribute(
00872          'default', $set->getDefaultCollection()->getAlias()
00873       );
00874       $set_node->appendChild($col_node);
00875 
00876       // authorized collections
00877       if (count($set->getAuthorizedCollections())) {
00878          $allowed_node = $this->getDomDocument()->createElement("allowed");
00879          $col_node->appendChild($allowed_node);
00880          foreach ($set->getAuthorizedCollections() as $c) {
00881             $col = $this->getDomDocument()->createElement("collection");
00882             $col->setAttribute("alias", $c->getAlias());
00883             $allowed_node->appendChild($col);
00884          }
00885       }
00886 
00887       $rv_node = $this->getDomDocument()->createElement("resultsView");
00888       $set_node->appendChild($rv_node);
00889 
00890       // results per page
00891       if ($set->getNumResultsPerPage()) {
00892          $rpp_node = $this->getDomDocument()->createElement("resultsPerPage",
00893                $set->getNumResultsPerPage());
00894          $rv_node->appendChild($rpp_node);
00895       }
00896 
00897       // grid view fields
00898       if (count($set->getGridViewFields())) {
00899          $gv_node = $this->getDomDocument()->createElement("gridView");
00900          foreach ($set->getGridViewFields() as $f) {
00901             $fnode = $this->getDomDocument()->createElement("field");
00902             $fnode->setAttribute("nick", $f->getNick());
00903             $gv_node->appendChild($fnode);
00904          }
00905          $rv_node->appendChild($gv_node);
00906       }
00907 
00908       // tile columns
00909       if ($set->getNumTileViewColumns()) {
00910          $tv_node = $this->getDomDocument()->createElement("tileView");
00911          $col_node = $this->getDomDocument()->createElement(
00912             "columns", $set->getNumTileViewColumns());
00913          $tv_node->appendChild($col_node);
00914          $rv_node->appendChild($tv_node);
00915       }
00916 
00917       //$this->getDomDocument()->formatOutput = true;
00918       //die($this->getDomDocument()->saveXML());
00919 
00920       $this->save();
00921    }
00922 
00928    public function getTemplateSetForCollection(DMCollection $col) {
00929       $use = $this->getCollectionPropertyValue(
00930             "/templateSets/templateSet/@use", $col);
00931       switch ($use) {
00932          case "id":
00933             $id = $this->getCollectionPropertyValue(
00934                "/templateSets/templateSet/@id", $col);
00935             break;
00936          default:
00937             $default_col = DMCollectionFactory::getCollection("/dmdefault");
00938             $id = $this->getCollectionPropertyValue(
00939                "/templateSets/templateSet/@id", $default_col);
00940             break;
00941       }
00942       return new DMTemplateSet($id);
00943    }
00944 
00948    public function isValid() {
00949       libxml_use_internal_errors(true);
00950       @$this->getDomDocument()->schemaValidate(
00951          dirname(__FILE__) . "/../../includes/config.xsd");
00952    }
00953 
00961    public function getVersion() {
00962       $tmp = $this->parseVersionFile();
00963       return $tmp[0];
00964    }
00965 
00972    public function getVersionSequence() {
00973       $tmp = $this->parseVersionFile();
00974       return $tmp[1];
00975    }
00976 
00983    protected function getDomDocument() {
00984       if (!$this->dxml instanceof DOMDocument) {
00985          // if a file doesn't exist at $abs_path, attempt to create it
00986          $did_create = false;
00987          if (!file_exists($this->config_xml_pathname)) {
00988             $source_path = dirname(__FILE__)
00989                . "/../../data/config.default.xml";
00990             $result = copy($source_path, $this->config_xml_pathname);
00991             if (!$result) {
00992                throw new DMIOException(sprintf(
00993                      DMLocalizedString::getString("ERROR_WRITING_FILE"),
00994                         $this->config_xml_pathname));
00995             }
00996             $did_create = true;
00997          }
00998          $this->dxml = new DOMDocument("1.0", "utf-8");
00999          $this->dxml->preserveWhiteSpace = false;
01000          if (!$this->dxml->load($this->config_xml_pathname)) {
01001             throw new DMIOException(sprintf(
01002                   DMLocalizedString::getString("ERROR_WRITING_FILE"),
01003                      $this->config_xml_pathname));
01004          }
01005          if ($did_create) {
01006             // config.default.xml contains references to the "/oclctest"
01007             // collection, which may not be available; so change these to
01008             // a collection that is available
01009             $all_collections = DMCollection::getAuthorized();
01010             if (count($all_collections)) {
01011                $this->setNodeValue("//collections/@default",
01012                      $all_collections[0]->getAlias());
01013             }
01014             // assign the basic template set (ID 1) to each collection
01015             foreach (DMCollection::getAuthorized() as $col) {
01016                $col->setUsingDefaultTemplateSet(true);
01017                $this->saveCollection($col);
01018             }
01019          }
01020       }
01021       return $this->dxml;
01022    }
01023 
01031    private function getCollectionPropertyValue(DMCollection $col, $rel_xpath,
01032          $load_default = true) {
01033       $str = "collections/collection[@alias='%s']/" . ltrim($rel_xpath, "/");
01034       $xpath = sprintf($str, $col->getAlias());
01035       $value = $this->getNodeValue($xpath);
01036       if (strlen($value) > 0) {
01037          return $value;
01038       }
01039       if ($load_default) {
01040          $xpath = sprintf($str, "/dmdefault");
01041          return $this->getNodeValue($xpath);
01042       }
01043       return null;
01044    }
01045 
01053    private function getCollectionPropertyValues(DMCollection $col, $rel_xpath,
01054          $load_default = true) {
01055       $str = "collections/collection[@alias='%s']/" . ltrim($rel_xpath, "/");
01056       $xpath = sprintf($str, $col->getAlias());
01057       $result = $this->getNodeList($xpath);
01058       if ($result->length > 0) {
01059          return $result;
01060       }
01061       if ($load_default) {
01062          $xpath = sprintf($str, "/dmdefault");
01063          return $this->getNodeList($xpath);
01064       }
01065       return null;
01066    }
01067 
01068    private function getNode($xpath) {
01069       $tmp = $this->getNodeList($xpath);
01070       return $tmp->item(0);
01071    }
01072 
01073    private function getNodeList($xpath) {
01074       $xp = new DOMXPath($this->getDomDocument());
01075       return $xp->query($xpath);
01076    }
01077 
01078    private function getNodeValue($xpath) {
01079       $node = $this->getNode($xpath);
01080       if ($node instanceof DOMElement) {
01081          return $node->nodeValue;
01082       }
01083       if ($node instanceof DOMNode) {
01084          return $node->value;
01085       }
01086       return null;
01087    }
01088 
01089    private function setNodeValue($xpath, $value) {
01090       $node = $this->getNode($xpath);
01091       if ($value || $value === 0) {
01092          if ($node instanceof DOMElement) {
01093             $node->nodeValue = $value;
01094             $node->removeAttribute("xsi:nil");
01095          } else if ($node instanceof DOMAttr) {
01096             $node->value = $value;
01097          }
01098       } else if (!$value) {
01099          if ($node instanceof DOMElement) {
01100             $node->nodeValue = "";
01101             $node->setAttribute("xsi:nil", "true");
01102          } else if ($node instanceof DOMAttr) {
01103             $node->parentNode->removeAttributeNode($node);
01104          }
01105       }
01106    }
01107 
01108    private function removeNode($xpath) {
01109       $xp = new DOMXPath($this->getDomDocument());
01110       $result = $xp->query($xpath);
01111       if ($result->item(0)) {
01112          if ($result->item(0)->parentNode instanceof DOMNode) {
01113             $result->item(0)->parentNode->removeChild($result->item(0));
01114          }
01115       }
01116    }
01117 
01123    public function save() {
01124       // append generator attributes to root element
01125       $now = new DMDateTime();
01126       $this->dxml->documentElement->setAttribute("generator", "dmBridge");
01127       $this->dxml->documentElement->setAttribute("generatorVersion",
01128          DMBridgeVersion::getDmBridgeVersion());
01129       $this->dxml->documentElement->setAttribute("generated",
01130          $now->asISO8601());
01131 
01132       $this->dxml->formatOutput = true;
01133 
01134       if (@$this->getDomDocument($this->config_xml_pathname)->save(
01135             $this->config_xml_pathname)) {
01136       } else {
01137          throw new DMIOException(
01138             sprintf(DMLocalizedString::getString("ERROR_WRITING_FILE"),
01139                   $this->config_xml_pathname));
01140       }
01141    }
01142 
01147    private function getTemplateSetPropertyValue($rel_xpath, DMTemplateSet $ts) {
01148       $str = "//templates/set[@id = '%d']/" . ltrim($rel_xpath, '/');
01149       $xpath = sprintf($str, $ts->getID());
01150       return $this->getNodeValue($xpath);
01151    }
01152 
01157    private function getTemplateSetPropertyValues($rel_xpath, DMTemplateSet $ts) {
01158       $str = "//templates/set[@id = '%d']/" . ltrim($rel_xpath, '/');
01159       $xpath = sprintf($str, $ts->getID());
01160       return $this->getNodeList($xpath);
01161    }
01162 
01166    private function parseVersionFile() {
01167       $file = dirname(__FILE__) . '/../../VERSION.txt';
01168       if (file_exists($file)) {
01169          $data = file_get_contents($file);
01170          $tmp = explode("\n", $data);
01171          $version = (string) substr($tmp[0], 0, 20);
01172          $seq = (int) substr($tmp[1], 0, 3);
01173          return array($version, $seq);
01174       }
01175       return array(null, null);
01176    }
01177 
01178 }
 All Data Structures Functions Variables