tripal_organism.module 18 KB

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