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          $vd->setWidth(
00211             $node->getElementsByTagName("maxWidth")->item(0)->nodeValue);
00212          $vd->setHeight(
00213             $node->getElementsByTagName("maxHeight")->item(0)->nodeValue);
00214          $col->addViewerDefinition($vd);
00215       }
00216    }
00217 
00223    public function saveCollection(DMCollection $col) {
00224       // delete old collection node if it exists
00225       $xpath = sprintf("collections/collection[@alias = '%s']",
00226          $col->getAlias());
00227       $this->removeNode($xpath);
00228 
00229       // get collections node
00230       $parent_node = $this->getNode("collections");
00231       $col_node = $this->getDomDocument()->createElement("collection");
00232       $col_node->setAttribute("alias", $col->getAlias());
00233       $parent_node->appendChild($col_node);
00234 
00235       // overview URI
00236       if ($col->getAlias() != "/dmdefault") {
00237          $node = $this->getDomDocument()->createElement("overviewURI",
00238             DMString::xmlentities($col->getOverviewURI()));
00239          $col_node->appendChild($node);
00240       }
00241 
00242       // image URI
00243       if ($col->getAlias() != "/dmdefault") {
00244          $node = $this->getDomDocument()->createElement("image512URI",
00245             DMString::xmlentities($col->getImage512URI()));
00246          $col_node->appendChild($node);
00247       }
00248 
00249       // description
00250       if ($col->getAlias() != "/dmdefault") {
00251          $node = $this->getDomDocument()->createElement("description",
00252             DMString::xmlentities($col->getDescription()));
00253          $col_node->appendChild($node);
00254       }
00255 
00256       // redirect reference URLs
00257       if ($col->getAlias() != "/dmdefault") {
00258          $node = $this->getDomDocument()->createElement("redirectReferenceURLs",
00259             (int) $col->isRedirectingReferenceURLs());
00260          $col_node->appendChild($node);
00261       }
00262 
00263       // search view
00264       if ($col->getDateSearchBeginYear() || $col->getDateSearchEndYear()) {
00265          $sv_node = $this->getDomDocument()->createElement("searchView");
00266          $ds_node = $this->getDomDocument()->createElement("dateSearch");
00267          $sv_node->appendChild($ds_node);
00268          $col_node->appendChild($sv_node);
00269 
00270          // date search begin year
00271          $year_node = $this->getDomDocument()->createElement("beginYear",
00272             $col->getDateSearchBeginYear());
00273          $ds_node->appendChild($year_node);
00274 
00275          // date search end year
00276          $year_node = $this->getDomDocument()->createElement("endYear",
00277             $col->getDateSearchEndYear());
00278          $ds_node->appendChild($year_node);
00279       }
00280 
00281       // grid view
00282       if (count($col->getGridViewFields())) {
00283          $gv_node = $this->getDomDocument()->createElement("gridView");
00284          $col_node->appendChild($gv_node);
00285          foreach ($col->getGridViewFields() as $f) {
00286             if ($f instanceof DMDCElement) {
00287                if (strlen($f->getNick()) < 1) {
00288                   continue;
00289                }
00290                $fnode = $this->getDomDocument()->createElement("field");
00291                $fnode->setAttribute("nick", $f->getNick());
00292                $gv_node->appendChild($fnode);
00293             }
00294          }
00295       }
00296 
00297       // template sets
00298       $ts_node = $this->getDomDocument()->createElement("templateSets");
00299       $col_node->appendChild($ts_node);
00300 
00301       $d_node = $this->getDomDocument()->createElement("templateSet");
00302       $ts_node->appendChild($d_node);
00303       if ($col->getTemplateSet() instanceof DMTemplateSet
00304             && $col->getTemplateSet()->getID()) {
00305          $d_node->setAttribute("use", "id");
00306          $d_node->setAttribute("id", $col->getTemplateSet()->getID());
00307       } else {
00308          $d_node->setAttribute("use", "default");
00309       }
00310 
00311       // results view
00312       $rv_node = $this->getDomDocument()->createElement("resultsView");
00313       $col_node->appendChild($rv_node);
00314 
00315       // fields
00316       $fi_node = $this->getDomDocument()->createElement("fields");
00317       $rv_node->appendChild($fi_node);
00318       $added_fields = 0;
00319       foreach ($col->getFields() as $f) {
00320          if (strlen($f->getNick()) < 1) {
00321             continue;
00322          }
00323          if ($f->getNick() == "index" || $f->getNick() == "thumb") {
00324             continue;
00325          }
00326          // only save fields that are sortable, default sort, and/or have max
00327          // words
00328          if (!$f->isSortable() && !$f->isDefaultSort() && !$f->getMaxWords()) {
00329             continue;
00330          }
00331          $fnode = $this->getDomDocument()->createElement("field");
00332          $fnode->setAttribute("nick", $f->getNick());
00333          $fnode->setAttribute("isSortable", (int) $f->isSortable());
00334          $fnode->setAttribute("isDefaultSort", (int) $f->isDefaultSort());
00335          $fnode->setAttribute("maxWords", (int) $f->getMaxWords());
00336          $fi_node->appendChild($fnode);
00337          $added_fields++;
00338       }
00339       if ($added_fields) {
00340          $rv_node->appendChild($fi_node);
00341       }
00342 
00343       // facets
00344       $fa_node = $this->getDomDocument()->createElement("facets");
00345       $rv_node->appendChild($fa_node);
00346       $added_fields = 0;
00347       foreach ($col->getFacets() as $f) {
00348          $fnode = $this->getDomDocument()->createElement("field");
00349          $fnode->setAttribute("nick", $f->getField()->getNick());
00350          $fa_node->appendChild($fnode);
00351          $added_fields++;
00352       }
00353       if ($added_fields) {
00354          $rv_node->appendChild($fa_node);
00355       }
00356 
00357       // viewer definitions
00358       $added_fields = 0;
00359       $ov_node = $this->getDomDocument()->createElement("objectView");
00360       foreach ($col->getViewerDefinitions() as $d) {
00361          $media_node = $this->getDomDocument()->createElement("media");
00362          $media_node->setAttribute("type", $d->getMediaType());
00363          $class_node = $this->getDomDocument()->createElement("class",
00364             $d->getClass());
00365          $width_node = $this->getDomDocument()->createElement("maxWidth",
00366             $d->getWidth());
00367          $height_node = $this->getDomDocument()->createElement("maxHeight",
00368             $d->getHeight());
00369          $media_node->appendChild($class_node);
00370          $media_node->appendChild($width_node);
00371          $media_node->appendChild($height_node);
00372          $ov_node->appendChild($media_node);
00373          $added_fields++;
00374       }
00375       if ($added_fields) {
00376          $col_node->appendChild($ov_node);
00377       }
00378 
00379       //$this->getDomDocument()->formatOutput = true;
00380       //die($this->getDomDocument()->saveXML());
00381 
00382       $this->save();
00383    }
00384 
00389    public function isCommentingEnabled() {
00390       return (bool) $this->getNodeValue("comments/@enabled");
00391    }
00392 
00397    public function setCommentingEnabled($bool) {
00398       $this->setNodeValue('comments/@enabled', (int) $bool);
00399    }
00400 
00405    public function isCommentModerationEnabled() {
00406       return (bool) $this->getNodeValue("comments/moderation/@enabled");
00407    }
00408 
00412    public function setCommentModerationEnabled($bool) {
00413       $this->setNodeValue('comments/moderation/@enabled', (int) $bool);
00414    }
00415 
00420    public function getCommentNotificationEmail() {
00421       return $this->getNodeValue("comments/email");
00422    }
00423 
00427    public function setCommentNotificationEmail($str) {
00428       $this->setNodeValue('comments/email', $str);
00429    }
00430 
00435    public function isCommentNotificationEnabled() {
00436       return (bool) $this->getNodeValue("comments/email/@enabled");
00437    }
00438 
00442    public function setCommentNotificationEnabled($bool) {
00443       $this->setNodeValue("comments/email/@enabled", (int) $bool);
00444    }
00445 
00451    public function getDataDir() {
00452       $root = DMConfigIni::getInstance()->getString("dmbridge.data_path");
00453       if (!$this->data_dir) {
00454          if ($root) {
00455             $this->data_dir = str_replace("\\", "/", $root);
00456          } else {
00457             $this->data_dir = realpath(dirname(__FILE__) . "/../../data");
00458          }
00459       }
00460       return $this->data_dir;
00461    }
00462 
00467    public function getFeedCopyright() {
00468       return $this->getNodeValue("feed/copyright");
00469    }
00470 
00474    public function setFeedCopyright($str) {
00475       $this->setNodeValue('feed/copyright', $str);
00476    }
00477 
00482    public function getFeedLanguage() {
00483       return $this->getNodeValue("feed/language");
00484    }
00485 
00489    public function setFeedLanguage($str) {
00490       $this->setNodeValue('feed/language', $str);
00491    }
00492 
00497    public function getFeedManagingEditorEmail() {
00498       return $this->getNodeValue("feed/managingEditor/email");
00499    }
00500 
00504    public function setFeedManagingEditorEmail($str) {
00505       $this->setNodeValue('feed/managingEditor/email', $str);
00506    }
00507 
00512    public function getFeedManagingEditorName() {
00513       return $this->getNodeValue("feed/managingEditor/name");
00514    }
00515 
00519    public function setFeedManagingEditorName($str) {
00520       $this->setNodeValue('feed/managingEditor/name', $str);
00521    }
00522 
00527    public function getFeedTitle() {
00528       return $this->getNodeValue("feed/title");
00529    }
00530 
00534    public function setFeedTitle($str) {
00535       $this->setNodeValue("feed/title", $str);
00536    }
00537 
00542    public function getFeedSubtitle() {
00543       return $this->getNodeValue("feed/subtitle");
00544    }
00545 
00549    public function setFeedSubtitle($str) {
00550       $this->setNodeValue("feed/subtitle", $str);
00551    }
00552 
00557    public function getFeedWebMasterEmail() {
00558       return $this->getNodeValue("feed/webMaster/email");
00559    }
00560 
00564    public function setFeedWebMasterEmail($str) {
00565       $this->setNodeValue('feed/webMaster/email', $str);
00566    }
00567 
00572    public function getFeedWebMasterName() {
00573       return $this->getNodeValue("feed/webMaster/name");
00574    }
00575 
00579    public function setFeedWebMasterName($str) {
00580       $this->setNodeValue('feed/webMaster/name', $str);
00581    }
00582 
00586    public function getFullPath() {
00587       return $this->config_xml_pathname;
00588    }
00589 
00594    public function getInstitutionName() {
00595       return $this->getNodeValue("institution/name");
00596    }
00597 
00602    public function setInstitutionName($str) {
00603       $this->setNodeValue("institution/name", $str);
00604    }
00605 
00610    public function isLoggingEnabled() {
00611       return (bool) $this->getNodeValue("logging/@enabled");
00612    }
00613 
00614    private function getNextAvailableTemplateSetID() {
00615       $id = 1;
00616       $xp = new DOMXPath($this->getDomDocument());
00617       foreach ($xp->query('//templates/set/@id') as $c) {
00618          $id = ((int) $c->nodeValue > $id) ? (int) $c->nodeValue : $id;
00619       }
00620       return $id + 1;
00621    }
00622 
00628    public function isModuleEnabled($module) {
00629       if ($module instanceof DMBridgeModule) {
00630          $module = $module->getName();
00631       }
00632       $xpath = sprintf("//modules/module[name = '%s']/@enabled", $module);
00633       return (bool) $this->getNodeValue($xpath);
00634    }
00635 
00645    public function isModuleOfSameNameInstalled(DMBridgeModule $module) {
00646       $xpath = sprintf("//modules/module[name = '%s']",
00647             $module->getName());
00648       return ($this->getNodeList($xpath)->length);
00649    }
00650 
00660    public function isModuleOfSameNameAndVersionInstalled(
00661          DMBridgeModule $module) {
00662       $xpath = sprintf("//modules/module[name = '%s' and version = '%s']",
00663             $module->getName(), $module->getVersion());
00664       return ($this->getNodeList($xpath)->length);
00665    }
00666 
00672    public function setModuleEnabled(DMBridgeModule $module, $enabled) {
00673       // delete old module node if it exists
00674       $xpath = sprintf("modules/module[name = '%s' and version = '%s']",
00675          $module->getName(), $module->getVersion());
00676       $this->removeNode($xpath);
00677 
00678       // create new module node
00679       $module_node = $this->getDomDocument()->createElement("module");
00680       $module_node->setAttribute("enabled", (int) $enabled);
00681       $name_node = $this->getDomDocument()->createElement("name",
00682             $module->getName());
00683       $version_node = $this->getDomDocument()->createElement("version",
00684             $module->getVersion());
00685 
00686       $module_node->appendChild($name_node);
00687       $module_node->appendChild($version_node);
00688       $this->getDomDocument()->getElementsByTagName("modules")->item(0)
00689             ->appendChild($module_node);
00690    }
00691 
00696    public function isRatingEnabled() {
00697       return (bool) $this->getNodeValue("rating/@enabled");
00698    }
00699 
00704    public function setRatingEnabled($bool) {
00705       $this->setNodeValue("rating/@enabled", (int) $bool);
00706    }
00707 
00712    public function isTaggingEnabled() {
00713       return (bool) $this->getNodeValue("tagging/@enabled");
00714    }
00715 
00720    public function setTaggingEnabled($bool) {
00721       $this->setNodeValue("tagging/@enabled", (int) $bool);
00722    }
00723 
00728    public function isTagModerationEnabled() {
00729       return (bool) $this->getNodeValue("tagging/@moderated");
00730    }
00731 
00735    public function setTagModerationEnabled($bool) {
00736       $this->setNodeValue("tagging/@moderated", (int) $bool);
00737    }
00738 
00744    public function deleteTemplateSet(DMTemplateSet $set) {
00745       // first check to make sure no collections are using it.
00746       $normal_xpath = "//collection/templateSets/templateSet/@id";
00747       $dxp = new DOMXPath($this->getDomDocument());
00748 
00749       $result = $dxp->query($normal_xpath);
00750       $in_use = false;
00751       foreach ($result as $node) {
00752          if ($node->value == $set->getID()) {
00753             $in_use = true;
00754          }
00755       }
00756 
00757       if ($in_use) {
00758          throw new DMConstraintException(
00759                DMLocalizedString::getString("TPL_IN_USE"));
00760       }
00761 
00762       // delete template node
00763       if ($set->getID()) {
00764          $xpath = sprintf("templates/set[@id = '%d']", $set->getID());
00765          $this->removeNode($xpath);
00766       }
00767       $this->save();
00768    }
00769 
00778    public function loadTemplateSet(DMTemplateSet $set) {
00779       $xpath = sprintf("//templates/set[@id = '%d']", $set->getID());
00780       $result = $this->getNode($xpath);
00781       if (!$result instanceof DOMNode) {
00782          throw new DMUnavailableModelException(
00783             DMLocalizedString::getString("INVALID_TPL_SET"));
00784       }
00785       $set->setName($result->getAttribute("name"));
00786       $alias = null;
00787       if ($result->getElementsByTagName("set")->item(0) instanceof DOMNode) {
00788          $alias = $result->getElementsByTagName("set")->item(0)
00789                ->getAttribute("default");
00790       }
00791 
00792       $xpath = sprintf("//templates/set[@id = '%d']/collections/@default",
00793          $set->getID());
00794       $alias = $this->getNodeValue($xpath);
00795       try {
00796          $col = DMCollectionFactory::getCollection($alias);
00797       } catch (DMUnavailableModelException $e) {
00798          $collections = DMCollection::getAuthorized();
00799          $col = $collections[0];
00800       }
00801       $set->setDefaultCollection($col);
00802 
00803       // results view fields
00804       $result = $this->getTemplateSetPropertyValues(
00805             "/resultsView/gridView/field", $set);
00806       foreach ($result as $node) {
00807          $set->addGridViewField(
00808                new DMDCElement($node->getAttribute('nick')));
00809       }
00810 
00811       // tile view columns
00812       $result = $this->getTemplateSetPropertyValue(
00813             "/resultsView/tileView/columns", $set);
00814       $set->setNumTileViewColumns((int) $result);
00815 
00816       // results per page
00817       $result = $this->getTemplateSetPropertyValue(
00818             "/resultsView/resultsPerPage", $set);
00819       $set->setNumResultsPerPage($result);
00820 
00821       // authorized collections
00822       $xpath = sprintf(
00823          "//templates/set[@id = '%d']/collections/allowed/collection/@alias",
00824          DMString::xmlentities($set->getID()));
00825       $dxp = new DOMXPath($this->getDomDocument());
00826       foreach ($dxp->query($xpath) as $alias) {
00827          $set->addAuthorizedCollection(
00828                DMCollectionFactory::getCollection($alias->value));
00829       }
00830 
00831    }
00832 
00839    public function saveTemplateSet(DMTemplateSet $set) {
00840       // delete old template node if it exists
00841       if ($set->getID()) {
00842          $xpath = sprintf("templates/set[@id = '%d']", $set->getID());
00843          $this->removeNode($xpath);
00844       } else { // find next id
00845          $set->setID($this->getNextAvailableTemplateSetID());
00846       }
00847 
00848       // make sure name is unique
00849       $xpath = sprintf("templates/set[@id != '%d' and @name = '%s']",
00850          $set->getID(),
00851          DMString::xmlentities($set->getName())
00852       );
00853       $dxp = new DOMXPath($this->getDomDocument());
00854       if ($dxp->query($xpath)->length) {
00855          throw new DMException(
00856                DMLocalizedString::getString("TPL_NAME_EXISTS"));
00857       }
00858 
00859       // get templates node
00860       $tpl_node = $this->getNode("templates");
00861       $set_node = $this->getDomDocument()->createElement("set");
00862       $set_node->setAttribute("id", $set->getID());
00863       $set_node->setAttribute("name", $set->getName());
00864       $tpl_node->appendChild($set_node);
00865 
00866       // default collection
00867       $col_node = $this->getDomDocument()->createElement("collections");
00868       $col_node->setAttribute(
00869          'default', $set->getDefaultCollection()->getAlias()
00870       );
00871       $set_node->appendChild($col_node);
00872 
00873       // authorized collections
00874       if (count($set->getAuthorizedCollections())) {
00875          $allowed_node = $this->getDomDocument()->createElement("allowed");
00876          $col_node->appendChild($allowed_node);
00877          foreach ($set->getAuthorizedCollections() as $c) {
00878             $col = $this->getDomDocument()->createElement("collection");
00879             $col->setAttribute("alias", $c->getAlias());
00880             $allowed_node->appendChild($col);
00881          }
00882       }
00883 
00884       $rv_node = $this->getDomDocument()->createElement("resultsView");
00885       $set_node->appendChild($rv_node);
00886 
00887       // results per page
00888       if ($set->getNumResultsPerPage()) {
00889          $rpp_node = $this->getDomDocument()->createElement("resultsPerPage",
00890                $set->getNumResultsPerPage());
00891          $rv_node->appendChild($rpp_node);
00892       }
00893 
00894       // grid view fields
00895       if (count($set->getGridViewFields())) {
00896          $gv_node = $this->getDomDocument()->createElement("gridView");
00897          foreach ($set->getGridViewFields() as $f) {
00898             $fnode = $this->getDomDocument()->createElement("field");
00899             $fnode->setAttribute("nick", $f->getNick());
00900             $gv_node->appendChild($fnode);
00901          }
00902          $rv_node->appendChild($gv_node);
00903       }
00904 
00905       // tile columns
00906       if ($set->getNumTileViewColumns()) {
00907          $tv_node = $this->getDomDocument()->createElement("tileView");
00908          $col_node = $this->getDomDocument()->createElement(
00909             "columns", $set->getNumTileViewColumns());
00910          $tv_node->appendChild($col_node);
00911          $rv_node->appendChild($tv_node);
00912       }
00913 
00914       //$this->getDomDocument()->formatOutput = true;
00915       //die($this->getDomDocument()->saveXML());
00916 
00917       $this->save();
00918    }
00919 
00925    public function getTemplateSetForCollection(DMCollection $col) {
00926       $use = $this->getCollectionPropertyValue(
00927             "/templateSets/templateSet/@use", $col);
00928       switch ($use) {
00929          case "id":
00930             $id = $this->getCollectionPropertyValue(
00931                "/templateSets/templateSet/@id", $col);
00932             break;
00933          default:
00934             $default_col = DMCollectionFactory::getCollection("/dmdefault");
00935             $id = $this->getCollectionPropertyValue(
00936                "/templateSets/templateSet/@id", $default_col);
00937             break;
00938       }
00939       return new DMTemplateSet($id);
00940    }
00941 
00945    public function isValid() {
00946       libxml_use_internal_errors(true);
00947       @$this->getDomDocument()->schemaValidate(
00948          dirname(__FILE__) . "/../../includes/config.xsd");
00949    }
00950 
00958    public function getVersion() {
00959       $tmp = $this->parseVersionFile();
00960       return $tmp[0];
00961    }
00962 
00969    public function getVersionSequence() {
00970       $tmp = $this->parseVersionFile();
00971       return $tmp[1];
00972    }
00973 
00980    protected function getDomDocument() {
00981       if (!$this->dxml instanceof DOMDocument) {
00982          // if a file doesn't exist at $abs_path, attempt to create it
00983          $did_create = false;
00984          if (!file_exists($this->config_xml_pathname)) {
00985             $source_path = dirname(__FILE__)
00986                . "/../../data/config.default.xml";
00987             $result = copy($source_path, $this->config_xml_pathname);
00988             if (!$result) {
00989                throw new DMIOException(sprintf(
00990                      DMLocalizedString::getString("ERROR_WRITING_FILE"),
00991                         $this->config_xml_pathname));
00992             }
00993             $did_create = true;
00994          }
00995          $this->dxml = new DOMDocument("1.0", "utf-8");
00996          $this->dxml->preserveWhiteSpace = false;
00997          if (!$this->dxml->load($this->config_xml_pathname)) {
00998             throw new DMIOException(sprintf(
00999                   DMLocalizedString::getString("ERROR_WRITING_FILE"),
01000                      $this->config_xml_pathname));
01001          }
01002          if ($did_create) {
01003             // config.default.xml contains references to the "/oclctest"
01004             // collection, which may not be available; so change these to
01005             // a collection that is available
01006             $all_collections = DMCollection::getAuthorized();
01007             if (count($all_collections)) {
01008                $this->setNodeValue("//collections/@default",
01009                      $all_collections[0]->getAlias());
01010             }
01011             // assign the basic template set (ID 1) to each collection
01012             foreach (DMCollection::getAuthorized() as $col) {
01013                $col->setUsingDefaultTemplateSet(true);
01014                $this->saveCollection($col);
01015             }
01016          }
01017       }
01018       return $this->dxml;
01019    }
01020 
01028    private function getCollectionPropertyValue(DMCollection $col, $rel_xpath,
01029          $load_default = true) {
01030       $str = "collections/collection[@alias='%s']/" . ltrim($rel_xpath, "/");
01031       $xpath = sprintf($str, $col->getAlias());
01032       $value = $this->getNodeValue($xpath);
01033       if (strlen($value) > 0) {
01034          return $value;
01035       }
01036       if ($load_default) {
01037          $xpath = sprintf($str, "/dmdefault");
01038          return $this->getNodeValue($xpath);
01039       }
01040       return null;
01041    }
01042 
01050    private function getCollectionPropertyValues(DMCollection $col, $rel_xpath,
01051          $load_default = true) {
01052       $str = "collections/collection[@alias='%s']/" . ltrim($rel_xpath, "/");
01053       $xpath = sprintf($str, $col->getAlias());
01054       $result = $this->getNodeList($xpath);
01055       if ($result->length > 0) {
01056          return $result;
01057       }
01058       if ($load_default) {
01059          $xpath = sprintf($str, "/dmdefault");
01060          return $this->getNodeList($xpath);
01061       }
01062       return null;
01063    }
01064 
01065    private function getNode($xpath) {
01066       $tmp = $this->getNodeList($xpath);
01067       return $tmp->item(0);
01068    }
01069 
01070    private function getNodeList($xpath) {
01071       $xp = new DOMXPath($this->getDomDocument());
01072       return $xp->query($xpath);
01073    }
01074 
01075    private function getNodeValue($xpath) {
01076       $node = $this->getNode($xpath);
01077       if ($node instanceof DOMElement) {
01078          return $node->nodeValue;
01079       }
01080       if ($node instanceof DOMNode) {
01081          return $node->value;
01082       }
01083       return null;
01084    }
01085 
01086    private function setNodeValue($xpath, $value) {
01087       $node = $this->getNode($xpath);
01088       if ($value || $value === 0) {
01089          if ($node instanceof DOMElement) {
01090             $node->nodeValue = $value;
01091             $node->removeAttribute("xsi:nil");
01092          } else if ($node instanceof DOMAttr) {
01093             $node->value = $value;
01094          }
01095       } else if (!$value) {
01096          if ($node instanceof DOMElement) {
01097             $node->nodeValue = "";
01098             $node->setAttribute("xsi:nil", "true");
01099          } else if ($node instanceof DOMAttr) {
01100             $node->parentNode->removeAttributeNode($node);
01101          }
01102       }
01103    }
01104 
01105    private function removeNode($xpath) {
01106       $xp = new DOMXPath($this->getDomDocument());
01107       $result = $xp->query($xpath);
01108       if ($result->item(0)) {
01109          if ($result->item(0)->parentNode instanceof DOMNode) {
01110             $result->item(0)->parentNode->removeChild($result->item(0));
01111          }
01112       }
01113    }
01114 
01120    public function save() {
01121       // append generator attributes to root element
01122       $now = new DMDateTime();
01123       $this->dxml->documentElement->setAttribute("generator", "dmBridge");
01124       $this->dxml->documentElement->setAttribute("generatorVersion",
01125          DMBridgeVersion::getDmBridgeVersion());
01126       $this->dxml->documentElement->setAttribute("generated",
01127          $now->asISO8601());
01128 
01129       $this->dxml->formatOutput = true;
01130 
01131       if (@$this->getDomDocument($this->config_xml_pathname)->save(
01132             $this->config_xml_pathname)) {
01133       } else {
01134          throw new DMIOException(
01135             sprintf(DMLocalizedString::getString("ERROR_WRITING_FILE"),
01136                   $this->config_xml_pathname));
01137       }
01138    }
01139 
01144    private function getTemplateSetPropertyValue($rel_xpath, DMTemplateSet $ts) {
01145       $str = "//templates/set[@id = '%d']/" . ltrim($rel_xpath, '/');
01146       $xpath = sprintf($str, $ts->getID());
01147       return $this->getNodeValue($xpath);
01148    }
01149 
01154    private function getTemplateSetPropertyValues($rel_xpath, DMTemplateSet $ts) {
01155       $str = "//templates/set[@id = '%d']/" . ltrim($rel_xpath, '/');
01156       $xpath = sprintf($str, $ts->getID());
01157       return $this->getNodeList($xpath);
01158    }
01159 
01163    private function parseVersionFile() {
01164       $file = dirname(__FILE__) . '/../../VERSION.txt';
01165       if (file_exists($file)) {
01166          $data = file_get_contents($file);
01167          $tmp = explode("\n", $data);
01168          $version = (string) substr($tmp[0], 0, 20);
01169          $seq = (int) substr($tmp[1], 0, 3);
01170          return array($version, $seq);
01171       }
01172       return array(null, null);
01173    }
01174 
01175 }
 All Data Structures Functions Variables