Tripal v1.0 (6.x-1.0)
tripal_project.module
Go to the documentation of this file.
00001 <?php
00002 
00003 require('includes/tripal_project.admin.inc');
00004 require('api/tripal_project.api.inc');
00005 
00023 function tripal_project_views_api() {
00024   return array(
00025       'api' => 2.0,
00026   );
00027 }
00028 
00032 function tripal_project_menu() {
00033   $items[ 'admin/tripal/tripal_project' ]= array(
00034     'title' => 'Projects',
00035     'page callback' => 'theme',
00036     'page arguments' => array('tripal_project_admin'),   
00037     'access arguments' => array('adminster tripal projects'),
00038     'type' => MENU_NORMAL_ITEM
00039   );
00040 
00041   $items[ 'admin/tripal/tripal_project/configuration' ]= array(
00042     'title' => 'Configuration',
00043     'page callback' => 'drupal_get_form',
00044     'page arguments' => array('tripal_project_admin'),
00045     'access arguments' => array('adminster tripal projects'),
00046     'type' => MENU_NORMAL_ITEM
00047   );
00048 
00049   return $items;
00050 }
00051 
00059 function tripal_project_perm() {
00060   return array(
00061     'access chado_projects content',
00062     'create chado_projects content',
00063     'delete chado_projects content',
00064     'edit chado_projects content',
00065     'adminster tripal projects',
00066   );
00067 }
00068 
00087 function chado_project_access($op, $node, $account) {
00088 
00089   if ($op == 'create') {
00090     // Only users with permission to do so may create this node type.
00091     if (!user_access('create chado_projects', $account)) {
00092       return FALSE;
00093     }
00094   }
00095 
00096   // Users who create a node may edit or delete it later, assuming they have the necessary permissions.
00097   if ($op == 'update' || $op == 'delete') {
00098 
00099     if (!user_access('edit own chado_projects', $account)) {
00100       return FALSE;
00101     }
00102     if (user_access('edit own chado_projects', $account) &&
00103       $account->uid != $node->uid) {
00104       return FALSE;
00105     }
00106   }
00107 
00108   if ($op == 'view') {
00109     if (!user_access('access chado_projects', $account)) {
00110       return FALSE;
00111     }
00112   }
00113   return NULL;
00114 }
00115 
00124 function tripal_project_node_info() {
00125   return array(
00126     'chado_project' => array(
00127       'name' => t('Project'),
00128       'module' => 'chado_project',
00129       'description' => t('A module for interfacing the GMOD chado database with Drupal, providing viewing of projects'),
00130       'has_title' => TRUE,
00131       'title_label' => t('Project Name'),
00132       'had_body' => TRUE,
00133       'body_label' => t('Full Description'),
00134     )
00135   );
00136 }
00144 function tripal_project_theme() {
00145   return array(
00146     'tripal_project_base' => array(
00147       'arguments' => array('node' => NULL),
00148       'template' => 'tripal_project_base',
00149     ), 
00150     'tripal_project_contact' => array(
00151        'arguments' => array('node' => NULL),
00152        'template' => 'tripal_project_contact',
00153     ),
00154     'tripal_project_publications' => array(
00155        'arguments' => array('node' => NULL),
00156        'template' => 'tripal_project_publications',
00157     ),
00158     'tripal_project_relationships' => array(
00159        'arguments' => array('node' => NULL),
00160        'template' => 'tripal_project_relationships',
00161     ),
00162     'tripal_project_properties' => array(
00163        'arguments' => array('node' => NULL),
00164        'template' => 'tripal_project_properties',
00165     ),
00166     'tripal_project_admin' => array(
00167       'template' => 'tripal_project_admin',  
00168       'arguments' =>  array(NULL),  
00169       'path' => drupal_get_path('module', 'tripal_project') . '/theme' 
00170     ),
00171   );
00172 }
00189 function chado_project_form(&$node, $form_state) {
00190   $form = array();
00191 
00192   $project = $node->project;
00193 
00194   // get the project default values.  When this module was first created
00195   // the project description was incorrectly stored in the $node->body field.
00196   // It is better to store it in the Chado tables.  However, the 'description'
00197   // field of the project table is only 255 characters.  So, we are going
00198   // to follow the same as the project module and store the description in
00199   // the projectprop table and leave the project.description field blank.
00200   // however, for backwards compatibitily, we check to see if the description
00201   // is in the $node->body field. If it is we'll use that.  When the node is
00202   // edited the text will be moved out of the body and into the projectprop 
00203   // table where it should belong.
00204   if ($node->body) {
00205     $project_description = $node->body;
00206   }
00207   else {
00208     $project_description = $node->project_description;
00209   }
00210   if (!$project_description) {
00211     $projectprop = tripal_project_get_property($project->project_id, 'project_description');
00212     $project_description = $projectprop->value;
00213   }
00214 
00215   // keep track of the project id if we have.  If we do have one then
00216   // this is an update as opposed to an insert.
00217   $form['project_id'] = array(
00218     '#type' => 'value',
00219     '#value' => $project->project_id,
00220   );
00221 
00222   $form['title']= array(
00223     '#type'          => 'textfield',
00224     '#title'         => t('Project Title'),
00225     '#description'   => t('Please enter the title for this project. This appears at the top of the project page.'),
00226     '#required'      => TRUE,
00227     '#default_value' => $node->title,
00228     '#weight'        => 1
00229   );
00230  
00231   $form['project_description']= array(
00232     '#type'          => 'textarea',
00233     '#title'         => t('Project Description'),
00234     '#description'   => t('A brief description of the project'),
00235     '#required'      => TRUE,
00236     '#default_value' => $project_description,
00237     '#weight'        => 5
00238   );
00239 
00240   return $form;
00241  
00242 }
00248 function chado_project_validate($node) {
00249   $project = 0;
00250   // check to make sure the name on the project is unique
00251   // before we try to insert into chado.
00252   if ($node->project_id) {
00253     $sql = "SELECT * FROM {project} WHERE name = '%s' AND NOT project_id = %d";
00254     $project = db_fetch_object(chado_query($sql, $node->title, $node->project_id));
00255   }
00256   else {
00257     $sql = "SELECT * FROM {project} WHERE name = '%s'";
00258     $project = db_fetch_object(chado_query($sql, $node->title));
00259   }
00260   if ($project) {
00261     form_set_error('title', t('The unique project name already exists. Please choose another'));
00262   }
00263 }
00271 function chado_project_insert($node) {
00272 
00273   if ($node->project_id) {
00274     $project['project_id'] = $node->project_id;
00275   }
00276   else {    
00277     $values = array(
00278       'name' => $node->title,
00279       'description' => '',
00280     );
00281     $project = tripal_core_chado_insert('project', $values);
00282   }
00283 
00284   if ($project) {
00285      // add the description property
00286     tripal_project_insert_property($project['project_id'], 'project_description', 
00287       $node->project_description);
00288 
00289     // make sure the entry for this feature doesn't already exist in the chado_project table
00290     // if it doesn't exist then we want to add it.
00291     $project_id = chado_get_id_for_node('project', $node) ;
00292     if (!$project_id) {
00293        // next add the item to the drupal table
00294       $sql = "INSERT INTO {chado_project} (nid, vid, project_id) ".
00295              "VALUES (%d, %d, %d)";
00296       db_query($sql, $node->nid, $node->vid, $project['project_id']);
00297     }
00298   }
00299   else {
00300     drupal_set_message(t('Unable to add project.', 'warning'));
00301     watchdog('tripal_project', 'Insert feature: Unable to create project where values: %values',
00302       array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
00303   }
00304 }
00305 
00315 function chado_project_delete($node) {
00316 
00317   $project_id = chado_get_id_for_node('project', $node);
00318   
00319   // if we don't have a project id for this node then this isn't a node of
00320   // type chado_project or the entry in the chado_project table was lost.
00321   if (!$project_id) {
00322     return;
00323   }
00324   
00325   // Remove data from {chado_project}, {node} and {node_revisions} tables of
00326   // drupal database
00327   $sql_del = "DELETE FROM {chado_project} ".
00328              "WHERE nid = %d ".
00329              "AND vid = %d";
00330   db_query($sql_del, $node->nid, $node->vid);
00331   $sql_del = "DELETE FROM {node_revisions} ".
00332              "WHERE nid = %d ".
00333              "AND vid = %d";
00334   db_query($sql_del, $node->nid, $node->vid);
00335   $sql_del = "DELETE FROM {node} ".
00336              "WHERE nid = %d ".
00337              "AND vid = %d";
00338   db_query($sql_del, $node->nid, $node->vid);
00339 
00340 
00341   // Remove data from project and projectprop tables of chado database as well
00342   chado_query("DELETE FROM {projectprop} WHERE project_id = %d", $project_id);
00343   chado_query("DELETE FROM {project} WHERE project_id = %d", $project_id);  
00344 }
00345 
00354 function chado_project_update($node) {
00355  if ($node->revision) {
00356     // there is no way to handle revisions in Chado but leave
00357     // this here just to make not we've addressed it.
00358   }
00359 
00360   // update the project and the description
00361   $project_id = chado_get_id_for_node('project', $node) ;
00362   $match = array(
00363      'project_id' => $project_id,
00364   );
00365   $values = array(
00366      'name' => $node->title,
00367      'description' => '',
00368   );
00369   $status = tripal_core_chado_update('project', $match, $values);
00370   tripal_project_update_property($project_id, 'project_description', $node->project_description, 1);
00371   
00372 
00373 }
00374 
00385 function chado_project_load($node) {
00386 
00387   // get the feature details from chado
00388   $project_id = chado_get_id_for_node('project', $node);
00389 
00390   $values = array('project_id' => $project_id);
00391   $project = tripal_core_generate_chado_var('project', $values);
00392 
00393   $additions = new stdClass();
00394   $additions->project = $project;
00395   return $additions;
00396 
00397 }
00398 
00407 function tripal_project_block($op = 'list', $delta = '0', $edit = array()) {
00408   switch ($op) {
00409     case 'list':
00410 
00411     $blocks['projectbase']['info'] = t('Tripal Project Details');
00412     $blocks['projectbase']['cache'] = BLOCK_NO_CACHE;
00413 
00414     $blocks['projectprops']['info'] = t('Tripal Project Properties');
00415     $blocks['projectprops']['cache'] = BLOCK_NO_CACHE;
00416     
00417     $blocks['projectpubs']['info'] = t('Tripal Project Publications');
00418     $blocks['projectpubs']['cache'] = BLOCK_NO_CACHE;
00419     
00420     $blocks['projectcont']['info'] = t('Tripal Project Contact');
00421     $blocks['projectcont']['cache'] = BLOCK_NO_CACHE;
00422     
00423     $blocks['projectrels']['info'] = t('Tripal Project Relationships');
00424     $blocks['projectrels']['cache'] = BLOCK_NO_CACHE;
00425 
00426     return $blocks;
00427 
00428     case 'view':
00429       if (user_access('access chado_project content') and arg(0) == 'node' and is_numeric(arg(1))) {
00430         $nid = arg(1);
00431         $node = node_load($nid);
00432 
00433         $block = array();
00434         switch ($delta) {
00435           case 'projectbase':
00436             $block['subject'] = t('Project Details');
00437             $block['content'] = theme('tripal_project_base', $node);
00438             break;
00439           case 'projectprops':
00440             $block['subject'] = t('Properties');
00441             $block['content'] = theme('tripal_project_properties', $node);
00442             break;
00443           case 'projectpubs':
00444             $block['subject'] = t('Publications');
00445             $block['content'] = theme('tripal_project_publications', $node);
00446             break;
00447           case 'projectcont':
00448             $block['subject'] = t('Contact');
00449             $block['content'] = theme('tripal_project_contact', $node);
00450             break;
00451           case 'projectrels':
00452             $block['subject'] = t('Relationships');
00453             $block['content'] = theme('tripal_project_relationships', $node);
00454             break;
00455           default :
00456         }
00457         return $block;
00458       }
00459   }
00460 }
00466 function tripal_project_preprocess_tripal_project_relationships(&$variables) {
00467   // we want to provide a new variable that contains the matched projects.
00468   $project = $variables['node']->project;
00469    
00470   // normally we would use tripal_core_expand_chado_vars to expand our
00471   // organism object and add in the relationships, however whan a large
00472   // number of relationships are present this significantly slows the
00473   // query, therefore we will manually perform the query
00474   $sql = "
00475     SELECT P.name, P.project_id, CP.nid, CVT.name as rel_type
00476     FROM project_relationship PR
00477       INNER JOIN project P       ON PR.object_project_id = P.project_id
00478       INNER JOIN cvterm CVT      ON PR.type_id           = CVT.cvterm_id
00479       LEFT JOIN chado_project CP ON P.project_id         = CP.project_id
00480     WHERE PR.subject_project_id = %d      
00481   ";
00482   $as_subject = chado_query($sql, $project->project_id);
00483   $sql = "
00484     SELECT P.name, P.project_id, CP.nid, CVT.name as rel_type
00485     FROM project_relationship PR
00486       INNER JOIN project P       ON PR.subject_project_id = P.project_id
00487       INNER JOIN cvterm CVT      ON PR.type_id            = CVT.cvterm_id
00488       LEFT JOIN chado_project CP ON P.project_id          = CP.project_id
00489     WHERE PR.object_project_id = %d      
00490   ";
00491   $as_object = chado_query($sql, $project->project_id);   
00492   
00493   // combine both object and subject relationshisp into a single array
00494   $relationships = array();
00495   $relationships['object'] = array();
00496   $relationships['subject'] = array();
00497   
00498   // iterate through the object relationships
00499   while ($relationship = db_fetch_object($as_object)) {
00500      
00501      // get the relationship and child types
00502      $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
00503      $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
00504      
00505      if (!array_key_exists($rel_type, $relationships['object'])) {
00506        $relationships['object'][$rel_type] = array();   
00507      }
00508      if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
00509        $relationships['object'][$rel_type][$sub_type] = array();   
00510      }
00511      $relationships['object'][$rel_type][$sub_type][] = $relationship;     
00512   }
00513   
00514   // now add in the subject relationships
00515   while ($relationship = db_fetch_object($as_subject)) {
00516      
00517      // get the relationship and child types
00518      $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
00519      $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
00520      
00521      if (!array_key_exists($rel_type, $relationships['subject'])) {
00522        $relationships['subject'][$rel_type] = array();   
00523      }
00524      if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
00525        $relationships['subject'][$rel_type][$obj_type] = array();   
00526      }
00527      $relationships['subject'][$rel_type][$obj_type][] = $relationship;     
00528   }
00529   
00530   
00531   $project->all_relationships = $relationships;
00532 
00533 }
 All Classes Files Functions Variables