tripal_organism.module 19 KB

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