tripal_organism.module 21 KB

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