From 663589d8fcf05074f772f913753d546a4b4a6136 Mon Sep 17 00:00:00 2001 From: Alan Stanley Date: Wed, 10 Apr 2013 11:31:56 -0300 Subject: [PATCH 01/20] added function to create tableselect form item for content model selection --- includes/utilities.inc | 98 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 81 insertions(+), 17 deletions(-) diff --git a/includes/utilities.inc b/includes/utilities.inc index f061f72c..ae7f4b61 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -124,8 +124,7 @@ function islandora_describe_repository($url = NULL) { try { $info = $connection->api->a->describeRepository(); return $info; - } - catch (RepositoryException $e) { + } catch (RepositoryException $e) { return FALSE; } } @@ -204,13 +203,13 @@ function islandora_escape_pid_for_function($pid) { // Apparently, case doesn't matter for function calls in PHP, so let's not // really worry about changing the case. return str_replace( - // Any PID characters which are not valid in the name of a PHP function. - array( - ':', - '-', - ), - '_', - $pid + // Any PID characters which are not valid in the name of a PHP function. + array( + ':', + '-', + ), + '_', + $pid ); } @@ -281,16 +280,15 @@ function islandora_namespace_accessible($id) { function islandora_get_parents_from_rels_ext(FedoraObject $object) { try { $collections = array_merge( - $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOfCollection'), - $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOf')); - } - catch (RepositoryException $e) { + $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOfCollection'), + $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOf')); + } catch (RepositoryException $e) { // @todo some logging would be nice, not sure what this throws. return array(); } $map = function($o) { - return islandora_object_load($o['object']['value']); - }; + return islandora_object_load($o['object']['value']); + }; $collections = array_map($map, $collections); return array_filter($collections); } @@ -505,7 +503,7 @@ function islandora_display_repository_inaccessible_message() { $text = t('Islandora configuration'); $link = l($text, 'admin/islandora/configure', array('attributes' => array('title' => $text))); $message = t('Could not connect to the repository. Please check the settings on the !link page.', - array('!link' => $link)); + array('!link' => $link)); drupal_set_message($message, 'error', FALSE); } @@ -731,10 +729,76 @@ function islandora_get_content_models($ignore_system_namespace = TRUE) { $label = $result['label']['value']; $namespace = islandora_get_namespace($content_model); $ignore = $ignore_system_namespace && $namespace == 'fedora-system'; - $ignore |= !islandora_namespace_accessible($namespace); + $ignore |= ! islandora_namespace_accessible($namespace); if (!$ignore) { $content_models[$content_model] = array('pid' => $content_model, 'label' => $label); } } return $content_models; } + +/** + * Returns Drupal tableselect element allowing selection of one or more Content Models + * Primarily useful for Admin screens + * @param string $variable + * the name of the Drupal variable holding selected content models + * Content models held in this variable will appear at the top of the displyed list + * @return array Drupal form element allowing content model selection + */ + +function islandora_content_model_select_table_form_element($variable) { + $connection = islandora_get_tuque_connection(); + $restricted = variable_get('islandora_namespace_restriction_enforced', FALSE); + $allowed_string = variable_get('islandora_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 = $connection->repository->ri->itqlQuery($query, 'unlimited'); + $other_list = islandora_get_content_models(TRUE); + $options = array(); + + foreach ($other_list as $pid => $label) { + if ($pid) { + $item_namespace = explode(':', $pid); + if (!$restricted || in_array($item_namespace[0], $allowed)) { + + if (!preg_match('/fedora-system/', $pid)) { + $options[$pid] = $label; + } + } + } + } + + $selected = variable_get($variable, array('')); + foreach ($selected as $cmodel) { + $options = array($cmodel => $options[$cmodel]) + $options; + } + foreach ($options as $key => $value) { + $rows[$key] = array( + 'pid' => $key, + 'title' => $value, + ); + in_array($key, $selected) ? $defaults[$key] = TRUE : $defaults[$key] = FALSE; + } + $header = array( + 'pid' => array('data' => t('PID')), + 'title' => array('data' => t('Content Model')), + ); +//build and return table element + return array( + '#type' => 'tableselect', + '#header' => $header, + '#options' => $rows, + '#default_value' => $defaults, + ); +} \ No newline at end of file From 1d1e72e1e79c39d9209ceaabfad5d818d4fee4ac Mon Sep 17 00:00:00 2001 From: Alan Stanley Date: Thu, 11 Apr 2013 13:44:43 -0300 Subject: [PATCH 02/20] simplified method --- includes/utilities.inc | 25 +------------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/includes/utilities.inc b/includes/utilities.inc index ae7f4b61..4d51c05e 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -745,7 +745,6 @@ function islandora_get_content_models($ignore_system_namespace = TRUE) { * Content models held in this variable will appear at the top of the displyed list * @return array Drupal form element allowing content model selection */ - function islandora_content_model_select_table_form_element($variable) { $connection = islandora_get_tuque_connection(); $restricted = variable_get('islandora_namespace_restriction_enforced', FALSE); @@ -756,29 +755,7 @@ function islandora_content_model_select_table_form_element($variable) { $allowed[] = trim($namespace); } } - $query = 'select $object $title from <#ri> - where ($object $title - and ($object - or $object ) - and $object ) - order by $title'; - - $list = $connection->repository->ri->itqlQuery($query, 'unlimited'); - $other_list = islandora_get_content_models(TRUE); - $options = array(); - - foreach ($other_list as $pid => $label) { - if ($pid) { - $item_namespace = explode(':', $pid); - if (!$restricted || in_array($item_namespace[0], $allowed)) { - - if (!preg_match('/fedora-system/', $pid)) { - $options[$pid] = $label; - } - } - } - } - + $options = islandora_get_content_models(TRUE); $selected = variable_get($variable, array('')); foreach ($selected as $cmodel) { $options = array($cmodel => $options[$cmodel]) + $options; From 74beb0ca6591a14a6cf2cce57836c961d0506173 Mon Sep 17 00:00:00 2001 From: Alan Stanley Date: Thu, 11 Apr 2013 14:48:31 -0300 Subject: [PATCH 03/20] coder changes --- includes/utilities.inc | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/includes/utilities.inc b/includes/utilities.inc index 4d51c05e..a7ec74e0 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -124,7 +124,8 @@ function islandora_describe_repository($url = NULL) { try { $info = $connection->api->a->describeRepository(); return $info; - } catch (RepositoryException $e) { + } + catch (RepositoryException $e) { return FALSE; } } @@ -282,7 +283,8 @@ function islandora_get_parents_from_rels_ext(FedoraObject $object) { $collections = array_merge( $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOfCollection'), $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOf')); - } catch (RepositoryException $e) { + } + catch (RepositoryException $e) { // @todo some logging would be nice, not sure what this throws. return array(); } @@ -398,7 +400,7 @@ function islandora_get_datastreams_requirements_from_content_model(FedoraObject $xml = new SimpleXMLElement($object[DS_COMP_STREAM]->content); foreach ($xml->dsTypeModel as $ds) { $dsid = (string) $ds['ID']; - $optional = strtolower((string) $ds['optional']); + $optional = drupal_strtolower((string) $ds['optional']); $mime = array(); foreach ($ds->form as $form) { $mime[] = (string) $form['MIME']; @@ -740,12 +742,12 @@ function islandora_get_content_models($ignore_system_namespace = TRUE) { /** * Returns Drupal tableselect element allowing selection of one or more Content Models * Primarily useful for Admin screens - * @param string $variable + * @param string $drupal_variable * the name of the Drupal variable holding selected content models * Content models held in this variable will appear at the top of the displyed list * @return array Drupal form element allowing content model selection */ -function islandora_content_model_select_table_form_element($variable) { +function islandora_content_model_select_table_form_element($drupal_variable) { $connection = islandora_get_tuque_connection(); $restricted = variable_get('islandora_namespace_restriction_enforced', FALSE); $allowed_string = variable_get('islandora_pids_allowed', 'default: demo: changeme: islandora:'); @@ -756,7 +758,7 @@ function islandora_content_model_select_table_form_element($variable) { } } $options = islandora_get_content_models(TRUE); - $selected = variable_get($variable, array('')); + $selected = variable_get($drupal_variable, array('')); foreach ($selected as $cmodel) { $options = array($cmodel => $options[$cmodel]) + $options; } @@ -772,10 +774,12 @@ function islandora_content_model_select_table_form_element($variable) { 'title' => array('data' => t('Content Model')), ); //build and return table element - return array( + $element = array( '#type' => 'tableselect', '#header' => $header, '#options' => $rows, '#default_value' => $defaults, ); + + return $element; } \ No newline at end of file From 177aaced8685de682dbbc5c71d098e5fc5be1692 Mon Sep 17 00:00:00 2001 From: Alan Stanley Date: Thu, 11 Apr 2013 14:55:29 -0300 Subject: [PATCH 04/20] format changes --- includes/utilities.inc | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/includes/utilities.inc b/includes/utilities.inc index a7ec74e0..ea0fc849 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -204,13 +204,13 @@ function islandora_escape_pid_for_function($pid) { // Apparently, case doesn't matter for function calls in PHP, so let's not // really worry about changing the case. return str_replace( - // Any PID characters which are not valid in the name of a PHP function. - array( - ':', - '-', - ), - '_', - $pid + // Any PID characters which are not valid in the name of a PHP function. + array( + ':', + '-', + ), + '_', + $pid ); } @@ -281,16 +281,16 @@ function islandora_namespace_accessible($id) { function islandora_get_parents_from_rels_ext(FedoraObject $object) { try { $collections = array_merge( - $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOfCollection'), - $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOf')); + $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOfCollection'), + $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOf')); } catch (RepositoryException $e) { // @todo some logging would be nice, not sure what this throws. return array(); } $map = function($o) { - return islandora_object_load($o['object']['value']); - }; + return islandora_object_load($o['object']['value']); + }; $collections = array_map($map, $collections); return array_filter($collections); } @@ -400,7 +400,7 @@ function islandora_get_datastreams_requirements_from_content_model(FedoraObject $xml = new SimpleXMLElement($object[DS_COMP_STREAM]->content); foreach ($xml->dsTypeModel as $ds) { $dsid = (string) $ds['ID']; - $optional = drupal_strtolower((string) $ds['optional']); + $optional = strtolower((string) $ds['optional']); $mime = array(); foreach ($ds->form as $form) { $mime[] = (string) $form['MIME']; @@ -505,7 +505,7 @@ function islandora_display_repository_inaccessible_message() { $text = t('Islandora configuration'); $link = l($text, 'admin/islandora/configure', array('attributes' => array('title' => $text))); $message = t('Could not connect to the repository. Please check the settings on the !link page.', - array('!link' => $link)); + array('!link' => $link)); drupal_set_message($message, 'error', FALSE); } @@ -731,7 +731,7 @@ function islandora_get_content_models($ignore_system_namespace = TRUE) { $label = $result['label']['value']; $namespace = islandora_get_namespace($content_model); $ignore = $ignore_system_namespace && $namespace == 'fedora-system'; - $ignore |= ! islandora_namespace_accessible($namespace); + $ignore |= !islandora_namespace_accessible($namespace); if (!$ignore) { $content_models[$content_model] = array('pid' => $content_model, 'label' => $label); } From 3b9ae796ef06725459f822b6a7251adfb275ef4d Mon Sep 17 00:00:00 2001 From: Alan Stanley Date: Thu, 11 Apr 2013 15:18:05 -0300 Subject: [PATCH 05/20] code sniffer compliant --- includes/utilities.inc | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/includes/utilities.inc b/includes/utilities.inc index ea0fc849..11c65b50 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -740,12 +740,15 @@ function islandora_get_content_models($ignore_system_namespace = TRUE) { } /** - * Returns Drupal tableselect element allowing selection of one or more Content Models - * Primarily useful for Admin screens + * Returns Drupal tableselect element allowing selection of Content Models. + * * @param string $drupal_variable * the name of the Drupal variable holding selected content models - * Content models held in this variable will appear at the top of the displyed list - * @return array Drupal form element allowing content model selection + * Content models held in this variable will appear at the top of + * the displayed list + * + * @return array + * Drupal form element allowing content model selection */ function islandora_content_model_select_table_form_element($drupal_variable) { $connection = islandora_get_tuque_connection(); @@ -773,7 +776,7 @@ function islandora_content_model_select_table_form_element($drupal_variable) { 'pid' => array('data' => t('PID')), 'title' => array('data' => t('Content Model')), ); -//build and return table element + // Build and return table element. $element = array( '#type' => 'tableselect', '#header' => $header, @@ -782,4 +785,4 @@ function islandora_content_model_select_table_form_element($drupal_variable) { ); return $element; -} \ No newline at end of file +} From b4b33df1f5eed4f07ef37861e6aeaf6012d7eef9 Mon Sep 17 00:00:00 2001 From: Alan Stanley Date: Thu, 11 Apr 2013 15:36:50 -0300 Subject: [PATCH 06/20] removed space, coding standards --- includes/utilities.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/utilities.inc b/includes/utilities.inc index 11c65b50..54fe9317 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -776,7 +776,7 @@ function islandora_content_model_select_table_form_element($drupal_variable) { 'pid' => array('data' => t('PID')), 'title' => array('data' => t('Content Model')), ); - // Build and return table element. + // Build and return table element. $element = array( '#type' => 'tableselect', '#header' => $header, From c5d7485153cb44192e0ac45f54364f1ce0cf4ca1 Mon Sep 17 00:00:00 2001 From: Alan Stanley Date: Fri, 12 Apr 2013 09:46:29 -0300 Subject: [PATCH 07/20] Converted CM query to sparql to allow for unlabeled Content Models --- includes/utilities.inc | 75 +++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/includes/utilities.inc b/includes/utilities.inc index 54fe9317..7e5a339a 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -124,8 +124,7 @@ function islandora_describe_repository($url = NULL) { try { $info = $connection->api->a->describeRepository(); return $info; - } - catch (RepositoryException $e) { + } catch (RepositoryException $e) { return FALSE; } } @@ -204,13 +203,13 @@ function islandora_escape_pid_for_function($pid) { // Apparently, case doesn't matter for function calls in PHP, so let's not // really worry about changing the case. return str_replace( - // Any PID characters which are not valid in the name of a PHP function. - array( - ':', - '-', - ), - '_', - $pid + // Any PID characters which are not valid in the name of a PHP function. + array( + ':', + '-', + ), + '_', + $pid ); } @@ -281,16 +280,15 @@ function islandora_namespace_accessible($id) { function islandora_get_parents_from_rels_ext(FedoraObject $object) { try { $collections = array_merge( - $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOfCollection'), - $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOf')); - } - catch (RepositoryException $e) { + $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOfCollection'), + $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOf')); + } catch (RepositoryException $e) { // @todo some logging would be nice, not sure what this throws. return array(); } $map = function($o) { - return islandora_object_load($o['object']['value']); - }; + return islandora_object_load($o['object']['value']); + }; $collections = array_map($map, $collections); return array_filter($collections); } @@ -505,7 +503,7 @@ function islandora_display_repository_inaccessible_message() { $text = t('Islandora configuration'); $link = l($text, 'admin/islandora/configure', array('attributes' => array('title' => $text))); $message = t('Could not connect to the repository. Please check the settings on the !link page.', - array('!link' => $link)); + array('!link' => $link)); drupal_set_message($message, 'error', FALSE); } @@ -718,20 +716,23 @@ function islandora_get_allowed_namespaces() { function islandora_get_content_models($ignore_system_namespace = TRUE) { module_load_include('inc', 'islandora', 'includes/utilities'); $tuque = islandora_get_tuque_connection(); - $query = 'select $object $label from <#ri> - where ($object $label - and ($object - or $object ) - and $object ) - order by $label'; + $query = 'PREFIX fm: + SELECT ?object ?label + FROM <#ri> + WHERE { + ?object fm:hasModel ; + OPTIONAL{ + ?object fm:label ?label + } + }'; $content_models = array(); - $results = $tuque->repository->ri->itqlQuery($query, 'unlimited'); + $results = $tuque->repository->ri->sparqlQuery($query, 'unlimited'); foreach ($results as $result) { $content_model = $result['object']['value']; $label = $result['label']['value']; $namespace = islandora_get_namespace($content_model); $ignore = $ignore_system_namespace && $namespace == 'fedora-system'; - $ignore |= !islandora_namespace_accessible($namespace); + $ignore |= ! islandora_namespace_accessible($namespace); if (!$ignore) { $content_models[$content_model] = array('pid' => $content_model, 'label' => $label); } @@ -760,17 +761,25 @@ function islandora_content_model_select_table_form_element($drupal_variable) { $allowed[] = trim($namespace); } } - $options = islandora_get_content_models(TRUE); - $selected = variable_get($drupal_variable, array('')); - foreach ($selected as $cmodel) { - $options = array($cmodel => $options[$cmodel]) + $options; + $defaults = array(); + $options = array(); + $options = $options + islandora_get_content_models(TRUE); + + foreach($options as $option){ + $content_models[$option['pid']] = $option['label']; } - foreach ($options as $key => $value) { - $rows[$key] = array( - 'pid' => $key, - 'title' => $value, + + $selected = array_values(variable_get($drupal_variable, array(''))); + foreach($selected as $selection){ + $content_models = array($selection => $content_models[$selection]) + $content_models; + } + + foreach ($content_models as $pid => $label) { + $rows[$pid] = array( + 'pid' => $pid, + 'title' => $label, ); - in_array($key, $selected) ? $defaults[$key] = TRUE : $defaults[$key] = FALSE; + in_array($pid, $selected) ? $defaults[$pid] = TRUE : $defaults[$pid] = FALSE; } $header = array( 'pid' => array('data' => t('PID')), From cb3e9b083d50a9cf29dce7ba383c7875d4c8be79 Mon Sep 17 00:00:00 2001 From: Alan Stanley Date: Fri, 12 Apr 2013 09:59:51 -0300 Subject: [PATCH 08/20] Converted CM query to sparql to allow for unlabeled Content Models --- includes/utilities.inc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/includes/utilities.inc b/includes/utilities.inc index 7e5a339a..cd2cbd86 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -762,8 +762,9 @@ function islandora_content_model_select_table_form_element($drupal_variable) { } } $defaults = array(); - $options = array(); - $options = $options + islandora_get_content_models(TRUE); + $rows = array(); + $content_models = array(); + $options = islandora_get_content_models(TRUE); foreach($options as $option){ $content_models[$option['pid']] = $option['label']; @@ -771,7 +772,9 @@ function islandora_content_model_select_table_form_element($drupal_variable) { $selected = array_values(variable_get($drupal_variable, array(''))); foreach($selected as $selection){ - $content_models = array($selection => $content_models[$selection]) + $content_models; + if(isset($content_models[$selection])){ + $content_models = array($selection => $content_models[$selection]) + $content_models; + } } foreach ($content_models as $pid => $label) { @@ -791,6 +794,7 @@ function islandora_content_model_select_table_form_element($drupal_variable) { '#header' => $header, '#options' => $rows, '#default_value' => $defaults, + '#empty' => t("There are no content models in this Fedora Repository.") ); return $element; From 0731a26e15c303516c08d04d4c739631f7459ccb Mon Sep 17 00:00:00 2001 From: Alan Stanley Date: Fri, 12 Apr 2013 10:57:28 -0300 Subject: [PATCH 09/20] Made query more robust --- includes/utilities.inc | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/includes/utilities.inc b/includes/utilities.inc index cd2cbd86..f5922ddc 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -717,14 +717,24 @@ function islandora_get_content_models($ignore_system_namespace = TRUE) { module_load_include('inc', 'islandora', 'includes/utilities'); $tuque = islandora_get_tuque_connection(); $query = 'PREFIX fm: + PREFIX fr: SELECT ?object ?label FROM <#ri> WHERE { - ?object fm:hasModel ; + {?object fm:hasModel ; + fm:state + OPTIONAL{ + ?object fm:label ?label + } + } + UNION + {?object fr:isMemberOfCollection ; + fm:state OPTIONAL{ ?object fm:label ?label } - }'; + } + }'; $content_models = array(); $results = $tuque->repository->ri->sparqlQuery($query, 'unlimited'); foreach ($results as $result) { @@ -754,18 +764,11 @@ function islandora_get_content_models($ignore_system_namespace = TRUE) { function islandora_content_model_select_table_form_element($drupal_variable) { $connection = islandora_get_tuque_connection(); $restricted = variable_get('islandora_namespace_restriction_enforced', FALSE); - $allowed_string = variable_get('islandora_pids_allowed', 'default: demo: changeme: islandora:'); - $namespaces = explode(':', $allowed_string); - foreach ($namespaces as $namespace) { - if ($namespace) { - $allowed[] = trim($namespace); - } - } $defaults = array(); $rows = array(); $content_models = array(); + $options = islandora_get_content_models(TRUE); - foreach($options as $option){ $content_models[$option['pid']] = $option['label']; } From 273e591f6e33b3da45a202b419876238be193754 Mon Sep 17 00:00:00 2001 From: Alan Stanley Date: Fri, 12 Apr 2013 11:01:41 -0300 Subject: [PATCH 10/20] reverted auto-formatting --- includes/utilities.inc | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/includes/utilities.inc b/includes/utilities.inc index f5922ddc..90117d59 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -124,7 +124,8 @@ function islandora_describe_repository($url = NULL) { try { $info = $connection->api->a->describeRepository(); return $info; - } catch (RepositoryException $e) { + } + catch (RepositoryException $e) { return FALSE; } } @@ -203,13 +204,13 @@ function islandora_escape_pid_for_function($pid) { // Apparently, case doesn't matter for function calls in PHP, so let's not // really worry about changing the case. return str_replace( - // Any PID characters which are not valid in the name of a PHP function. - array( - ':', - '-', - ), - '_', - $pid + // Any PID characters which are not valid in the name of a PHP function. + array( + ':', + '-', + ), + '_', + $pid ); } @@ -280,15 +281,16 @@ function islandora_namespace_accessible($id) { function islandora_get_parents_from_rels_ext(FedoraObject $object) { try { $collections = array_merge( - $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOfCollection'), - $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOf')); - } catch (RepositoryException $e) { + $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOfCollection'), + $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOf')); + } + catch (RepositoryException $e) { // @todo some logging would be nice, not sure what this throws. return array(); } $map = function($o) { - return islandora_object_load($o['object']['value']); - }; + return islandora_object_load($o['object']['value']); + }; $collections = array_map($map, $collections); return array_filter($collections); } @@ -503,7 +505,7 @@ function islandora_display_repository_inaccessible_message() { $text = t('Islandora configuration'); $link = l($text, 'admin/islandora/configure', array('attributes' => array('title' => $text))); $message = t('Could not connect to the repository. Please check the settings on the !link page.', - array('!link' => $link)); + array('!link' => $link)); drupal_set_message($message, 'error', FALSE); } From c431a070f32213247a44716af63f248070a0348e Mon Sep 17 00:00:00 2001 From: Alan Stanley Date: Fri, 12 Apr 2013 11:18:49 -0300 Subject: [PATCH 11/20] minor formatting changes --- includes/utilities.inc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/includes/utilities.inc b/includes/utilities.inc index 90117d59..52186681 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -744,7 +744,7 @@ function islandora_get_content_models($ignore_system_namespace = TRUE) { $label = $result['label']['value']; $namespace = islandora_get_namespace($content_model); $ignore = $ignore_system_namespace && $namespace == 'fedora-system'; - $ignore |= ! islandora_namespace_accessible($namespace); + $ignore |= !islandora_namespace_accessible($namespace); if (!$ignore) { $content_models[$content_model] = array('pid' => $content_model, 'label' => $label); } @@ -771,13 +771,13 @@ function islandora_content_model_select_table_form_element($drupal_variable) { $content_models = array(); $options = islandora_get_content_models(TRUE); - foreach($options as $option){ + foreach ($options as $option){ $content_models[$option['pid']] = $option['label']; } $selected = array_values(variable_get($drupal_variable, array(''))); - foreach($selected as $selection){ - if(isset($content_models[$selection])){ + foreach ($selected as $selection){ + if (isset($content_models[$selection])){ $content_models = array($selection => $content_models[$selection]) + $content_models; } } @@ -799,7 +799,7 @@ function islandora_content_model_select_table_form_element($drupal_variable) { '#header' => $header, '#options' => $rows, '#default_value' => $defaults, - '#empty' => t("There are no content models in this Fedora Repository.") + '#empty' => t("There are no content models in this Fedora Repository."), ); return $element; From 37071f0ec084e1f950a4fcb7bebca080676ac611 Mon Sep 17 00:00:00 2001 From: Alan Stanley Date: Fri, 12 Apr 2013 11:31:48 -0300 Subject: [PATCH 12/20] minor formatting changes --- includes/utilities.inc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/utilities.inc b/includes/utilities.inc index 52186681..f1d221fc 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -771,13 +771,13 @@ function islandora_content_model_select_table_form_element($drupal_variable) { $content_models = array(); $options = islandora_get_content_models(TRUE); - foreach ($options as $option){ + foreach ($options as $option) { $content_models[$option['pid']] = $option['label']; } $selected = array_values(variable_get($drupal_variable, array(''))); - foreach ($selected as $selection){ - if (isset($content_models[$selection])){ + foreach ($selected as $selection) { + if (isset($content_models[$selection])) { $content_models = array($selection => $content_models[$selection]) + $content_models; } } From 8f5cf316041f997ddb5be9502668c6ec704342c1 Mon Sep 17 00:00:00 2001 From: Alan Stanley Date: Mon, 15 Apr 2013 20:01:16 -0300 Subject: [PATCH 13/20] used constants, removed unused variable --- includes/utilities.inc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/includes/utilities.inc b/includes/utilities.inc index f1d221fc..df924dce 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -718,8 +718,8 @@ function islandora_get_allowed_namespaces() { function islandora_get_content_models($ignore_system_namespace = TRUE) { module_load_include('inc', 'islandora', 'includes/utilities'); $tuque = islandora_get_tuque_connection(); - $query = 'PREFIX fm: - PREFIX fr: + $query = "PREFIX fm: <" . FEDORA_MODEL_URI . "> + PREFIX fr: <" . FEDORA_RELS_EXT_URI . "> SELECT ?object ?label FROM <#ri> WHERE { @@ -736,7 +736,7 @@ function islandora_get_content_models($ignore_system_namespace = TRUE) { ?object fm:label ?label } } - }'; + }"; $content_models = array(); $results = $tuque->repository->ri->sparqlQuery($query, 'unlimited'); foreach ($results as $result) { @@ -765,7 +765,6 @@ function islandora_get_content_models($ignore_system_namespace = TRUE) { */ function islandora_content_model_select_table_form_element($drupal_variable) { $connection = islandora_get_tuque_connection(); - $restricted = variable_get('islandora_namespace_restriction_enforced', FALSE); $defaults = array(); $rows = array(); $content_models = array(); From 69ce869b818dcd9bfad37bed2879160755541913 Mon Sep 17 00:00:00 2001 From: Alan Stanley Date: Wed, 17 Apr 2013 11:01:08 -0300 Subject: [PATCH 14/20] removed unused variable --- includes/utilities.inc | 1 - 1 file changed, 1 deletion(-) diff --git a/includes/utilities.inc b/includes/utilities.inc index df924dce..90676923 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -764,7 +764,6 @@ function islandora_get_content_models($ignore_system_namespace = TRUE) { * Drupal form element allowing content model selection */ function islandora_content_model_select_table_form_element($drupal_variable) { - $connection = islandora_get_tuque_connection(); $defaults = array(); $rows = array(); $content_models = array(); From 3da2fdcb9f46dd1ae8ad6485680eb8b610e8e3ae Mon Sep 17 00:00:00 2001 From: Alan Stanley Date: Fri, 19 Apr 2013 09:48:45 -0300 Subject: [PATCH 15/20] added default, changed sorting --- includes/utilities.inc | 52 +++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/includes/utilities.inc b/includes/utilities.inc index 90676923..d1b32d4e 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -204,13 +204,13 @@ function islandora_escape_pid_for_function($pid) { // Apparently, case doesn't matter for function calls in PHP, so let's not // really worry about changing the case. return str_replace( - // Any PID characters which are not valid in the name of a PHP function. - array( - ':', - '-', - ), - '_', - $pid + // Any PID characters which are not valid in the name of a PHP function. + array( + ':', + '-', + ), + '_', + $pid ); } @@ -281,16 +281,16 @@ function islandora_namespace_accessible($id) { function islandora_get_parents_from_rels_ext(FedoraObject $object) { try { $collections = array_merge( - $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOfCollection'), - $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOf')); + $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOfCollection'), + $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOf')); } catch (RepositoryException $e) { // @todo some logging would be nice, not sure what this throws. return array(); } $map = function($o) { - return islandora_object_load($o['object']['value']); - }; + return islandora_object_load($o['object']['value']); + }; $collections = array_map($map, $collections); return array_filter($collections); } @@ -505,7 +505,7 @@ function islandora_display_repository_inaccessible_message() { $text = t('Islandora configuration'); $link = l($text, 'admin/islandora/configure', array('attributes' => array('title' => $text))); $message = t('Could not connect to the repository. Please check the settings on the !link page.', - array('!link' => $link)); + array('!link' => $link)); drupal_set_message($message, 'error', FALSE); } @@ -744,7 +744,7 @@ function islandora_get_content_models($ignore_system_namespace = TRUE) { $label = $result['label']['value']; $namespace = islandora_get_namespace($content_model); $ignore = $ignore_system_namespace && $namespace == 'fedora-system'; - $ignore |= !islandora_namespace_accessible($namespace); + $ignore |= ! islandora_namespace_accessible($namespace); if (!$ignore) { $content_models[$content_model] = array('pid' => $content_model, 'label' => $label); } @@ -759,11 +759,12 @@ function islandora_get_content_models($ignore_system_namespace = TRUE) { * the name of the Drupal variable holding selected content models * Content models held in this variable will appear at the top of * the displayed list - * + * @param array $default_values_array + * default values to display if $drupal_variable is unset * @return array * Drupal form element allowing content model selection */ -function islandora_content_model_select_table_form_element($drupal_variable) { +function islandora_content_model_select_table_form_element($drupal_variable, $default_values_array = array('')) { $defaults = array(); $rows = array(); $content_models = array(); @@ -773,19 +774,24 @@ function islandora_content_model_select_table_form_element($drupal_variable) { $content_models[$option['pid']] = $option['label']; } - $selected = array_values(variable_get($drupal_variable, array(''))); - foreach ($selected as $selection) { - if (isset($content_models[$selection])) { - $content_models = array($selection => $content_models[$selection]) + $content_models; - } - } - + $selected = array_values(variable_get($drupal_variable, $default_values_array)); + $comparator = function ($a, $b) use ($selected) { + $a_val = $b_val = 0; + if (in_array($a, $selected)) { + $a_val = 1; + } + if (in_array($b, $selected)) { + $b_val = 1; + } + return $a_val = $b_val; + }; + uksort($content_models, $comparator); foreach ($content_models as $pid => $label) { $rows[$pid] = array( 'pid' => $pid, 'title' => $label, ); - in_array($pid, $selected) ? $defaults[$pid] = TRUE : $defaults[$pid] = FALSE; + $defaults[$pid] = in_array($pid, $selected); } $header = array( 'pid' => array('data' => t('PID')), From b58fd31f9c179c8c83124c797af2eaf384b8d4eb Mon Sep 17 00:00:00 2001 From: Alan Stanley Date: Fri, 19 Apr 2013 11:38:40 -0300 Subject: [PATCH 16/20] formatting, made query more efficient --- includes/utilities.inc | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/includes/utilities.inc b/includes/utilities.inc index d1b32d4e..5b60df50 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -204,7 +204,7 @@ function islandora_escape_pid_for_function($pid) { // Apparently, case doesn't matter for function calls in PHP, so let's not // really worry about changing the case. return str_replace( - // Any PID characters which are not valid in the name of a PHP function. + // Any PID characters which are not valid in the name of a PHP function. array( ':', '-', @@ -281,8 +281,8 @@ function islandora_namespace_accessible($id) { function islandora_get_parents_from_rels_ext(FedoraObject $object) { try { $collections = array_merge( - $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOfCollection'), - $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOf')); + $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOfCollection'), + $object->relationships->get(FEDORA_RELS_EXT_URI, 'isMemberOf')); } catch (RepositoryException $e) { // @todo some logging would be nice, not sure what this throws. @@ -724,17 +724,14 @@ function islandora_get_content_models($ignore_system_namespace = TRUE) { FROM <#ri> WHERE { {?object fm:hasModel ; - fm:state - OPTIONAL{ - ?object fm:label ?label + fm:state fm:Active } - } - UNION - {?object fr:isMemberOfCollection ; - fm:state - OPTIONAL{ - ?object fm:label ?label + UNION{ + ?object fr:isMemberOfCollection ; + fm:state fm:Active } + OPTIONAL{ + ?object fm:label ?label } }"; $content_models = array(); @@ -744,7 +741,7 @@ function islandora_get_content_models($ignore_system_namespace = TRUE) { $label = $result['label']['value']; $namespace = islandora_get_namespace($content_model); $ignore = $ignore_system_namespace && $namespace == 'fedora-system'; - $ignore |= ! islandora_namespace_accessible($namespace); + $ignore |= !islandora_namespace_accessible($namespace); if (!$ignore) { $content_models[$content_model] = array('pid' => $content_model, 'label' => $label); } @@ -776,15 +773,15 @@ function islandora_content_model_select_table_form_element($drupal_variable, $de $selected = array_values(variable_get($drupal_variable, $default_values_array)); $comparator = function ($a, $b) use ($selected) { - $a_val = $b_val = 0; - if (in_array($a, $selected)) { - $a_val = 1; - } - if (in_array($b, $selected)) { - $b_val = 1; - } - return $a_val = $b_val; - }; + $a_val = $b_val = 0; + if (in_array($a, $selected)) { + $a_val = 1; + } + if (in_array($b, $selected)) { + $b_val = 1; + } + return $a_val = $b_val; + }; uksort($content_models, $comparator); foreach ($content_models as $pid => $label) { $rows[$pid] = array( From 84aa0c58f4181132b17fecad2108ecbd39e3bfc0 Mon Sep 17 00:00:00 2001 From: Alan Stanley Date: Fri, 19 Apr 2013 14:46:11 -0300 Subject: [PATCH 17/20] changed operator --- includes/utilities.inc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/utilities.inc b/includes/utilities.inc index 5b60df50..ca1867d3 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -778,9 +778,9 @@ function islandora_content_model_select_table_form_element($drupal_variable, $de $a_val = 1; } if (in_array($b, $selected)) { - $b_val = 1; + $b_val - 1; } - return $a_val = $b_val; + return $a_val - $b_val; }; uksort($content_models, $comparator); foreach ($content_models as $pid => $label) { From 553810a133fbb6ed8f05bd78f7ac5d53ac5d9d8e Mon Sep 17 00:00:00 2001 From: Alan Stanley Date: Fri, 19 Apr 2013 14:48:29 -0300 Subject: [PATCH 18/20] changed operator --- includes/utilities.inc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/utilities.inc b/includes/utilities.inc index ca1867d3..d40b044a 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -778,9 +778,9 @@ function islandora_content_model_select_table_form_element($drupal_variable, $de $a_val = 1; } if (in_array($b, $selected)) { - $b_val - 1; + $b_val = 1; } - return $a_val - $b_val; + return $b_val - $a_val; }; uksort($content_models, $comparator); foreach ($content_models as $pid => $label) { From 9496b0e5fda04424717ff201c8921ecedd116607 Mon Sep 17 00:00:00 2001 From: Alan Stanley Date: Fri, 19 Apr 2013 17:01:31 -0300 Subject: [PATCH 19/20] formatting --- includes/utilities.inc | 1 - 1 file changed, 1 deletion(-) diff --git a/includes/utilities.inc b/includes/utilities.inc index d40b044a..6d20dff5 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -765,7 +765,6 @@ function islandora_content_model_select_table_form_element($drupal_variable, $de $defaults = array(); $rows = array(); $content_models = array(); - $options = islandora_get_content_models(TRUE); foreach ($options as $option) { $content_models[$option['pid']] = $option['label']; From f9e406471593e71ae36ef872474ffb96de9036c4 Mon Sep 17 00:00:00 2001 From: Alan Stanley Date: Fri, 19 Apr 2013 17:16:32 -0300 Subject: [PATCH 20/20] coder standards --- includes/utilities.inc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/includes/utilities.inc b/includes/utilities.inc index b6bb1854..fbc4585a 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -204,7 +204,7 @@ function islandora_escape_pid_for_function($pid) { // Apparently, case doesn't matter for function calls in PHP, so let's not // really worry about changing the case. return str_replace( - // Any PID characters which are not valid in the name of a PHP function. + // Any PID characters which are not valid in the name of a PHP function. array( ':', '-', @@ -289,8 +289,8 @@ function islandora_get_parents_from_rels_ext(FedoraObject $object) { return array(); } $map = function($o) { - return islandora_object_load($o['object']['value']); - }; + return islandora_object_load($o['object']['value']); + }; $collections = array_map($map, $collections); return array_filter($collections); } @@ -770,6 +770,7 @@ function islandora_get_content_models($ignore_system_namespace = TRUE) { * the displayed list * @param array $default_values_array * default values to display if $drupal_variable is unset + * * @return array * Drupal form element allowing content model selection */