Browse Source

Finished (for now) the example module. Also fixed bugs in custom tables

Stephen Ficklin 11 years ago
parent
commit
425130a50a

+ 4 - 0
tripal_core/api/tripal_core.chado_variables.api.inc

@@ -700,6 +700,10 @@ function chado_expand_var($object, $type, $to_expand, $table_options = array())
         return $object;
       }
       $foreign_table_desc = chado_get_schema($foreign_table);
+      
+      // TODO: if we don't get a foreign_table (which could happen of a custom table
+      // is not correctly defined or the table name is mispelled then we should return
+      // gracefully.
 
       // BASE CASE: If it's connected to the base table via a FK constraint
       // then we have all the information needed to expand it now

+ 6 - 1
tripal_core/api/tripal_core.custom_tables.api.inc

@@ -139,7 +139,12 @@ function chado_create_custom_table($table, $schema, $skip_creation = 1) {
     // drop the table we'll recreate it with the new schema
     try {
       chado_query('DROP TABLE {' . $table . '}');
-      db_create_table('chado.' . $table, $schema);
+      // remove any 'referring_tables' from the array as the db_create_table doesn't use that
+      $new_schema = $schema;
+      if (array_key_exists('referring_tables', $new_schema)) {
+        unset($new_schema['referring_tables']);
+      }
+      db_create_table('chado.' . $table, $new_schema);
       $recreated = 1;
     }
     catch (Exception $e) {

+ 4 - 5
tripal_core/includes/custom_tables.inc

@@ -184,12 +184,11 @@ function tripal_custom_tables_form($form, &$form_state = NULL, $table_id = NULL)
     // set the default values.  If there is a value set in the
     // form_state then let's use that, otherwise, we'll pull
     // the values from the database
-    $default_schema = $form_state['values']['schema'];
-    $default_force_drop = $form_state['values']['force_drop'];
-
-    if (!$default_table_name) {
-      $default_table = $custom_table->table_name;
+    if (array_key_exists('values', $form_state)) {
+      $default_schema     = $form_state['values']['schema'];
+      $default_force_drop = $form_state['values']['force_drop'];
     }
+
     if (!$default_schema) {
       $default_schema = var_export(unserialize($custom_table->schema), 1);
       $default_schema = preg_replace('/=>\s+\n\s+array/', '=> array', $default_schema);

+ 2 - 2
tripal_core/tripal_core.module

@@ -356,7 +356,7 @@ function tripal_core_menu() {
   $items['admin/tripal/schema/custom_tables/edit/%'] = array(
     'title' => 'Edit Custom Table',
     'page callback' => 'drupal_get_form',
-    'page arguments' => array('tripal_custom_tables_form', 4),
+    'page arguments' => array('tripal_custom_tables_form', 5),
     'access arguments' => array('administer tripal'),
     'type' => MENU_CALLBACK,
   );
@@ -560,7 +560,7 @@ function tripal_core_node_view_alter(&$build) {
   $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 block of content so that we can associate
   // a table of contents and add administrator and curator messages

+ 6 - 0
tripal_example/includes/tripal_example.admin.inc

@@ -17,6 +17,11 @@ function tripal_example_admin_examples_listing() {
   $breadcrumb[] = l('Examples', 'admin/tripal/chado/tripal_example');
   drupal_set_breadcrumb($breadcrumb);
 
+  
+  // EXPLANATION:  Typically for all Tripal modules the home administrative
+  // page for the module contains a search form to help the adminstrator
+  // locate records.  The following example code adds a default View to
+  // the page
   /*
   // Add the view
   $view = views_embed_view('tripal_example_admin_examples','default');
@@ -33,6 +38,7 @@ function tripal_example_admin_examples_listing() {
     $output .= '</ul>';
   }
   */
+  $output = 'Typically a search view goes here';  
 
   return $output;
 }

+ 11 - 10
tripal_example/includes/tripal_example.chado_node.inc

@@ -704,7 +704,7 @@ function tripal_example_node_update($node) {
  */
 function tripal_example_node_view($node, $view_mode, $langcode) {
   
-  // EXPLANATION: This function defines the content "blocks" that appear 
+  // EXPLANATION: This function defines the content "blocks" that appear
   // when thhe node is displayed. It is node type agnostic so we can add
   // content to any node type.  So, we use this function to add the content
   // from all of our theme templates onto our new node type. We will also
@@ -715,7 +715,7 @@ function tripal_example_node_view($node, $view_mode, $langcode) {
       // there are different ways a node can be viewed. Primarily Tripal
       // supports full page view and teaser view.
       if ($view_mode == 'full') {
-        
+
         // There is always a base template.  This is the template that
         // is first shown when the example node type is first displayed. 
         // if you are using the default Tripal node template, then you should
@@ -741,12 +741,12 @@ function tripal_example_node_view($node, $view_mode, $langcode) {
           '#tripal_toc_title' => 'Properties',
         );
         $node->content['tripal_example_references'] = array(
-          '#markup' => theme('tripal_feature_references', array('node' => $node)),
+          '#markup' => theme('tripal_example_references', array('node' => $node)),
           '#tripal_toc_id'    => 'references',
           '#tripal_toc_title' => 'Cross References',
         );
         $node->content['tripal_example_relationships'] = array(
-          '#markup' => theme('tripal_feature_relationships', array('node' => $node)),
+          '#markup' => theme('tripal_example_relationships', array('node' => $node)),
           '#tripal_toc_id'    => 'relationships',
           '#tripal_toc_title' => 'Relationships',
         );
@@ -762,12 +762,13 @@ function tripal_example_node_view($node, $view_mode, $langcode) {
     // you can add custom content to any node type by adding
     // content to the node in the same way as above.
     case 'chado_organism':
-      break;
-    case 'chado_library':
-      break;
-    case 'chado_stock':
-      break;
-    case 'chado_analysis':
+      if ($view_mode == 'full') {
+        $node->content['tripal_organism_examples'] = array(
+          '#markup' => theme('tripal_organism_examples', array('node' => $node)),
+          '#tripal_toc_id'    => 'examples',
+          '#tripal_toc_title' => 'Examples',
+        );
+      }
       break;
     // ... etc
   }

+ 84 - 0
tripal_example/theme/templates/tripal_example_base.tpl.php

@@ -1 +1,85 @@
 <?php
+$example  = $variables['node']->example;  ?>
+
+<div class="tripal_example-data-block-desc tripal-data-block-desc"></div> <?php
+ 
+// the $headers array is an array of fields to use as the colum headers. 
+// additional documentation can be found here 
+// https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
+// This table for the analysis has a vertical header (down the first column)
+// so we do not provide headers here, but specify them in the $rows array below.
+$headers = array();
+
+// the $rows array contains an array of rows where each row is an array
+// of values for each column of the table in that row.  Additional documentation
+// can be found here:
+// https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7 
+$rows = array();
+
+// Unique Name row
+$rows[] = array(
+  array(
+    'data' => 'Unique Name',
+    'header' => TRUE
+  ),
+  $example->uniquename
+);
+// Type row
+$rows[] = array(
+  array(
+    'data' => 'Type',
+    'header' => TRUE
+  ),
+  $example->type_id->name
+);
+// Organism row
+$organism = $example->organism_id->genus ." " . $example->organism_id->species ." (" . $example->organism_id->common_name .")";
+if (property_exists($example->organism_id, 'nid')) {
+  $organism = l("<i>" . $example->organism_id->genus . " " . $example->organism_id->species . "</i> (" . $example->organism_id->common_name .")", "node/".$example->organism_id->nid, array('html' => TRUE));
+} 
+$rows[] = array(
+  array(
+    'data' => 'Organism',
+    'header' => TRUE,
+  ),
+  $organism
+);
+
+// allow site admins to see the example ID
+if (user_access('administer tripal')) { 
+  // Feature ID
+  $rows[] = array(
+    array(
+      'data' => 'Example ID',
+      'header' => TRUE,
+      'class' => 'tripal-site-admin-only-table-row',
+    ),
+    array(
+      'data' => $example->example_id,
+      'class' => 'tripal-site-admin-only-table-row',
+    ),
+  );
+}
+
+
+// the $table array contains the headers and rows array as well as other
+// options for controlling the display of the table.  Additional
+// documentation can be found here:
+// https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
+$table = array(
+  'header' => $headers,
+  'rows' => $rows,
+  'attributes' => array(
+    'id' => 'tripal_example-table-base',
+    'class' => 'tripal-data-table'
+  ),
+  'sticky' => FALSE,
+  'caption' => '',
+  'colgroups' => array(),
+  'empty' => '',
+);
+
+// once we have our table array structure defined, we call Drupal's theme_table()
+// function to generate the table.
+print theme_table($table); ?>
+<div style="text-align: justify"><?php print $example->description ?></div>

+ 6 - 22
tripal_example/theme/templates/tripal_example_properties.tpl.php

@@ -1,33 +1,17 @@
 <?php
-$pub = $node->pub;
+$example = $node->example;
 
-// expand the pub to include the properties.
+// expand the example to include the properties.
 $options = array(
   'return_array' => 1,
   'order_by' => array('rank' => 'ASC'),
 );
-$pub = tripal_core_expand_chado_vars($pub,'table', 'pubprop', $options);
-$pubprops = $pub->pubprop;
+$example = tripal_core_expand_chado_vars($example,'table', 'exampleprop', $options);
+$exampleprops = $example->exampleprop;
 $properties = array();
-if (is_array($pubprops)) {
-  foreach ($pubprops as $property) {
-    // skip the following properties as those are already on other templates
-    if ($property->type_id->name == 'Abstract' or
-        $property->type_id->name == 'Citation' or
-        $property->type_id->name == 'Publication Dbxref' or
-        $property->type_id->name == 'Authors' or
-        $property->type_id->name == 'Structured Abstract Part')  {
-      continue;
-    }
-    $property = tripal_core_expand_chado_vars($property,'field','pubprop.value');
-    $properties[] = $property;
-  }
-}
-// we'll keep track of the keywords so we can lump them into a single row
-$keywords = array(); 
 
 if (count($properties)) { ?>
-  <div class="tripal_pub-data-block-desc tripal-data-block-desc">Additional details for this publication include:</div> <?php 
+  <div class="tripal_example-data-block-desc tripal-data-block-desc">Additional details for this example include:</div> <?php 
 
   // the $headers array is an array of fields to use as the colum headers.
   // additional documentation can be found here
@@ -70,7 +54,7 @@ if (count($properties)) { ?>
     'header' => $headers,
     'rows' => $rows,
     'attributes' => array(
-      'id' => 'tripal_pub-table-properties',
+      'id' => 'tripal_example-table-properties',
       'class' => 'tripal-data-table'
     ),
     'sticky' => FALSE,

+ 9 - 10
tripal_example/theme/templates/tripal_example_references.tpl.php

@@ -1,21 +1,20 @@
 <?php
-$pub = $variables['node']->pub;
+$example = $variables['node']->example;
 
-
-// expand the pub object to include the records from the pub_dbxref table
+// expand the example object to include the records from the example_dbxref table
 $options = array('return_array' => 1);
-$pub = tripal_core_expand_chado_vars($pub, 'table', 'pub_dbxref', $options);
-$pub_dbxrefs = $pub->pub_dbxref;
+$example = chado_expand_var($example, 'table', 'example_dbxref', $options);
+$example_dbxrefs = $example->example_dbxref;
 
 $references = array();
-if (count($pub_dbxrefs) > 0 ) {
-  foreach ($pub_dbxrefs as $pub_dbxref) {    
-    $references[] = $pub_dbxref->dbxref_id;
+if (count($example_dbxrefs) > 0 ) {
+  foreach ($example_dbxrefs as $example_dbxref) {    
+    $references[] = $example_dbxref->dbxref_id;
   }
 }
 
 if(count($references) > 0){ ?>
-  <div class="tripal_pub-data-block-desc tripal-data-block-desc">This publication is also available in the following databases:</div><?php 
+  <div class="tripal_example-data-block-desc tripal-data-block-desc">This example is also available in the following databases:</div><?php 
   
   // the $headers array is an array of fields to use as the colum headers.
   // additional documentation can be found here
@@ -54,7 +53,7 @@ if(count($references) > 0){ ?>
     'header' => $headers,
     'rows' => $rows,
     'attributes' => array(
-      'id' => 'tripal_pub-table-references',
+      'id' => 'tripal_example-table-references',
       'class' => 'tripal-data-table'
     ),
     'sticky' => FALSE,

+ 20 - 20
tripal_example/theme/templates/tripal_example_relationships.tpl.php

@@ -1,34 +1,34 @@
 <?php
 /* Typically in a Tripal template, the data needed is retrieved using a call to
  * tripal_core_expand_chado_vars function.  For example, to retrieve all 
- * of the pub relationships for this node, the following function call would be made:
+ * of the example relationships for this node, the following function call would be made:
  * 
- *   $pub = tripal_core_expand_chado_vars($pub,'table','pub_relationship');
+ *   $example = tripal_core_expand_chado_vars($example,'table','example_relationship');
  * 
  * However, this function call can be extremely slow when there are numerous relationships.
  * This is because the tripal_core_expand_chado_vars function is recursive and expands 
  * all data following the foreign key relationships tree.  Therefore, to speed retrieval
  * of data, a special variable is provided to this template:
  * 
- *   $pub->all_relationships;
+ *   $example->all_relationships;
  *   
  * This variable is an array with two sub arrays with the keys 'object' and 'subject'.  The array with
- * key 'object' contains relationships where the pub is the object, and the array with
- * the key 'subject' contains relationships where the pub is the subject
+ * key 'object' contains relationships where the example is the object, and the array with
+ * the key 'subject' contains relationships where the example is the subject
  */
-$pub = $variables['node']->pub;
+$example = $variables['node']->example;
 
-$all_relationships = $pub->all_relationships;
+$all_relationships = $example->all_relationships;
 $object_rels = $all_relationships['object'];
 $subject_rels = $all_relationships['subject'];
 
 if (count($object_rels) > 0 or count($subject_rels) > 0) { ?>
-  <div class="tripal_pub-data-block-desc tripal-data-block-desc"></div> <?php
+  <div class="tripal_example-data-block-desc tripal-data-block-desc"></div> <?php
   
   // first add in the subject relationships.  
   foreach ($subject_rels as $rel_type => $rels){
     foreach ($rels as $obj_type => $objects){ ?>
-      <p>This <?php print $pub->type_id->name;?> is <?php print $rel_type ?> the following <b><?php print $obj_type ?></b> pub(s): <?php
+      <p>This <?php print $example->type_id->name;?> is <?php print $rel_type ?> the following <b><?php print $obj_type ?></b> example(s): <?php
        
       // the $headers array is an array of fields to use as the colum headers.
       // additional documentation can be found here
@@ -42,7 +42,7 @@ if (count($object_rels) > 0 or count($subject_rels) > 0) { ?>
       $rows = array();
       
       foreach ($objects as $object){
-        // link the pub to it's node
+        // link the example to it's node
         $title = $object->record->object_id->title;
         if (property_exists($object->record, 'nid')) {
           $title = l($title, "node/" . $object->record->nid, array('attributes' => array('target' => "_blank")));
@@ -50,13 +50,13 @@ if (count($object_rels) > 0 or count($subject_rels) > 0) { ?>
         
         // get the citation
         $values = array(
-          'pub_id' => $object->record->object_id->pub_id,
+          'example_id' => $object->record->object_id->example_id,
           'type_id' => array(
             'name' => 'Citation',
           ),
         );
-        $citation = tripal_core_generate_chado_var('pubprop', $values);
-        $citation = tripal_core_expand_chado_vars($citation, 'field', 'pubprop.value');
+        $citation = tripal_core_generate_chado_var('exampleprop', $values);
+        $citation = tripal_core_expand_chado_vars($citation, 'field', 'exampleprop.value');
         
         $rows[] = array(
           $title . '<br>' . htmlspecialchars($citation->value),
@@ -70,7 +70,7 @@ if (count($object_rels) > 0 or count($subject_rels) > 0) { ?>
          'header' => $headers,
          'rows' => $rows,
          'attributes' => array(
-           'id' => 'tripal_pub-table-relationship-object',
+           'id' => 'tripal_example-table-relationship-object',
            'class' => 'tripal-data-table'
          ),
          'sticky' => FALSE,
@@ -90,7 +90,7 @@ if (count($object_rels) > 0 or count($subject_rels) > 0) { ?>
   // second add in the object relationships.  
   foreach ($object_rels as $rel_type => $rels){
     foreach ($rels as $subject_type => $subjects){?>
-      <p>The following <b><?php print $subjects[0]->record->subject_id->type_id->name ?></b> pub(s) are <?php print $rel_type ?> this <?php print $pub->type_id->name;?>: <?php 
+      <p>The following <b><?php print $subjects[0]->record->subject_id->type_id->name ?></b> example(s) are <?php print $rel_type ?> this <?php print $example->type_id->name;?>: <?php 
       // the $headers array is an array of fields to use as the colum headers.
       // additional documentation can be found here
       // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
@@ -103,7 +103,7 @@ if (count($object_rels) > 0 or count($subject_rels) > 0) { ?>
       $rows = array();
       
       foreach ($subjects as $subject){
-        // link the pub to it's node
+        // link the example to it's node
         $title = $subject->record->subject_id->title;
         if (property_exists($subject->record, 'nid')) {
           $title = l($title, "node/" . $subject->record->nid, array('attributes' => array('target' => "_blank")));
@@ -111,13 +111,13 @@ if (count($object_rels) > 0 or count($subject_rels) > 0) { ?>
         
         // get the citation
         $values = array(
-          'pub_id' => $subject->record->subject_id->pub_id,
+          'example_id' => $subject->record->subject_id->example_id,
           'type_id' => array(
             'name' => 'Citation',
           ),
         );
-        $citation = tripal_core_generate_chado_var('pubprop', $values);
-        $citation = tripal_core_expand_chado_vars($citation, 'field', 'pubprop.value');
+        $citation = tripal_core_generate_chado_var('exampleprop', $values);
+        $citation = tripal_core_expand_chado_vars($citation, 'field', 'exampleprop.value');
         
         $rows[] = array(
           $title . '<br>' . htmlspecialchars($citation->value),
@@ -131,7 +131,7 @@ if (count($object_rels) > 0 or count($subject_rels) > 0) { ?>
          'header' => $headers,
          'rows' => $rows,
          'attributes' => array(
-           'id' => 'tripal_pub-table-relationship-subject',
+           'id' => 'tripal_example-table-relationship-subject',
            'class' => 'tripal-data-table'
          ),
          'sticky' => FALSE,

+ 9 - 0
tripal_example/tripal_example.install

@@ -353,6 +353,15 @@ function tripal_example_add_example_table() {
         ),
       ),
     ),
+    // EXPLANATION: the 'referring_tables' array is the list of tables
+    // that have a foreign key relationships with this table. This 
+    // information is required for the Tripal API to be able to expand
+    // tables in templates.
+    'referring_tables' => array(
+      0 => 'example_relationship',
+      1 => 'exampleprop',
+      2 => 'example_dbxref',
+    ),
   );
   chado_create_custom_table('example', $schema, TRUE);
 }

+ 9 - 26
tripal_example/tripal_example.module

@@ -80,14 +80,14 @@ function tripal_example_menu() {
   //    
   
   // EXPLANATION:  all extension modules should have an administrative menu item
-  // with the path set to 'admin/tripal/extesion/[module name]'.  This will
+  // with the path set to 'admin/tripal/extension/[module name]'.  This will
   // place the menu item in the 'Tripal' -> 'Extesion Modules' page.  Because this
   // is an administrative menu item we must be sure to set the 'access arguments'
   // to be 'administer tripal example' which is a permission type we created
   // in the tripal_example_permissions() function above.
   $items['admin/tripal/extension/tripal_example'] = array(
     'title' => 'Examples',
-    'description' => 'Any example.',
+    'description' => 'Example module for help with development of new extension modules.',
     'page callback' => 'tripal_example_admin_examples_listing',
     'access arguments' => array('administer tripal example'),
     'type' => MENU_NORMAL_ITEM,
@@ -101,7 +101,7 @@ function tripal_example_menu() {
   // function and the 'page_arguments' indicate the name of the template file
   // Thus, all help documentation should be provided in the 
   // [module name]/theme/tripal_example_help.tpl.php file. 
-  $items['admin/tripal/chado/tripal_example/help'] = array(
+  $items['admin/tripal/extension/tripal_example/help'] = array(
     'title' => 'Help',
     'description' => 'Basic Description of Tripal Library Module Functionality',
     'page callback' => 'theme',
@@ -120,7 +120,7 @@ function tripal_example_menu() {
   // drupal_get_form() function and the 'page argument' indicates the form
   // to call is named 'tripal_eample_admin'.  The function that describes
   // to form is in the includes/tripal_example.admin.inc file.
-  $items['admin/tripal/chado/tripal_example/configuration'] = array(
+  $items['admin/tripal/extension/tripal_example/configuration'] = array(
     'title' => 'Settings',
     'description' => 'Configure the Tripal Library module',
     'page callback' => 'drupal_get_form',
@@ -135,7 +135,7 @@ function tripal_example_menu() {
   // to quickly provide syncing functionality.  See the API documentation here
   // for more information on how that is setup:
   // http://api.tripal.info/api/tripal/tripal_core%21api%21tripal_core.chado_nodes.api.inc/function/chado_node_sync_form/2.x
-  $items['admin/tripal/chado/tripal_example/sync'] = array(
+  $items['admin/tripal/extension/tripal_example/sync'] = array(
     'title' => ' Sync',
     'description' => 'Create pages on this site for examples stored in Chado',
     'page callback' => 'drupal_get_form',
@@ -222,7 +222,7 @@ function tripal_example_theme($existing, $type, $theme, $path) {
     'tripal_example_help' => array(
       'template' => 'tripal_example_help',
       'variables' =>  array(NULL),
-      'path' => "$path/theme",
+      'path' => "$path/theme/templates",
     ),
     // the teaser template.
     'tripal_example_teaser' => array(
@@ -242,24 +242,14 @@ function tripal_example_theme($existing, $type, $theme, $path) {
       'template' => 'tripal_example_properties',
       'path' => "$path/theme/templates",
     ),
-    'tripal_example_publications' => array(
-      'variables' => array('node' => NULL),
-      'template' => 'tripal_example_publications',
-      'path' => "$path/theme/templates",
-    ),
     'tripal_example_references' => array(
       'variables' => array('node' => NULL),
       'template' => 'tripal_example_references',
       'path' => "$path/theme/templates",
     ),
-    'tripal_example_synonyms' => array(
-      'variables' => array('node' => NULL),
-      'template' => 'tripal_example_synonyms',
-      'path' => "$path/theme/templates",
-    ),
-    'tripal_example_terms' => array(
+    'tripal_example_relationships' => array(
       'variables' => array('node' => NULL),
-      'template' => 'tripal_example_terms',
+      'template' => 'tripal_example_relationships',
       'path' => "$path/theme/templates",
     ),
 
@@ -277,15 +267,8 @@ function tripal_example_theme($existing, $type, $theme, $path) {
       'template' => 'tripal_organism_examples',
       'path' => "$path/theme/templates",
     ),
-
-    // tripal_feature templates
-    'tripal_feature_examples' => array(
-      'variables' => array('node' => NULL),
-      'template' => 'tripal_feature_examples',
-      'path' => "$path/theme/templates",
-    ),
-
   );
+  
   return $items;
 }