tripal_organism.module 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. 'tripal_organism_references' => array(
  139. 'arguments' => array('node' => NULL),
  140. 'template' => 'tripal_organism_references',
  141. ),
  142. );
  143. }
  144. /**
  145. * Implement hook_access().
  146. *
  147. * This hook allows node modules to limit access to the node types they define.
  148. *
  149. * @param $op
  150. * The operation to be performed
  151. *
  152. * @param $node
  153. * The node on which the operation is to be performed, or, if it does not yet exist, the
  154. * type of node to be created
  155. *
  156. * @param $account
  157. * A user object representing the user for whom the operation is to be performed
  158. *
  159. * @return
  160. * If the permission for the specified operation is not set then return FALSE. If the
  161. * permission is set then return NULL as this allows other modules to disable
  162. * access. The only exception is when the $op == 'create'. We will always
  163. * return TRUE if the permission is set.
  164. *
  165. * @ingroup tripal_organism
  166. */
  167. function chado_organism_access($op, $node, $account) {
  168. if ($op == 'create') {
  169. if (!user_access('create chado_organism content', $account)) {
  170. return FALSE;
  171. }
  172. return TRUE;
  173. }
  174. if ($op == 'update') {
  175. if (!user_access('edit chado_organism content', $account)) {
  176. return FALSE;
  177. }
  178. }
  179. if ($op == 'delete') {
  180. if (!user_access('delete chado_organism content', $account)) {
  181. return FALSE;
  182. }
  183. }
  184. if ($op == 'view') {
  185. if (!user_access('access chado_organism content', $account)) {
  186. return FALSE;
  187. }
  188. }
  189. return NULL;
  190. }
  191. /**
  192. * Set the permission types that the chado module uses. Essentially we
  193. * want permissionis that protect creation, editing and deleting of chado
  194. * data objects
  195. *
  196. *
  197. @ingroup tripal_organism
  198. */
  199. function tripal_organism_perm() {
  200. return array(
  201. 'access chado_organism content',
  202. 'create chado_organism content',
  203. 'delete chado_organism content',
  204. 'edit chado_organism content',
  205. 'adminster tripal organism',
  206. );
  207. }
  208. /**
  209. *
  210. * @ingroup tripal_organism
  211. */
  212. function tripal_organism_nodeapi(&$node, $op, $teaser, $page) {
  213. switch ($op) {
  214. case 'view':
  215. switch ($node->type) {
  216. }
  217. }
  218. }
  219. /**
  220. *
  221. * @ingroup tripal_organism
  222. */
  223. function tripal_organism_cron() {
  224. // we want to make sure that any new organisms or features that were
  225. // added to the database external to drupal automatically get new
  226. // nodes created for themselves in drupal.
  227. // tripal_organism_sync_organisms();
  228. }
  229. /**
  230. *
  231. * @ingroup tripal_organism
  232. */
  233. function chado_organism_validate($node, &$form) {
  234. // if this is an update, we want to make sure that a different organism doesn't
  235. // already have this genus and speces
  236. if ($node->organism_id) {
  237. $sql = "SELECT *
  238. FROM {organism} O
  239. WHERE genus = '%s' and species = '%s' AND NOT organism_id = %d";
  240. $result = db_fetch_object(chado_query($sql, $node->genus, $node->species, $node->organism_id));
  241. if ($result) {
  242. form_set_error('genus', t("Update cannot proceed. The organism genus
  243. '$node->genus' and species '$node->species' is already present in the database."));
  244. watchdog('tripal_organism',
  245. 'Update organism: genus and species already exists: %values',
  246. array('%values' => "genus = $node->genus, species = $node->species"),
  247. WATCHDOG_WARNING);
  248. }
  249. }
  250. // if this is an insert then check to make sure the genus and species are unique
  251. else {
  252. $values = array(
  253. 'genus' => $node->genus,
  254. 'species' => $node->species,
  255. );
  256. $organism = tripal_core_chado_select('organism', array('organism_id'), $values);
  257. if (sizeof($organism) > 0) {
  258. form_set_error('genus', 'Cannot add the organism with this genus and species.
  259. The organism already exists.');
  260. watchdog('tripal_organism',
  261. 'Insert organism: genus and species already exists: %values',
  262. array('%values' => "genus = $node->genus, species = $node->species"),
  263. WATCHDOG_WARNING);
  264. }
  265. }
  266. }
  267. /**
  268. * When a new chado_organism node is created we also need to add information
  269. * to our chado_organism table. This function is called on insert of a new node
  270. * of type 'chado_organism' and inserts the necessary information.
  271. *
  272. * @ingroup tripal_organism
  273. */
  274. function chado_organism_insert($node) {
  275. $values = array(
  276. 'genus' => $node->genus,
  277. 'species' => $node->species,
  278. 'abbreviation' => $node->abbreviation,
  279. 'common_name' => $node->common_name,
  280. 'comment' => $node->description
  281. );
  282. // if there is an organism_id in the $node object then this must be a sync so
  283. // we can skip adding the organism as it is already there, although
  284. // we do need to proceed with the rest of the insert
  285. if (!$node->organism_id) {
  286. $organism = tripal_core_chado_insert('organism', $values);
  287. if (!$organism) {
  288. drupal_set_message(t('Unable to add organism.', 'warning'));
  289. watchdog('tripal_organism', 'Insert Organism: Unable to create organism where values:%values',
  290. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  291. return;
  292. }
  293. $organism_id = $organism['organism_id'];
  294. }
  295. else {
  296. $organism_id = $node->organism_id;
  297. }
  298. // Make sure the entry for this organism doesn't already exist in the
  299. // chado_organism table if it doesn't exist then we want to add it.
  300. if (!chado_get_id_for_node('organism', $node) ) {
  301. // next add the item to the drupal table
  302. $sql = "INSERT INTO {chado_organism} (nid, vid, organism_id) ".
  303. "VALUES (%d, %d, %d)";
  304. db_query($sql, $node->nid, $node->vid, $organism_id);
  305. }
  306. // set the title for the node
  307. $record = new stdClass();
  308. $record->title = "$node->genus $node->species";
  309. $record->nid = $node->nid;
  310. drupal_write_record('node', $record, 'nid');
  311. drupal_write_record('node_revisions', $record, 'nid');
  312. // add the image
  313. chado_organism_add_image($node);
  314. }
  315. /**
  316. * Update organisms
  317. *
  318. * @ingroup tripal_organism
  319. */
  320. function chado_organism_update($node) {
  321. if ($node->revision) {
  322. // there is no way to handle revisions in Chado but leave
  323. // this here just to make not we've addressed it.
  324. }
  325. $match = array(
  326. 'organism_id' => chado_get_id_for_node('organism', $node),
  327. );
  328. $values = array(
  329. 'genus' => $node->genus,
  330. 'species' => $node->species,
  331. 'abbreviation' => $node->abbreviation,
  332. 'common_name' => $node->common_name,
  333. 'comment' => $node->description
  334. );
  335. $org_status = tripal_core_chado_update('organism', $match, $values);
  336. // set the title for the node
  337. $record = new stdClass();
  338. $record->title = "$node->genus $node->species";
  339. $record->nid = $node->nid;
  340. drupal_write_record('node', $record, 'nid');
  341. drupal_write_record('node_revisions', $record, 'nid');
  342. // add the image
  343. chado_organism_add_image($node);
  344. }
  345. /**
  346. * Delete organism from both drupal and chado databases. Check dependency before
  347. * deleting from chado.
  348. *
  349. * @ingroup tripal_organism
  350. */
  351. function chado_organism_delete($node) {
  352. $organism_id = chado_get_id_for_node('organism', $node);
  353. // if we don't have an organism id for this node then this isn't a node of
  354. // type chado_organism or the entry in the chado_organism table was lost.
  355. if (!$organism_id) {
  356. return;
  357. }
  358. // Remove data from the {chado_organism}, {node}, and {node_revisions} tables
  359. $sql_del = "DELETE FROM {chado_organism} ".
  360. "WHERE nid = %d ".
  361. "AND vid = %d";
  362. db_query($sql_del, $node->nid, $node->vid);
  363. $sql_del = "DELETE FROM {node} ".
  364. "WHERE nid = %d ".
  365. "AND vid = %d";
  366. db_query($sql_del, $node->nid, $node->vid);
  367. $sql_del = "DELETE FROM {node_revisions} ".
  368. "WHERE nid = %d ".
  369. "AND vid = %d";
  370. db_query($sql_del, $node->nid, $node->vid);
  371. // Test dependency before deleting from chado database. If a library or
  372. // feature depends on this organism, don't delete it
  373. $sql = "SELECT feature_id FROM {feature} WHERE organism_id = %d";
  374. $check_feature = db_result(chado_query($sql, $organism_id));
  375. $sql = "SELECT library_id FROM {library} WHERE organism_id = %d";
  376. $check_lib = db_result(chado_query($sql, $organism_id));
  377. if ($check_lib == 0 && $check_feature == 0) {
  378. tripal_core_chado_delete('organism', array('organism_id' => $organism_id));
  379. }
  380. else {
  381. drupal_set_message(t("Organism deleted from drupal. Warning: at least one ".
  382. "library or feature depends on this organism. It was ".
  383. "not removed from chado."));
  384. }
  385. }
  386. /**
  387. *
  388. *
  389. * @ingroup tripal_organism
  390. */
  391. function chado_organism_add_image($node) {
  392. // check to see if a file was uploaded. If so then copy it to the images
  393. // directory for display with the organism
  394. if (isset($_FILES['files']) && $_FILES['files']['name']['organism_image'] &&
  395. is_uploaded_file($_FILES['files']['tmp_name']['organism_image'])) {
  396. $dest = file_directory_path() . "/tripal/tripal_organism/images";
  397. $validators = array(
  398. 'file_validate_is_image' => array(),
  399. );
  400. file_check_directory($dest, FILE_CREATE_DIRECTORY, 'organism_image');
  401. if (!$file = file_save_upload('organism_image', $validators, $dest)) {
  402. drupal_set_message(t("Organism image was not uploaded."));
  403. }
  404. // move this image into the images directory
  405. file_move($file->filepath, $dest . "/" . $node->nid . ".jpg", FILE_EXISTS_REPLACE);
  406. }
  407. }
  408. /**
  409. * When editing or creating a new node of type 'chado_organism' we need
  410. * a form. This function creates the form that will be used for this.
  411. *
  412. * @ingroup tripal_organism
  413. */
  414. function chado_organism_form($node, $param) {
  415. $organism = $node->organism;
  416. // add in the comment since it is a text field and may not be included if too big
  417. $organism = tripal_core_expand_chado_vars($organism, 'field', 'organism.comment');
  418. // get form defaults
  419. $abbreviation = $node->abbreviation;
  420. if (!$abbreviation) {
  421. $abbreviation = $organism->abbreviation;
  422. }
  423. $genus = $node->genus;
  424. if (!$genus) {
  425. $genus = $organism->genus;
  426. }
  427. $species = $node->species;
  428. if (!$species) {
  429. $species = $organism->species;
  430. }
  431. $common_name = $node->common_name;
  432. if (!$common_name) {
  433. $common_name = $organism->common_name;
  434. }
  435. $description = $node->description;
  436. if (!$description) {
  437. $description = $organism->comment;
  438. }
  439. $organism_image = $node->organism_image;
  440. $form = array();
  441. $form['#attributes']['enctype'] = 'multipart/form-data';
  442. // keep track of the organism id if we have one. If we do have one then
  443. // this would indicate an update as opposed to an insert.
  444. $form['organism_id'] = array(
  445. '#type' => 'value',
  446. '#value' => $organism->organism_id,
  447. );
  448. $form['abbreviation']= array(
  449. '#type' => 'textfield',
  450. '#title' => t('Abbreviation'),
  451. '#required' => TRUE,
  452. '#default_value' => $organism->abbreviation,
  453. '#weight' => 3
  454. );
  455. $form['genus']= array(
  456. '#type' => 'textfield',
  457. '#title' => t('Genus'),
  458. '#required' => TRUE,
  459. '#default_value' => $organism->genus,
  460. '#weight' => 1
  461. );
  462. $form['species']= array(
  463. '#type' => 'textfield',
  464. '#title' => t('Species'),
  465. '#required' => TRUE,
  466. '#default_value' => $organism->species,
  467. '#weight' => 2
  468. );
  469. $form['common_name']= array(
  470. '#type' => 'textfield',
  471. '#title' => t('Common Name'),
  472. '#required' => TRUE,
  473. '#default_value' => $organism->common_name,
  474. '#weight' => 4
  475. );
  476. $form['description']= array(
  477. '#type' => 'textarea',
  478. '#rows' => 15,
  479. '#title' => t('Description'),
  480. '#required' => TRUE,
  481. '#default_value' => $organism->comment,
  482. '#weight' => 5
  483. );
  484. $form['organism_image']= array(
  485. '#type' => 'file',
  486. '#title' => t('Organism Image'),
  487. '#description' => 'Add an image for this organism',
  488. '#weight' => 6
  489. );
  490. return $form;
  491. }
  492. /**
  493. * When a node is requested by the user this function is called to allow us
  494. * to add auxiliary data to the node object.
  495. *
  496. * @ingroup tripal_organism
  497. */
  498. function chado_organism_load($node) {
  499. // find the organism and add in the details
  500. $organism_id = chado_get_id_for_node('organism', $node);
  501. $values = array('organism_id' => $organism_id);
  502. $organism = tripal_core_generate_chado_var('organism', $values);
  503. // add in the description field
  504. $organism = tripal_core_expand_chado_vars($organism, 'field', 'organism.comment');
  505. $additions = new stdClass();
  506. $additions->organism = $organism;
  507. return $additions;
  508. }
  509. /**
  510. * This function customizes the view of the chado_organism node. It allows
  511. * us to generate the markup.
  512. *
  513. * @ingroup tripal_organism
  514. */
  515. function chado_organism_view($node, $teaser = FALSE, $page = FALSE) {
  516. // use drupal's default node view:
  517. $node = node_prepare($node, $teaser);
  518. return $node;
  519. }
  520. /**
  521. * Display help and module information
  522. * @param path which path of the site we're displaying help
  523. * @param arg array that holds the current path as would be returned from arg()
  524. * function
  525. * @return help text for the path
  526. *
  527. * @ingroup tripal_organism
  528. */
  529. function tripal_organism_help($path, $arg) {
  530. $output = '';
  531. switch ($path) {
  532. case "admin/help#tripal_organism":
  533. $output = '<p>'.
  534. t("Displays links to nodes created on this date") .
  535. '</p>';
  536. break;
  537. }
  538. return $output;
  539. }
  540. /**
  541. * This function uses organism_id's of all drupal organism nodes as input and
  542. * pull the organism information (genus, species, common_name, comment) from
  543. * chado database. The return type is an object array that stores $organism
  544. * objects sorted by common_name
  545. *
  546. * @ingroup tripal_organism
  547. */
  548. function get_chado_organisms() {
  549. $sql_drupal = "SELECT COUNT (organism_id) FROM {chado_organism}";
  550. $no_orgs = db_result(db_query($sql_drupal));
  551. if ($no_orgs != 0) {
  552. $sql = "SELECT organism_id, nid FROM {chado_organism}";
  553. $result = chado_query($sql);
  554. $sql = "SELECT genus, species, common_name, comment ".
  555. "FROM {Organism} ".
  556. "WHERE organism_id=%d";
  557. $organisms = array();
  558. $count = 0;
  559. while ($data = db_fetch_object($result)) {
  560. $organism = db_fetch_object(chado_query($sql, $data->organism_id));
  561. $organism->node_id = $data->nid;
  562. // Use common_name plus $count as the key so we can sort by common
  563. // name later. Since common_name is not unique, we need to add $count
  564. // to the key
  565. $key = drupal_strtolower($organism->common_name) . $count;
  566. $organisms [$key] = $organism;
  567. $count ++;
  568. }
  569. //Sort organisms by common name
  570. ksort($organisms, SORT_STRING);
  571. return $organisms;
  572. }
  573. }
  574. /**
  575. * Implements hook_views_api()
  576. * Purpose: Essentially this hook tells drupal that there is views support for
  577. * for this module which then includes tripal_db.views.inc where all the
  578. * views integration code is
  579. *
  580. * @ingroup tripal_organism
  581. */
  582. function tripal_organism_views_api() {
  583. return array(
  584. 'api' => 2.0,
  585. );
  586. }
  587. /**
  588. *
  589. *
  590. * @ingroup tripal_organism
  591. */
  592. function tripal_organism_job_describe_args($callback, $args) {
  593. $new_args = array();
  594. if ($callback == 'tripal_organism_sync_organisms') {
  595. $organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[0]));
  596. $new_args['Organism'] = $organism[0]->genus ." ". $organism[0]->species;
  597. }
  598. return $new_args;
  599. }