Drupal modules for browsing and managing Fedora-based digital repositories.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

200 lines
5.7 KiB

<?php
/**
* @file
* This is the new Islandora Collection module.
*/
define('ISLANDORA_COLLECTION_CONTENT_TYPE', 'islandora_collection');
define('ISLANDORA_COLLECTION_RDF_TYPE', 'islandora:collection');
/**
* Implements hook_views_api().
*/
function islandora_collection_views_api() {
return array(
'api' => 3.0,
'path' => drupal_get_path('module', 'islandora_collection') . '/include',
);
}
/**
* Implements hook_node_info().
*/
function islandora_collection_node_info() {
return array(
ISLANDORA_COLLECTION_CONTENT_TYPE => array(
'name' => t("Collection"),
'base' => ISLANDORA_COLLECTION_CONTENT_TYPE,
'description' => t("A Drupal node modeling a pcdm:Collection."),
),
);
}
/**
* Implements hook_form().
*/
function islandora_collection_form($node, array &$form_state) {
return node_content_form($node, $form_state);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function islandora_collection_form_islandora_collection_node_form_alter(&$form, array &$form_state, $form_id) {
$node = $form_state['node'];
// Add a collection picker if node is new (e.g. this is an insert form).
if (!isset($node->nid) || isset($node->is_new)) {
// Construct list of options from db and a default.
$default = array('' => t('Fedora Root'));
$from_db = db_select('node', 'n')
->fields('n', array('uuid', 'title'))
->condition('n.type', ISLANDORA_COLLECTION_CONTENT_TYPE)
->execute()
->fetchAllKeyed();
$options = $default + $from_db;
// Add the select to the form.
$form['parent_collection'] = array(
'#type' => 'select',
'#title' => t('Parent Collection'),
'#description' => t('Select the parent collection to add this to. Select "Fedora Root" if you want it to be a top-level collection'),
'#options' => $options,
'#default_value' => '',
'#weight' => -4,
);
// Keep title on top.
$form['title']['#weight'] = -5;
// Tack on a custom submit handler to attach data on the entity for use
// later on in the post action hooks.
array_unshift($form['#submit'], 'islandora_collection_add_parent_uuid');
}
}
/**
* Custom submit handler to pass along parent uuid.
*
* @param array $form
* Drupal form.
* @param array $form_state
* Drupal form state.
*/
function islandora_collection_add_parent_uuid(array $form, array &$form_state) {
$node = $form_state['node'];
$node->parent_collection = "";
if (isset($form_state['parent_collection']['#value'])) {
$node->parent_collection = $form_state['parent_collection']['#value'];
}
}
/**
* Implements hook_rdf_mapping().
*/
function islandora_collection_rdf_mapping() {
module_load_include('inc', 'islandora', 'include/rdf_mapping');
$rdf_types = array(
ISLANDORA_PCDM_COLLECTION_RDF_TYPE,
ISLANDORA_COLLECTION_RDF_TYPE,
);
return islandora_get_default_rdf_mapping(ISLANDORA_COLLECTION_CONTENT_TYPE, $rdf_types);
}
/**
* Implements hook_node_postinsert().
*/
function islandora_collection_node_postinsert($node) {
module_load_include('inc', 'islandora', 'include/utils');
if (islandora_ignore_hooks()) {
return;
}
// Exit early if it's not a collection.
if (strcmp($node->type, ISLANDORA_COLLECTION_CONTENT_TYPE) != 0) {
return;
}
$parent_collection = $node->parent_collection;
$nodes = entity_uuid_load('node', array($node->uuid));
// Exit early if the node never was successfully inserted in the database.
if (empty($nodes)) {
return;
}
$node = array_pop($nodes);
$url = "http://127.0.0.1:8181/cxf/islandora/collection/$parent_collection";
// Don't forget the trailing slash.
httprl_request($url,
array(
'method' => 'POST',
'headers' => array(
'Content-Type' => 'application/json',
),
'data' => json_encode($node),
));
$response = httprl_send_request();
}
/**
* Implements hook_node_postupdate().
*/
function islandora_collection_node_postupdate($node) {
module_load_include('inc', 'islandora', 'include/utils');
if (islandora_ignore_hooks()) {
return;
}
// Exit early if it's not a collection.
if (strcmp($node->type, ISLANDORA_COLLECTION_CONTENT_TYPE) != 0) {
return;
}
// Unfortunately, there's no way to tell if the db transaction got rolled
// back in this hook, so we'll default to doing it anyway so as not to miss
// any updates. Open to suggestions on this one. But we gotta do post_action
// hooks otherwise things can get crazy if the db transaction hasn't closed.
$nodes = entity_uuid_load('node', array($node->uuid));
// Exit early if something has gone wrong and there's no node info.
if (empty($nodes)) {
return;
}
$node = array_pop($nodes);
$uuid = $node->uuid;
httprl_request("http://127.0.0.1:8181/cxf/islandora/collection/$uuid",
array(
'method' => 'PUT',
'headers' => array(
'Content-Type' => 'application/json',
),
'data' => json_encode($node),
));
$response = httprl_send_request();
}
/**
* Implements hook_node_postdelete().
*/
function islandora_collection_node_postdelete($node) {
module_load_include('inc', 'islandora', 'include/utils');
if (islandora_ignore_hooks()) {
return;
}
// Exit early if it's not a collection.
if (strcmp($node->type, ISLANDORA_COLLECTION_CONTENT_TYPE) != 0) {
return;
}
$uuid = $node->uuid;
httprl_request("http://127.0.0.1:8181/cxf/islandora/collection/$uuid",
array(
'method' => 'DELETE',
));
$response = httprl_send_request();
}