Browse Source

nightly check in

Stephen Ficklin 10 years ago
parent
commit
40b5ee89a3

+ 7 - 24
tripal_core/api/tripal_core.tripal.api.inc

@@ -208,7 +208,8 @@ function tripal_set_message($message, $importance = TRIPAL_INFO, $options = arra
  * @param $message
  *   The message to be logged. Need not contain date/time information
  * @param $log_type
- *   The type of log. Should be one of 'error' or 'job'
+ *   The type of log. Should be one of 'error' or 'job' although other types
+ *   are supported.
  * @param $options
  *   An array of options where the following keys are supported:
  *     - first_progress_bar: this sohuld be used for the first log call for a progress bar
@@ -218,36 +219,18 @@ function tripal_set_message($message, $importance = TRIPAL_INFO, $options = arra
  * @return
  *   The number of bytes that were written to the file, or FALSE on failure
  */
-function tripal_log($message, $type, $options = array()) {
-
-  $date   = date("Y-m-d H:i:s");
-  $prefix = "[$date]";
-  $indent = "\t\t\t";
-
-  $log_filename = tripal_get_logfile_path($type);
+function tripal_log($message, $type = 'error', $options = array()) {
+  global $base_url;
+  $prefix = '[TRIPAL ' . strtoupper(check_plain($type)) . '] ';
 
   if (!isset($options['is_progress_bar'])) {
-    $message = $prefix . "\t" . str_replace("\n", "\n$indent", trim($message)) . "\n";
+    $message = $prefix . str_replace("\n", "", trim($message));
   }
 
   if (isset($options['first_progress_bar'])) {
     $message = trim($message);
   }
 
-  return file_put_contents($log_filename, $message, FILE_APPEND);
+  return error_log($message);
 
 }
-
-/**
- * Return the filename including path to the specified log file
- *
- * @param $type
- *   The type of log. See tripal_log for supported types
- * @return
- *  The filename including path to the specified log file
- */
-function tripal_get_logfile_path($type) {
-
-  return '/tmp/tripal_' . $type . '_' . date('Ymd') . '.log';
-
-}

+ 470 - 1
tripal_core/includes/tripal_core.toc.inc

@@ -3,13 +3,482 @@
 /**
  * 
  */
-function tripal_core_node_toc_form($form, &$form_state) {
+function tripal_core_node_toc_form($form, &$form_state, $node) {
   
+  $form["#tree"] = TRUE;
+  
+  $form["instructions"] = array(
+    '#markup' => '</p>' . t('Below is a list of the titles of
+      content panes that can appear on this page.  These titles appear in the 
+      the following order in the Table of Contents (TOC). You may rename 
+      the titles or drag and drop them to change the order.  <b>Any changes will
+      only apply to this page</b>. If you would like to make changes apply to multiple
+      pages of the same tpye, please visit the TOC administrative page.') . '<p>' .
+      '<p>' . t('The list below shows all possible content panes that can appear.
+      However, those without content are hidden and do not appear in the TOC.' . '</p>'),
+  );
+  
+  $form['node'] = array(
+    '#type' => 'value',
+    '#value' => $node,
+  );
+  
+  // Get the content array for this node, then pass it through the 
+  // tripal_core_node_view_alter which generates the TOC.  After that
+  // we can use the $build array to build the form.
+  node_build_content($node);
+  $build = $node->content;
+  $build["#node"] = $node;
+  tripal_core_node_view_alter($build);
+
+  // Iterate through the built items and add form elemetns for each one.
+  foreach(element_children($build) as $key) {
+    $element = $build[$key];
+    if (array_key_exists('#tripal_toc_id', $element)) {
+      $toc_id = $element['#tripal_toc_id'];
+      $toc_title = $element['#tripal_toc_title'];
+      $toc_weight = $element['#weight'];
+      $form['toc_items'][$toc_id]['title'] = array(
+        '#type' => 'textfield',
+        '#default_value' => $toc_title,
+      );
+      $form['toc_items'][$toc_id]['weight'] = array(
+        '#type' => 'textfield',
+        '#default_value' => $toc_weight,
+        '#attributes' => array(
+          'class' => array('tripal-node-toc-items-weights'),
+        ),
+        '#size' => 5,
+      );
+    }
+  }
+  $form['toc_items']['#theme'] = 'tripal_node_toc_items_table';
+  
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#name' => 'toc_submit',
+    '#value' => t('Submit'),
+  );
+  return $form;
+}
+/**
+ * 
+ * @param $variables
+ */
+function theme_tripal_node_toc_items_table($variables) {
+  $elements = $variables['element'];
+  $toc_items = array();
+
+  // Sort the toc_items using a custom sort function. But we need to include
+  // only the items we want in the table (exclude renderable stuff).
+  foreach(element_children($elements) as $key) {
+    $toc_items[] = $elements[$key];
+  }
+  usort($toc_items, 'theme_tripal_node_sort_toc_items');
+
+  // Build the table header.
+  $headers = array('Content Pane Name', 'Weight');
+  
+  // Format the form elements as rows in the table.
+  $rows = array();
+  foreach ($toc_items as $key => $item) {
+    $rows[] = array(
+      'data' => array(
+        drupal_render($item['title']),
+        drupal_render($item['weight']),
+      ),
+      'class' => array('draggable'),
+    );
+  }
+  
+  // Theme and return the table.
+  $table = array(
+    'header' => $headers,
+    'rows' => $rows,
+    'attributes' => array("id" => 'tripal-node-toc-items-table'),
+    'sticky' => TRUE,
+    'caption' => t('Content Panes Available in the TOC'),
+    'colgroups' => array(),
+    'empty' => t('There are no content panes for this page'),
+  );
+  drupal_add_tabledrag('tripal-node-toc-items-table', 'order', 'sibling', 'tripal-node-toc-items-weights');
+  return theme_table($table);
 }
 
 /**
  * 
+ * @param $a
+ * @param $b
+ */
+function theme_tripal_node_sort_toc_items($a, $b) {
+  
+  if ($a['weight']['#value'] < $b['weight']['#value']) {
+    return -1;
+  }
+  if ($a['weight']['#value'] > $b['weight']['#value']) {
+    return 1;
+  }
+  if ($a['weight']['#value'] == $b['weight']['#value']) {
+    return strcmp($a['title']['#value'], $b['title']['#value']);
+  }
+}
+/**
+ * Implements hook_validate for the tripal_core_node_toc_form.
+ */
+function tripal_core_node_toc_form_validate($form, &$form_state) {
+  $toc_items = $form_state['values']['toc_items'];
+
+  // Iterate through the TOC items and validate.
+  foreach ($toc_items as $toc_id => $item) {
+    if (!$item['title']) {
+      form_set_error('toc_items][' . $toc_id, "Please provide a valid title.");
+    }
+  }
+}
+/**
+ * Implements hook_submit for the tripal_core_node_toc_form.
  */
 function tripal_core_node_toc_form_submit($form, &$form_state) {
+  $toc_items = $form_state['values']['toc_items'];
+  $node      = $form_state['values']['node'];
+  
+  $transaction = db_transaction();
+  try {
+    // First delete any settings for this node
+    db_delete('tripal_toc')
+      ->condition('nid', $node->nid)
+      ->execute();
+
+    // Second add in any new settings for this node 
+    foreach ($toc_items as $toc_id => $item) {
+      db_insert('tripal_toc')
+        ->fields(array(
+          'node_type' => $node->type,
+          'key' => $toc_id,
+          'title' => $item['title'],
+          'weight' => $item['weight'],
+          'nid' => $node->nid,
+        ))
+        ->execute();
+    }
+    drupal_set_message("TOC changes successfully applied to this node only.");
+  }
+  catch (Exception $e) {
+    $transaction->rollback();
+    drupal_set_message("Failed to apply TOC changes.", "error");
+  }
+}
+
+/**
+ * To be called by tripal_core_node_view_alter() to generate the TOC.
+ * 
+ * @param $build
+ *   The build array passed to hook_node_view_alter()
+ */
+function tripal_core_node_view_add_toc(&$build) {
+  global $theme;
+  
+  // if this is not a full node view, we do not want to alter
+  if ($build['#view_mode'] != 'full' OR !array_key_exists('#tripal_generic_node_template', $build)) {
+    return;
+  }
+  
+  $node_type = $build["#node"]->type;
+  $nid = $build["#node"]->nid;
+  
+  
+  $cache = cache_get("theme_registry:$theme", 'cache');
+  $node = $build['#node'];
+  $toc = array();
+  $toc_html = '';
+  
+  // If we are looking at a Tripal node template then we want to
+  // make some changes to each pane of content so that we can associate
+  // a table of contents and add administrator and curator messages.
+  if ($build['#tripal_generic_node_template'] == TRUE) {
+  
+    // Iterate through all the elements of the $build array and for those
+    // that are wanting to provide content for this node.
+    $markup = array();
+    foreach ($build as $key => $value) {
+      $value = $build[$key];
+      
+      // Skip the body element as the Tripal node types do not use it.
+      if ($key == 'body') {
+        continue;
+      }
+  
+      // Skip the table of contents and links as those will be placed elsewhere.
+      if (preg_match('/^#/', $key) or $key == 'tripal_toc' or $key == 'links') {
+        continue;
+      }
+
+      // 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 ($key == "field_resource_links") {
+        // links should just appear on the sidebar as is and not open up a panel
+        foreach (element_children($build[$key]) as $index) {
+          $element = $build[$key][$index];
+          $weight = 0;
+          $parts = explode("|", $element['#markup']);
+          if (count($parts) == 2) {
+            $toc[$weight][$parts[0]] = "<div class=\"tripal_toc_list_item\">" . l($parts[0], $parts[1], array('attributes' => array('target' => '_blank'))) . "</div>";
+          }
+          else {
+            $toc[$weight][$parts[0]] = "<div class=\"tripal_toc_list_item\">" . $element['#markup'] . "</div>";
+          }
+          // remove this link from the build array as we've moved it to appear in the TOC
+          unset($build[$key]);
+        }
+        continue;
+      }
+      if ($key == "field_resource_titles") {
+        // ignore these, we will use them in the field_resource_blocks if
+        // statement below
+        continue;
+      }
+      if ($key == "field_resource_blocks") {
+        foreach (element_children($build[$key]) as $index) {
+          
+          // get the details and the title
+          $weight = 0;
+          $markup = $build[$key][$index]["#markup"];
+          $toc_item_id = "resource-$index";
+          
+          // Get any overrides for this key.
+          $overrides = tripal_core_get_toc_overrides($nid, $toc_item_id, $node_type);
+          
+          // If the element should be hidden then unset this key the build
+          // array continue to the next one
+          if ($overrides['hide'] == 1) {
+            unset($build[$key]);
+            continue;
+          }
+          
+          $toc_item_title = $build["field_resource_titles"][$index]["#markup"];
+          $toc_item_title = $overrides['title'] ? $overrides['title'] : $toc_item_title;
+          $weight = $overrides['weight'] ? $overrides['weight'] : $weight;
+          $updated_markup = "
+            <div id=\"$toc_item_id-tripal-data-pane\" class=\"tripal-data-pane\">
+              <div class=\"$toc_item_id-tripal-data-pane-title tripal-data-pane-title\">$toc_item_title</div>
+                $markup
+              </div>
+            </div>
+          ";
+          $build[$toc_item_id]['#markup'] = $updated_markup;
+          $build[$toc_item_id]['#toc_handled'] = TRUE;
+          $build[$toc_item_id]['#tripal_toc_id'] = $toc_item_id;
+          $build[$toc_item_id]['#tripal_toc_title'] = $toc_item_title;
+          $build[$toc_item_id]['#weight'] = $weight;
+          // add the entry to the TOC
+          $toc_item_link = "<div class=\"tripal_toc_list_item\"><a id=\"$toc_item_id\" class=\"tripal_toc_list_item_link\" href=\"?pane=$toc_item_id\">$toc_item_title</a></div>";
+          $toc[$weight][$toc_item_title] = $toc_item_link;
+        }
+        // Remove the key from the build array. We have have replaced it
+        unset($build[$key]);
+        unset($build["field_resource_titles"]);
+        continue;
+      }
+  
+      // Skip any keys we may have already handled. This is the case for
+      // the field_resource_blocks where we removed the old CCK fields
+      // and added new ones.  We don't want these new ones to be processed
+      // again by the code below.
+      if (array_key_exists('#toc_handled', $build[$key]) and $build[$key]['#toc_handled'] == TRUE) {
+        continue;
+      }
+
+      // For all other fields we will handle in the following way.
+      //-----------------------
+      // INITIALIZE THE CONTENT VARIABLES
+      //-----------------------
+      $toc_item_title = $key;
+      $toc_item_id    = $key;
+      $toc_item_link  = '';
+      $weight = 0;
+
+      // get the title for the table of contents.  Tripal templates should
+      // have a '#tripal_toc_title' element in the build array
+      if (array_key_exists('#tripal_toc_title', $build[$key])) {
+        $toc_item_title = $build[$key]['#tripal_toc_title'];
+      }
+      // other elements in the $build array may just have a '#title' element,
+      if (array_key_exists('#title', $build[$key])) {
+        $toc_item_title = $build[$key]['#title'];
+      }
+      $toc_item_title = ucwords($toc_item_title);
+      if (array_key_exists('#weight', $build[$key])) {
+        $weight = $build[$key]['#weight'];
+      }
+      
+      // Get any overrides for this key.
+      $overrides = tripal_core_get_toc_overrides($nid, $toc_item_id, $node_type);
+      
+      // If the element should be hidden then unset this key the build
+      // array continue to the next one
+      if ($overrides['hide'] == 1) {
+        unset($build[$key]);
+        continue;
+      }
+
+      // now override the title if a value is set in the tripal_toc table
+      $toc_item_title = $overrides['title'] ? $overrides['title'] : $toc_item_title;
+      $weight = $overrides['weight'] ? $overrides['weight'] : $weight;
+  
+      if (array_key_exists('#tripal_toc_id', $build[$key])) {
+        $toc_item_id = $build[$key]['#tripal_toc_id'];
+      }
+      $toc_item_link = "<div class=\"tripal_toc_list_item\"><a id=\"$toc_item_id\" class=\"tripal_toc_list_item_link\" href=\"?pane=$toc_item_id\">$toc_item_title</a></div>";
+  
+  
+      //-----------------------
+      // GET THE MARKUP FOR EACH ELEMENT
+      //-----------------------
+      $markup = '';
+  
+      // find the markup. Some fields will have a '#markup' and others, such
+      // as CCK elements may have a set of '#markup' elements organized by
+      // numerical keys.
+      if (array_key_exists('#markup', $build[$key]) and trim($build[$key]['#markup'])) {
+        $markup = $build[$key]['#markup'];
+      }
+      // For backwards copmatibility we should support the '#value' element as well.
+      elseif (array_key_exists('#value', $build[$key]) and trim($build[$key]['#value'])) {
+        $markup = $build[$key]['#markup'];
+      }
+  
+      // if we have no '#markup' field then this element has not yet
+      // been rendered.  Let's render it and substitute that for markup
+      if (!$markup) {
+        $markup = trim(render($build[$key]));
+        $build[$key] = array(
+          '#markup' => $markup,
+          '#tripal_toc_id' => $toc_item_id,
+          '#tripal_toc_title' => $toc_item_title,
+          '#weight' => $weight,
+        );
+      }
+  
+        // if we still don't have markup then skip this one
+      if (!$markup) {
+        continue;
+      }
+  
+      //-----------------------
+      // FIND THE TEMPLATE PATH
+      //-----------------------
+      // get the template path so we can put it in an admin message box
+      $path = '';
+      if (!array_key_exists('#tripal_template_show', $build[$key]) or
+        $build[$key]['#tripal_template_show'] == TRUE) {
+        if ($cache and array_key_exists($key, $cache->data) and array_key_exists('path', $cache->data[$key])) {
+    
+          $path = $cache->data[$key]['path'] . '/' . $key . '.tpl.php';
+    
+          $path = tripal_set_message("Administrators, you can
+            customize the way the content above is presented.  Tripal provides a template
+            file for each pane of content.  To customize, copy the template file to your
+            site's default theme, edit then " .
+            l('clear the Drupal cache', 'admin/config/development/performance', array('attributes' => array('target' => '_blank'))) . ".
+            Currently, the content above is provided by this template: <br><br>$path",
+            TRIPAL_INFO,
+            array('return_html' => 1)
+          );
+        }
+      }
+    
+      //-----------------------
+      // SET THE WEIGHTS FOR THE TOC ELEMENTS
+      //-----------------------
+      // set the weight of the TOC item and add it to our $toc array
+      // for building of the TOC below
+      $weight = 0;
+      if (array_key_exists('#weight', $build[$key])) {
+        $weight = $build[$key]['#weight'];
+      }
+      // override the weight if it's set in the tripal_toc table
+      $weight = $overrides['weight'] ? $overrides['weight'] : $weight;
+      $toc[$weight][$toc_item_title] = $toc_item_link;
+
+      //-----------------------
+      // CREATE THE DATA BLOCK
+      //-----------------------
+      // add a surrounding <div> box around the content
+      $updated_markup = "
+        <div id=\"$toc_item_id-tripal-data-pane\" class=\"tripal-data-pane\">
+          <div class=\"$toc_item_id-tripal-data-pane-title tripal-data-pane-title\">$toc_item_title</div>
+            $markup
+            $path
+          </div>
+        </div>
+      ";
+  
+      $build[$key]['#markup'] = $updated_markup;
+      $build[$key]['#weight'] = $weight;
+    } // end foreach ($build as $key => $value) {
+  } // end if ($build['#tripal_generic_node_template'] == TRUE) {
+  
+  //-----------------------
+  // BUILD THE TABLE OF CONTENTS LINKS
+  //-----------------------
+  // first sort the links numerically by their weight
+  ksort($toc, SORT_NUMERIC);
+  $toc_html = '';
+  foreach ($toc as $weight => $links) {
+    // for links in the same weight, sort them alphabetically
+    ksort($links);
+    foreach ($links as $toc_item_title => $toc_item_link) {
+      $toc_html .= $toc_item_link;
+    }
+  }
+  $build['tripal_toc']['#markup'] = "<div id=\"$node->type-tripal-toc-pane\" class=\"tripal-toc-pane\">$toc_html</div>";
+  
+}
+
+/**
+ * 
+ * @param $build
+ */
+function tripal_core_get_toc_overrides($nid, $key, $node_type) {
+  // Set override defaults
+  $override_title = '';
+  $override_weight = '';
+  $override_hide = 0;
+  
+  // First look to see if the node has customizations for this item.
+  $toc_item_overrides = db_select('tripal_toc', 'tc')
+    ->fields('tc', array('title', 'weight', 'hide'))
+    ->condition('key', $key)
+    ->condition('nid', $nid)
+    ->execute()
+    ->fetchObject();
+  if ($toc_item_overrides) {
+    $override_title = $toc_item_overrides->title;
+    $override_weight = $toc_item_overrides->weight;
+    $override_hide = $toc_item_overrides->hide;
+  }
+  // If there are no specific node customizations then look to see if there
+  // are customizations for this content type.
+  else {
+    $toc_item_overrides = db_select('tripal_toc', 'tc')
+      ->fields('tc', array('title', 'weight', 'hide'))
+      ->condition('node_type', $node_type)
+      ->condition('key', $key)
+      ->isNull('nid')
+      ->execute()
+      ->fetchObject();
+    if ($toc_item_overrides) {
+      $override_title = $toc_item_overrides->title;
+      $override_weight = $toc_item_overrides->weight;
+      $override_hide = $toc_item_overrides->hide;
+    }
+  }
   
+  return array(
+    'title' => $override_title,
+    'weight' => $override_weight,
+    'hide' => $override_hide,
+  );
 }

+ 12 - 271
tripal_core/tripal_core.module

@@ -492,7 +492,13 @@ function tripal_core_theme($existing, $type, $theme, $path) {
     'tripal_admin_message' => array(
       'function' => 'theme_tripal_admin_message',
       'variables' => array('message' => NULL),
-    )
+    ),
+    
+    // Form and form element themes.
+    // --------------------------------
+    'tripal_node_toc_items_table' => array(
+      'render element' => 'element',
+    ),
   );
 }
 
@@ -568,283 +574,18 @@ function tripal_core_views_api() {
 }
 
 /**
- * After the node is built, we want to add instructions to each
- * content section letting the administrator know which template
- * they can customize
+ * Implements hook_node_view_alter()
  *
  * @param unknown $build
  */
 function tripal_core_node_view_alter(&$build) {
-  global $theme;
-
-  // if this is not a full node view, we do not want to alter
-  if ($build['#view_mode'] != 'full' OR !array_key_exists('#tripal_generic_node_template', $build)) {
-    return;
-  }
-
-  $node_type = $build["#node"]->type;
-  $nid = $build["#node"]->nid;
-
-
-  $cache = cache_get("theme_registry:$theme", 'cache');
-  $node = $build['#node'];
-  $toc = array();
-  $toc_html = '';
-
-  // If we are looking at a Tripal node template then we want to
-  // make some changes to each pane of content so that we can associate
-  // a table of contents and add administrator and curator messages.
-  if ($build['#tripal_generic_node_template'] == TRUE) {
-
-    // Iterate through all the elements of the $build array and for those
-    // that are wanting to provide content for this node.
-    $markup = array();
-    foreach ($build as $key => $value) {
-
-      // Skip the body element as the Tripal node types do not use it.
-      if ($key == 'body') {
-        continue;
-      }
-
-      // Examine elements without a '#' prefix as these should be adding
-      // contents to the page. Skip the table of contents and links as those
-      // will be placed elsewhere.
-      if (preg_match('/^#/', $key) or $key == 'tripal_toc' or $key == 'links') {
-        continue;
-      }
-      // Look to see if the title, position and visibilty of this element has
-      // custom settings.  First check if the node is customized then check
-      // if the node type.
-      $override_title = '';
-      $override_weight = '';
-      $override_hide = 0;
-      $toc_item_overrides = db_select('tripal_toc', 'tc')
-        ->fields('tc', array('toc_item_id', 'title', 'weight', 'hide'))
-        ->condition('node_type', $node_type)
-        ->condition('key', $key)
-        ->condition('nid', $nid)
-        ->execute()
-        ->fetchObject();
-      if ($toc_item_overrides) {
-        $override_title = $toc_item_overrides->title;
-        $override_weight = $toc_item_overrides->weight;
-        $override_hide = $toc_item_overrides->hide;
-      }
-      else {
-
-        // get the override title, weight for the general case
-        $toc_item_overrides = db_select('tripal_toc', 'tc')
-          ->fields('tc', array('toc_item_id', 'title', 'weight', 'hide'))
-          ->condition('node_type', $node_type)
-          ->condition('key', $key)
-          ->isNull('nid')
-          ->execute()
-          ->fetchObject();
-        if ($toc_item_overrides) {
-          $override_title = $toc_item_overrides->title;
-          $override_weight = $toc_item_overrides->weight;
-          $override_hide = $toc_item_overrides->hide;
-        }
-      }
-
-      // If the element should be hidden then unset this key the build
-      // array continue to the next one
-      if ($override_hide == 1) {
-        unset($build[$key]);
-        continue;
-      }
-
-      // 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 ($key == "field_resource_links") {
-        // links should just appear on the sidebar as is and not open up a panel
-        foreach (element_children($build[$key]) as $index) {
-          $element = $build[$key][$index];
-          $weight = 0;
-          $parts = explode("|", $element['#markup']);
-          if (count($parts) == 2) {
-            $toc[$weight][$parts[0]] = "<div class=\"tripal_toc_list_item\">" . l($parts[0], $parts[1], array('attributes' => array('target' => '_blank'))) . "</div>";
-          }
-          else {
-            $toc[$weight][$parts[0]] = "<div class=\"tripal_toc_list_item\">" . $element['#markup'] . "</div>";
-          }
-          // remove this link from the build array as we've moved it to appear in the TOC
-          unset($build[$key]);
-        }
-        continue;
-      }
-      if ($key == "field_resource_titles") {
-        // ignore these, we will use them in the field_resource_blocks if
-        // statement below
-        continue;
-      }
-      if ($key == "field_resource_blocks") {
-        foreach (element_children($build[$key]) as $index) {
-          // get the details and the title
-          $weight = 0;
-          $markup = $build[$key][$index]["#markup"];
-          $toc_item_id = "resource-$index";
-          $toc_item_title = $build["field_resource_titles"][$index]["#markup"];
-          $updated_markup = "
-            <div id=\"$toc_item_id-tripal-data-pane\" class=\"tripal-data-pane\">
-              <div class=\"$toc_item_id-tripal-data-pane-title tripal-data-pane-title\">$toc_item_title</div>
-                $markup
-              </div>
-            </div>
-          ";
-          $build[$toc_item_id]['#markup'] = $updated_markup;
-          $build[$toc_item_id]['#weight'] = $weight;
-          $build[$toc_item_id]['#toc_handled'] = TRUE;
-          // add the entry to the TOC
-          $toc_item_link = "<div class=\"tripal_toc_list_item\"><a id=\"$toc_item_id\" class=\"tripal_toc_list_item_link\" href=\"?pane=$toc_item_id\">$toc_item_title</a></div>";
-          $toc[$weight][$toc_item_title] = $toc_item_link;
-        }
-        // Remove the key from the build array. We have have replaced it
-        unset($build[$key]);
-        unset($build["field_resource_titles"]);
-        continue;
-      }
-
-      // Skip any keys we may have already handled. This is the case for
-      // the field_resource_blocks where we removed the old CCK fields
-      // and added new ones.  We don't want these new ones to be processed
-      // again by the code below.
-      if (array_key_exists('#toc_handled', $build[$key]) and $build[$key]['#toc_handled'] == TRUE) {
-        continue;
-      }
-
-      // For all other fields we will handle in the following way
-      //-----------------------
-      // INITIALIZE THE CONTENT VARIABLES
-      //-----------------------
-      $toc_item_title = $key;
-      $toc_item_id    = $key;
-      $toc_item_link  = '';
-
-      // get the title for the table of contents.  Tripal templates should
-      // have a '#tripal_toc_title' element in the build array
-      if (array_key_exists('#tripal_toc_title', $build[$key])) {
-        $toc_item_title = $build[$key]['#tripal_toc_title'];
-      }
-      // other elements in the $build array may just have a '#title' element,
-      if (array_key_exists('#title', $build[$key])) {
-        $toc_item_title = $build[$key]['#title'];
-      }
-      $toc_item_title = ucwords($toc_item_title);
-
-      // now override the title if a value is set in the tripal_toc table
-      $toc_item_title = $override_title ? $override_title : $toc_item_title;
-
-      if (array_key_exists('#tripal_toc_id', $build[$key])) {
-        $toc_item_id = $build[$key]['#tripal_toc_id'];
-      }
-      $toc_item_link = "<div class=\"tripal_toc_list_item\"><a id=\"$toc_item_id\" class=\"tripal_toc_list_item_link\" href=\"?pane=$toc_item_id\">$toc_item_title</a></div>";
-
-
-      //-----------------------
-      // GET THE MARKUP FOR EACH ELEMENT
-      //-----------------------
-      $markup = '';
-
-      // find the markup. Some fields will have a '#markup' and others, such
-      // as CCK elements may have a set of '#markup' elements organized by
-      // numerical keys.
-      if (array_key_exists('#markup', $build[$key]) and trim($build[$key]['#markup'])) {
-        $markup = $build[$key]['#markup'];
-      }
-      // For backwards copmatibility we should support the '#value' element as well.
-      elseif (array_key_exists('#value', $build[$key]) and trim($build[$key]['#value'])) {
-        $markup = $build[$key]['#markup'];
-      }
-
-      // if we have no '#markup' field then this element has not yet
-      // been rendered.  Let's render it and substitute that for markup
-      if (!$markup) {
-        $markup = trim(render($build[$key]));
-        $build[$key] = array(
-          '#markup' => $markup,
-        );
-      }
-
-      // if we still don't have markup then skip this one
-      if (!$markup) {
-        continue;
-      }
-
-      //-----------------------
-      // FIND THE TEMPLATE PATH
-      //-----------------------
-      // get the template path so we can put it in an admin message box
-      $path = '';
-      if (!array_key_exists('#tripal_template_show', $build[$key]) or
-           $build[$key]['#tripal_template_show'] == TRUE) {
-        if ($cache and array_key_exists($key, $cache->data) and array_key_exists('path', $cache->data[$key])) {
-
-          $path = $cache->data[$key]['path'] . '/' . $key . '.tpl.php';
-
-          $path = tripal_set_message("Administrators, you can
-            customize the way the content above is presented.  Tripal provides a template
-            file for each pane of content.  To customize, copy the template file to your
-            site's default theme, edit then " .
-            l('clear the Drupal cache', 'admin/config/development/performance', array('attributes' => array('target' => '_blank'))) . ".
-            Currently, the content above is provided by this template: <br><br>$path",
-            TRIPAL_INFO,
-            array('return_html' => 1)
-          );
-        }
-      }
-
-      //-----------------------
-      // SET THE WEIGHTS FOR THE TOC ELEMENTS
-      //-----------------------
-      // set the weight of the TOC item and add it to our $toc array
-      // for building of the TOC below
-      $weight = 0;
-      if (array_key_exists('#weight', $build[$key])) {
-        $weight = $build[$key]['#weight'];
-      }
-      // override the weight if it's set in the tripal_toc table
-      $weight = $override_weight ? $override_weight : $weight;
-      $toc[$weight][$toc_item_title] = $toc_item_link;
-
-      //-----------------------
-      // CREATE THE DATA BLOCK
-      //-----------------------
-      // add a surrounding <div> box around the content
-      $updated_markup = "
-        <div id=\"$toc_item_id-tripal-data-pane\" class=\"tripal-data-pane\">
-          <div class=\"$toc_item_id-tripal-data-pane-title tripal-data-pane-title\">$toc_item_title</div>
-            $markup
-            $path
-          </div>
-        </div>
-      ";
-
-      $build[$key]['#markup'] = $updated_markup;
-      $build[$key]['#weight'] = $weight;
-    } // end foreach ($build as $key => $value) {
-  } // end if ($build['#tripal_generic_node_template'] == TRUE) {
-
-  //-----------------------
-  // BUILD THE TABLE OF CONTENTS LINKS
-  //-----------------------
-  // first sort the links numerically by their weight
-  ksort($toc, SORT_NUMERIC);
-  $toc_html = '';
-  foreach ($toc as $weight => $links) {
-    // for links in the same weight, sort them alphabetically
-    ksort($links);
-    foreach ($links as $toc_item_title => $toc_item_link) {
-      $toc_html .= $toc_item_link;
-    }
-  }
-  $build['tripal_toc']['#markup'] = "<div id=\"$node->type-tripal-toc-pane\" class=\"tripal-toc-pane\">$toc_html</div>";
+  module_load_include('inc', 'tripal_core', 'includes/tripal_core.toc');
+  tripal_core_node_view_add_toc($build);
 }
 
 /**
- *
+ * Implements hook_node_view()
+ * 
  * @ingroup tripal_core
  */
 function tripal_core_node_view($node, $view_mode, $langcode) {

+ 2 - 2
tripal_db/api/tripal_db.api.inc

@@ -293,8 +293,8 @@ function tripal_get_dbxref($identifiers, $options = array()) {
  *   - urlprefix: (Optional) The URL that is to be used as a prefix when constructing a
  *     link to a database term
  * @param $options
- *   An associative array of options including:
- *   - update_existing: (Optional) Set this to '1' to force an update of the database if it
+ *   Optional. An associative array of options that can include:
+ *   - update_existing: Set this to '1' to force an update of the database if it
  *     already exists. The default is to not update. If the database exists
  *     then nothing is added.
  *

+ 1 - 1
tripal_feature/api/tripal_feature.api.inc

@@ -385,7 +385,7 @@ function tripal_get_feature_sequences($feature, $options) {
       $dir = 'forward';
       $length = strlen($seq);
       if ($parent->strand < 0) {
-        $seq = tripal_feature_reverse_complement($seq);
+        $seq = tripal_reverse_compliment_sequence($seq);
         $dir = 'reverse';
       }
       // now format for display

+ 2 - 2
tripal_feature/theme/tripal_feature.theme.inc

@@ -158,7 +158,7 @@ function tripal_feature_load_featureloc_sequences($feature_id, $featurelocs) {
       $sequence = chado_query($sql, $args)->fetchObject();
       $residues = $sequence->residues;
       if ($featureloc->strand < 0) {
-        $residues = tripal_feature_reverse_complement($residues);
+        $residues = tripal_reverse_compliment_sequence($residues);
       }
       $strand = '.';
       if ($featureloc->strand == 1) {
@@ -911,4 +911,4 @@ function tripal_feature_get_feature_relationships($feature) {
     }
   }
   return $relationships;
-}
+}

+ 1 - 1
tripal_organism/api/tripal_organism.DEPRECATED.inc

@@ -89,5 +89,5 @@ function tripal_organism_get_image_url($organism, $nid = NULL) {
     )
   );
 
-  return tripal_get_organism_image($organism, $nid);
+  return tripal_get_organism_image_url($organism);
 }

+ 33 - 22
tripal_organism/api/tripal_organism.api.inc

@@ -174,23 +174,34 @@ function tripal_get_organism_select_options($syncd_only = TRUE) {
  *
  * @param $organism
  *   An organism table record
- * @param $nid
- *   (Optional). The node id of the organism node. if not supplied it will be looked up
- * @param $type
- *   (Optional). Specify the type of path, either 'url' or path'. Default is 'path'
  *
  * @return
  *   If the type parameter is 'url' (the default) then the fully qualified 
- *   url to the image is returend. If the type is 'path' then the full 
- *   filesystem is returned.
+ *   url to the image is returend. If no image is present then NULL is returned
  */
-function tripal_get_organism_image($organism, $nid = NULL, $type = 'url') {
+function tripal_get_organism_image_url($organism) {
   $url = '';
+  
+  // Get the organism's node
+  $nid = chado_get_nid_from_id('organism', $organism->organism_id);
+  
+  // Look in the file_usage table of Drupal for the image file. This
+  // is the current way for handling uploaded images. It allows the file to
+  // keep it's proper name and extension.
+  $fid = db_select('file_usage', 'fu')
+    ->fields('fu', array('fid'))
+    ->condition('module', 'tripal_organism')
+    ->condition('type', 'organism_image')
+    ->condition('id', $nid)
+    ->execute()
+    ->fetchField();
+  if ($fid) {
+    $file = file_load($fid);
+    return file_create_url($file->uri);
+  }
 
-  // first look for an image with the genus/species name.  This is old-style tripal
-  // and we keep it for backwards compatibility.  If we don't find that file
-  // then look for the image with the node ID in the name. If we don't find that then
-  // no image tag is generated
+  // First look for an image with the genus/species name.  This is old-style tripal
+  // and we keep it for backwards compatibility.  
   $base_path = realpath('.');
   $image_dir = tripal_get_files_dir('tripal_organism') . "/images";
   $image_name =  $organism->genus . "_" . $organism->species . ".jpg";
@@ -198,19 +209,19 @@ function tripal_get_organism_image($organism, $nid = NULL, $type = 'url') {
 
   if (file_exists($image_path)) {
     $url = file_create_url("$image_dir/$image_name");
+    return $url;
   }
-  else {
-     $image_name = $nid . ".jpg";
-     $image_path = "$base_path/$image_dir/$image_name";
-     if (file_exists($image_path)) {
-       $url = file_create_url("$image_dir/$image_name");
-     }
-  }
-  if ($type == "path") {
-    return $image_path;
-  }
-  else {
+  
+  // If we don't find the file using the genus ans species then look for the 
+  // image with the node ID in the name. This method was used for Tripal 1.1
+  // and 2.x-alpha version.
+  $image_name = $nid . ".jpg";
+  $image_path = "$base_path/$image_dir/$image_name";
+  if (file_exists($image_path)) {
+    $url = file_create_url("$image_dir/$image_name");
     return $url;
   }
+
+  return NULL;
 }
 

+ 38 - 40
tripal_organism/includes/tripal_organism.chado_node.inc

@@ -171,11 +171,13 @@ function chado_organism_form($node, $form_state) {
     '#title' => t('Description'),
     '#default_value' => $description,
   );
+  
   $form['organism_image']= array(
-    '#type' => 'file',
+    '#type' => 'managed_file',
     '#title' => t('Organism Image'),
-    '#description' => 'Add an image for this organism',
+    '#description' => t('Add an image to display for this organism.'),
     '#progress_indicator' => 'bar',
+    '#upload_location' => 'public://tripal/tripal_organism/images/',
   );
 
   // PROPERTIES FORM
@@ -350,6 +352,7 @@ function chado_organism_insert($node) {
  * @ingroup tripal_organism
  */
 function chado_organism_update($node) {
+
   // remove any white space around values
   $node->genus        = trim($node->genus);
   $node->species      = trim($node->species);
@@ -374,10 +377,8 @@ function chado_organism_update($node) {
     'comment' => $node->description
   );
   $org_status = chado_update_record('organism', $match, $values);
-
-  // add the image
   chado_organism_add_image($node);
-
+  
   // * Properties Form *
   $details = array(
     'property_table' => 'organismprop',   // the name of the prop table
@@ -396,6 +397,38 @@ function chado_organism_update($node) {
   chado_update_node_form_dbxrefs($node, $details);
 }
 
+/**
+ * Adds the image to the organism node and cleans up any old images.
+ * 
+ * @param $node
+ *   The node object.
+ */
+function chado_organism_add_image($node) {
+  
+  // If there is already an organism image, then remove it it if 
+  // no other modules are using it
+  $fid = db_select('file_usage', 'fu')
+    ->fields('fu', array('fid'))
+    ->condition('module', 'tripal_organism')
+    ->condition('type', 'organism_image')
+    ->condition('id', $node->nid)
+    ->execute()
+    ->fetchField();
+  if ($fid) {
+    $file = file_load($fid);
+    file_usage_delete($file, 'tripal_organism', 'organism_image', $node->nid);
+    file_delete($file);
+  }
+
+  // Save the uploaded file
+  $file = file_load($node->organism_image);
+  if ($file) {
+    $file->status = FILE_STATUS_PERMANENT;
+    file_save($file);
+    file_usage_add($file, 'tripal_organism', 'organism_image', $node->nid);
+  }
+}
+
 /**
  * Implements hook_delete().
  *
@@ -438,41 +471,6 @@ function chado_organism_delete($node) {
     drupal_set_message(t("Warning: other data depends on this organism. The organism page was removed from this site but the organism was removed from Chado."), 'warning');
   }
 }
-
-/**
- * Add an image to an organims node
- *
- * @param $node
- *   The node to add an image to
- *
- * The file is specified in the $_FILES array created by Drupal
- *
- * @ingroup tripal_organism
- */
-function chado_organism_add_image($node) {
-  // check to see if a file was uploaded. If so then copy it to the images
-  // directory for display with the organism
-  if (isset($_FILES['files']) &&
-  $_FILES['files']['name']['organism_image'] &&
-  is_uploaded_file($_FILES['files']['tmp_name']['organism_image'])) {
-
-    // make sure the destination directory exists
-    $dest = tripal_get_files_dir() . "/tripal_organism/images";
-    file_prepare_directory($dest, FILE_CREATE_DIRECTORY);
-
-    // now move the file
-    $validators = array('file_validate_is_image' => array());
-    $destination = "public://tripal/tripal_organism/images/";
-    $file = file_save_upload('organism_image', $validators, $destination);
-    if (!$file) {
-      drupal_set_message(t("Organism image was not uploaded."));
-    }
-    else {
-      file_move($file, $destination . "/" . $node->nid . ".jpg", FILE_EXISTS_REPLACE);
-    }
-  }
-}
-
 /**
  *  Implements hook_load().
  *

+ 1 - 1
tripal_organism/theme/templates/tripal_organism_base.tpl.php

@@ -7,7 +7,7 @@ $organism = chado_expand_var($organism,'field','organism.comment'); ?>
 
 // generate the image tag
 $image = '';
-$image_url = tripal_get_organism_image($organism, $node->nid); 
+$image_url = tripal_get_organism_image_url($organism); 
 if ($image_url) {
   $image = "<img class=\"tripal-organism-img\" src=\"$image_url\">";
 }

+ 5 - 5
tripal_pub/api/tripal_pub.api.inc

@@ -427,7 +427,7 @@ function tripal_publication_exists($pub_details) {
 
   // first try to find the publication using the accession number if that key exists in the details array
   if (array_key_exists('Publication Dbxref', $pub_details)) {
-    $pub = chado_get_publication(array('dbxref' => $pub_details['Publication Dbxref']));
+    $pub = tripal_get_publication(array('dbxref' => $pub_details['Publication Dbxref']));
     if($pub) {
       return array($pub->pub_id);
     }
@@ -435,7 +435,7 @@ function tripal_publication_exists($pub_details) {
 
   // make sure the citation is unique
   if (array_key_exists('Citation', $pub_details)) {
-    $pub = chado_get_publication(array('uniquename' => $pub_details['Citation']));
+    $pub = tripal_get_publication(array('uniquename' => $pub_details['Citation']));
     if($pub) {
       return array($pub->pub_id);
     }
@@ -460,13 +460,13 @@ function tripal_publication_exists($pub_details) {
   }
   else {
     tripal_report_error('tripal_pub', TRIPAL_ERROR, 
-      "chado_does_pub_exist(): The Publication Type is a " .
+      "tripal_publication_exists(): The Publication Type is a " .
       "required property but is missing", array());
     return array();
   }
   if (!$pub_type) {
     tripal_report_error('tripal_pub', TRIPAL_ERROR, 
-     "chado_does_pub_exist(): Cannot find publication type: '%type'",
+     "tripal_publication_exists(): Cannot find publication type: '%type'",
       array('%type' => $pub_details['Publication Type'][0]));
     return array();
   }
@@ -864,4 +864,4 @@ function tripal_import_pub_by_dbxref($pub_dbxref, $do_contact = FALSE, $do_updat
   }
 
   print "Done.\n";
-}
+}

+ 1 - 1
tripal_pub/includes/tripal_pub.chado_node.inc

@@ -441,7 +441,7 @@ function chado_pub_validate($node, $form, &$form_state) {
 function chado_pub_validate_check_uniquename($uniquename, $pub_id = NULL) {
 
   // check to see if a pub exists with this uniquename
-  $pub = chado_get_publication(array('uniquename' => $uniquename));
+  $pub = tripal_get_publication(array('uniquename' => $uniquename));
   if ($pub) {
     // if a $pub_id is provided to the function then this is an update
     // if the pub_id's don't match then a different pub with the same

+ 16 - 3
tripal_pub/includes/tripal_pub.pub_importers.inc

@@ -859,7 +859,20 @@ function tripal_pub_add_publications($pubs, $do_contact, $update = FALSE) {
     if ($pub_id){
       // add the publication cross reference (e.g. to PubMed)
       if ($pub_id and $pub['Publication Dbxref']) {
-        $pub_dbxref = tripal_pub_add_pub_dbxref($pub_id, $pub['Publication Dbxref']);
+        $dbxref = array();
+        if (preg_match('/^(.*?):(.*?)$/', trim($pub['Publication Dbxref']), $matches)) {
+          $dbxref['db_name']   = $matches[1];
+          $dbxref['accession'] = $matches[2];
+        }
+        else {
+          tripal_report_error(
+            'tripal_pub',
+            TRIPAL_ERROR,
+            'Unable to extract the dbxref to be associated with the publication (pub ID=@pub_id) from @dbxref. This reference should be [database-name]:[accession]',
+            array('@pub_id' => $pub_id, '@dbxref' => $pub['Publication Dbxref'])
+          );
+        }
+        $pub_dbxref = tripal_associate_dbxref('pub', $pub_id, $dbxref);
       }
       $pub['pub_id'] = $pub_id;
     }
@@ -923,7 +936,7 @@ function tripal_pub_add_publication($pub_details, &$action, $do_contact = FALSE,
 
   // before proceeding check to see if the publication already exists. If there is only one match
   // and the $update_if_exists is NOT set then return FALSE
-  $pub_ids = chado_does_pub_exist($pub_details);
+  $pub_ids = tripal_publication_exists($pub_details);
 
   if(count($pub_ids) == 1 and !$update_if_exists) {
     tripal_report_error('tripal_pub', TRIPAL_NOTICE,
@@ -1399,4 +1412,4 @@ function tripal_pub_get_publication_array($pub_id, $skip_existing = TRUE) {
     }
   }
   return $pub_array;
-}
+}

+ 1 - 1
tripal_pub/includes/tripal_pub.pub_search.inc

@@ -42,7 +42,7 @@ function tripal_pub_search_page() {
     $page = isset($_GET['page']) ? $_GET['page'] : '0';
     $offset = $page * $limit;
     $total_records = 0;
-    $pubs = pub_search($search_array, $offset, $limit, $total_records);
+    $pubs = tripal_search_publications($search_array, $offset, $limit, $total_records);
     pager_default_initialize($total_records, $limit, 0);
     
     // iterate through the results and construct the table displaying the publications

+ 1 - 1
tripal_pub/tripal_pub.drush.inc

@@ -99,5 +99,5 @@ function drush_tripal_pub_tripal_pubs_update() {
   $dbxref = drush_get_option('dbxref');
   $db = drush_get_option('db');
 
-  chado_reimport_publications($create_contacts, $dbxref, $db);
+  tripal_reimport_publications($create_contacts, $dbxref, $db);
 }