Tripal v1.0 (6.x-1.0)
|
00001 <?php 00002 00003 require_once "api/tripal_organism.api.inc"; 00004 require_once "includes/tripal_organism.admin.inc"; 00005 00016 function tripal_organism_init() { 00017 00018 // add the jGCharts JS and CSS 00019 drupal_add_js(drupal_get_path('theme', 'tripal') . '/js/tripal_organism.js'); 00020 drupal_add_css(drupal_get_path('theme', 'tripal') . '/css/tripal_organism.css'); 00021 } 00028 function tripal_organism_node_info() { 00029 $nodes = array(); 00030 $nodes['chado_organism'] = array( 00031 'name' => t('Organism'), 00032 'module' => 'chado_organism', 00033 'description' => t('An organism from the chado database'), 00034 'has_title' => FALSE, 00035 'title_label' => t('Organism'), 00036 'has_body' => FALSE, 00037 'body_label' => t('Organism Description'), 00038 'locked' => TRUE 00039 ); 00040 return $nodes; 00041 } 00042 00051 function tripal_organism_block($op = 'list', $delta = '0', $edit = array()) { 00052 switch ($op) { 00053 case 'list': 00054 $blocks['base']['info'] = t('Tripal Organism Details'); 00055 $blocks['base']['cache'] = BLOCK_NO_CACHE; 00056 00057 $blocks['description']['info'] = t('Tripal Organism Description'); 00058 $blocks['description']['cache'] = BLOCK_NO_CACHE; 00059 00060 $blocks['image']['info'] = t('Tripal Organism Image'); 00061 $blocks['image']['cache'] = BLOCK_NO_CACHE; 00062 00063 return $blocks; 00064 00065 case 'view': 00066 if (user_access('access chado_feature content') and arg(0) == 'node' and is_numeric(arg(1))) { 00067 $nid = arg(1); 00068 $node = node_load($nid); 00069 00070 $block = array(); 00071 switch ($delta) { 00072 case 'base': 00073 $block['subject'] = t('Organism Details'); 00074 $block['content'] = theme('tripal_organism_base', $node); 00075 break; 00076 case 'description': 00077 $block['subject'] = t('Organism Description'); 00078 $block['content'] = theme('tripal_organism_description', $node); 00079 break; 00080 case 'image': 00081 $block['subject'] = t('Organism Image'); 00082 $block['content'] = theme('tripal_organism_image', $node); 00083 break; 00084 default: 00085 } 00086 return $block; 00087 } 00088 } 00089 } 00097 function tripal_organism_menu() { 00098 $items = array(); 00099 00100 // the administative settings menu 00101 $items['admin/tripal/tripal_organism'] = array( 00102 'title' => 'Organisms', 00103 'description' => 'Basic Description of Tripal Organism Module Functionality', 00104 'page callback' => 'theme', 00105 'page arguments' => array('tripal_organism_admin'), 00106 'access arguments' => array('adminster tripal organism'), 00107 'type' => MENU_NORMAL_ITEM, 00108 ); 00109 00110 $items['admin/tripal/tripal_organism/configuration'] = array( 00111 'title' => 'Configuration', 00112 'description' => 'Manage integration of Chado organisms including associated features', 00113 'page callback' => 'drupal_get_form', 00114 'page arguments' => array('tripal_organism_admin'), 00115 'access arguments' => array('adminster tripal organism'), 00116 'type' => MENU_NORMAL_ITEM, 00117 ); 00118 00119 00120 return $items; 00121 } 00129 function tripal_organism_theme() { 00130 return array( 00131 'tripal_organism_base' => array( 00132 'arguments' => array('node' => NULL), 00133 'template' => 'tripal_organism_base', 00134 ), 00135 'tripal_organism_description' => array( 00136 'arguments' => array('node' => NULL), 00137 'template' => 'tripal_organism_description', 00138 ), 00139 'tripal_organism_image' => array( 00140 'arguments' => array('node' => NULL), 00141 'template' => 'tripal_organism_image', 00142 ), 00143 'tripal_organism_teaser' => array( 00144 'arguments' => array('node' => NULL), 00145 'template' => 'tripal_organism_teaser', 00146 ), 00147 'tripal_organism_admin' => array( 00148 'template' => 'tripal_organism_admin', 00149 'arguments' => array(NULL), 00150 'path' => drupal_get_path('module', 'tripal_organism') . '/theme' 00151 ), 00152 ); 00153 } 00160 function chado_organism_access($op, $node, $account) { 00161 if ($op == 'create') { 00162 if (!user_access('create chado_organism content', $account)) { 00163 return FALSE; 00164 } 00165 } 00166 if ($op == 'update') { 00167 if (!user_access('edit chado_organism content', $account)) { 00168 return FALSE; 00169 } 00170 } 00171 if ($op == 'delete') { 00172 if (!user_access('delete chado_organism content', $account)) { 00173 return FALSE; 00174 } 00175 } 00176 if ($op == 'view') { 00177 if (!user_access('access chado_organism content', $account)) { 00178 return FALSE; 00179 } 00180 } 00181 return NULL; 00182 } 00191 function tripal_organism_perm() { 00192 return array( 00193 'access chado_organism content', 00194 'create chado_organism content', 00195 'delete chado_organism content', 00196 'edit chado_organism content', 00197 'adminster tripal organism', 00198 ); 00199 } 00200 00201 00206 function tripal_organism_nodeapi(&$node, $op, $teaser, $page) { 00207 00208 switch ($op) { 00209 case 'view': 00210 switch ($node->type) { 00211 00212 } 00213 } 00214 } 00219 function tripal_organism_cron() { 00220 // we want to make sure that any new organisms or features that were 00221 // added to the database external to drupal automatically get new 00222 // nodes created for themselves in drupal. 00223 // tripal_organism_sync_organisms(); 00224 } 00225 00230 function chado_organism_validate($node, &$form) { 00231 // if this is an update, we want to make sure that a different organism doesn't 00232 // already have this genus and speces 00233 if ($node->organism_id) { 00234 $sql = "SELECT * 00235 FROM {organism} O 00236 WHERE genus = '%s' and species = '%s' AND NOT organism_id = %d"; 00237 $result = db_fetch_object(chado_query($sql, $node->genus, $node->species, $node->organism_id)); 00238 if ($result) { 00239 form_set_error('genus', t("Update cannot proceed. The organism genus 00240 '$node->genus' and species '$node->species' is already present in the database.")); 00241 watchdog('tripal_organism', 00242 'Update organism: genus and species already exists: %values', 00243 array('%values' => "genus = $node->genus, species = $node->species"), 00244 WATCHDOG_WARNING); 00245 } 00246 } 00247 // if this is an insert then check to make sure the genus and species are unique 00248 else { 00249 $values = array( 00250 'genus' => $node->genus, 00251 'species' => $node->species, 00252 ); 00253 $organism = tripal_core_chado_select('organism', array('organism_id'), $values); 00254 if (sizeof($organism) > 0) { 00255 form_set_error('genus', 'Cannot add the organism with this genus and species. 00256 The organism already exists.'); 00257 watchdog('tripal_organism', 00258 'Insert organism: genus and species already exists: %values', 00259 array('%values' => "genus = $node->genus, species = $node->species"), 00260 WATCHDOG_WARNING); 00261 } 00262 } 00263 } 00271 function chado_organism_insert($node) { 00272 00273 $values = array( 00274 'genus' => $node->genus, 00275 'species' => $node->species, 00276 'abbreviation' => $node->abbreviation, 00277 'common_name' => $node->common_name, 00278 'comment' => $node->description 00279 ); 00280 // if there is an organism_id in the $node object then this must be a sync so 00281 // we can skip adding the organism as it is already there, although 00282 // we do need to proceed with the rest of the insert 00283 if (!$node->organism_id) { 00284 $organism = tripal_core_chado_insert('organism', $values); 00285 if (!$organism) { 00286 drupal_set_message(t('Unable to add organism.', 'warning')); 00287 watchdog('tripal_organism', 00288 'Insert Organism: Unable to create organism where values:%values', 00289 array('%values' => print_r($values, TRUE)), 00290 WATCHDOG_WARNING 00291 ); 00292 return; 00293 } 00294 $organism_id = $organism['organism_id']; 00295 } 00296 else { 00297 $organism_id = $node->organism_id; 00298 } 00299 00300 // Make sure the entry for this organism doesn't already exist in the 00301 // chado_organism table if it doesn't exist then we want to add it. 00302 00303 if (!chado_get_id_for_node('organism', $node) ) { 00304 // next add the item to the drupal table 00305 $sql = "INSERT INTO {chado_organism} (nid, vid, organism_id) ". 00306 "VALUES (%d, %d, %d)"; 00307 chado_query($sql, $node->nid, $node->vid, $organism_id); 00308 } 00309 00310 // set the title for the node 00311 $record = new stdClass(); 00312 $record->title = "$node->genus $node->species"; 00313 $record->nid = $node->nid; 00314 drupal_write_record('node', $record, 'nid'); 00315 drupal_write_record('node_revisions', $record, 'nid'); 00316 00317 // add the image 00318 chado_organism_add_image($node); 00319 } 00325 function chado_organism_update($node) { 00326 if ($node->revision) { 00327 // there is no way to handle revisions in Chado but leave 00328 // this here just to make not we've addressed it. 00329 } 00330 $match = array( 00331 'organism_id' => chado_get_id_for_node('organism', $node), 00332 ); 00333 $values = array( 00334 'genus' => $node->genus, 00335 'species' => $node->species, 00336 'abbreviation' => $node->abbreviation, 00337 'common_name' => $node->common_name, 00338 'comment' => $node->description 00339 ); 00340 $org_status = tripal_core_chado_update('organism', $match, $values); 00341 00342 // set the title for the node 00343 $record = new stdClass(); 00344 $record->title = "$node->genus $node->species"; 00345 $record->nid = $node->nid; 00346 drupal_write_record('node', $record, 'nid'); 00347 drupal_write_record('node_revisions', $record, 'nid'); 00348 00349 // add the image 00350 chado_organism_add_image($node); 00351 } 00358 function chado_organism_delete($node) { 00359 $organism_id = chado_get_id_for_node('organism', $node); 00360 00361 // if we don't have an organism id for this node then this isn't a node of 00362 // type chado_organism or the entry in the chado_organism table was lost. 00363 if (!$organism_id) { 00364 return; 00365 } 00366 00367 // Remove data from the {chado_organism}, {node}, and {node_revisions} tables 00368 $sql_del = "DELETE FROM {chado_organism} ". 00369 "WHERE nid = %d ". 00370 "AND vid = %d"; 00371 db_query($sql_del, $node->nid, $node->vid); 00372 $sql_del = "DELETE FROM {node} ". 00373 "WHERE nid = %d ". 00374 "AND vid = %d"; 00375 db_query($sql_del, $node->nid, $node->vid); 00376 $sql_del = "DELETE FROM {node_revisions} ". 00377 "WHERE nid = %d ". 00378 "AND vid = %d"; 00379 db_query($sql_del, $node->nid, $node->vid); 00380 00381 // Test dependency before deleting from chado database. If a library or 00382 // feature depends on this organism, don't delete it 00383 00384 $sql = "SELECT feature_id FROM {feature} WHERE organism_id = %d"; 00385 $check_feature = db_result(chado_query($sql, $organism_id)); 00386 $sql = "SELECT library_id FROM {library} WHERE organism_id = %d"; 00387 $check_lib = db_result(chado_query($sql, $organism_id)); 00388 00389 if ($check_lib == 0 && $check_feature == 0) { 00390 tripal_core_chado_delete('organism', array('organism_id' => $organism_id)); 00391 } 00392 else { 00393 drupal_set_message(t("Organism deleted from drupal. Warning: at least one ". 00394 "library or feature depends on this organism. It was ". 00395 "not removed from chado.")); 00396 } 00397 } 00403 function chado_organism_add_image($node) { 00404 // check to see if a file was uploaded. If so then copy it to the images 00405 // directory for display with the organism 00406 if (isset($_FILES['files']) && $_FILES['files']['name']['organism_image'] && 00407 is_uploaded_file($_FILES['files']['tmp_name']['organism_image'])) { 00408 $dest = file_directory_path() . "/tripal/tripal_organism/images"; 00409 $validators = array( 00410 'file_validate_is_image' => array(), 00411 ); 00412 file_check_directory($dest, FILE_CREATE_DIRECTORY, 'organism_image'); 00413 if (!$file = file_save_upload('organism_image', $validators, $dest)) { 00414 drupal_set_message(t("Organism image was not uploaded.")); 00415 } 00416 // move this image into the images directory 00417 file_move($file->filepath, $dest . "/" . $node->nid . ".jpg", FILE_EXISTS_REPLACE); 00418 } 00419 } 00420 00421 00428 function chado_organism_form($node, $param) { 00429 $organism = $node->organism; 00430 00431 // add in the comment since it is a text field and may not be included if too big 00432 $organism = tripal_core_expand_chado_vars($organism, 'field', 'organism.comment'); 00433 00434 // get form defaults 00435 $abbreviation = $node->abbreviation; 00436 if (!$abbreviation) { 00437 $abbreviation = $organism->abbreviation; 00438 } 00439 $genus = $node->genus; 00440 if (!$genus) { 00441 $genus = $organism->genus; 00442 } 00443 $species = $node->species; 00444 if (!$species) { 00445 $species = $organism->species; 00446 } 00447 $common_name = $node->common_name; 00448 if (!$common_name) { 00449 $common_name = $organism->common_name; 00450 } 00451 $description = $node->description; 00452 if (!$description) { 00453 $description = $organism->comment; 00454 } 00455 $organism_image = $node->organism_image; 00456 00457 00458 $form = array(); 00459 $form['#attributes']['enctype'] = 'multipart/form-data'; 00460 00461 // keep track of the organism id if we have one. If we do have one then 00462 // this would indicate an update as opposed to an insert. 00463 $form['organism_id'] = array( 00464 '#type' => 'value', 00465 '#value' => $organism->organism_id, 00466 ); 00467 00468 $form['abbreviation']= array( 00469 '#type' => 'textfield', 00470 '#title' => t('Abbreviation'), 00471 '#required' => TRUE, 00472 '#default_value' => $organism->abbreviation, 00473 '#weight' => 3 00474 ); 00475 $form['genus']= array( 00476 '#type' => 'textfield', 00477 '#title' => t('Genus'), 00478 '#required' => TRUE, 00479 '#default_value' => $organism->genus, 00480 '#weight' => 1 00481 ); 00482 $form['species']= array( 00483 '#type' => 'textfield', 00484 '#title' => t('Species'), 00485 '#required' => TRUE, 00486 '#default_value' => $organism->species, 00487 '#weight' => 2 00488 ); 00489 $form['common_name']= array( 00490 '#type' => 'textfield', 00491 '#title' => t('Common Name'), 00492 '#required' => TRUE, 00493 '#default_value' => $organism->common_name, 00494 '#weight' => 4 00495 ); 00496 $form['description']= array( 00497 '#type' => 'textarea', 00498 '#rows' => 15, 00499 '#title' => t('Description'), 00500 '#required' => TRUE, 00501 '#default_value' => $organism->comment, 00502 '#weight' => 5 00503 ); 00504 $form['organism_image']= array( 00505 '#type' => 'file', 00506 '#title' => t('Organism Image'), 00507 '#description' => 'Add an image for this organism', 00508 '#weight' => 6 00509 ); 00510 return $form; 00511 } 00512 00519 function chado_organism_load($node) { 00520 // find the organism and add in the details 00521 $organism_id = chado_get_id_for_node('organism', $node); 00522 $values = array('organism_id' => $organism_id); 00523 00524 $organism = tripal_core_generate_chado_var('organism', $values); 00525 00526 // add in the description field 00527 $organism = tripal_core_expand_chado_vars($organism, 'field', 'organism.comment'); 00528 00529 $additions = new stdClass(); 00530 $additions->organism = $organism; 00531 return $additions; 00532 } 00533 00540 function chado_organism_view($node, $teaser = FALSE, $page = FALSE) { 00541 // use drupal's default node view: 00542 $node = node_prepare($node, $teaser); 00543 return $node; 00544 } 00545 00546 00556 function tripal_organism_help($path, $arg) { 00557 $output = ''; 00558 switch ($path) { 00559 case "admin/help#tripal_organism": 00560 $output = '<p>'. 00561 t("Displays links to nodes created on this date") . 00562 '</p>'; 00563 break; 00564 } 00565 return $output; 00566 } 00567 00568 00577 function get_chado_organisms() { 00578 $sql_drupal = "SELECT COUNT (organism_id) FROM {chado_organism}"; 00579 $no_orgs = db_result(db_query($sql_drupal)); 00580 if ($no_orgs != 0) { 00581 $sql = "SELECT organism_id, nid FROM {chado_organism}"; 00582 $result = chado_query($sql); 00583 $sql = "SELECT genus, species, common_name, comment ". 00584 "FROM {Organism} ". 00585 "WHERE organism_id=%d"; 00586 $organisms = array(); 00587 $count = 0; 00588 while ($data = db_fetch_object($result)) { 00589 $organism = db_fetch_object(chado_query($sql, $data->organism_id)); 00590 $organism->node_id = $data->nid; 00591 // Use common_name plus $count as the key so we can sort by common 00592 // name later. Since common_name is not unique, we need to add $count 00593 // to the key 00594 $key = drupal_strtolower($organism->common_name) . $count; 00595 $organisms [$key] = $organism; 00596 $count ++; 00597 } 00598 00599 //Sort organisms by common name 00600 ksort($organisms, SORT_STRING); 00601 return $organisms; 00602 } 00603 } 00604 00605 00614 function tripal_organism_views_api() { 00615 return array( 00616 'api' => 2.0, 00617 ); 00618 } 00624 function tripal_organism_job_describe_args($callback, $args) { 00625 00626 $new_args = array(); 00627 if ($callback == 'tripal_organism_sync_organisms') { 00628 $organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[0])); 00629 $new_args['Organism'] = $organism[0]->genus ." ". $organism[0]->species; 00630 } 00631 return $new_args; 00632 }