tripal_organism.module 18 KB

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