Преглед изворни кода

Fixed citation bug in pub module. Added support for tokens in field_resource_links

Stephen Ficklin пре 9 година
родитељ
комит
896d8a8454

+ 2 - 2
tripal_analysis/includes/tripal_analysis.chado_node.inc

@@ -164,14 +164,14 @@ function chado_analysis_form($node, &$form_state) {
   );
   $form['program']= array(
     '#type' => 'textfield',
-    '#title' => t('Program, Pipeline Name or Method Name'),
+    '#title' => t('Program, Pipeline or Method Name'),
     '#required' => TRUE,
     '#default_value' => $program,
     '#description' => t("Program name, e.g. blastx, blastp, sim4, genscan. If the analysis was not derived from a software package, provide a very brief description of the pipeline or method."),
   );
   $form['programversion']= array(
     '#type' => 'textfield',
-    '#title' => t('Program, Pipeline  or Method version'),
+    '#title' => t('Program, Pipeline  or Method Version'),
     '#required' => TRUE,
     '#default_value' => $programversion,
     '#description' => t("Version description, e.g. TBLASTX 2.0MP-WashU [09-Nov-2000]. Enter 'n/a' if no version is available or applicable."),

+ 25 - 5
tripal_core/includes/tripal_core.toc.inc

@@ -307,10 +307,11 @@ function tripal_core_node_view_build_toc(&$build) {
 
 
       // For backwards compatibility we will handle the content type fields
-      // named 'field_resource_blocks', 'field_resource_titles', and 'field_resource_links'
-      // these fields can be added on the Drupal content types page and were
-      // specifically recoginzed by Tripal v1.1.  If the mode type is "manage_type"
-      // then remove these content panes because they are node specific.
+      // named 'field_resource_blocks', 'field_resource_titles', and
+      // 'field_resource_links' these fields can be added on the Drupal content
+      // types page and were specifically recoginzed by Tripal v1.1.  If the
+      // mode type is "manage_type" then remove these content panes because
+      // they are node specific.
       if ($mode == "manage_type" and (
           $key == "field_resource_links" or
           $key == "field_resource_titles" or
@@ -337,6 +338,24 @@ function tripal_core_node_view_build_toc(&$build) {
             continue;
           }
 
+          // This field supports tokens, so we need to perform the substitutions
+          // if one is needed. Get the tokens and format
+          $base_table = preg_replace('/^chado_(.*)$/', '\1', $node_type);
+          $tokens = chado_node_generate_tokens($base_table);
+          $markup = $element['#markup'];
+          // Determine which tokens were used in the format string
+          if (preg_match_all('/\[[^]]+\]/', $markup, $used_tokens)) {
+            // Get the value for each token used
+            foreach ($used_tokens[0] as $token) {
+              $token_info = $tokens[$token];
+              if (!empty($token_info)) {
+                $value = chado_get_token_value($token_info, $node);
+                $markup = str_replace($token, $value, $markup);
+              }
+            }
+            $element['#markup'] = $markup;
+          }
+
           // Add the link to the TOC
           $parts = explode("|", $element['#markup']);
           if (count($parts) == 2) {
@@ -795,4 +814,5 @@ function tripal_core_content_type_toc_form_submit($form, &$form_state) {
       drupal_set_message("Failed to apply TOC changes.", "error");
     }
   }
-}
+}
+

+ 40 - 0
tripal_core/tripal_core.module

@@ -655,6 +655,46 @@ function tripal_core_exclude_type_by_default() {
   return array('text' => 'strlen("<field_value> ") > 250');
 }
 
+/**
+ * Adds support for tokens in the field_resource_links field.
+ *
+ * The field_resource_links field is a special field that can be manually
+ * added by the site admin for providing links on the Tripal TOC sidebar.
+ * Using tokens will allow for creation of custom links. This function
+ * will add a fieldset contiaining the list of appropriate tokens for the
+ * content type.
+ *
+ * @param unknown $element
+ * @param unknown $form_state
+ * @param unknown $context
+ */
+function tripal_core_field_widget_form_alter(&$element, &$form_state, $context) {
+
+  // If the name of the field is 'field_resource_links' then we want to
+  // add a fieldset of tokens.
+  if ($element['#field_name'] == 'field_resource_links') {
+
+    // Add the tokens fieldset to the last element.
+    $num_elements = count($context['items']);
+    if ($num_elements == $element['#delta']) {
+      $bundle = $element['#bundle'];
+      $base_table = preg_replace('/^chado_(.*)$/', '\1', $bundle);
+      $tokens = chado_node_generate_tokens($base_table);
+      $token_list = chado_node_format_tokens($tokens);
+      $element['tokens'] = array(
+        '#type' => 'fieldset',
+        '#title' => 'Available tokens',
+        '#collapsible' => TRUE,
+        '#collapsed' => TRUE,
+        '#weight' => 100
+      );
+      $element['tokens']['tokens_table'] = array(
+        '#type' => 'item',
+        '#markup' => $token_list
+      );
+    }
+  }
+}
 
 /**
  * Imports all of the Tripal API into scope.

+ 54 - 41
tripal_pub/includes/importers/tripal_pub.AGL.inc

@@ -1,17 +1,22 @@
 <?php
 /**
  * @file
- * This file provides support for importing and parsing of results from the 
- * USDA National Agricultural Library (AGL) database.  The functions here are used by
- * both the publication importer setup form and the publication importer.
- * 
- * The USDA AGL database uses a YAZ protocol for querying and retrieving records.
- * 
+ *
+ * Importer for the USDA Agricultural Library (Agricola).
+ *
+ * This file provides support for importing and parsing of results from the
+ * USDA National Agricultural Library (AGL) database.  The functions here are
+ * used by both the publication importer setup form and the publication
+ * importer. The USDA AGL database uses a YAZ protocol for querying and
+ * retrieving records.
+ *
  */
 
 /**
- * A hook for altering the publication importer form.  It Changes the 
- * 'Days' element to 'Year' and removes the 'Journal Name' filter.
+ * A hook for altering the publication importer form.
+ *
+ * It Changes the 'Days' element to 'Year' and removes the 'Journal Name'
+ * filter.
  *
  * @param $form
  *   The Drupal form array
@@ -47,7 +52,7 @@ function tripal_pub_remote_alter_form_AGL($form, $form_state, $num_criteria = 1)
  *   The Drupal form array
  * @param $form_state
  *   The form state array
- *  
+ *
  * @return
  *  The form (drupal form api)
  *
@@ -103,14 +108,17 @@ function tripal_pub_remote_search_AGL($search_array, $num_to_retrieve, $page) {
   // set some defaults
   $search_array['limit'] = $num_to_retrieve;
 
-  // To build the CCL search string we want to have a single entry for 'author', 'title', 'abstract'
-  // or 'id', and also the corresponding 'not for each of those.
-  // But the search form allows the user to have multiple rows of the same type. So, we will build the
-  // search string separately for each category and it's negative category (if NOT is selected as the op)
-  // and at the end we will put them together into a single search string.  We need to keep
-  // track of the first entry of any category because it will not have an op (e.g. 'or' or 'and') but the
-  // operation will be pushed out to separate the categories.  The op for any second or third instance of
-  // the same category will be included within the search string for the catgory.
+  // To build the CCL search string we want to have a single entry for
+  // 'author', 'title', 'abstract' or 'id', and also the corresponding 'not
+  // for each of those. But the search form allows the user to have multiple
+  // rows of the same type. So, we will build the search string separately for
+  // each category and it's negative category (if NOT is selected as the op)
+  // and at the end we will put them together into a single search string.  We
+  // need to keep track of the first entry of any category because it will not
+  // have an op (e.g. 'or' or 'and') but the operation will be pushed out to
+  // separate the categories.  The op for any second or third instance of
+  // the same category will be included within the search string for the
+  // category.
   $ccl = '';
   $title = '';
   $author = '';
@@ -143,18 +151,17 @@ function tripal_pub_remote_search_AGL($search_array, $num_to_retrieve, $page) {
       $op = strtolower($op);
     }
     $search_terms = trim($search_terms);
-    // if this is not a phrase then make sure the AND and OR are lower-case
+    // If this is not a phrase then make sure the AND and OR are lower-case.
     if (!$is_phrase) {
       $search_terms = preg_replace('/ OR /', ' or ', $search_terms);
       $search_terms = preg_replace('/ AND /', ' and ', $search_terms);
     }
-    // else make sure the search terms are surrounded by quotes
+    // Else make sure the search terms are surrounded by quotes.
     else {
       $search_terms = "\"$search_terms\"";
     }
 
-    // if this is a 'not' operation then we want to change it to an
-    // and
+    // If this is a 'not' operation then we want to change it to an "and".
     $negate = '';
     if ($op == 'not') {
       $scope = "negate_$scope";
@@ -162,7 +169,7 @@ function tripal_pub_remote_search_AGL($search_array, $num_to_retrieve, $page) {
     }
     $order[] = array('scope' => $scope, 'op' => $op);
 
-    // build each category
+    // Build each category.
     if ($scope == 'title') {
       if ($first_title) {
         $title .= "($search_terms) ";
@@ -272,7 +279,7 @@ function tripal_pub_remote_search_AGL($search_array, $num_to_retrieve, $page) {
       }
     }
   }
-  // now build the CCL string in order
+  // Now build the CCL string in order.
   $abstract_done = 0;
   $author_done = 0;
   $journal_done = 0;
@@ -342,24 +349,28 @@ function tripal_pub_remote_search_AGL($search_array, $num_to_retrieve, $page) {
     }
   }
 
-  // for AGL the 'days' form element was converted to represent the year
+  // For AGL the 'days' form element was converted to represent the year.
   if ($days) {
     $ccl .= "and year=($days)";
   }
 
-  // remove any preceeding 'and' or 'or'
+  // Remove any preceeding 'and' or 'or'.
   $ccl = preg_replace('/^\s*(and|or)/', '', $ccl);
 
-  // yaz_connect() prepares for a connection to a Z39.50 server. This function is non-blocking
-  // and does not attempt to establish a connection - it merely prepares a connect to be
-  // performed later when yaz_wait() is called.
-  //$yazc = yaz_connect('agricola.nal.usda.gov:7090/voyager'); // NAL Catalog
-  $yazc = yaz_connect('agricola.nal.usda.gov:7190/voyager');  // NAL Article Citation Database
+  // yaz_connect() prepares for a connection to a Z39.50 server. This function
+  // is non-blocking and does not attempt to establish a connection - it merely
+  // prepares a connect to be performed later when yaz_wait() is called.
+
+  // NAL Catalog
+  // $yazc = yaz_connect('agricola.nal.usda.gov:7090/voyager');
 
-  // use the USMARC record type.  But OPAC is also supported by Agricola
+  // NAL Article Citation Database
+  $yazc = yaz_connect('agricola.nal.usda.gov:7190/voyager');
+
+  // Use the USMARC record type.  But OPAC is also supported by Agricola.
   yaz_syntax($yazc, "usmarc");
 
-  // the search query is built using CCL, we need to first
+  // The search query is built using CCL, we need to first
   // configure it so it can map the attributes to defined identifiers
   // The attribute set used by AGL can be found at the bottom of this page:
   // http://agricola.nal.usda.gov/help/z3950.html
@@ -403,7 +414,7 @@ function tripal_pub_remote_search_AGL($search_array, $num_to_retrieve, $page) {
 }
 
 /**
- * Retreives a range of publications from AGL
+ * Retreives a range of publications from AGL.
  *
  * @param $yazc
  *   The YAZC connection object.
@@ -414,9 +425,9 @@ function tripal_pub_remote_search_AGL($search_array, $num_to_retrieve, $page) {
  * @param $num_to_retrieve
  *   The number of publications to retrieve
  * @param $total_records
- *   The total number of records in the dataset.  This value should have 
+ *   The total number of records in the dataset.  This value should have
  *   been retrieved by tripal_pub_AGL_count() function.
- *   
+ *
  * @return
  *  An array containing the total_records in the dataaset, the search string
  *  and an array of the publications that were retreived.
@@ -424,6 +435,7 @@ function tripal_pub_remote_search_AGL($search_array, $num_to_retrieve, $page) {
  * @ingroup tripal_pub
  */
 function tripal_pub_AGL_range($yazc, $search_str, $start, $num_to_retrieve, $total_records) {
+
   yaz_range($yazc, 1, $total_records);
   if (!yaz_present($yazc)) {
     $error_no = yaz_errno($yazc);
@@ -468,7 +480,7 @@ function tripal_pub_AGL_range($yazc, $search_str, $start, $num_to_retrieve, $tot
  *   The YAZC connection object.
  * @param $search_str
  *   The search string to use for searching.
- *   
+ *
  * @return
  *   a count of the total number of publications that match the search string
  *
@@ -476,7 +488,8 @@ function tripal_pub_AGL_range($yazc, $search_str, $start, $num_to_retrieve, $tot
  */
 function tripal_pub_AGL_count($yazc, $search_str) {
 
-  //yaz_sort($yazc, "1=31 id"); // sort by publication date descending
+  // Sort by publication date descending.
+  // yaz_sort($yazc, "1=31 id");
   if (!yaz_search($yazc, "rpn", $search_str)){
     $error_no = yaz_errno($yazc);
     $error_msg = yaz_error($yazc);
@@ -918,12 +931,12 @@ function tripal_pub_AGL_parse_pubxml($pub_xml) {
   $pub['Citation'] = tripal_pub_create_citation($pub);
 
   $pub['raw'] = $pub_xml;
-  
+
   return $pub;
 }
 
 /**
- * Used for parsing of the XML results to get a set of subfields 
+ * Used for parsing of the XML results to get a set of subfields
  *
  * @param $xml
  *   The XMl object to read
@@ -952,14 +965,14 @@ function tripal_pub_remote_search_AGL_get_subfield($xml) {
 }
 
 /**
- * Used for parsing of the XML results to get details about an author 
+ * Used for parsing of the XML results to get details about an author
  *
  * @param $xml
  *   The XML object to read
  * @param $ind1
  *   Indicates how an author record is stored; 0 means given name is first
  *   1 means surname is first, 3 means a family name is given
- *   
+ *
  * @return
  *
  *

+ 7 - 5
tripal_pub/includes/tripal_pub.pub_importers.inc

@@ -125,26 +125,28 @@ function tripal_pub_importer_setup_page($action = 'new', $pub_import_id = NULL)
       $search_array['criteria'][$i]['operation']    = $_SESSION['tripal_pub_import']['criteria'][$i]['operation'];
     }
 
-    // if the form has been submitted with the 'test' button then get the results
+    // If the form has been submitted with the 'test' button then get results.
     if ($_SESSION['tripal_pub_import']['perform_search']) {
 
       $limit = 25;
 
-      // get the list of publications from the remote database using the search criteria.
+      // Get the list of publications from the remote database using the search
+      // criteria.
       $page = isset($_GET['page']) ? $_GET['page'] : '0';
       $results = tripal_get_remote_pubs($remote_db, $search_array, $limit, $page);
       $total_records = $results['total_records'];
       $search_str    = $results['search_str'];
       $pubs          = $results['pubs'];
 
-      // iterate through the results and construct the table displaying the publications
+      // Iterate through the results and construct the table displaying the
+      // publications
       $rows = array();
       $i = $page * $limit + 1;
       if (count($pubs) > 0) {
         foreach ($pubs as $pub) {
-          $citation = htmlspecialchars($pub['Citation']);
+          $citation = array_key_exists('Citation', $pub) ? htmlspecialchars($pub['Citation']) : 'Unable to generate citation';
           $raw_link = '';
-          if($pub['Publication Dbxref']) {
+          if(array_key_exists('Publication Dbxref', $pub) and $pub['Publication Dbxref']) {
             $raw_link = l('raw', 'admin/tripal/chado/tripal_pub/import/raw/' . $pub['Publication Dbxref'], array('attributes' => array('target' => '_blank')));
           }
           $rows[] = array(