瀏覽代碼

Example Module: Added code to implement the properties, dbxref and relationship apis

Lacey Sanderson 11 年之前
父節點
當前提交
cefade377f
共有 1 個文件被更改,包括 146 次插入40 次删除
  1. 146 40
      tripal_example/includes/tripal_example.chado_node.inc

+ 146 - 40
tripal_example/includes/tripal_example.chado_node.inc

@@ -2,12 +2,12 @@
 
 /**
  * @file
- * This file should contain all Drupal hooks for interacting with nodes. 
+ * This file should contain all Drupal hooks for interacting with nodes.
  *
  */
 
 /**
- *  Implementation of hook_node_info().  This hook provides information to drupal 
+ *  Implementation of hook_node_info().  This hook provides information to drupal
  *  about any node types that are being created by this module. If your module
  *  does not create any node types then this function is not required.
  *
@@ -29,12 +29,12 @@ function tripal_example_node_info() {
   // coordinating which node belongs to which record in Chado. Therefore,
   // Tripal maintains tables in the Drupal schema that maps Drupal nodes
   // to recrords in Chado.  Syncing is the process of creating Drupal nodes
-  // and linking them to the appropriate record. 
+  // and linking them to the appropriate record.
   //  'chado_node_api' => array(
   //    'base_table' => 'example',       // the base table name (e.g. feature, library, contact)
   //    'hook_prefix' => 'chado_example',// the node type hook prefix
   //    'record_type_title' => array(
-  //      'singular' => t('Library'),    // how to refer to the record 
+  //      'singular' => t('Library'),    // how to refer to the record
   //      'plural' => t('Libraries')     // how to refer to the record in plurals
   //    ),
   //    'sync_filters' => array(
@@ -43,7 +43,7 @@ function tripal_example_node_info() {
   //    ),
   //  )
   //);
-  
+
   return $nodes;
 }
 
@@ -54,13 +54,13 @@ function tripal_example_node_info() {
  * are set in the chado_example_permissions() hook in the
  * tripal_example.module file.  This hook is not needed
  * if no node types were defined in the hook_node_info() hook.
- * 
+ *
  * @return
  * This function should return null if it does not specificially
  * deny access. This allows for other mechanisms to to deny
  * or reject access. If the return value is TRUE then access
  * is granted regardless of any other rules that might be implemented
- * by other modules.  
+ * by other modules.
  */
 function chado_example_node_access($node, $op, $account) {
   if ($op == 'create') {
@@ -91,7 +91,7 @@ function chado_example_node_access($node, $op, $account) {
  * Implementation of hook_form() when a node type of chado_example is defined.
  * If a node type is not defined then this function is not needed. The table
  * name in chado for this example module is named 'example' so there is a
- * corresponding example_id in that table (similar to feature.feature_id, 
+ * corresponding example_id in that table (similar to feature.feature_id,
  * contact.contact_id, etc).
  *
  * @ingroup tripal_example
@@ -136,9 +136,48 @@ function chado_example_form($node, &$form_state) {
     // $uniquename   = $form_state['input']['uniquename'];
 
   }
-  
+
   // add form elements here.
-  
+
+  // PROPERTIES FORM
+  //---------------------------------------------
+  // If there is a exampleprop table and you want to allow users to add/remove entries
+  // from it through your node form then add this section to your own node form
+  $details = array(
+    'property_table' => 'exampleprop',      // the name of the prop table
+    'base_foreign_key' => 'example_id',     // the name of the key in your base chado table
+    'base_key_value' => $example_id,        // the value of example_id for this record
+    'cv_name' => 'example_property_types'   // the cv.name of the cv governing exampleprop.type_id
+  );
+  // Adds the form elements to your current form
+  chado_node_properties_form($form, $form_state, $details);
+
+  // ADDITIONAL DBXREFS FORM
+  //---------------------------------------------
+  // If there is a example_dbxref table and you want to allow users to add/remove entries
+  // from it through your node form then add this section to your own node form
+  $details = array(
+    'linking_table' => 'example_dbxref',  // the name of the _dbxref table
+    'base_foreign_key' => 'example_id',   // the name of the key in your base chado table
+    'base_key_value' => $example_id       // the value of example_id for this record
+  );
+  // Adds the form elements to your current form
+  chado_node_additional_dbxrefs_form($form, $form_state, $details);
+
+  // RELATIONSHIPS FORM
+  //---------------------------------------------
+  // If there is a example_relationship table and you want to allow users to add/remove entries
+  // from it through your node form then add this section to your own node form
+  $details = array(
+    'relationship_table' => 'example_relationship', // the name of the _relationship table
+    'base_table' => 'example',                      // the name of your chado base table
+    'base_foreign_key' => 'example_id',             // the name of the key in your base chado table
+    'base_key_value' => $example_id,                // the value of example_id for this record
+    'nodetype' => 'example',                        // the human-readable name of your node type
+    'cv_id' => 'example_relationship_types'         // the cv.name of the cv governing example_relationship.type_id
+  );
+  // Adds the form elements to your current form
+  chado_node_relationships_form($form, $form_state, $details);
 
   // return the form
   return $form;
@@ -192,11 +231,11 @@ function chado_example_validate($node, $form, &$form_state) {
  *  node is inserted into the database. We need it so that we can insert
  *  appropriate fields as provided by the user into the database. And so that
  *  we can link the new Drupal node to the data in Chado via the chado_example
- *  linking table. We can get to this function also during "syncing". 
- *  With syncing, however, the data already exists in Chado and we do not want 
+ *  linking table. We can get to this function also during "syncing".
+ *  With syncing, however, the data already exists in Chado and we do not want
  *  to try to re-add it. But we do need to add an entry to the chado_example table
- *  to link the Drupal node with the data in the 'example' table of Chado. 
- *  
+ *  to link the Drupal node with the data in the 'example' table of Chado.
+ *
  *  This function is not required if the hook_node_info() does not define
  *  any custom node types.
  *
@@ -210,7 +249,10 @@ function chado_example_insert($node) {
   // we can skip adding the example as it is already there, although
   // we do need to proceed with the rest of the insert
   if (!property_exists($node, 'example_id')) {
-    
+
+    // ADD TO CHADO
+
+    // * Example Table *
     // perform the insert using the tripal_core_chado_insert function();
     //$values = array(
     //  'uniquename' => $node->uniquename,
@@ -223,9 +265,43 @@ function chado_example_insert($node) {
     //  array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
     //  return;
     //}
-    
+
     // get the example_id for linking Drupal node with Chado data
     // $example_id = $example->example_id;
+
+    // Only add to other chado tables if the base record was inserted properly
+    if ($example_id > 0) {
+
+      // * Properties Form *
+      // If you implemented the properties form in chado_example_form then you need to
+      // handle inserting these properties into your chado prop table.
+      // $details = array(
+      //   'property_table' => 'exampleprop',   // the name of the prop table
+      //   'base_table' => 'example',           // the name of your chado base table
+      //   'foreignkey_name' => 'example_id',   // the name of the key in your base table
+      //   'foreignkey_value' => $example_id    // the value of the example_id key
+      // );
+      // chado_node_properties_form_update_properties($node, $details);
+
+      // * Additional DBxrefs Form *
+      // If you implemented the dbxrefs form in chado_example_form then you need to
+      // handle inserting these database references into your chado _dbxref table.
+      // $details = array(
+      //   'linking_table' => 'example_dbxref',   // the name of your _dbxref table
+      //   'foreignkey_name' => 'example_id',     // the name of the key in your base table
+      //   'foreignkey_value' => $example_id      // the value of the example_id key
+      // );
+      // chado_node_additional_dbxrefs_form_update_dbxrefs($node, $details);
+
+      // * Relationships Form *
+      // If you implemented the relationships form in chado_example_form then you need to
+      // handle inserting these relationships into your chado _relationship table.
+      // $details = array(
+      //   'relationship_table' => 'example_relationship',  // name of the _relationship table
+      //   'foreignkey_value' => $example_id                // value of the example_id key
+      // );
+      // chado_node_relationships_form_update_relationships($node, $details);
+    }
   }
   else {
     // the node has an example_id so get it for linking Drupal node with Chado data
@@ -248,7 +324,7 @@ function chado_example_insert($node) {
  * Implementation of hook_update().  This function runs after the
  * node has been inserted into the Drupal schema and allows us to
  * update the record in Chado.
- * 
+ *
  *  This function is not required if the hook_node_info() does not define
  *  any custom node types.
  *
@@ -274,15 +350,45 @@ function chado_example_update($node) {
   //    array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
   //}
 
+  // * Properties Form *
+  // If you implemented the properties form in chado_example_form then you need to
+  // handle updating these properties into your chado prop table.
+  // $details = array(
+  //   'property_table' => 'exampleprop',   // the name of the prop table
+  //   'base_table' => 'example',           // the name of your chado base table
+  //   'foreignkey_name' => 'example_id',   // the name of the key in your base table
+  //   'foreignkey_value' => $example_id    // the value of the example_id key
+  // );
+  // chado_node_properties_form_update_properties($node, $details);
+
+  // * Additional DBxrefs Form *
+  // If you implemented the dbxrefs form in chado_example_form then you need to
+  // handle updating these database references into your chado _dbxref table.
+  // $details = array(
+  //   'linking_table' => 'example_dbxref',   // the name of your _dbxref table
+  //   'foreignkey_name' => 'example_id',     // the name of the key in your base table
+  //   'foreignkey_value' => $example_id      // the value of the example_id key
+  // );
+  // chado_node_additional_dbxrefs_form_update_dbxrefs($node, $details);
+
+  // * Relationships Form *
+  // If you implemented the relationships form in chado_example_form then you need to
+  // handle updating these relationships into your chado _relationship table.
+  // $details = array(
+  //   'relationship_table' => 'example_relationship',  // name of the _relationship table
+  //   'foreignkey_value' => $example_id                // value of the example_id key
+  // );
+  // chado_node_relationships_form_update_relationships($node, $details);
+
 }
 /**
- * Implementation of hook_delete().  This function runs after the 
- * node has been deleted from the Drupal schema and allows us to 
+ * Implementation of hook_delete().  This function runs after the
+ * node has been deleted from the Drupal schema and allows us to
  * delete the corresponding recrod in Chado.
  *
  * This function is not required if the hook_node_info() does not define
  * any custom node types.
- *  
+ *
  * @ingroup tripal_example
  */
 function chado_example_delete($node) {
@@ -317,7 +423,7 @@ function chado_example_delete($node) {
  * for the feature table, the chado_feature_load() function adds in
  * a feature object which contains all of the fields and sub objects
  * for data in tables with foreign key relationships.
- *  
+ *
  * This function is not required if the hook_node_info() does not define
  * any custom node types.
  *
@@ -334,17 +440,17 @@ function chado_example_load($nodes) {
     // build the example variable by using the tripal_core_generate_chado_var() function
     //$values = array('example_id' => $example_id);
     //$example = tripal_core_generate_chado_var('example', $values);
-    
+
     // for fields in the table that are of type 'text' you may want to include those
     // by default, the tripal_core_generate_chado_var does not include text fields as
     // they may be very large and including a large text field can slow the page load.
-    // If you know a text field will never be large and it is important for the 
-    // other functions that will see the node to have access to a field you can 
+    // If you know a text field will never be large and it is important for the
+    // other functions that will see the node to have access to a field you can
     // include it here using the tripal_core_expand_chado_vars() function.  In most
     // cases it is probably best to let the end-user decide if text fields should
     // be included by using this function in the templates.
     //$example = tripal_core_expand_chado_vars($example, 'field', 'example.residues');
-        
+
     // add the new example object to this node.
     //$nodes[$nid]->example = $example;
   }
@@ -358,12 +464,12 @@ function chado_example_load($nodes) {
  * what genus and species is provided. This hook can allow the title to
  * be set using user supplied data before the node is saved.  In practice
  * any change can be made to any fields in the node object.
- * 
+ *
  * This function is not required. You probably won't need it if you
  * don't define a custom node type in the hook_node_info() function. But
  * it is node type agnostic, so you can use this function to change the
  * contents of any node regardless of it's type.
- * 
+ *
  * @ingroup tripal_example
  */
 function tripal_example_node_presave($node) {
@@ -377,7 +483,7 @@ function tripal_example_node_presave($node) {
 }
 
 /**
- * Implementation of hook node_insert().  This function is used 
+ * Implementation of hook node_insert().  This function is used
  * after any a node is inserted into the database.  It is different
  * from the hook_insert() function above in that it is called after
  * any node is saved, regardlesss of it's type. This function is useful
@@ -388,17 +494,17 @@ function tripal_example_node_presave($node) {
  * of a node cannot be set in the hook_insert() function. Therefore
  * the tripal_feature module uses this function to set the url path
  * of a newly inserted feature node.
- * 
+ *
  * This function is not required. You probably won't need it if you
  * don't define a custom node type in the hook_node_info() function. But
  * it is node type agnostic, so you can use this function to do any
  * activity after insert of a node.
- * 
+ *
  * @ingroup tripal_example
  */
 function tripal_example_node_insert($node) {
 
-  // set the URL path after inserting.  We do it here because we do not know the 
+  // set the URL path after inserting.  We do it here because we do not know the
   // example_id in the presave and cannot do it in the hook_insert()
   //switch ($node->type) {
   //  case 'chado_example':
@@ -419,7 +525,7 @@ function tripal_example_node_insert($node) {
   //}
 }
 /**
- * Implementation of hook node_update().  This function is used 
+ * Implementation of hook node_update().  This function is used
  * after any a node is updated in the database.  It is different
  * from the hook_update() function above in that it is called after
  * any node is updated, regardlesss of it's type. This function is useful
@@ -430,12 +536,12 @@ function tripal_example_node_insert($node) {
  * of a node cannot be set in the hook_update() function. Therefore
  * the tripal_feature module uses this function to reset the url path
  * of an updated feature node.
- * 
+ *
  * This function is not required. You probably won't need it if you
  * don't define a custom node type in the hook_node_info() function. But
  * it is node type agnostic, so you can use this function to do any
  * activity after insert of a node.
- * 
+ *
  */
 function tripal_example_node_update($node) {
 
@@ -456,12 +562,12 @@ function tripal_example_node_update($node) {
  * Implementation of hook_node_view().  This function allows you to
  * add custom content to any node page.  It is node type agnostic.
  * Here we typically use it to add content to our custom node type or
- * to other Tripal node types. Typically for Tripal, a content "block" 
+ * to other Tripal node types. Typically for Tripal, a content "block"
  * (e.g. feature properties, feature dbxref, feature pub) has a corresponding
- * template file.  Those template files are first defined to Drupal using 
+ * template file.  Those template files are first defined to Drupal using
  * the hook_theme() function defined in the tripal_example.module file.  Here
- * we can add items to a node's content by calling those templates as needed. 
- * 
+ * we can add items to a node's content by calling those templates as needed.
+ *
  * @ingroup tripal_example
  */
 function tripal_example_node_view($node, $view_mode, $langcode) {
@@ -470,12 +576,12 @@ 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 
+        // there is always a base template.  This is the template that
         // is first shown when the example node type is first displayed
         //$node->content['tripal_example_base'] = array(
         //  '#value' => theme('tripal_example_base', array('node' => $node)),
         //);
-         
+
         // we can add other templates as well.
         //$node->content['tripal_example_properties'] = array(
         //  '#value' => theme('tripal_example_properties', array('node' => $node)),