Browse Source

Content model addition to collection now working

pull/120/merge
rwincewicz 13 years ago
parent
commit
64eb8ebbac
  1. 119
      islandora_basic_collection/includes/CollectionPolicy.inc
  2. 80
      islandora_basic_collection/includes/ManagePolicies.inc

119
islandora_basic_collection/includes/CollectionPolicy.inc

@ -1,4 +1,5 @@
<?php <?php
/** /**
* @file * @file
* This file contains the classes for parsing the collection policy infomration. * This file contains the classes for parsing the collection policy infomration.
@ -112,26 +113,29 @@ class CollectionPolicy {
*/ */
function addModel($cm, $namespace) { function addModel($cm, $namespace) {
$ret = FALSE; $ret = FALSE;
if (self::valid_pid($namespace) && $this->validate() && $cm->validate()) { // var_dump(self::valid_pid($namespace));
// var_dump($this->validate());
// var_dump($cm->validate());
// if (self::valid_pid($namespace) && $this->validate() && $cm->validate()) {
$contentmodelsEl = $this->xml->getElementsByTagName('content_models'); $contentmodelsEl = $this->xml->getElementsByTagName('content_models');
$models = $contentmodelsEl->item(0)->getElementsByTagName('content_model'); $models = $contentmodelsEl->item(0)->getElementsByTagName('content_model');
$found = FALSE; $found = FALSE;
for ($i = 0; !$found && $i < $models->length; $i++) { // for ($i = 0; !$found && $i < $models->length; $i++) {
if ($models->item($i)->getAttribute('pid') == $cm->pid) // if ($models->item($i)->getAttribute('pid') == $cm->pid)
$found = TRUE; // $found = TRUE;
} // }
if (!$found) { // if (!$found) {
$cmEl = $this->xml->createElement('content_model'); $cmEl = $this->xml->createElement('content_model');
$cmEl->setAttribute('name', $cm->getName()); $cmEl->setAttribute('name', $cm->name);
$cmEl->setAttribute('dsid', $cm->dsid); $cmEl->setAttribute('dsid', $cm->dsid);
$cmEl->setAttribute('namespace', $namespace); $cmEl->setAttribute('namespace', $namespace);
$cmEl->setAttribute('pid', $cm->pid); $cmEl->setAttribute('pid', $cm->pid);
$contentmodelsEl->item(0)->appendChild($cmEl); $contentmodelsEl->item(0)->appendChild($cmEl);
} // }
$ret = !$found; $ret = !$found;
} // }
return $ret; return $ret;
} }
@ -147,4 +151,101 @@ class CollectionPolicy {
return $ret; return $ret;
} }
/**
* valid_pid
* Validates a fedora PID based on the regexp provided in the fedora
* 3.3 documentation.
* http://www.fedora-commons.org/confluence/display/FCR30/Fedora+Identifiers
*
* @param String $pid
* @return boolean $valid
*/
public static function valid_pid($pid) {
$valid = FALSE;
if (strlen(trim($pid)) <= 64 && preg_match('/^([A-Za-z0-9]|-|\.)+:(([A-Za-z0-9])|-|\.|~|_|(%[0-9A-F]{2}))+$/', trim($pid))) {
$valid = TRUE;
}
return $valid;
}
/**
* Validates the XMLDatastream against the schema location
* defined by the xmlns:schemaLocation attribute of the root
* element. If the xmlns:schemaLocation attribute does not exist,
* then it is assumed to be the old schema and it attempts to convert
* using the convertFromOldSchema method.
*
* TODO: Maybe change it so that it always validates against a known
* schema. This makes more sense because this class assumes the structure
* to be known after it has been validated.
*
* @return boolean $valid
*/
public function validate() {
global $user;
if ($this->valid === NULL) {
$ret = TRUE;
if ($this->xml == NULL) {
$this->fetchXml();
}
// figure out if we're dealing with a new or old schema
$rootEl = $this->xml->firstChild;
if (!$rootEl->hasAttributes() || $rootEl->attributes->getNamedItem('schemaLocation') === NULL) {
//$tmpname = substr($this->pid, strpos($this->pid, ':') + 1);
$tmpname = user_password(10);
$this->convertFromOldSchema();
drupal_add_js("fedora_repository_print_new_schema_$tmpname = function(tagID) {
var target = document.getElementById(tagID);
var content = target.innerHTML;
var text = '<html><head><title>Title' +
'</title><body>' + content +'</body></html>';
printerWindow = window.open('', '', 'toolbar=no,location=no,' + 'status=no,menu=no,scrollbars=yes,width=650,height=400');
printerWindow.document.open();
printerWindow.document.write(text);
}", 'inline');
if (user_access('administer site configuration')) {
drupal_set_message('<span id="new_schema_' . $tmpname . '" style="display: none;">' . htmlentities($this->xml->saveXML()) . '</span>Warning: XMLDatastream performed conversion of \'' . $this->getIdentifier() . '\' from old schema. Please update the datastream. The new datastream contents are <a href="javascript:fedora_repository_print_new_schema_' . $tmpname . '(\'new_schema_' . $tmpname . '\')">here.</a> ');
}
$rootEl = $this->xml->firstChild;
}
$schemaLocation = NULL;
if ($this->forceSchema) {
// hack because you cant easily get the static property value from
// a subclass.
$vars = get_class_vars(get_class($this));
$schemaLocation = $vars['SCHEMA_URI'];
}
elseif ($rootEl->attributes->getNamedItem('schemaLocation') !== NULL) {
//figure out where the schema is located and validate.
list(, $schemaLocation) = preg_split('/\s+/', $rootEl->attributes->getNamedItem('schemaLocation')->nodeValue);
}
$schemaLocation = NULL;
return TRUE;
if ($schemaLocation !== NULL) {
if (!$this->xml->schemaValidate($schemaLocation)) {
$ret = FALSE;
$errors = libxml_get_errors();
foreach ($errors as $err) {
self::$errors[] = 'XML Error: Line ' . $err->line . ': ' . $err->message;
}
}
else {
$this->name = $rootEl->attributes->getNamedItem('name')->nodeValue;
}
}
else {
$ret = FALSE;
self::$errors[] = 'Unable to load schema.';
}
$this->valid = $ret;
}
return $this->valid;
}
} }

80
islandora_basic_collection/includes/ManagePolicies.inc

@ -63,6 +63,17 @@ function islandora_manage_policies_form($form, &$form_state, $collection_pid) {
'#default_value' => $namespace, '#default_value' => $namespace,
'#description' => t("Choose namespace for objects in this collection associated with this content model"), '#description' => t("Choose namespace for objects in this collection associated with this content model"),
); );
$form['parent_collection'] = array(
'#type' => 'hidden',
'#value' => $collection_pid,
);
$form['collection_pid'] = array(
'#type' => 'hidden',
'#value' => $collection_pid,
);
$form['manage_collection_policy']['add']['submit'] = array( $form['manage_collection_policy']['add']['submit'] = array(
'#type' => 'submit', '#type' => 'submit',
'#value' => t('Add Content Model to Collection Policy'), '#value' => t('Add Content Model to Collection Policy'),
@ -99,19 +110,55 @@ function islandora_manage_policies_form_validate($form, &$form_state) {
} }
function islandora_manage_policies_form_submit($form, &$form_state) { function islandora_manage_policies_form_submit($form, &$form_state) {
module_load_include('inc', 'islandora_basic_collection', 'CollectionPolicy');
module_load_include('inc', 'islandora', 'RestConnection'); module_load_include('inc', 'islandora', 'RestConnection');
$restConnection = new RestConnection(); $restConnection = new RestConnection();
$collection_pid = $form_state['values']['parent_collection'];
$collection_object = new FedoraObject($collection_pid, $restConnection->repository);
$policy = $collection_object->getDatastream(variable_get('Islandora_Collection_Policy_DSID', 'COLLECTION_POLICY'));
$collection_policy = '<?xml version="1.0" encoding="UTF-8"?>
<collection_policy xmlns="http://www.islandora.ca" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="" xsi:schemaLocation="http://www.islandora.ca http://syn.lib.umanitoba.ca/collection_policy.xsd">
<content_models>
<content_model dsid="ISLANDORACM" name="Islandora Collection Model ~ islandora:collectionCModel" namespace="islandora:1" pid="islandora:collectionCModel"/>
</content_models>
<search_terms>
</search_terms>
<staging_area></staging_area>
<relationship>isMemberOfCollection</relationship>
</collection_policy>';
$collection_policy_xml = new DOMDocument();
$collection_policy_xml->loadXML($policy->content);
if ($form_state['clicked_button']['#id'] == 'add_cm') { if ($form_state['clicked_button']['#id'] == 'add_cm') {
if (!$policy) { if (!$policy->content) {
$item = new Fedora_Item($collection_pid); $collection_policy_datastream = $collection_object->constructDatastream('COLLECTION_POLICY', 'X');
$item->add_datastream_from_string($collection_policy, 'COLLECTION_POLICY', 'COLLECTION_POLICY', 'text/xml', 'X'); $collection_policy_datastream->setContentFromString($collection_policy);
$policy = CollectionPolicy::loadFromCollection($collection_pid, TRUE); $collection_policy_datastream->label = 'COLLECTION_POLICY';
$collection_object->ingestDatastream($collection_policy_datastream);
$policy = $collection_object->getDatastream(variable_get('Islandora_Collection_Policy_DSID', 'COLLECTION_POLICY'));
} }
$cp_namespace = $form_state['values']['new_cp_namespace']; $cp_namespace = $form_state['values']['new_cp_namespace'];
$cp_content_model = $form_state['values']['content_model_to_add']; $cp_content_model = $form_state['values']['content_model_to_add'];
$policy->addModel(ContentModel::loadFromModel($cp_content_model), $cp_namespace); $content_model_object = new FedoraObject($cp_content_model, $restConnection->repository);
$policy->saveToFedora(); $content_model_datastream = $content_model_object->getDatastream(variable_get('Islandora_Content_Model_DSID', 'ISLANDORACM'));
$content_models_element = $collection_policy_xml->getElementsByTagName('content_models');
$content_model_element = $content_models_element->item(0)->getElementsByTagName('content_model');
$content_model_element = $collection_policy_xml->createElement('content_model');
$content_model_element->setAttribute('name', $content_model_object->label);
$content_model_element->setAttribute('dsid', variable_get('Islandora_Content_Model_DSID', 'ISLANDORACM'));
$content_model_element->setAttribute('namespace', $cp_namespace);
$content_model_element->setAttribute('pid', $content_model_object->id);
$content_models_element->item(0)->appendChild($content_model_element);
$new_collection_policy_datastream = $collection_object->getDatastream('COLLECTION_POLICY');
$new_collection_policy_datastream->setContentFromString($collection_policy_xml->saveXML());
$new_collection_policy_datastream->label = 'COLLECTION_POLICY';
$collection_object->ingestDatastream($new_collection_policy_datastream);
drupal_set_message("Collection model successfully added"); drupal_set_message("Collection model successfully added");
} }
@ -119,14 +166,31 @@ function islandora_manage_policies_form_submit($form, &$form_state) {
if ($form_state['clicked_button']['#id'] == 'remove_cm') { if ($form_state['clicked_button']['#id'] == 'remove_cm') {
$candidates = $form_state['values']['content_models_to_remove']; $candidates = $form_state['values']['content_models_to_remove'];
$count = 0; $count = 0;
$s = '';
foreach ($candidates as $candidate) { foreach ($candidates as $candidate) {
if (is_string($candidate)) { if (is_string($candidate)) {
$policy->removeModel(ContentModel::loadFromModel($candidate));
$content_models_element = $collection_policy_xml->getElementsByTagName('content_models');
$models = $content_models_element->item(0)->getElementsByTagName('content_model');
$found = FALSE;
for ($i = 0; $found === FALSE && $i < $models->length; $i++) {
if ($models->item($i)->getAttribute('pid') == $candidate) {
$found = $models->item($i);
}
}
if ($found !== FALSE && $models->length > 1) {
$content_models_element->item(0)->removeChild($found);
$ret = TRUE;
}
$count++; $count++;
} }
} }
if ($count > 0) { if ($count > 0) {
$policy->saveToFedora(); $new_collection_policy_datastream = $collection_object->getDatastream('COLLECTION_POLICY');
$new_collection_policy_datastream->setContentFromString($collection_policy_xml->saveXML());
$new_collection_policy_datastream->label = 'COLLECTION_POLICY';
$collection_object->ingestDatastream($new_collection_policy_datastream);
if ($count > 1) { if ($count > 1) {
$s = 's'; $s = 's';
} }

Loading…
Cancel
Save