tripal_organism.module 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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',
  268. 'Insert Organism: Unable to create organism where values:%values',
  269. array('%values' => print_r($values, TRUE)),
  270. WATCHDOG_WARNING
  271. );
  272. return;
  273. }
  274. $organism_id = $organism['organism_id'];
  275. }
  276. else {
  277. $organism_id = $node->organism_id;
  278. }
  279. // Make sure the entry for this organism doesn't already exist in the
  280. // chado_organism table if it doesn't exist then we want to add it.
  281. if (!chado_get_id_for_node('organism', $node) ) {
  282. // next add the item to the drupal table
  283. $sql = "INSERT INTO {chado_organism} (nid, vid, organism_id) ".
  284. "VALUES (%d, %d, %d)";
  285. chado_query($sql, $node->nid, $node->vid, $organism_id);
  286. }
  287. // set the title for the node
  288. $record = new stdClass();
  289. $record->title = "$node->genus $node->species";
  290. $record->nid = $node->nid;
  291. drupal_write_record('node', $record, 'nid');
  292. drupal_write_record('node_revisions', $record, 'nid');
  293. // add the image
  294. chado_organism_add_image($node);
  295. }
  296. /**
  297. * Update organisms
  298. *
  299. * @ingroup tripal_organism
  300. */
  301. function chado_organism_update($node) {
  302. if ($node->revision) {
  303. // there is no way to handle revisions in Chado but leave
  304. // this here just to make not we've addressed it.
  305. }
  306. $match = array(
  307. 'organism_id' => chado_get_id_for_node('organism', $node),
  308. );
  309. $values = array(
  310. 'genus' => $node->genus,
  311. 'species' => $node->species,
  312. 'abbreviation' => $node->abbreviation,
  313. 'common_name' => $node->common_name,
  314. 'comment' => $node->description
  315. );
  316. $org_status = tripal_core_chado_update('organism', $match, $values);
  317. // set the title for the node
  318. $record = new stdClass();
  319. $record->title = "$node->genus $node->species";
  320. $record->nid = $node->nid;
  321. drupal_write_record('node', $record, 'nid');
  322. drupal_write_record('node_revisions', $record, 'nid');
  323. // add the image
  324. chado_organism_add_image($node);
  325. }
  326. /**
  327. * Delete organism from both drupal and chado databases. Check dependency before
  328. * deleting from chado.
  329. *
  330. * @ingroup tripal_organism
  331. */
  332. function chado_organism_delete($node) {
  333. $organism_id = chado_get_id_for_node('organism', $node);
  334. // if we don't have an organism id for this node then this isn't a node of
  335. // type chado_organism or the entry in the chado_organism table was lost.
  336. if (!$organism_id) {
  337. return;
  338. }
  339. // Remove data from the {chado_organism}, {node}, and {node_revisions} tables
  340. $sql_del = "DELETE FROM {chado_organism} ".
  341. "WHERE nid = %d ".
  342. "AND vid = %d";
  343. db_query($sql_del, $node->nid, $node->vid);
  344. $sql_del = "DELETE FROM {node} ".
  345. "WHERE nid = %d ".
  346. "AND vid = %d";
  347. db_query($sql_del, $node->nid, $node->vid);
  348. $sql_del = "DELETE FROM {node_revisions} ".
  349. "WHERE nid = %d ".
  350. "AND vid = %d";
  351. db_query($sql_del, $node->nid, $node->vid);
  352. // Test dependency before deleting from chado database. If a library or
  353. // feature depends on this organism, don't delete it
  354. $sql = "SELECT feature_id FROM {feature} WHERE organism_id = %d";
  355. $check_feature = db_result(chado_query($sql, $organism_id));
  356. $sql = "SELECT library_id FROM {library} WHERE organism_id = %d";
  357. $check_lib = db_result(chado_query($sql, $organism_id));
  358. if ($check_lib == 0 && $check_feature == 0) {
  359. tripal_core_chado_delete('organism', array('organism_id' => $organism_id));
  360. }
  361. else {
  362. drupal_set_message(t("Organism deleted from drupal. Warning: at least one ".
  363. "library or feature depends on this organism. It was ".
  364. "not removed from chado."));
  365. }
  366. }
  367. /**
  368. *
  369. *
  370. * @ingroup tripal_organism
  371. */
  372. function chado_organism_add_image($node) {
  373. // check to see if a file was uploaded. If so then copy it to the images
  374. // directory for display with the organism
  375. if (isset($_FILES['files']) && $_FILES['files']['name']['organism_image'] &&
  376. is_uploaded_file($_FILES['files']['tmp_name']['organism_image'])) {
  377. $dest = file_directory_path() . "/tripal/tripal_organism/images";
  378. $validators = array(
  379. 'file_validate_is_image' => array(),
  380. );
  381. file_check_directory($dest, FILE_CREATE_DIRECTORY, 'organism_image');
  382. if (!$file = file_save_upload('organism_image', $validators, $dest)) {
  383. drupal_set_message(t("Organism image was not uploaded."));
  384. }
  385. // move this image into the images directory
  386. file_move($file->filepath, $dest . "/" . $node->nid . ".jpg", FILE_EXISTS_REPLACE);
  387. }
  388. }
  389. /**
  390. * When editing or creating a new node of type 'chado_organism' we need
  391. * a form. This function creates the form that will be used for this.
  392. *
  393. * @ingroup tripal_organism
  394. */
  395. function chado_organism_form($node, $param) {
  396. $organism = $node->organism;
  397. // add in the comment since it is a text field and may not be included if too big
  398. $organism = tripal_core_expand_chado_vars($organism, 'field', 'organism.comment');
  399. // get form defaults
  400. $abbreviation = $node->abbreviation;
  401. if (!$abbreviation) {
  402. $abbreviation = $organism->abbreviation;
  403. }
  404. $genus = $node->genus;
  405. if (!$genus) {
  406. $genus = $organism->genus;
  407. }
  408. $species = $node->species;
  409. if (!$species) {
  410. $species = $organism->species;
  411. }
  412. $common_name = $node->common_name;
  413. if (!$common_name) {
  414. $common_name = $organism->common_name;
  415. }
  416. $description = $node->description;
  417. if (!$description) {
  418. $description = $organism->comment;
  419. }
  420. $organism_image = $node->organism_image;
  421. $form = array();
  422. $form['#attributes']['enctype'] = 'multipart/form-data';
  423. // keep track of the organism id if we have one. If we do have one then
  424. // this would indicate an update as opposed to an insert.
  425. $form['organism_id'] = array(
  426. '#type' => 'value',
  427. '#value' => $organism->organism_id,
  428. );
  429. $form['abbreviation']= array(
  430. '#type' => 'textfield',
  431. '#title' => t('Abbreviation'),
  432. '#required' => TRUE,
  433. '#default_value' => $organism->abbreviation,
  434. '#weight' => 3
  435. );
  436. $form['genus']= array(
  437. '#type' => 'textfield',
  438. '#title' => t('Genus'),
  439. '#required' => TRUE,
  440. '#default_value' => $organism->genus,
  441. '#weight' => 1
  442. );
  443. $form['species']= array(
  444. '#type' => 'textfield',
  445. '#title' => t('Species'),
  446. '#required' => TRUE,
  447. '#default_value' => $organism->species,
  448. '#weight' => 2
  449. );
  450. $form['common_name']= array(
  451. '#type' => 'textfield',
  452. '#title' => t('Common Name'),
  453. '#required' => TRUE,
  454. '#default_value' => $organism->common_name,
  455. '#weight' => 4
  456. );
  457. $form['description']= array(
  458. '#type' => 'textarea',
  459. '#rows' => 15,
  460. '#title' => t('Description'),
  461. '#required' => TRUE,
  462. '#default_value' => $organism->comment,
  463. '#weight' => 5
  464. );
  465. $form['organism_image']= array(
  466. '#type' => 'file',
  467. '#title' => t('Organism Image'),
  468. '#description' => 'Add an image for this organism',
  469. '#weight' => 6
  470. );
  471. return $form;
  472. }
  473. /**
  474. * When a node is requested by the user this function is called to allow us
  475. * to add auxiliary data to the node object.
  476. *
  477. * @ingroup tripal_organism
  478. */
  479. function chado_organism_load($node) {
  480. // find the organism and add in the details
  481. $organism_id = chado_get_id_for_node('organism', $node);
  482. $values = array('organism_id' => $organism_id);
  483. $organism = tripal_core_generate_chado_var('organism', $values);
  484. // add in the description field
  485. $organism = tripal_core_expand_chado_vars($organism, 'field', 'organism.comment');
  486. $additions = new stdClass();
  487. $additions->organism = $organism;
  488. return $additions;
  489. }
  490. /**
  491. * This function customizes the view of the chado_organism node. It allows
  492. * us to generate the markup.
  493. *
  494. * @ingroup tripal_organism
  495. */
  496. function chado_organism_view($node, $teaser = FALSE, $page = FALSE) {
  497. // use drupal's default node view:
  498. $node = node_prepare($node, $teaser);
  499. return $node;
  500. }
  501. /**
  502. * Display help and module information
  503. * @param path which path of the site we're displaying help
  504. * @param arg array that holds the current path as would be returned from arg()
  505. * function
  506. * @return help text for the path
  507. *
  508. * @ingroup tripal_organism
  509. */
  510. function tripal_organism_help($path, $arg) {
  511. $output = '';
  512. switch ($path) {
  513. case "admin/help#tripal_organism":
  514. $output = '<p>'.
  515. t("Displays links to nodes created on this date") .
  516. '</p>';
  517. break;
  518. }
  519. return $output;
  520. }
  521. /**
  522. * This function uses organism_id's of all drupal organism nodes as input and
  523. * pull the organism information (genus, species, common_name, comment) from
  524. * chado database. The return type is an object array that stores $organism
  525. * objects sorted by common_name
  526. *
  527. * @ingroup tripal_organism
  528. */
  529. function get_chado_organisms() {
  530. $sql_drupal = "SELECT COUNT (organism_id) FROM {chado_organism}";
  531. $no_orgs = db_result(db_query($sql_drupal));
  532. if ($no_orgs != 0) {
  533. $sql = "SELECT organism_id, nid FROM {chado_organism}";
  534. $result = chado_query($sql);
  535. $sql = "SELECT genus, species, common_name, comment ".
  536. "FROM {Organism} ".
  537. "WHERE organism_id=%d";
  538. $organisms = array();
  539. $count = 0;
  540. while ($data = db_fetch_object($result)) {
  541. $organism = db_fetch_object(chado_query($sql, $data->organism_id));
  542. $organism->node_id = $data->nid;
  543. // Use common_name plus $count as the key so we can sort by common
  544. // name later. Since common_name is not unique, we need to add $count
  545. // to the key
  546. $key = drupal_strtolower($organism->common_name) . $count;
  547. $organisms [$key] = $organism;
  548. $count ++;
  549. }
  550. //Sort organisms by common name
  551. ksort($organisms, SORT_STRING);
  552. return $organisms;
  553. }
  554. }
  555. /**
  556. * Implements hook_views_api()
  557. * Purpose: Essentially this hook tells drupal that there is views support for
  558. * for this module which then includes tripal_db.views.inc where all the
  559. * views integration code is
  560. *
  561. * @ingroup tripal_organism
  562. */
  563. function tripal_organism_views_api() {
  564. return array(
  565. 'api' => 2.0,
  566. );
  567. }
  568. /**
  569. *
  570. *
  571. * @ingroup tripal_organism
  572. */
  573. function tripal_organism_job_describe_args($callback, $args) {
  574. $new_args = array();
  575. if ($callback == 'tripal_organism_sync_organisms') {
  576. $organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[0]));
  577. $new_args['Organism'] = $organism[0]->genus ." ". $organism[0]->species;
  578. }
  579. return $new_args;
  580. }