tripal_organism.module 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. <?php
  2. require_once "api/tripal_organism.api.inc";
  3. require_once "includes/tripal_organism.admin.inc";
  4. /**
  5. * @file
  6. * @defgroup tripal_organism Organism Module
  7. * @ingroup tripal_modules
  8. */
  9. /**
  10. *
  11. * @ingroup tripal_organism
  12. */
  13. function tripal_organism_init() {
  14. // add the jGCharts JS and CSS
  15. drupal_add_js(drupal_get_path('theme', 'tripal') . '/js/tripal_organism.js');
  16. drupal_add_css(drupal_get_path('theme', 'tripal') . '/css/tripal_organism.css');
  17. }
  18. /**
  19. * Provide information to drupal about the node types that we're creating
  20. * in this module
  21. *
  22. * @ingroup tripal_organism
  23. */
  24. function tripal_organism_node_info() {
  25. $nodes = array();
  26. $nodes['chado_organism'] = array(
  27. 'name' => t('Organism'),
  28. 'module' => 'chado_organism',
  29. 'description' => t('An organism from the chado database'),
  30. 'has_title' => FALSE,
  31. 'title_label' => t('Organism'),
  32. 'has_body' => FALSE,
  33. 'body_label' => t('Organism Description'),
  34. 'locked' => TRUE
  35. );
  36. return $nodes;
  37. }
  38. /**
  39. * Display block with organisms
  40. * @param op - parameter to define the phase being called for the block
  41. * @param delta - id of the block to return (ignored when op is list)
  42. * @param edit - when op is save, contains the submitted form data
  43. *
  44. * @ingroup tripal_organism
  45. */
  46. function tripal_organism_block($op = 'list', $delta = '0', $edit = array()) {
  47. switch ($op) {
  48. case 'list':
  49. $blocks['base']['info'] = t('Tripal Organism Details');
  50. $blocks['base']['cache'] = BLOCK_NO_CACHE;
  51. $blocks['description']['info'] = t('Tripal Organism Description');
  52. $blocks['description']['cache'] = BLOCK_NO_CACHE;
  53. $blocks['image']['info'] = t('Tripal Organism Image');
  54. $blocks['image']['cache'] = BLOCK_NO_CACHE;
  55. return $blocks;
  56. case 'view':
  57. if (user_access('access chado_feature content') and arg(0) == 'node' and is_numeric(arg(1))) {
  58. $nid = arg(1);
  59. $node = node_load($nid);
  60. $block = array();
  61. switch ($delta) {
  62. case 'base':
  63. $block['subject'] = t('Organism Details');
  64. $block['content'] = theme('tripal_organism_base', $node);
  65. break;
  66. case 'description':
  67. $block['subject'] = t('Organism Description');
  68. $block['content'] = theme('tripal_organism_description', $node);
  69. break;
  70. case 'image':
  71. $block['subject'] = t('Organism Image');
  72. $block['content'] = theme('tripal_organism_image', $node);
  73. break;
  74. default:
  75. }
  76. return $block;
  77. }
  78. }
  79. }
  80. /**
  81. * Menu items are automatically added for the new node types created
  82. * by this module to the 'Create Content' Navigation menu item. This function
  83. * adds more menu items needed for this module.
  84. *
  85. * @ingroup tripal_organism
  86. */
  87. function tripal_organism_menu() {
  88. $items = array();
  89. // the administative settings menu
  90. $items['admin/tripal/tripal_organism'] = array(
  91. 'title' => 'Organisms',
  92. 'description' => 'Basic Description of Tripal Organism Module Functionality',
  93. 'page callback' => 'theme',
  94. 'page arguments' => array('tripal_organism_admin'),
  95. 'access arguments' => array('adminster tripal organism'),
  96. 'type' => MENU_NORMAL_ITEM,
  97. );
  98. $items['admin/tripal/tripal_organism/configuration'] = array(
  99. 'title' => 'Configuration',
  100. 'description' => 'Manage integration of Chado organisms including associated features',
  101. 'page callback' => 'drupal_get_form',
  102. 'page arguments' => array('tripal_organism_admin'),
  103. 'access arguments' => array('adminster tripal organism'),
  104. 'type' => MENU_NORMAL_ITEM,
  105. );
  106. return $items;
  107. }
  108. /**
  109. * We need to let drupal know about our theme functions and their arguments.
  110. * We create theme functions to allow users of the module to customize the
  111. * look and feel of the output generated in this module
  112. *
  113. * @ingroup tripal_organism
  114. */
  115. function tripal_organism_theme() {
  116. return array(
  117. 'tripal_organism_base' => array(
  118. 'arguments' => array('node' => NULL),
  119. 'template' => 'tripal_organism_base',
  120. ),
  121. 'tripal_organism_description' => array(
  122. 'arguments' => array('node' => NULL),
  123. 'template' => 'tripal_organism_description',
  124. ),
  125. 'tripal_organism_image' => array(
  126. 'arguments' => array('node' => NULL),
  127. 'template' => 'tripal_organism_image',
  128. ),
  129. 'tripal_organism_teaser' => array(
  130. 'arguments' => array('node' => NULL),
  131. 'template' => 'tripal_organism_teaser',
  132. ),
  133. 'tripal_organism_admin' => array(
  134. 'template' => 'tripal_organism_admin',
  135. 'arguments' => array(NULL),
  136. 'path' => drupal_get_path('module', 'tripal_organism') . '/theme'
  137. ),
  138. );
  139. }
  140. /**
  141. * The following function proves access control for users trying to
  142. * perform actions on data managed by this module
  143. *
  144. * @ingroup tripal_organism
  145. */
  146. function chado_organism_access($op, $node, $account) {
  147. if ($op == 'create') {
  148. if (!user_access('create chado_organism content', $account)) {
  149. return FALSE;
  150. }
  151. }
  152. if ($op == 'update') {
  153. if (!user_access('edit chado_organism content', $account)) {
  154. return FALSE;
  155. }
  156. }
  157. if ($op == 'delete') {
  158. if (!user_access('delete chado_organism content', $account)) {
  159. return FALSE;
  160. }
  161. }
  162. if ($op == 'view') {
  163. if (!user_access('access chado_organism content', $account)) {
  164. return FALSE;
  165. }
  166. }
  167. return NULL;
  168. }
  169. /**
  170. * Set the permission types that the chado module uses. Essentially we
  171. * want permissionis that protect creation, editing and deleting of chado
  172. * data objects
  173. *
  174. *
  175. @ingroup tripal_organism
  176. */
  177. function tripal_organism_perm() {
  178. return array(
  179. 'access chado_organism content',
  180. 'create chado_organism content',
  181. 'delete chado_organism content',
  182. 'edit chado_organism content',
  183. 'adminster tripal organism',
  184. );
  185. }
  186. /**
  187. *
  188. * @ingroup tripal_organism
  189. */
  190. function tripal_organism_nodeapi(&$node, $op, $teaser, $page) {
  191. switch ($op) {
  192. case 'view':
  193. switch ($node->type) {
  194. }
  195. }
  196. }
  197. /**
  198. *
  199. * @ingroup tripal_organism
  200. */
  201. function tripal_organism_cron() {
  202. // we want to make sure that any new organisms or features that were
  203. // added to the database external to drupal automatically get new
  204. // nodes created for themselves in drupal.
  205. // tripal_organism_sync_organisms();
  206. }
  207. /**
  208. *
  209. * @ingroup tripal_organism
  210. */
  211. function chado_organism_validate($node, &$form) {
  212. // if this is an update, we want to make sure that a different organism doesn't
  213. // already have this genus and speces
  214. if ($node->organism_id) {
  215. $sql = "SELECT *
  216. FROM {organism} O
  217. WHERE genus = '%s' and species = '%s' AND NOT organism_id = %d";
  218. $result = db_fetch_object(chado_query($sql, $node->genus, $node->species, $node->organism_id));
  219. if ($result) {
  220. form_set_error('genus', t("Update cannot proceed. The organism genus
  221. '$node->genus' and species '$node->species' is already present in the database."));
  222. watchdog('tripal_organism',
  223. 'Update organism: genus and species already exists: %values',
  224. array('%values' => "genus = $node->genus, species = $node->species"),
  225. WATCHDOG_WARNING);
  226. }
  227. }
  228. // if this is an insert then check to make sure the genus and species are unique
  229. else {
  230. $values = array(
  231. 'genus' => $node->genus,
  232. 'species' => $node->species,
  233. );
  234. $organism = tripal_core_chado_select('organism', array('organism_id'), $values);
  235. if (sizeof($organism) > 0) {
  236. form_set_error('genus', 'Cannot add the organism with this genus and species.
  237. The organism already exists.');
  238. watchdog('tripal_organism',
  239. 'Insert organism: genus and species already exists: %values',
  240. array('%values' => "genus = $node->genus, species = $node->species"),
  241. WATCHDOG_WARNING);
  242. }
  243. }
  244. }
  245. /**
  246. * When a new chado_organism node is created we also need to add information
  247. * to our chado_organism table. This function is called on insert of a new node
  248. * of type 'chado_organism' and inserts the necessary information.
  249. *
  250. * @ingroup tripal_organism
  251. */
  252. function chado_organism_insert($node) {
  253. $values = array(
  254. 'genus' => $node->genus,
  255. 'species' => $node->species,
  256. 'abbreviation' => $node->abbreviation,
  257. 'common_name' => $node->common_name,
  258. 'comment' => $node->description
  259. );
  260. // if there is an organism_id in the $node object then this must be a sync so
  261. // we can skip adding the organism as it is already there, although
  262. // we do need to proceed with the rest of the insert
  263. if (!$node->organism_id) {
  264. $organism = tripal_core_chado_insert('organism', $values);
  265. if (!$organism) {
  266. drupal_set_message(t('Unable to add organism.', 'warning'));
  267. watchdog('tripal_organism', 'Insert Organism: Unable to create organism where values:%values',
  268. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  269. return;
  270. }
  271. $organism_id = $organism['organism_id'];
  272. }
  273. else {
  274. $organism_id = $node->organism_id;
  275. }
  276. // Make sure the entry for this organism doesn't already exist in the
  277. // chado_organism table if it doesn't exist then we want to add it.
  278. if (!chado_get_id_for_node('organism', $node) ) {
  279. // next add the item to the drupal table
  280. $sql = "INSERT INTO {chado_organism} (nid, vid, organism_id) ".
  281. "VALUES (%d, %d, %d)";
  282. db_query($sql, $node->nid, $node->vid, $organism_id);
  283. }
  284. // set the title for the node
  285. $record = new stdClass();
  286. $record->title = "$node->genus $node->species";
  287. $record->nid = $node->nid;
  288. drupal_write_record('node', $record, 'nid');
  289. drupal_write_record('node_revisions', $record, 'nid');
  290. // add the image
  291. chado_organism_add_image($node);
  292. }
  293. /**
  294. * Update organisms
  295. *
  296. * @ingroup tripal_organism
  297. */
  298. function chado_organism_update($node) {
  299. if ($node->revision) {
  300. // there is no way to handle revisions in Chado but leave
  301. // this here just to make not we've addressed it.
  302. }
  303. $match = array(
  304. 'organism_id' => chado_get_id_for_node('organism', $node),
  305. );
  306. $values = array(
  307. 'genus' => $node->genus,
  308. 'species' => $node->species,
  309. 'abbreviation' => $node->abbreviation,
  310. 'common_name' => $node->common_name,
  311. 'comment' => $node->description
  312. );
  313. $org_status = tripal_core_chado_update('organism', $match, $values);
  314. // set the title for the node
  315. $record = new stdClass();
  316. $record->title = "$node->genus $node->species";
  317. $record->nid = $node->nid;
  318. drupal_write_record('node', $record, 'nid');
  319. drupal_write_record('node_revisions', $record, 'nid');
  320. // add the image
  321. chado_organism_add_image($node);
  322. }
  323. /**
  324. * Delete organism from both drupal and chado databases. Check dependency before
  325. * deleting from chado.
  326. *
  327. * @ingroup tripal_organism
  328. */
  329. function chado_organism_delete($node) {
  330. $organism_id = chado_get_id_for_node('organism', $node);
  331. // if we don't have an organism id for this node then this isn't a node of
  332. // type chado_organism or the entry in the chado_organism table was lost.
  333. if (!$organism_id) {
  334. return;
  335. }
  336. // Remove data from the {chado_organism}, {node}, and {node_revisions} tables
  337. $sql_del = "DELETE FROM {chado_organism} ".
  338. "WHERE nid = %d ".
  339. "AND vid = %d";
  340. db_query($sql_del, $node->nid, $node->vid);
  341. $sql_del = "DELETE FROM {node} ".
  342. "WHERE nid = %d ".
  343. "AND vid = %d";
  344. db_query($sql_del, $node->nid, $node->vid);
  345. $sql_del = "DELETE FROM {node_revisions} ".
  346. "WHERE nid = %d ".
  347. "AND vid = %d";
  348. db_query($sql_del, $node->nid, $node->vid);
  349. // Test dependency before deleting from chado database. If a library or
  350. // feature depends on this organism, don't delete it
  351. $sql = "SELECT feature_id FROM {feature} WHERE organism_id = %d";
  352. $check_feature = db_result(chado_query($sql, $organism_id));
  353. $sql = "SELECT library_id FROM {library} WHERE organism_id = %d";
  354. $check_lib = db_result(chado_query($sql, $organism_id));
  355. if ($check_lib == 0 && $check_feature == 0) {
  356. tripal_core_chado_delete('organism', array('organism_id' => $organism_id));
  357. }
  358. else {
  359. drupal_set_message(t("Organism deleted from drupal. Warning: at least one ".
  360. "library or feature depends on this organism. It was ".
  361. "not removed from chado."));
  362. }
  363. }
  364. /**
  365. *
  366. *
  367. * @ingroup tripal_organism
  368. */
  369. function chado_organism_add_image($node) {
  370. // check to see if a file was uploaded. If so then copy it to the images
  371. // directory for display with the organism
  372. if (isset($_FILES['files']) && $_FILES['files']['name']['organism_image'] &&
  373. is_uploaded_file($_FILES['files']['tmp_name']['organism_image'])) {
  374. $dest = file_directory_path() . "/tripal/tripal_organism/images";
  375. $validators = array(
  376. 'file_validate_is_image' => array(),
  377. );
  378. file_check_directory($dest, FILE_CREATE_DIRECTORY, 'organism_image');
  379. if (!$file = file_save_upload('organism_image', $validators, $dest)) {
  380. drupal_set_message(t("Organism image was not uploaded."));
  381. }
  382. // move this image into the images directory
  383. file_move($file->filepath, $dest . "/" . $node->nid . ".jpg", FILE_EXISTS_REPLACE);
  384. }
  385. }
  386. /**
  387. * When editing or creating a new node of type 'chado_organism' we need
  388. * a form. This function creates the form that will be used for this.
  389. *
  390. * @ingroup tripal_organism
  391. */
  392. function chado_organism_form($node, $param) {
  393. $organism = $node->organism;
  394. // add in the comment since it is a text field and may not be included if too big
  395. $organism = tripal_core_expand_chado_vars($organism, 'field', 'organism.comment');
  396. // get form defaults
  397. $abbreviation = $node->abbreviation;
  398. if (!$abbreviation) {
  399. $abbreviation = $organism->abbreviation;
  400. }
  401. $genus = $node->genus;
  402. if (!$genus) {
  403. $genus = $organism->genus;
  404. }
  405. $species = $node->species;
  406. if (!$species) {
  407. $species = $organism->species;
  408. }
  409. $common_name = $node->common_name;
  410. if (!$common_name) {
  411. $common_name = $organism->common_name;
  412. }
  413. $description = $node->description;
  414. if (!$description) {
  415. $description = $organism->comment;
  416. }
  417. $organism_image = $node->organism_image;
  418. $form = array();
  419. $form['#attributes']['enctype'] = 'multipart/form-data';
  420. // keep track of the organism id if we have one. If we do have one then
  421. // this would indicate an update as opposed to an insert.
  422. $form['organism_id'] = array(
  423. '#type' => 'value',
  424. '#value' => $organism->organism_id,
  425. );
  426. $form['abbreviation']= array(
  427. '#type' => 'textfield',
  428. '#title' => t('Abbreviation'),
  429. '#required' => TRUE,
  430. '#default_value' => $organism->abbreviation,
  431. '#weight' => 3
  432. );
  433. $form['genus']= array(
  434. '#type' => 'textfield',
  435. '#title' => t('Genus'),
  436. '#required' => TRUE,
  437. '#default_value' => $organism->genus,
  438. '#weight' => 1
  439. );
  440. $form['species']= array(
  441. '#type' => 'textfield',
  442. '#title' => t('Species'),
  443. '#required' => TRUE,
  444. '#default_value' => $organism->species,
  445. '#weight' => 2
  446. );
  447. $form['common_name']= array(
  448. '#type' => 'textfield',
  449. '#title' => t('Common Name'),
  450. '#required' => TRUE,
  451. '#default_value' => $organism->common_name,
  452. '#weight' => 4
  453. );
  454. $form['description']= array(
  455. '#type' => 'textarea',
  456. '#rows' => 15,
  457. '#title' => t('Description'),
  458. '#required' => TRUE,
  459. '#default_value' => $organism->comment,
  460. '#weight' => 5
  461. );
  462. $form['organism_image']= array(
  463. '#type' => 'file',
  464. '#title' => t('Organism Image'),
  465. '#description' => 'Add an image for this organism',
  466. '#weight' => 6
  467. );
  468. return $form;
  469. }
  470. /**
  471. * When a node is requested by the user this function is called to allow us
  472. * to add auxiliary data to the node object.
  473. *
  474. * @ingroup tripal_organism
  475. */
  476. function chado_organism_load($node) {
  477. // find the organism and add in the details
  478. $organism_id = chado_get_id_for_node('organism', $node);
  479. $values = array('organism_id' => $organism_id);
  480. $organism = tripal_core_generate_chado_var('organism', $values);
  481. // add in the description field
  482. $organism = tripal_core_expand_chado_vars($organism, 'field', 'organism.comment');
  483. $additions = new stdClass();
  484. $additions->organism = $organism;
  485. return $additions;
  486. }
  487. /**
  488. * This function customizes the view of the chado_organism node. It allows
  489. * us to generate the markup.
  490. *
  491. * @ingroup tripal_organism
  492. */
  493. function chado_organism_view($node, $teaser = FALSE, $page = FALSE) {
  494. // use drupal's default node view:
  495. $node = node_prepare($node, $teaser);
  496. return $node;
  497. }
  498. /**
  499. * Display help and module information
  500. * @param path which path of the site we're displaying help
  501. * @param arg array that holds the current path as would be returned from arg()
  502. * function
  503. * @return help text for the path
  504. *
  505. * @ingroup tripal_organism
  506. */
  507. function tripal_organism_help($path, $arg) {
  508. $output = '';
  509. switch ($path) {
  510. case "admin/help#tripal_organism":
  511. $output = '<p>'.
  512. t("Displays links to nodes created on this date") .
  513. '</p>';
  514. break;
  515. }
  516. return $output;
  517. }
  518. /**
  519. * This function uses organism_id's of all drupal organism nodes as input and
  520. * pull the organism information (genus, species, common_name, comment) from
  521. * chado database. The return type is an object array that stores $organism
  522. * objects sorted by common_name
  523. *
  524. * @ingroup tripal_organism
  525. */
  526. function get_chado_organisms() {
  527. $sql_drupal = "SELECT COUNT (organism_id) FROM {chado_organism}";
  528. $no_orgs = db_result(db_query($sql_drupal));
  529. if ($no_orgs != 0) {
  530. $sql = "SELECT organism_id, nid FROM {chado_organism}";
  531. $result = chado_query($sql);
  532. $sql = "SELECT genus, species, common_name, comment ".
  533. "FROM {Organism} ".
  534. "WHERE organism_id=%d";
  535. $organisms = array();
  536. $count = 0;
  537. while ($data = db_fetch_object($result)) {
  538. $organism = db_fetch_object(chado_query($sql, $data->organism_id));
  539. $organism->node_id = $data->nid;
  540. // Use common_name plus $count as the key so we can sort by common
  541. // name later. Since common_name is not unique, we need to add $count
  542. // to the key
  543. $key = drupal_strtolower($organism->common_name) . $count;
  544. $organisms [$key] = $organism;
  545. $count ++;
  546. }
  547. //Sort organisms by common name
  548. ksort($organisms, SORT_STRING);
  549. return $organisms;
  550. }
  551. }
  552. /**
  553. * Implements hook_views_api()
  554. * Purpose: Essentially this hook tells drupal that there is views support for
  555. * for this module which then includes tripal_db.views.inc where all the
  556. * views integration code is
  557. *
  558. * @ingroup tripal_organism
  559. */
  560. function tripal_organism_views_api() {
  561. return array(
  562. 'api' => 2.0,
  563. );
  564. }
  565. /**
  566. *
  567. *
  568. * @ingroup tripal_organism
  569. */
  570. function tripal_organism_job_describe_args($callback, $args) {
  571. $new_args = array();
  572. if ($callback == 'tripal_organism_sync_organisms') {
  573. $organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[0]));
  574. $new_args['Organism'] = $organism[0]->genus ." ". $organism[0]->species;
  575. }
  576. return $new_args;
  577. }