tripal_organism.module 19 KB

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