tripal_organism.module 18 KB

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