diff --git a/islandora_basic_collection/includes/change_content_models.inc b/islandora_basic_collection/includes/change_content_models.inc new file mode 100644 index 00000000..6ae8b9b2 --- /dev/null +++ b/islandora_basic_collection/includes/change_content_models.inc @@ -0,0 +1,150 @@ +repository); + $cm_name = $item->Label; + $cm_options[$content_model] = $cm_name; + } + } + + $namespace = substr($collection_pid, 0, strpos($collection_pid, ":")); + + $collection_policy_dsid = variable_get('Islandora_Collection_Policy_DSID', 'COLLECTION_POLICY'); + $collection_object = new FedoraObject($collection_pid, $rest_connection->repository); + $collection_policy_datastream = $collection_object->getDatastream($collection_policy_dsid); + + $supported_collection_models = array(); + if ($collection_policy_datastream && $collection_policy_datastream->content) { + $collection_policy = new CollectionPolicy($collection_policy_datastream->content); + $supported_collection_models = $collection_policy->getContentModels(); + } + $collection_namespace = substr($collection_pid, 0, strpos($collection_pid, ":")); + + $collection_name = $collection_object->label; + $new_content_models = get_content_models_as_option_array(); + $current_models_in_policy = array(); + + if ($supported_collection_models) { + foreach ($supported_collection_models as $supported_model) { + $current_models_in_policy[$supported_model['pid']] = $supported_model['pid']; + } + } + + $form['change_cmodel']['titlebox'] = array( + '#type' => 'item', + '#title' => t("Change content models within @collection_pid", array('@collection_pid' => $collection_pid)), + ); + + $form['change_cmodel']['current_content_model'] = array( + '#title' => "Choose content model to be changed", + '#type' => 'select', + '#options' => $current_models_in_policy, + '#description' => t("All objects in this collection with the selected content model will be changed."), + ); + + $form['change_cmodel']['new_content_model'] = array( + '#title' => "Choose new content model", + '#type' => 'select', + '#options' => $new_content_models, + '#description' => t("The new content model to be assigned to selected objects."), + ); + + $form['change_cmodel']['collection_pid'] = array( + '#type' => 'hidden', + '#value' => $collection_pid, + ); + + $form['change_cmodel']['submit'] = array( + '#type' => 'submit', + '#value' => t('Change content model associations'), + '#id' => 'change_model', + ); + + return $form; +} + +function islandora_change_content_models_form_validate($form, &$form_state) { + +} + +function islandora_change_content_models_form_submit($form, &$form_state) { + module_load_include('inc', 'islandora_basic_collection', 'includes/CollectionPolicy'); + module_load_include('inc', 'islandora', 'RestConnection'); + $rest_connection = new RestConnection(); + + $current_content_model = $form_state['values']['current_content_model']; + $new_content_model = $form_state['values']['new_content_model']; + $collection_pid = $form_state['values']['collection_pid']; + + $current_content_model_object = new FedoraObject($current_content_model, $rest_connection->repository); + + $collection_object = new FedoraObject($form_state['values']['collection_pid'], $rest_connection->repository); + $collection_policy_datastream = $collection_object->getDatastream(variable_get('Islandora_Collection_Policy_DSID', 'COLLECTION_POLICY')); + $policy = new CollectionPolicy($collection_policy_datastream->content); + + $collection_policy_xml = new DOMDocument(); + $collection_policy_xml->loadXML($collection_policy_datastream->content); + + $add_to_policy = TRUE; + $policy_content_models = $policy->getContentModels(); + foreach ($policy_content_models as $policy_content_model) { + if ($policy_content_model['pid'] == $current_content_model) { + $namespace = $policy_content_model['namespace']; + } + if ($policy_content_model['pid'] == $new_content_model) { + $add_to_policy = FALSE; + } + } + if ($add_to_policy) { + $new_content_model_object = new FedoraObject($new_content_model, $rest_connection->repository); + $new_content_model_datastream = $new_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', $new_content_model_object->label); + $content_model_element->setAttribute('dsid', variable_get('Islandora_Content_Model_DSID', 'ISLANDORACM')); + $content_model_element->setAttribute('namespace', $namespace); + $content_model_element->setAttribute('pid', $new_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); + } + + $query = "select \$object from <#ri> + where (\$object + and (\$object + or \$object ) + and \$object )"; + + $objects = $rest_connection->repository->ri->itqlQuery($query, 'unlimited', '0'); + + $count = 0; + foreach ($objects as $object) { + if (!$object['object']['value']) { + continue; + } + $fedora_item = new FedoraObject($object['object']['value'], $rest_connection->repository); + $fedora_item->relationships->remove(FEDORA_MODEL_URI, 'hasModel', $current_content_model); + $fedora_item->relationships->add(FEDORA_MODEL_URI, 'hasModel', $new_content_model); + $count++; + } + drupal_set_message(t('@current_content_model changed to @new_content_model on @count objects', array('@current_content_model' => $current_content_model, '@new_content_model' => $new_content_model, '@count' => $count))); +} \ No newline at end of file diff --git a/islandora_basic_collection/includes/child_collection.inc b/islandora_basic_collection/includes/child_collection.inc new file mode 100644 index 00000000..669442c5 --- /dev/null +++ b/islandora_basic_collection/includes/child_collection.inc @@ -0,0 +1,167 @@ + 'item', + '#title' => t("This object is not a collection and so can't contain child collections. To fix this add the islandora:collectionCModel to this object's collection policy"), + ); + return $form; + } + $restricted = FALSE; + if (variable_get('fedora_namespace_restriction_enforced', TRUE)) { + $restricted = TRUE; + $allowed_string = variable_get('fedora_pids_allowed', 'default: demo: changeme: islandora:'); + $namespaces = explode(':', $allowed_string); + foreach ($namespaces as $namespace) { + if ($namespace) { + $allowed[trim($namespace)] = trim($namespace); + } + } + } + $collection_namespace = substr($this_collection_pid, 0, strpos($this_collection_pid, ":")); + + $content_models = get_content_models_as_option_array(); + unset($content_models['islandora:collectionCModel']); + + $form['child_creation']['titlebox'] = array( + '#type' => 'item', + '#title' => t("Create new child collection within @collection", array('@collection' => $this_collection_pid)), + ); + + $form['child_creation']['collection_name'] = array( + '#title' => "Collection name", + '#type' => 'textfield', + '#size' => 25, + '#description' => t("Human readable name for this collection"), + ); + + $form['child_creation']['new_collection_pid'] = array( + '#title' => "Collection PID", + '#type' => 'textfield', + '#size' => 15, + '#default_value' => $rest_connection->repository->api->m->getNextPid($collection_namespace), + '#description' => t("Unique PID for this collection.
Pids take the general form of namespace:collection (eg. islandora:pamphlets)"), + ); + + if (!$restricted) { + $form['child_creation']['collection_namespace'] = array( + '#title' => "Collection namespace", + '#type' => 'textfield', + '#size' => 15, + '#default_value' => $collection_namespace, + '#description' => t("Namespace for objects in this collection."), + ); + } + else { + $form['child_creation']['collection_namespace'] = array( + '#title' => "Collection namespace", + '#type' => 'select', + '#options' => $allowed, + '#default_value' => 'default', + '#description' => t("Namespace for objects in this collection."), + ); + } + + $form['current'] = array( + '#type' => 'hidden', + '#value' => $this_collection_pid, + ); + + $form['child_creation']['content_models'] = array( + '#title' => "Choose allowable content models for this collection", + '#type' => 'checkboxes', + '#options' => $content_models, + '#description' => t("Content models describe the behaviours of objects with which they are associated."), + ); + + $form['child_creation']['submit'] = array( + '#type' => 'submit', + '#value' => t('Create collection'), + '#id' => 'create_child' + ); + return $form; +} + +function islandora_create_child_collection_form_validate($form, &$form_state) { + +} + +/** + * Submit handler for child collection creation form + * + * @global type $base_url + * @param array $form + * @param array $form_state + */ +function islandora_create_child_collection_form_submit($form, &$form_state) { + global $base_url; + module_load_include('inc', 'islandora', '/includes/islandora.ingest'); + $thumbnail = $base_url . '/' . drupal_get_path('module', 'islandora_basic_collection') . '/Crystal_Clear_filesystem_folder_grey.png'; + $new_collection_pid = $form_state['values']['new_collection_pid']; + $new_collection_label = $form_state['values']['collection_name']; + $namespace = $form_state['values']['collection_namespace']; + $all_cModels = get_content_models_as_option_array(); + $collection_content_models = array(array('pid' => 'islandora:collectionCModel')); + $relationship = 'isMemberOfCollection'; + + $collection_policy = ' + + + + + + + + isMemberOfCollection +'; + + $content_models = $form_state['values']['content_models']; + + $collection_policy_xml = new DOMDocument(); + $collection_policy_xml->loadXML($collection_policy); + + $content_models_element = $collection_policy_xml->getElementsByTagName('content_models'); + $content_model_element = $content_models_element->item(0)->getElementsByTagName('content_model'); + foreach ($content_models as $content_model) { + if ($content_model) { + $content_model_element = $collection_policy_xml->createElement('content_model'); + $content_model_element->setAttribute('name', $all_cModels[$content_model]); + $content_model_element->setAttribute('dsid', variable_get('Islandora_Content_Model_DSID', 'ISLANDORACM')); + $content_model_element->setAttribute('namespace', $namespace . ':1'); + $content_model_element->setAttribute('pid', $content_model); + $content_models_element->item(0)->appendChild($content_model_element); + } + } + $fedora_object = islandora_ingest_get_object($collection_content_models, $form_state['values']['current'], $relationship, $new_collection_pid); + $fedora_object->label = $new_collection_label; + $thumbnail_datastream = $fedora_object->constructDatastream('TN'); + $thumbnail_datastream->setContentFromUrl($thumbnail); + $thumbnail_datastream->label = 'Thumbnail'; + $thumbnail_datastream->mimetype = 'image/png'; + $fedora_object->ingestDatastream($thumbnail_datastream); + $policy_datastream = $fedora_object->constructDatastream(variable_get('Islandora_Collection_Policy_DSID', 'COLLECTION_POLICY'), 'X'); + $policy_datastream->setContentFromString($collection_policy_xml->saveXML()); + $policy_datastream->label = 'Collection policy'; + $fedora_object->ingestDatastream($policy_datastream); + $new_fedora_object = islandora_ingest_add_object($fedora_object); + + drupal_goto('/islandora/object/' . $new_collection_pid); +} \ No newline at end of file diff --git a/islandora_basic_collection/includes/collection_management.inc b/islandora_basic_collection/includes/collection_management.inc new file mode 100644 index 00000000..85541ee6 --- /dev/null +++ b/islandora_basic_collection/includes/collection_management.inc @@ -0,0 +1,239 @@ + +where (\$object + or \$object ) + and \$object \$model + and \$object \$title"; + + $model_pids = $rest_connection->repository->ri->itqlQuery($query, 'unlimited', '0'); + $represented_models = array(); + foreach ($model_pids as $model_pid) { + + if ($model_pid['model']['value'] && $model_pid['model']['value'] != 'fedora-system:FedoraObject-3.0') { + $fedora_object = new FedoraObject($model_pid['model']['value'], $rest_connection->repository); + $content_model_title = $fedora_object->label; + $represented_models[$model_pid['model']['value']] = $model_pid['model']['value'] . ' ~ ' . $content_model_title; + } + } + return $represented_models; +} + +function get_child_collections($collection_pid) { + module_load_include('inc', 'islandora', 'RestConnection'); + $rest_connection = new RestConnection(); + require_once 'sites/all/libraries/tuque/RepositoryQuery.php'; + + $query = << + where \$object + and \$object +EOD; + + $lines = $rest_connection->repository->ri->itqlQuery($query, 'unlimited', '0'); + $collection_pids = array_values(array_filter($lines)); + return $collection_pids; +} + +function islandora_collections_get_collection_from_pid($pid) { + module_load_include('inc', 'islandora', 'RestConnection'); + $rest_connection = new RestConnection(); + require_once 'sites/all/libraries/tuque/RepositoryQuery.php'; + + $query = 'select $parent from <#ri> + where ($object $parent + or $object $parent) + and $object \'' . $pid . '\' + order by $object'; + + $object_pids = $rest_connection->repository->ri->itqlQuery($query, 'unlimited', '0'); + $object_pids = array_values(array_filter($object_pids)); + return $object_pids; +} + +/** + * Returns an array of pids that match the query contained in the collection + * object's QUERY datastream or in the suppled $query parameter. + * @param $collection_pid + * @param $query + * @param $query_format R + */ +function get_related_items_as_array($collection_pid, $relationship = array('isMemberOfCollection'), $limit = 10000, $offset = 0, $active_objects_only = TRUE, $cmodel = NULL, $orderby = '$title') { + module_load_include('inc', 'islandora', 'RestConnection'); + $rest_connection = new RestConnection(); + require_once 'sites/all/libraries/tuque/RepositoryQuery.php'; + + global $user; + +// Not sure if this is necessary given that we should never be able to delete objects in a namespace that we don't have access to. +// if (!fedora_repository_access('view fedora repository', $collection_pid['object']['value'])) { +// drupal_set_message(t("You do not have access to Fedora objects within the attempted namespace or access to Fedora denied."), 'error'); +// return array(); +// } + + $query_string = 'select $object $title $content from <#ri> + where ($object $title + and $object $content + and ('; + + if (is_array($relationship)) { + foreach ($relationship as $rel) { + $query_string .= '$object '; + if (next($relationship)) { + $query_string .= ' OR '; + } + } + } + elseif (is_string($relationship)) { + $query_string .= '$object '; + } + else { + return ''; + } + + $query_string .= ') '; + $query_string .= $active_objects_only ? 'and $object ' : ''; + + if ($cmodel) { + $query_string .= ' and $content '; + } + + $query_string .= ') + minus $content + order by ' . $orderby; + + $results = $rest_connection->repository->ri->itqlQuery($query_string, $limit, $offset); + + return $results; +} + +/** + * determines whether we can see the object or not + * checks PID namespace permissions, and user permissions + * @global type $user + * @param type $op + * @param type $pid + * @return type + */ +function fedora_repository_access($permission, $pid) { + global $user; + $name_space_access = FALSE; + $is_restricted = variable_get('islandora_namespace_restriction_enforced', TRUE); + if (!$is_restricted) { + $name_space_access = TRUE; + } + if ($pid == NULL) { + $pid = variable_get('islandora_repository_pid', 'islandora:root'); + } + $name_space_allowed = explode(" ", variable_get('fedora_pids_allowed', 'default: demo: changeme: islandora: ilives: islandora-book: books: newspapers: ')); + $pos = NULL; + foreach ($name_space_allowed as $name_space) { + $pos = stripos($pid, $name_space); + if ($pos === 0) { + $name_space_access = TRUE; + } + } + if ($name_space_access) { + $user_access = user_access($permission); + if ($user_access == NULL) { + return FALSE; + } + return $user_access; + } + else { + return FALSE; + } +} + +/** + * gets the name of the content models for the specified object + * this now returns an array of pids as in Fedora 3 we can have more then one Cmodel for an object + * @param type $pid + * @param type $include_fedora_system_content_models + * @return array + */ +function get_content_models_list($pid, $include_fedora_system_content_models = FALSE) { + + module_load_include('inc', 'islandora', 'RestConnection'); + require_once 'sites/all/libraries/tuque/RepositoryQuery.php'; + $rest_connection = new RestConnection(); + $pids = array(); + $query = 'select $object from <#ri> + where $object + and $object '; + $content_models = $rest_connection->repository->ri->itqlQuery($query, 'unlimited', '0'); + + if (empty($content_models)) { + return $pids; + } + + $cmodels = array(); + foreach ($content_models as $content_model) { + if (strpos($content_model['object']['uri'], 'fedora-system:FedoraObject-3.0') != FALSE && $include_fedora_system_content_models == FALSE) { + continue; + } + $cmodels[] = substr(strstr($content_model['object']['uri'], '/'), 1); + } + + return $cmodels; +} + +/** + * Function: get_content_models_as_option_array + * + * Description: Returns an associative array of all available content models in Fedora instance + * + * @return array + */ +function get_content_models_as_option_array() { + module_load_include('inc', 'islandora', 'RestConnection'); + $rest_connection = new RestConnection(); + require_once 'sites/all/libraries/tuque/RepositoryQuery.php'; + + $restricted = variable_get('fedora_namespace_restriction_enforced', TRUE); + $allowed_string = variable_get('fedora_pids_allowed', 'default: demo: changeme: islandora:'); + $namespaces = explode(':', $allowed_string); + foreach ($namespaces as $namespace) { + if ($namespace) { + $allowed[] = trim($namespace); + } + } + $query = 'select $object $title from <#ri> + where ($object $title + and ($object + or $object ) + and $object ) + order by $title'; + + $list = $rest_connection->repository->ri->itqlQuery($query, 'unlimited', '0'); + $options = array(); + foreach ($list as $item) { //removes blanks + if ($item) { + $item_namespace = explode(':', $item['object']['value']); + if (!$restricted || in_array($item_namespace[0], $allowed)) { + + if (!preg_match('/fedora-system/', $item['object']['value'])) { + $options[$item['object']['value']] = $item['title']['value'] . ' ~ ' . $item['object']['value']; + } + } + } + } + + return $options; +} \ No newline at end of file diff --git a/islandora_basic_collection/includes/collection_manager_table.inc b/islandora_basic_collection/includes/collection_manager_table.inc new file mode 100644 index 00000000..63a91b25 --- /dev/null +++ b/islandora_basic_collection/includes/collection_manager_table.inc @@ -0,0 +1,117 @@ + + where ($object + or $object ) + and $object $title'; + $results = $rest_connection->repository->ri->itqlQuery($query, 'unlimited', '0'); + $keys = array(); + $objects = array(); + foreach ($results as $result) { + $objects[$result['object']['value']] = $result['title']['value']; + $keys[] = $result['object']['value']; + } + $rows = array(); + foreach ($objects as $key => $object) { + $rows[$key] = array( + '#pid' => $key, + 'pid' => array('#value' => l($key, 'islandora/object/' . $key)), + 'title' => array( + 'data' => array( + '#type' => 'link', + '#title' => $object, + '#href' => 'islandora/object/' . $key, + ), + ), + ); + } + + $header = array( + 'title' => array('data' => t('Title')), + ); + + if (!$rows) { + return; + } + + $table = array( + '#type' => 'tableselect', + '#header' => $header, + '#options' => $rows, + ); + + return $table; + +} + +/** + * themes the form table. + * + * @param array $element Drupal Form Element. + * @return string + */ +function theme_islandora_basic_collection_management_form_table(array $element) { + $rows = array(); + foreach (element_children($element['rows']) as $child) { + $setting = $element['rows'][$child]; + $pid = $setting['#pid']; + $fields = array( + drupal_render($element['selections'][$pid]) // First field is a checkbox + ); + foreach (element_children($setting) as $property) { + $field = $setting[$property]; + $fields[] = drupal_render($field); + } + $rows[] = array( + 'data' => $fields, + 'class' => isset($setting['#attributes']['class']) ? $setting['#attributes']['class'] : NULL + ); + } + $attributes = isset($element['#id']) ? array('id' => $element['#id']) : NULL; + return theme_table($element['#header'], $rows, $attributes); +} + +function get_collections_as_option_array() { + module_load_include('inc', 'islandora', 'RestConnection'); + require_once 'sites/all/libraries/tuque/RepositoryQuery.php'; + + $restricted = variable_get('fedora_namespace_restriction_enforced', TRUE); + $allowed_string = variable_get('fedora_pids_allowed', 'default: demo: changeme: islandora:'); + $namespaces = explode(':', $allowed_string); + + $rest_connection = new RestConnection(); + $query = 'select $object $title from <#ri> + where ($object $title + and $object + and $object ) + order by $title'; + $results = $rest_connection->repository->ri->itqlQuery($query, 'unlimited', '0'); + foreach ($namespaces as $namespace) { + $trimmed_names[] = trim($namespace); + } + $options = array(); + foreach ($results as $item) { //removes blanks + $namespace = explode(':', $item['object']['value']); + $namespace = trim($namespace[0]); + if (!$restricted || in_array($namespace, $trimmed_names)) { + $options[$item['object']['value']] = $item['title']['value']; + } + } + unset($options['islandora:ContentModelCollection']); + return $options; +} \ No newline at end of file diff --git a/islandora_basic_collection/includes/delete_collection.inc b/islandora_basic_collection/includes/delete_collection.inc new file mode 100644 index 00000000..3f0e441c --- /dev/null +++ b/islandora_basic_collection/includes/delete_collection.inc @@ -0,0 +1,126 @@ + 'item', + '#title' => t("Delete objects from @collection_pid", array('@collection_pid' => $pid)), + ); + + if ($table) { + $form['collection_delete']['table'] = array( + 'table' => $table, + ); + } + else { + $form['collection_delete']['delete_root'] = array( + '#type' => 'checkbox', + '#title' => "Remove this empty collection?", + '#id' => 'delete_collection', + ); + } + $form['current'] = array( + '#type' => 'hidden', + '#value' => $pid, + ); + + $form['collection_delete']['message'] = array( + '#type' => 'item', + '#value' => t("This action is permanant and cannot be undone."), + ); + $form['collection_delete']['submit'] = array( + '#type' => 'submit', + '#description' => t("This action is permanant and cannot be undone."), + '#value' => t('@message', array('@message' => $submit_text_message)), + ); + + return $form; +} + +/** + * Submit handler for object deletion form in the collection manager. + * + * @global type $user + * @param array $form + * @param array $form_state + */ +function islandora_collection_deletion_form_submit($form, &$form_state) { + global $user; + module_load_include('inc', 'islandora', 'RestConnection'); + $rest_connection = new RestConnection(); + + $collection_pid = $form_state['values']['current']; + $fedora_object = new FedoraObject($collection_pid, $rest_connection->repository); + $parents = $fedora_object->relationships->get(NULL, 'isMemberOfCollection'); + $parents = Islandora_collections_get_collection_from_pid($collection_pid); + $collection_pid = $form_state['values']['current']; + if (isset($form_state['values']['delete_root']) && $form_state['values']['delete_root'] == 1) { + delete_root_collection($collection_pid); + drupal_goto("islandora/object/" . $parents[0]['parent']['value']); + } + + $child_collections = get_child_collections($collection_pid); + + $populated_child_collections = array(); + $pids = @array_filter($form_state['values']['table']); + + if (!empty($child_collections)) { + foreach ($child_collections as $child) { + $child_pids = get_related_items_as_array($child, 'isMemberOfCollection'); + if (!empty($child_pids)) { + $populated_child_collections[] = $child; + } + } + } + if (!empty($populated_child_collections)) { + $conflict = FALSE; + foreach ($populated_child_collections as $collection) { + if (in_array($collection, $pids)) { + $conflict = TRUE; + drupal_set_message(t("Populated child collections were not deleted.")); + } + } + } + $pids_to_delete = array_diff($pids, $populated_child_collections); + + foreach ($pids_to_delete as $pid_to_delete) { + $rest_connection->repository->purgeObject($pid_to_delete); + } + drupal_goto("islandora/object/" . $collection_pid); +} + +/** + * Deletes the collection object + * + * @param string $pid + */ +function delete_root_collection($pid) { + module_load_include('inc', 'islandora', 'RestConnection'); + try { + $rest_connection = new RestConnection(); + $rest_connection->repository->purgeObject($pid); + } catch (Exception $e) { + drupal_set_message(t("Collection '@pid' could not be deleted!", array('@pid' => $pid)), 'error'); + return; + } + drupal_set_message(t("Collection '@pid' deleted.", array('@pid' => $pid))); +} diff --git a/islandora_basic_collection/includes/manage_policies.inc b/islandora_basic_collection/includes/manage_policies.inc new file mode 100644 index 00000000..3b6572c9 --- /dev/null +++ b/islandora_basic_collection/includes/manage_policies.inc @@ -0,0 +1,223 @@ +repository); + $cm_name = $item->Label; + $cm_options[$content_model] = $cm_name; + } + } + + $namespace = substr($collection_pid, 0, strpos($collection_pid, ":")); + + $collection_policy_dsid = variable_get('Islandora_Collection_Policy_DSID', 'COLLECTION_POLICY'); + $collection_object = new FedoraObject($collection_pid, $rest_connection->repository); + $collection_policy_string = $collection_object->getDatastream($collection_policy_dsid); + + $supported_collection_models = array(); + if ($collection_policy_string) { + $collection_policy = new CollectionPolicy($collection_policy_string->content); + $supported_collection_models = $collection_policy->getContentModels(); + } + + $collection_namespace = substr($collection_pid, 0, strpos($collection_pid, ":")); + + $represented_content_models = get_represented_content_models($collection_pid); + $collection_name = $collection_object->label; + $new_content_models = get_content_models_as_option_array(); + $current_models_in_policy = array(); + + if ($supported_collection_models) { + foreach ($supported_collection_models as $supported_model) { + $current_models_in_policy[$supported_model['pid']] = $supported_model['pid']; + } + } + + $new_options = array(); + if (is_array($new_content_models) && is_array($current_models_in_policy)) { + $new_options = array_diff_key($new_content_models, $current_models_in_policy); + } + + $s = ''; + if (count($current_models_in_policy) > 1) { + $s = 's'; + } + + $form['manage_collection_policy']['titlebox'] = array( + '#type' => 'item', + '#title' => t("Manage collection policy for @collection_pid", array('@collection_pid' => $collection_pid)), + ); + + $form ['manage_collection_policy']['add']['content_model_to_add'] = array( + '#title' => "Choose content model", + '#type' => 'select', + '#options' => $new_options, + '#description' => t("Choose content model to add to this collection policy."), + ); + + $form ['manage_collection_policy']['add']['new_cp_namespace'] = array( + '#title' => "Choose namespace", + '#type' => 'textfield', + '#size' => 15, + '#default_value' => $namespace, + '#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( + '#type' => 'submit', + '#value' => t('Add content model to collection policy'), + '#id' => 'add_cm' + ); + + if (count($current_models_in_policy) > 0) { + $form['manage_collection_policy']['remove'] = array( + '#title' => "Delete content model from collection policy", + '#type' => 'fieldset', + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + + $form ['manage_collection_policy']['remove']['content_models_to_remove'] = array( + '#title' => "Choose content model to remove", + '#type' => 'checkboxes', + '#options' => $current_models_in_policy, + '#description' => t("Choose content models to remove from this collection policy."), + ); + + $form['manage_collection_policy']['remove']['submit'] = array( + '#type' => 'submit', + '#value' => t('Remove content model@s from collection policy', array('@s' => $s)), + '#id' => 'remove_cm' + ); + } + return $form; +} + +function islandora_manage_policies_form_validate($form, &$form_state) { + +} + +/** + * Submit handler for collection policy management form + * + * @param array $form + * @param array $form_state + */ +function islandora_manage_policies_form_submit($form, &$form_state) { + module_load_include('inc', 'islandora', 'RestConnection'); + $rest_connection = new RestConnection(); + + $collection_pid = $form_state['values']['parent_collection']; + $collection_object = new FedoraObject($collection_pid, $rest_connection->repository); + $policy = $collection_object->getDatastream(variable_get('Islandora_Collection_Policy_DSID', 'COLLECTION_POLICY')); + $collection_policy = ' + + + + + + + + isMemberOfCollection +'; + + $collection_policy_xml = new DOMDocument(); + $collection_policy_xml->loadXML($policy->content); + + if ($form_state['clicked_button']['#id'] == 'add_cm') { + if (empty($policy->content)) { + $collection_policy_datastream = $collection_object->constructDatastream('COLLECTION_POLICY', 'X'); + $collection_policy_datastream->setContentFromString($collection_policy); + $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_content_model = $form_state['values']['content_model_to_add']; + $content_model_object = new FedoraObject($cp_content_model, $rest_connection->repository); + $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(t("Collection model @model successfully added", array('@model' => $content_model_object->label))); + } + + //remove content model from collection policy + if ($form_state['clicked_button']['#id'] == 'remove_cm') { + $candidates = $form_state['values']['content_models_to_remove']; + $count = 0; + $s = ''; + foreach ($candidates as $candidate) { + if (is_string($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 > 0) { + $content_models_element->item(0)->removeChild($found); + $ret = TRUE; + } + $count++; + } + } + if ($count > 0) { + $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) { + $s = 's'; + } + drupal_set_message(t("$count collection model@s removed", array('@s' => $s))); + } + } +} \ No newline at end of file diff --git a/islandora_basic_collection/includes/move_collection.inc b/islandora_basic_collection/includes/move_collection.inc new file mode 100644 index 00000000..7f6a2d6f --- /dev/null +++ b/islandora_basic_collection/includes/move_collection.inc @@ -0,0 +1,78 @@ + 'item', + '#title' => t('No objects found in this collection'), + ); + return $form; + } + $form = array(); + + $form['migrate']['titlebox'] = array( + '#type' => 'item', + '#title' => t("Move objects from @collection_pid", array('@collection_pid' => $pid)), + ); + + $form['migrate']['new_collection'] = array( + '#title' => t('New collection'), + '#description' => t("All content will be migrated from @pid to the selected collection", array('@pid' => $pid)), + '#type' => 'select', + '#options' => $potential_collections, + ); + + $form['migrate']['table'] = $table; + + $form['current'] = array( + '#type' => 'hidden', + '#value' => $pid, + ); + + $form['migrate']['message'] = array( + '#type' => 'item', + '#value' => t(""), + ); + + $form['migrate']['submit'] = array( + '#type' => 'submit', + '#value' => t('Migrate selected objects'), + ); + return $form; +} + +/** + * Submit handler for object migration form + * + * @param array $form + * @param array $form_state + */ +function islandora_collection_migrate_form_submit($form, &$form_state) { + module_load_include('inc', 'islandora', 'RestConnection'); + $rest_connection = new RestConnection(); + + $pids = array_filter($form_state['values']['table']); + $new_collection = $form_state['values']['new_collection']; + $current = $form_state['values']['current']; + foreach ($pids as $pid) { + $fedora_object = new FedoraObject($pid, $rest_connection->repository); + $fedora_object->relationships->remove(FEDORA_RELS_EXT_URI, 'isMemberOfCollection', $current); + $fedora_object->relationships->add(FEDORA_RELS_EXT_URI, 'isMemberOfCollection', $new_collection); + } +} \ No newline at end of file diff --git a/islandora_basic_collection/islandora_basic_collection.module b/islandora_basic_collection/islandora_basic_collection.module index 4314de6a..f5ccf59c 100644 --- a/islandora_basic_collection/islandora_basic_collection.module +++ b/islandora_basic_collection/islandora_basic_collection.module @@ -88,13 +88,13 @@ function islandora_basic_collection_menu_local_tasks_alter(&$data, $router_item, */ function islandora_basic_collection_manage_object($object_id) { - module_load_include('inc', 'islandora_basic_collection', 'includes/CollectionManagement'); - module_load_include('inc', 'islandora_basic_collection', 'includes/CollectionManagerTable'); - module_load_include('inc', 'islandora_basic_collection', 'includes/DeleteCollection'); - module_load_include('inc', 'islandora_basic_collection', 'includes/MoveCollection'); - module_load_include('inc', 'islandora_basic_collection', 'includes/ChildCollection'); - module_load_include('inc', 'islandora_basic_collection', 'includes/ManagePolicies'); - module_load_include('inc', 'islandora_basic_collection', 'includes/ChangeContentModels'); + module_load_include('inc', 'islandora_basic_collection', 'includes/collection_management'); + module_load_include('inc', 'islandora_basic_collection', 'includes/collection_manager_table'); + module_load_include('inc', 'islandora_basic_collection', 'includes/delete_collection'); + module_load_include('inc', 'islandora_basic_collection', 'includes/move_collection'); + module_load_include('inc', 'islandora_basic_collection', 'includes/child_collection'); + module_load_include('inc', 'islandora_basic_collection', 'includes/manage_policies'); + module_load_include('inc', 'islandora_basic_collection', 'includes/change_content_models'); $form = array(); @@ -200,15 +200,11 @@ function islandora_basic_collection_theme($existing, $type, $theme, $path) { 'islandora_basic_collection_grid' => array( 'template' => 'islandora-basic-collection-grid', 'pattern' => 'islandora_basic_collection_grid__', - 'variables' => array('islandora_object' => NULL, 'collection_results' => NULL), - ), - 'islandora_basic_collection_wrapper' => array( - 'template' => 'islandora-basic-collection-wrapper', 'variables' => array('islandora_object' => NULL), ), 'islandora_basic_collection_management_form_table' => array( 'arguments' => array('element' => NULL), - 'file' => 'includes/CollectionManagerTable.inc', + 'file' => 'includes/collection_manager_table.inc', ), ); } @@ -246,7 +242,13 @@ function islandora_basic_collection_islandora_view_object($object, $user) { $models = $object->models; foreach ($object->models as $model) { if (isset($cmodel_list[$model][ISLANDORA_VIEW_HOOK]) && $cmodel_list[$model][ISLANDORA_VIEW_HOOK] == TRUE) { - $output = theme('islandora_basic_collection_wrapper', array('islandora_object' => $object,)); + $display = (empty($_GET['display'])) ? 'list' : $_GET['display']; + if ($display == 'grid') { + $output = theme('islandora_basic_collection_grid', array('islandora_object' => $object)); + } + else { + $output = theme('islandora_basic_collection', array('islandora_object' => $object)); + } return array('Collection View' => $output); } } @@ -262,24 +264,6 @@ function islandora_basic_collection_preprocess_islandora_basic_collection_grid(& islandora_basic_collection_preprocess_islandora_basic_collection($variables); } -function islandora_basic_collection_preprocess_islandora_basic_collection_wrapper(&$variables) { - $page_number = (empty($_GET['page'])) ? 0 : $_GET['page']; - $page_size = (empty($_GET['pagesize'])) ? variable_get('islandora_basic_collection_page_size', '10') : $_GET['pagesize']; - $islandora_object = $variables['islandora_object']; - $results = islandora_basic_collection_get_objects($islandora_object, $page_number, $page_size); - $total_count = count($results); - pager_default_initialize($total_count, $page_size); - $variables['collection_pager'] = theme('pager', array('quantity' => $total_count)); - $display = (empty($_GET['display'])) ? 'list' : $_GET['display']; - if ($display == 'grid') { - $collection_content = theme('islandora_basic_collection_grid', array('islandora_object' => $islandora_object, 'collection_results' => $results)); - } - else { - $collection_content = theme('islandora_basic_collection', array('islandora_object' => $islandora_object, 'collection_results' => $results)); - } - $variables['collection_content'] = $collection_content; -} - /** * * @global type $base_url @@ -301,8 +285,12 @@ function islandora_basic_collection_preprocess_islandora_basic_collection(&$vari } $page_number = (empty($_GET['page'])) ? 0 : $_GET['page']; $page_size = (empty($_GET['pagesize'])) ? variable_get('islandora_basic_collection_page_size', '10') : $_GET['pagesize']; - $results = $variables['collection_results'];//islandora_basic_collection_get_objects($islandora_object, $page_number, $page_size); + $results = islandora_basic_collection_get_objects($islandora_object, $page_number, $page_size); $total_count = count($results); + pager_default_initialize($total_count, $page_size); + + $pager = theme('pager', array('quantity' => $total_count)); + $variables['pager'] = $pager; $variables['islandora_dublin_core'] = $dc_object; $variables['islandora_object_label'] = $islandora_object->label; $variables['theme_hook_suggestions'][] = 'islandora_basic_collection__' . str_replace(':', '_', $islandora_object->id); @@ -348,7 +336,6 @@ function islandora_basic_collection_preprocess_islandora_basic_collection(&$vari $thumbnail_img = ''; } else { - //TODO: change this default image url $thumbnail_img = ''; } $associated_objects_array[$pid]['thumbnail'] = $thumbnail_img;