tripal_organism.module 18 KB

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