From d1992386fece17eeac1131d9b92d0ab1ed344cf4 Mon Sep 17 00:00:00 2001 From: jonathangreen Date: Thu, 24 May 2012 10:40:18 -0300 Subject: [PATCH] Fixed my stupid mistakes in Ingest I added the collection policy class, although a more stripped down version of it and fixed in ingest function that calls it. --- .../includes/CollectionPolicy.inc | 150 ++++++++++++++++++ .../islandora_basic_collection.module | 15 +- 2 files changed, 154 insertions(+), 11 deletions(-) create mode 100644 islandora_basic_collection/includes/CollectionPolicy.inc diff --git a/islandora_basic_collection/includes/CollectionPolicy.inc b/islandora_basic_collection/includes/CollectionPolicy.inc new file mode 100644 index 00000000..94cad1e1 --- /dev/null +++ b/islandora_basic_collection/includes/CollectionPolicy.inc @@ -0,0 +1,150 @@ +xml = new DOMDocument(); + $this->xml->loadXML($xmlStr); + $this->name = 'Collection Policy'; + } + + /** + * Gets the name of the relationship to use + * for members of this collection. + * Returns FALSE on failure. + * + * @return string $relationship + */ + public function getRelationship() { + $ret = trim($this->xml->getElementsByTagName('relationship')->item(0)->nodeValue); + return $ret; + } + + /** + * Sets the name of the relationship to use + * for members of this collection. + * Returns FALSE on failure. + * + * @param string $relationship + * @return boolean $ret + */ + public function setRelationship($relationship) { + $ret = FALSE; + if ($this->validate()) { + $relationshipEl = $this->xml->getElementsByTagName('relationship')->item(0); + $relationshipEl->nodeValue = trim($relationship); + $ret = TRUE; + } + return $ret; + } + + /** + * Gets a list of ContentModel objects supported by this collection. + * + * @return ContentModel[] $models + */ + function getContentModels() { + $ret = array(); + $content_models = $this->xml->getElementsByTagName('content_models')->item(0)->getElementsByTagName('content_model'); + for ($i = 0; $i < $content_models->length; $i++) { + $cm = array(); + $cm['pid'] = $content_models->item($i)->getAttribute('pid'); + $cm['namespace'] = $content_models->item($i)->getAttribute('namespace'); + $cm['name'] = $content_models->item($i)->getAttribute('name'); + if ($cm !== FALSE) { + $ret[] = $cm; + } + } + return $ret; + } + + /** + * Removes the specified content model from the collection policy. This will only + * prevent future ingests of the removed model to the collection. $cm should be + * a valid ContentModel object. Returns FALSE on failure or when the CM was not found in + * the collection policy. + * + * @param ContentModel $cm + * @return boolean $valid + */ + function removeModel($cm) { + $ret = FALSE; + if ($this->validate() && $cm->validate()) { + $contentmodelsEl = $this->xml->getElementsByTagName('content_models'); + $models = $contentmodelsEl->item(0)->getElementsByTagName('content_model'); + $found = FALSE; + for ($i = 0; $found === FALSE && $i < $models->length; $i++) { + if ($models->item($i)->getAttribute('pid') == $cm->pid) { + $found = $models->item($i); + } + } + + if ($found !== FALSE && $models->length > 1) { + $contentmodelsEl->item(0)->removeChild($found); + $ret = TRUE; + } + } + return $ret; + } + + /** + * addModel ?? + * @param ContentModel $cm + * @param type $namespace + * @return type + */ + function addModel($cm, $namespace) { + $ret = FALSE; + if (self::valid_pid($namespace) && $this->validate() && $cm->validate()) { + $contentmodelsEl = $this->xml->getElementsByTagName('content_models'); + $models = $contentmodelsEl->item(0)->getElementsByTagName('content_model'); + $found = FALSE; + for ($i = 0; !$found && $i < $models->length; $i++) { + if ($models->item($i)->getAttribute('pid') == $cm->pid) + $found = TRUE; + } + + if (!$found) { + $cmEl = $this->xml->createElement('content_model'); + $cmEl->setAttribute('name', $cm->getName()); + $cmEl->setAttribute('dsid', $cm->dsid); + $cmEl->setAttribute('namespace', $namespace); + $cmEl->setAttribute('pid', $cm->pid); + $contentmodelsEl->item(0)->appendChild($cmEl); + } + + $ret = !$found; + } + return $ret; + } + + /** + * getName ?? + * @return type + */ + function getName() { + $ret = FALSE; + if ($this->validate()) { + $ret = $this->xml->getElementsByTagName('collection_policy')->item(0)->getAttribute('name'); + } + return $ret; + } + +} diff --git a/islandora_basic_collection/islandora_basic_collection.module b/islandora_basic_collection/islandora_basic_collection.module index 768c105b..2cf3f67b 100644 --- a/islandora_basic_collection/islandora_basic_collection.module +++ b/islandora_basic_collection/islandora_basic_collection.module @@ -140,18 +140,11 @@ function islandora_basic_collection_get_objects($object) { function islandora_basic_collection_islandora_ingest_get_information($models, $object) { if(in_array('info:fedora/islandora:collectionCModel', $models) && isset($object['COLLECTION_POLICY'])) { try { + module_load_include('inc', 'islandora_basic_collection', 'includes/CollectionPolicy'); $return = array(); - $xml = new SimpleXMLElement($object['COLLECTION_POLICY']); - $cms = $xml->getElementsByTagName('content_models')->item(0)->getElementsByTagName('content_model'); - $return['contentsmodels'] = array(); - foreach($cms as $cm) { - $contentmodel = array(); - $contentmodel['name'] = (string) $cm['name']; - $contentmodel['namespace'] = (string) $cm['namespace']; - $contentmodel['pid'] = (string) $cm['pid']; - $return['contentsmodels'] = (string) $contentmodel; - } - $return['relationship'] = trim($xml->getElementsByTagName('relationship')->item(0)->nodeValue); + $policy = new CollectionPolicy($object['COLLECTION_POLICY']->content); + $return['models'] = $policy->getContentModels(); + $return['relationship'] = $policy->getRelationship(); return $return; }