Browse Source

Added functionality to project module to better match our other modules

spficklin 12 years ago
parent
commit
59deb0a9f0

+ 9 - 0
tripal_core/api/tripal_core.schema_v1.11.api.inc

@@ -6120,6 +6120,15 @@ function tripal_core_chado_schema_v1_11_genotype() {
 function tripal_core_chado_schema_v1_11_library() {
   $description =  array(
     'table' => 'library',
+    'referring_tables' => array(
+      0 => 'cell_line_library',
+      1 => 'library_cvterm',
+      2 => 'library_dbxref',
+      3 => 'library_feature',
+      4 => 'library_pub',
+      5 => 'library_synonym',
+      6 => 'libraryprop',
+    ),
     'fields' => array(
       'library_id' => array(
         'type' => 'serial',

+ 0 - 50
tripal_library/api/tripal_library.api.inc

@@ -10,56 +10,6 @@
  * @ingroup tripal_library
  */
 
-/**
- * Implements hook_chado_schema_v1_11_table()
- * Purpose: To add descriptions and foreign keys to default table description
- * Note: This array will be merged with the array from all other implementations
- *
- * @return
- *    Array describing the library table
- *
- * @ingroup tripal_schema_api
- */
-function tripal_library_chado_schema_v1_11_library() {
-  $description = array();
-  $referring_tables = array(
-      'library_cvterm',
-      'library_feature',
-      'library_pub',
-      'library_synonym',
-      'libraryprop'
-  );
-  $description['referring_tables'] = $referring_tables;
-
-  return $description;
-}
-
-/**
- * Implements hook_chado_schema_v1_2_table()
- * Purpose: To add descriptions and foreign keys to default table description
- * Note: This array will be merged with the array from all other implementations
- *
- * @return
- *    Array describing the library table
- *
- * @ingroup tripal_schema_api
- */
-function tripal_library_chado_schema_v1_2_library() {
-  $description = array();
-  $referring_tables = array(
-      'library_cvterm',
-      'library_feature',
-      'library_pub',
-      'library_synonym',
-      'libraryprop'
-  );
-  $description['referring_tables'] = $referring_tables;
-
-  return $description;
-}
-
-
-
 /**
  * Retrieve properties of a given type for a given library
  *

+ 4 - 4
tripal_library/tripal_library.module

@@ -670,18 +670,18 @@ function chado_library_delete(&$node) {
             "WHERE nid = %d ".
             "AND vid = %d";
   db_query($sql_del, $node->nid, $node->vid);
-  $sql_del = "DELETE FROM {node} ".
+  $sql_del = "DELETE FROM {node_revisions} ".
             "WHERE nid = %d ".
             "AND vid = %d";
   db_query($sql_del, $node->nid, $node->vid);
-  $sql_del = "DELETE FROM {node_revisions} ".
+  $sql_del = "DELETE FROM {node} ".
             "WHERE nid = %d ".
             "AND vid = %d";
-  db_query($sql_del, $node->nid, $node->vid);
+  db_query($sql_del, $node->nid, $node->vid);  
 
   // Remove data from library and libraryprop tables of chado database as well
-  chado_query("DELETE FROM {library} WHERE library_id = %d", $library_id);
   chado_query("DELETE FROM {libraryprop} WHERE library_id = %d", $library_id);
+  chado_query("DELETE FROM {library} WHERE library_id = %d", $library_id);  
 }
 
 /**

+ 30 - 20
tripal_project/tripal_project.install

@@ -15,6 +15,7 @@
  */
 function tripal_project_install() {
   drupal_install_schema('tripal_project');
+  tripal_project_add_cvterms();
 }
 
 /**
@@ -28,39 +29,25 @@ function tripal_project_uninstall() {
  * Implementation of hook_schema().
  */
 function tripal_project_schema() {
-
-  //specification for 'tripal_project_instances'
   $schema['chado_project'] = array(
-
     'fields' => array(
-
-      //a int field that cannot be null and acts as a unique identifier for all nid's
       'nid' => array(
         'type' => 'int',
         'unsigned' => TRUE,
         'not null' => TRUE,
-        ),
-
-      //a int field that cannot be null and is vid
+      ),
       'vid' => array(
           'type' => 'int',
           'not null' => TRUE,
       ),
-
-      //a intfield, not null and project_id is the unique_id of the project in chado
-       'project_id' => array(
-          'type' => 'int',
-          'unsigned' => TRUE,
-          'not null' => TRUE,
+      'project_id' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
       ),
-
-
-    ),//end of shema
-
+    ),
     'primary key' => array('nid', 'vid', 'project_id'),
-
   );
-
   return $schema;
 }
 
@@ -81,4 +68,27 @@ function tripal_project_requirements($phase) {
     }
   }
   return $requirements;
+}
+/*
+ * 
+ */
+function tripal_project_add_cvterms() {
+  
+  // Insert cvterm 'library_description' into cvterm table of chado
+  // database. This CV term is used to keep track of the library
+  // description in the libraryprop table.
+  tripal_cv_add_cvterm(array('name' => 'project_description', 'def' => 'Description of a project'), 
+    'tripal', 0, 1, 'tripal'); 
+}
+/**
+ *  Update for Drupal 6.x, Tripal 1.0
+ *  This update
+ *   - adds the library types
+ *
+ * @ingroup tripal_library
+ */
+function tripal_project_update_6000() {
+  // add in the missing library typ cv terms
+  tripal_project_add_cvterms();
+  return $ret;
 }

+ 294 - 70
tripal_project/tripal_project.module

@@ -1,6 +1,7 @@
 <?php
 
 require('includes/tripal_project.admin.inc');
+require('api/tripal_project.api.inc');
 
 /**
  *  @file
@@ -145,7 +146,23 @@ function tripal_project_theme() {
     'tripal_project_base' => array(
       'arguments' => array('node' => NULL),
       'template' => 'tripal_project_base',
-    ),  
+    ), 
+    'tripal_project_contact' => array(
+       'arguments' => array('node' => NULL),
+       'template' => 'tripal_project_contact',
+    ),
+    'tripal_project_publications' => array(
+       'arguments' => array('node' => NULL),
+       'template' => 'tripal_project_publications',
+    ),
+    'tripal_project_relationships' => array(
+       'arguments' => array('node' => NULL),
+       'template' => 'tripal_project_relationships',
+    ),
+    'tripal_project_properties' => array(
+       'arguments' => array('node' => NULL),
+       'template' => 'tripal_project_properties',
+    ),
     'tripal_project_admin' => array(
       'template' => 'tripal_project_admin',  
       'arguments' =>  array(NULL),  
@@ -170,41 +187,80 @@ function tripal_project_theme() {
  *
  */
 function chado_project_form(&$node, $form_state) {
+  $form = array();
+
+  $project = $node->project;
+
+  // get the project default values.  When this module was first created
+  // the project description was incorrectly stored in the $node->body field.
+  // It is better to store it in the Chado tables.  However, the 'description'
+  // field of the project table is only 255 characters.  So, we are going
+  // to follow the same as the project module and store the description in
+  // the projectprop table and leave the project.description field blank.
+  // however, for backwards compatibitily, we check to see if the description
+  // is in the $node->body field. If it is we'll use that.  When the node is
+  // edited the text will be moved out of the body and into the projectprop 
+  // table where it should belong.
+  if ($node->body) {
+    $project_description = $node->body;
+  }
+  else {
+    $project_description = $node->project_description;
+  }
+  if (!$project_description) {
+    $projectprop = tripal_project_get_property($project->project_id, 'project_description');
+    $project_description = $projectprop->value;
+  }
 
-  $type = node_get_types('type', $node);
-
-  $form['title'] = array(
-    '#type' => 'textfield',
-    '#title' => check_plain($type->title_label),
-    '#required' => TRUE,
-    '#default_value' => $node->title,
-    '#weight' => -5
-  );
-
-  $form['description'] = array(
-    '#type' => 'textfield',
-    '#maxlength' => 255,
-    '#title' => 'Short Description',
-    '#description' => t('Please provide a very short description (less than 255 characters) describing this project.'),
-    '#default_value' => $node->project->description,
+  // keep track of the project id if we have.  If we do have one then
+  // this is an update as opposed to an insert.
+  $form['project_id'] = array(
+    '#type' => 'value',
+    '#value' => $project->project_id,
   );
 
-  $form['body_filter']['body'] = array(
-    '#type' => 'textarea',
-    '#title' => check_plain($type->body_label),
-    '#default_value' => $node->body,
+  $form['title']= array(
+    '#type'          => 'textfield',
+    '#title'         => t('Project Title'),
+    '#description'   => t('Please enter the title for this project. This appears at the top of the project page.'),
+    '#required'      => TRUE,
+    '#default_value' => $node->title,
+    '#weight'        => 1
   );
-  $form['body_filter']['filter'] = filter_form($node->format);
-
-  // whether or not the project exists in chado
-  $form['project_id'] = array(
-    '#type' => 'value',
-    '#value' => ($node->project->project_id) ? $node->project->project_id : FALSE,
+ 
+  $form['project_description']= array(
+    '#type'          => 'textarea',
+    '#title'         => t('Project Description'),
+    '#description'   => t('A brief description of the project'),
+    '#required'      => TRUE,
+    '#default_value' => $project_description,
+    '#weight'        => 5
   );
 
   return $form;
+ 
+}
+/**
+ *  validates submission of form when adding or updating a project node
+ *
+ * @ingroup tripal_project
+ */
+function chado_project_validate($node) {
+  $project = 0;
+  // check to make sure the name on the project is unique
+  // before we try to insert into chado.
+  if ($node->project_id) {
+    $sql = "SELECT * FROM {project} WHERE name = '%s' AND NOT project_id = %d";
+    $project = db_fetch_object(chado_query($sql, $node->title, $node->project_id));
+  }
+  else {
+    $sql = "SELECT * FROM {project} WHERE name = '%s'";
+    $project = db_fetch_object(chado_query($sql, $node->title));
+  }
+  if ($project) {
+    form_set_error('title', t('The unique project name already exists. Please choose another'));
+  }
 }
-
 /**
  * Implementation of hook_insert().
  *
@@ -214,20 +270,37 @@ function chado_project_form(&$node, $form_state) {
  */
 function chado_project_insert($node) {
 
-  $values =  array(
-     'name' => $node->title,
-     'description' => $node->description,
-  );
-
-  if (!$node->project_id) {
-     //inserts info into chado table.
-  $result = tripal_core_chado_insert('project', $values);
-  $node->project_id = $result['project_id'];
+  if ($node->project_id) {
+    $project['project_id'] = $node->project_id;
+  }
+  else {    
+    $values = array(
+      'name' => $node->title,
+      'description' => '',
+    );
+    $project = tripal_core_chado_insert('project', $values);
   }
 
-  //inserts the row of vid,nid,project_id into the chado_project table
-  db_query("INSERT INTO {chado_project} (vid, nid, project_id) VALUES (%d, %d, %d)", $node->vid, $node->nid, $node->project_id);
-
+  if ($project) {
+     // add the description property
+    tripal_project_insert_property($project['project_id'], 'project_description', 
+      $node->project_description);
+
+    // make sure the entry for this feature doesn't already exist in the chado_project table
+    // if it doesn't exist then we want to add it.
+    $project_id = chado_get_id_for_node('project', $node) ;
+    if (!$project_id) {
+       // next add the item to the drupal table
+      $sql = "INSERT INTO {chado_project} (nid, vid, project_id) ".
+             "VALUES (%d, %d, %d)";
+      db_query($sql, $node->nid, $node->vid, $project['project_id']);
+    }
+  }
+  else {
+    drupal_set_message(t('Unable to add project.', 'warning'));
+    watchdog('tripal_project', 'Insert feature: Unable to create project where values: %values',
+      array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
+  }
 }
 
 /**
@@ -240,17 +313,34 @@ function chado_project_insert($node) {
  *
  */
 function chado_project_delete($node) {
-  // Notice that we're matching all revision, by using the node's nid.
-
-  // Find the project to delete
-  $values =  array(
-     'project_id' => $node->project->project_id,
-  );
-  tripal_core_chado_delete('project', $values);
-
-  //deleteing in drupal chado_project table
-  db_query('DELETE FROM {chado_project} WHERE nid = %d', $node->nid);
 
+  $project_id = chado_get_id_for_node('project', $node);
+  
+  // if we don't have a project id for this node then this isn't a node of
+  // type chado_project or the entry in the chado_project table was lost.
+  if (!$project_id) {
+    return;
+  }
+  
+  // Remove data from {chado_project}, {node} and {node_revisions} tables of
+  // drupal database
+  $sql_del = "DELETE FROM {chado_project} ".
+             "WHERE nid = %d ".
+             "AND vid = %d";
+  db_query($sql_del, $node->nid, $node->vid);
+  $sql_del = "DELETE FROM {node_revisions} ".
+             "WHERE nid = %d ".
+             "AND vid = %d";
+  db_query($sql_del, $node->nid, $node->vid);
+  $sql_del = "DELETE FROM {node} ".
+             "WHERE nid = %d ".
+             "AND vid = %d";
+  db_query($sql_del, $node->nid, $node->vid);
+
+
+  // Remove data from project and projectprop tables of chado database as well
+  chado_query("DELETE FROM {projectprop} WHERE project_id = %d", $project_id);
+  chado_query("DELETE FROM {project} WHERE project_id = %d", $project_id);  
 }
 
 /**
@@ -262,22 +352,23 @@ function chado_project_delete($node) {
  *
  */
 function chado_project_update($node) {
-  if ($node->revision) {
+ if ($node->revision) {
     // there is no way to handle revisions in Chado but leave
     // this here just to make not we've addressed it.
   }
-  // Find the project to update
-  $match= array(
-    'project_id' => $node->project_id,
-  );
 
-  // New values
-  $values =  array(
-    'name' => $node->title,
-    'description' => $node->description,
+  // update the project and the description
+  $project_id = chado_get_id_for_node('project', $node) ;
+  $match = array(
+     'project_id' => $project_id,
   );
-
-  $result = tripal_core_chado_update('project', $match, $values);
+  $values = array(
+     'name' => $node->title,
+     'description' => '',
+  );
+  $status = tripal_core_chado_update('project', $match, $values);
+  tripal_project_update_property($project_id, 'project_description', $node->project_description, 1);
+  
 
 }
 
@@ -293,17 +384,150 @@ function chado_project_update($node) {
  */
 function chado_project_load($node) {
 
-  //selecting the coresponding table information
-  $result = db_fetch_object(db_query('SELECT * FROM {chado_project} WHERE nid=%d AND vid=%d', $node->nid, $node->vid));
+  // get the feature details from chado
+  $project_id = chado_get_id_for_node('project', $node);
 
-  //assigning the project-Id to a variable
-  $values = array(
-    'project_id' => $result->project_id,
-  );
+  $values = array('project_id' => $project_id);
+  $project = tripal_core_generate_chado_var('project', $values);
 
-  //the current project set to the 'project' with the $values(project-Id)
-  $node->project = tripal_core_generate_chado_var('project', $values);
+  $additions = new stdClass();
+  $additions->project = $project;
+  return $additions;
 
-  return $node;
+}
+
+/**
+ * Display block with projects
+ * @param op    - parameter to define the phase being called for the block
+ * @param delta - id of the block to return (ignored when op is list)
+ * @param edit  - when op is save, contains the submitted form data
+ *
+ * @ingroup tripal_project
+ */
+function tripal_project_block($op = 'list', $delta = '0', $edit = array()) {
+  switch ($op) {
+    case 'list':
+
+    $blocks['projectbase']['info'] = t('Tripal Project Details');
+    $blocks['projectbase']['cache'] = BLOCK_NO_CACHE;
+
+    $blocks['projectprops']['info'] = t('Tripal Project Properties');
+    $blocks['projectprops']['cache'] = BLOCK_NO_CACHE;
+    
+    $blocks['projectpubs']['info'] = t('Tripal Project Publications');
+    $blocks['projectpubs']['cache'] = BLOCK_NO_CACHE;
+    
+    $blocks['projectcont']['info'] = t('Tripal Project Contact');
+    $blocks['projectcont']['cache'] = BLOCK_NO_CACHE;
+    
+    $blocks['projectrels']['info'] = t('Tripal Project Relationships');
+    $blocks['projectrels']['cache'] = BLOCK_NO_CACHE;
+
+    return $blocks;
+
+    case 'view':
+      if (user_access('access chado_project content') and arg(0) == 'node' and is_numeric(arg(1))) {
+        $nid = arg(1);
+        $node = node_load($nid);
+
+        $block = array();
+        switch ($delta) {
+          case 'projectbase':
+            $block['subject'] = t('Project Details');
+            $block['content'] = theme('tripal_project_base', $node);
+            break;
+          case 'projectprops':
+            $block['subject'] = t('Properties');
+            $block['content'] = theme('tripal_project_properties', $node);
+            break;
+          case 'projectpubs':
+            $block['subject'] = t('Publications');
+            $block['content'] = theme('tripal_project_publications', $node);
+            break;
+          case 'projectcont':
+            $block['subject'] = t('Contact');
+            $block['content'] = theme('tripal_project_contact', $node);
+            break;
+          case 'projectrels':
+            $block['subject'] = t('Relationships');
+            $block['content'] = theme('tripal_project_relationships', $node);
+            break;
+          default :
+        }
+        return $block;
+      }
+  }
+}
+/**
+ *
+ *
+ * @ingroup tripal_project
+ */
+function tripal_project_preprocess_tripal_project_relationships(&$variables) {
+  // we want to provide a new variable that contains the matched projects.
+  $project = $variables['node']->project;
+   
+  // normally we would use tripal_core_expand_chado_vars to expand our
+  // organism object and add in the relationships, however whan a large
+  // number of relationships are present this significantly slows the
+  // query, therefore we will manually perform the query
+  $sql = "
+    SELECT P.name, P.project_id, CP.nid, CVT.name as rel_type
+    FROM project_relationship PR
+      INNER JOIN project P       ON PR.object_project_id = P.project_id
+      INNER JOIN cvterm CVT      ON PR.type_id           = CVT.cvterm_id
+      LEFT JOIN chado_project CP ON P.project_id         = CP.project_id
+    WHERE PR.subject_project_id = %d      
+  ";
+  $as_subject = chado_query($sql, $project->project_id);
+  $sql = "
+    SELECT P.name, P.project_id, CP.nid, CVT.name as rel_type
+    FROM project_relationship PR
+      INNER JOIN project P       ON PR.subject_project_id = P.project_id
+      INNER JOIN cvterm CVT      ON PR.type_id            = CVT.cvterm_id
+      LEFT JOIN chado_project CP ON P.project_id          = CP.project_id
+    WHERE PR.object_project_id = %d      
+  ";
+  $as_object = chado_query($sql, $project->project_id);   
+  
+  // combine both object and subject relationshisp into a single array
+  $relationships = array();
+  $relationships['object'] = array();
+  $relationships['subject'] = array();
+  
+  // iterate through the object relationships
+  while ($relationship = db_fetch_object($as_object)) {
+     
+     // get the relationship and child types
+     $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
+     $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
+     
+     if (!array_key_exists($rel_type, $relationships['object'])) {
+       $relationships['object'][$rel_type] = array();   
+     }
+     if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
+       $relationships['object'][$rel_type][$sub_type] = array();   
+     }
+     $relationships['object'][$rel_type][$sub_type][] = $relationship;     
+  }
+  
+  // now add in the subject relationships
+  while ($relationship = db_fetch_object($as_subject)) {
+     
+     // get the relationship and child types
+     $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
+     $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
+     
+     if (!array_key_exists($rel_type, $relationships['subject'])) {
+       $relationships['subject'][$rel_type] = array();   
+     }
+     if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
+       $relationships['subject'][$rel_type][$obj_type] = array();   
+     }
+     $relationships['subject'][$rel_type][$obj_type][] = $relationship;     
+  }
+  
+  
+  $project->all_relationships = $relationships;
 
 }