tripal_organism.chado_node.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. <?php
  2. /**
  3. * @file
  4. * Implements the organims node content type
  5. */
  6. /**
  7. * Implements hook_node_info().
  8. *
  9. * Provide information to drupal about the node types that we're creating
  10. * in this module
  11. *
  12. * @ingroup tripal_organism
  13. */
  14. function tripal_organism_node_info() {
  15. $nodes = array();
  16. $nodes['chado_organism'] = array(
  17. 'name' => t('Organism'),
  18. 'base' => 'chado_organism',
  19. 'description' => t('An organism'),
  20. 'has_title' => TRUE,
  21. 'locked' => TRUE,
  22. 'chado_node_api' => array(
  23. 'base_table' => 'organism',
  24. 'hook_prefix' => 'chado_organism',
  25. 'record_type_title' => array(
  26. 'singular' => t('Organism'),
  27. 'plural' => t('Organisms')
  28. ),
  29. 'sync_filters' => array(
  30. 'type_id' => FALSE,
  31. 'organism_id' => FALSE,
  32. 'checkboxes' => array('genus', 'species'),
  33. ),
  34. )
  35. );
  36. return $nodes;
  37. }
  38. /**
  39. * Implement hook_node_access().
  40. *
  41. * This hook allows node modules to limit access to the node types they define.
  42. *
  43. * @param $node
  44. * The node on which the operation is to be performed, or, if it does not yet exist, the
  45. * type of node to be created
  46. *
  47. * @param $op
  48. * The operation to be performed
  49. *
  50. *
  51. * @param $account
  52. * A user object representing the user for whom the operation is to be performed
  53. *
  54. * @return
  55. * If the permission for the specified operation is not set then return FALSE. If the
  56. * permission is set then return NULL as this allows other modules to disable
  57. * access. The only exception is when the $op == 'create'. We will always
  58. * return TRUE if the permission is set.
  59. *
  60. * @ingroup tripal_organism
  61. */
  62. function chado_organism_node_access($node, $op, $account) {
  63. $node_type = $node;
  64. if (is_object($node)) {
  65. $node_type = $node->type;
  66. }
  67. if($node_type == 'chado_organism') {
  68. if ($op == 'create') {
  69. if (!user_access('create chado_organism', $account)) {
  70. return NODE_ACCESS_DENY;
  71. }
  72. return NODE_ACCESS_ALLOW;
  73. }
  74. if ($op == 'update') {
  75. if (!user_access('edit chado_organism', $account)) {
  76. return NODE_ACCESS_DENY;
  77. }
  78. }
  79. if ($op == 'delete') {
  80. if (!user_access('delete chado_organism', $account)) {
  81. return NODE_ACCESS_DENY;
  82. }
  83. }
  84. if ($op == 'view') {
  85. if (!user_access('access chado_organism', $account)) {
  86. return NODE_ACCESS_DENY;
  87. }
  88. }
  89. return NODE_ACCESS_IGNORE;
  90. }
  91. }
  92. /**
  93. * Implement hook_form().
  94. *
  95. * When editing or creating a new node of type 'chado_organism' we need
  96. * a form. This function creates the form that will be used for this.
  97. *
  98. * @ingroup tripal_organism
  99. */
  100. function chado_organism_form($node, $form_state) {
  101. $form = array();
  102. // we have a file upload element on the form soe we need the multipart encoding type
  103. $form['#attributes']['enctype'] = 'multipart/form-data';
  104. // if the organism is part of the node object then we are editing. If not we are inserting
  105. if (property_exists($node, 'organism')) {
  106. $organism = $node->organism;
  107. // add in the comment since it is a text field and may not be included if too big
  108. $organism = chado_expand_var($organism, 'field', 'organism.comment');
  109. // get form defaults
  110. $abbreviation = property_exists($node, 'abbreviation') ? property_exists($node, 'abbreviation') : $organism->abbreviation;
  111. $genus = property_exists($node, 'genus') ? property_exists($node, 'genus') : $organism->genus;
  112. $species = property_exists($node, 'species') ? property_exists($node, 'species') : $organism->species;
  113. $common_name = property_exists($node, 'common_name') ? property_exists($node, 'common_name') : $organism->common_name;
  114. $description = property_exists($node, 'description') ? property_exists($node, 'description') : $organism->comment;
  115. $organism_image = property_exists($node, 'organism_image') ? property_exists($node, 'organism_image') : '';
  116. // set the organism_id in the form
  117. $form['organism_id'] = array(
  118. '#type' => 'value',
  119. '#value' => $organism->organism_id,
  120. );
  121. $organism_id = $organism->organism_id;
  122. }
  123. else {
  124. // get form defaults
  125. $abbreviation = property_exists($node, 'abbreviation') ? property_exists($node, 'abbreviation') : '';
  126. $genus = property_exists($node, 'genus') ? property_exists($node, 'genus') : '';
  127. $species = property_exists($node, 'species') ? property_exists($node, 'species') : '';
  128. $common_name = property_exists($node, 'common_name') ? property_exists($node, 'common_name') : '';
  129. $description = property_exists($node, 'description') ? property_exists($node, 'description') : '';
  130. $organism_image = property_exists($node, 'organism_image') ? property_exists($node, 'organism_image') : '';
  131. $organism_id = NULL;
  132. }
  133. $form['genus']= array(
  134. '#type' => 'textfield',
  135. '#title' => t('Genus'),
  136. '#required' => TRUE,
  137. '#default_value' => $genus,
  138. );
  139. $form['species']= array(
  140. '#type' => 'textfield',
  141. '#title' => t('Species'),
  142. '#required' => TRUE,
  143. '#default_value' => $species,
  144. );
  145. $form['abbreviation']= array(
  146. '#type' => 'textfield',
  147. '#title' => t('Abbreviation'),
  148. '#required' => TRUE,
  149. '#default_value' => $abbreviation,
  150. );
  151. $form['common_name']= array(
  152. '#type' => 'textfield',
  153. '#title' => t('Common Name'),
  154. '#required' => TRUE,
  155. '#default_value' => $common_name,
  156. );
  157. $form['description']= array(
  158. '#type' => 'textarea',
  159. '#rows' => 15,
  160. '#title' => t('Description'),
  161. '#default_value' => $description,
  162. );
  163. $form['organism_image']= array(
  164. '#type' => 'file',
  165. '#title' => t('Organism Image'),
  166. '#description' => 'Add an image for this organism',
  167. '#progress_indicator' => 'bar',
  168. );
  169. // PROPERTIES FORM
  170. //---------------------------------------------
  171. $details = array(
  172. 'property_table' => 'organismprop', // the name of the prop table
  173. 'chado_id' => $organism_id, // the value of organism_id for this record
  174. 'cv_name' => 'organism_property' // the cv.name of the cv governing organismprop.type_id
  175. );
  176. // Adds the form elements to your current form
  177. chado_add_node_form_properties($form, $form_state, $details);
  178. // ADDITIONAL DBXREFS FORM
  179. //---------------------------------------------
  180. $details = array(
  181. 'linking_table' => 'organism_dbxref', // the name of the _dbxref table
  182. 'base_foreign_key' => 'organism_id', // the name of the key in your base chado table
  183. 'base_key_value' => $organism_id // the value of organism_id for this record
  184. );
  185. // Adds the form elements to your current form
  186. chado_add_node_form_dbxrefs($form, $form_state, $details);
  187. return $form;
  188. }
  189. /**
  190. * Implementation of hook_validate().
  191. *
  192. * @param $node
  193. * @param $form
  194. * @param $form_state
  195. *
  196. * @ingroup tripal_organism
  197. */
  198. function chado_organism_validate($node, $form, &$form_state) {
  199. // remove any white space around values
  200. $node->genus = trim($node->genus);
  201. $node->species = trim($node->species);
  202. $node->abbreviation = trim($node->abbreviation);
  203. $node->common_name = trim($node->common_name);
  204. $node->description = trim($node->description);
  205. // if this is a delete then don't validate
  206. if($node->op == 'Delete') {
  207. return;
  208. }
  209. // we are syncing if we do not have a node ID but we do have a organism_id. We don't
  210. // need to validate during syncing so just skip it.
  211. if (is_null($node->nid) and property_exists($node, 'organism_id') and $node->organism_id != 0) {
  212. return;
  213. }
  214. // Validating for an update
  215. if (property_exists($node, 'organism_id')) {
  216. $sql = "
  217. SELECT *
  218. FROM {organism} O
  219. WHERE
  220. genus = :genus AND
  221. species = :species AND NOT
  222. organism_id = :organism_id
  223. ";
  224. $args = array(':genus' => $node->genus, ':species' => $node->species, ':organism_id' => $node->organism_id);
  225. $result = chado_query($sql, $args)->fetchObject();
  226. if ($result) {
  227. form_set_error('genus', t("Update cannot proceed. The organism genus
  228. '$node->genus' and species '$node->species' is already present in the database."));
  229. tripal_report_error('tripal_organism', TRIPAL_WARNING,
  230. 'Update organism: genus and species already exists: %values',
  231. array('%values' => "genus = $node->genus, species = $node->species"));
  232. }
  233. }
  234. // Validating for an insert
  235. else {
  236. $values = array(
  237. 'genus' => $node->genus,
  238. 'species' => $node->species,
  239. );
  240. $organism = chado_select_record('organism', array('organism_id'), $values);
  241. if (sizeof($organism) > 0) {
  242. form_set_error('genus', 'Cannot add the organism with this genus and species.
  243. The organism already exists.');
  244. tripal_report_error('tripal_organism', TRIPAL_WARNING,
  245. 'Insert organism: genus and species already exists: %values',
  246. array('%values' => "genus = $node->genus, species = $node->species"));
  247. }
  248. }
  249. }
  250. /**
  251. * Implements hook_insert().
  252. *
  253. * When a new chado_organism node is created we also need to add information
  254. * to our chado_organism table. This function is called on insert of a new node
  255. * of type 'chado_organism' and inserts the necessary information.
  256. *
  257. * @ingroup tripal_organism
  258. */
  259. function chado_organism_insert($node) {
  260. // remove any white space around values
  261. $node->genus = trim($node->genus);
  262. $node->species = trim($node->species);
  263. $node->abbreviation = trim($node->abbreviation);
  264. $node->common_name = trim($node->common_name);
  265. $node->description = trim($node->description);
  266. // if there is an organism_id in the $node object then this must be a sync so
  267. // we can skip adding the organism as it is already there, although
  268. // we do need to proceed with the rest of the insert
  269. if (!property_exists($node,'organism_id')) {
  270. $values = array(
  271. 'genus' => $node->genus,
  272. 'species' => $node->species,
  273. 'abbreviation' => $node->abbreviation,
  274. 'common_name' => $node->common_name,
  275. 'comment' => $node->description
  276. );
  277. $organism = chado_insert_record('organism', $values);
  278. if (!$organism) {
  279. drupal_set_message(t('Unable to add organism.', 'warning'));
  280. tripal_report_error('tripal_organism', TRIPAL_ERROR, 'Insert Organism: Unable to create organism where values:%values',
  281. array('%values' => print_r($values, TRUE)));
  282. return;
  283. }
  284. $organism_id = $organism['organism_id'];
  285. if ($organism_id) {
  286. // * Properties Form *
  287. $details = array(
  288. 'property_table' => 'organismprop', // the name of the prop table
  289. 'base_table' => 'organism', // the name of your chado base table
  290. 'foreignkey_name' => 'organism_id', // the name of the key in your base table
  291. 'foreignkey_value' => $organism_id // the value of the example_id key
  292. );
  293. chado_update_node_form_properties($node, $details);
  294. // * Additional DBxrefs Form *
  295. $details = array(
  296. 'linking_table' => 'organism_dbxref', // the name of your _dbxref table
  297. 'foreignkey_name' => 'organism_id', // the name of the key in your base table
  298. 'foreignkey_value' => $organism_id // the value of the organism_id key
  299. );
  300. chado_update_node_form_dbxrefs($node, $details);
  301. }
  302. }
  303. else {
  304. $organism_id = $node->organism_id;
  305. }
  306. // Make sure the entry for this organism doesn't already exist in the
  307. // chado_organism table if it doesn't exist then we want to add it.
  308. $check_org_id = chado_get_id_from_nid('organism', $node->nid);
  309. if (!$check_org_id) {
  310. $record = new stdClass();
  311. $record->nid = $node->nid;
  312. $record->vid = $node->vid;
  313. $record->organism_id = $organism_id;
  314. drupal_write_record('chado_organism', $record);
  315. }
  316. // add the image
  317. chado_organism_add_image($node);
  318. }
  319. /**
  320. * Implements hook_update().
  321. *
  322. * @ingroup tripal_organism
  323. */
  324. function chado_organism_update($node) {
  325. // remove any white space around values
  326. $node->genus = trim($node->genus);
  327. $node->species = trim($node->species);
  328. $node->abbreviation = trim($node->abbreviation);
  329. $node->common_name = trim($node->common_name);
  330. $node->description = trim($node->description);
  331. $organism_id = chado_get_id_from_nid('organism', $node->nid);
  332. if ($node->revision) {
  333. // there is no way to handle revisions in Chado but leave
  334. // this here just to make not we've addressed it.
  335. }
  336. $match = array(
  337. 'organism_id' => $organism_id,
  338. );
  339. $values = array(
  340. 'genus' => $node->genus,
  341. 'species' => $node->species,
  342. 'abbreviation' => $node->abbreviation,
  343. 'common_name' => $node->common_name,
  344. 'comment' => $node->description
  345. );
  346. $org_status = chado_update_record('organism', $match, $values);
  347. // add the image
  348. chado_organism_add_image($node);
  349. // * Properties Form *
  350. $details = array(
  351. 'property_table' => 'organismprop', // the name of the prop table
  352. 'base_table' => 'organism', // the name of your chado base table
  353. 'foreignkey_name' => 'organism_id', // the name of the key in your base table
  354. 'foreignkey_value' => $organism_id // the value of the example_id key
  355. );
  356. chado_update_node_form_properties($node, $details);
  357. // * Additional DBxrefs Form *
  358. $details = array(
  359. 'linking_table' => 'organism_dbxref', // the name of your _dbxref table
  360. 'foreignkey_name' => 'organism_id', // the name of the key in your base table
  361. 'foreignkey_value' => $organism_id // the value of the organism_id key
  362. );
  363. chado_update_node_form_dbxrefs($node, $details);
  364. }
  365. /**
  366. * Implements hook_delete().
  367. *
  368. * Delete organism from both drupal and chado databases. Check dependency before
  369. * deleting from chado.
  370. *
  371. * @ingroup tripal_organism
  372. */
  373. function chado_organism_delete($node) {
  374. $organism_id = chado_get_id_from_nid('organism', $node->nid);
  375. // if we don't have an organism id for this node then this isn't a node of
  376. // type chado_organism or the entry in the chado_organism table was lost.
  377. if (!$organism_id) {
  378. return;
  379. }
  380. // Remove data from the {chado_organism}, {node}, and {node_revisions} tables
  381. $sql_del = "DELETE FROM {chado_organism} WHERE nid = :nid AND vid = :vid";
  382. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  383. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  384. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  385. $sql_del = "DELETE FROM {node_revision} WHERE nid = ':nid' AND vid = ':vid'";
  386. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  387. // Test dependency before deleting from chado database. If a library or
  388. // feature depends on this organism, don't delete it
  389. $sql = "SELECT feature_id FROM {feature} WHERE organism_id = :organism_id";
  390. $check_feature = chado_query($sql, array(':organism_id' => $organism_id))->fetchObject();
  391. $sql = "SELECT library_id FROM {library} WHERE organism_id = :organism_id";
  392. $check_lib = chado_query($sql, array(':organism_id' => $organism_id))->fetchObject();
  393. $sql = "SELECT stock_id FROM {stock} WHERE organism_id = :organism_id";
  394. $check_stock = chado_query($sql, array(':organism_id' => $organism_id))->fetchObject();
  395. if (!$check_lib && !$check_feature && !$check_stock) {
  396. chado_delete_record('organism', array('organism_id' => $organism_id));
  397. }
  398. else {
  399. drupal_set_message(t("Warning: other data depends on this organism. The organism page was removed from this site but the organism was removed from Chado."), 'warning');
  400. }
  401. }
  402. /**
  403. * Add an image to an organims node
  404. *
  405. * @param $node
  406. * The node to add an image to
  407. *
  408. * The file is specified in the $_FILES array created by Drupal
  409. *
  410. * @ingroup tripal_organism
  411. */
  412. function chado_organism_add_image($node) {
  413. // check to see if a file was uploaded. If so then copy it to the images
  414. // directory for display with the organism
  415. if (isset($_FILES['files']) &&
  416. $_FILES['files']['name']['organism_image'] &&
  417. is_uploaded_file($_FILES['files']['tmp_name']['organism_image'])) {
  418. // make sure the destination directory exists
  419. $dest = tripal_get_files_dir() . "/tripal_organism/images";
  420. file_prepare_directory($dest, FILE_CREATE_DIRECTORY);
  421. // now move the file
  422. $validators = array('file_validate_is_image' => array());
  423. $destination = "public://tripal/tripal_organism/images/";
  424. $file = file_save_upload('organism_image', $validators, $destination);
  425. if (!$file) {
  426. drupal_set_message(t("Organism image was not uploaded."));
  427. }
  428. else {
  429. file_move($file, $destination . "/" . $node->nid . ".jpg", FILE_EXISTS_REPLACE);
  430. }
  431. }
  432. }
  433. /**
  434. * Implements hook_load().
  435. *
  436. * When a node is requested by the user this function is called to allow us
  437. * to add auxiliary data to the node object.
  438. *
  439. * @ingroup tripal_organism
  440. */
  441. function chado_organism_load($nodes) {
  442. foreach ($nodes as $nid => $node) {
  443. // find the organism and add in the details
  444. $organism_id = chado_get_id_from_nid('organism', $nid);
  445. // build the organism variable
  446. $values = array('organism_id' => $organism_id);
  447. $organism = chado_generate_var('organism', $values);
  448. // add in the description field
  449. $organism = chado_expand_var($organism, 'field', 'organism.comment');
  450. $nodes[$nid]->organism = $organism;
  451. }
  452. }
  453. /**
  454. * Implements hook_node_presave(). Acts on all content types.
  455. *
  456. * @param $node
  457. * The node to be saved
  458. *
  459. * @ingroup tripal_organism
  460. */
  461. function tripal_organism_node_presave($node) {
  462. switch ($node->type) {
  463. case 'chado_organism':
  464. // for a form submission the 'genus' field will be set,
  465. // for a sync, we must pull from the organism object
  466. if(property_exists($node, 'genus')) {
  467. // set the title
  468. $node->title = $node->genus . " " . $node->species;
  469. }
  470. else {
  471. // set the title
  472. $node->title = $node->organism->genus . " " . $node->organism->species;
  473. }
  474. break;
  475. }
  476. }
  477. /**
  478. * Implements hook_node_view().
  479. *
  480. * @ingroup tripal_organism
  481. */
  482. function tripal_organism_node_view($node, $view_mode, $langcode) {
  483. switch ($node->type) {
  484. case 'chado_organism':
  485. // Show feature browser and counts
  486. if ($view_mode == 'full') {
  487. $node->content['tripal_organism_base'] = array(
  488. '#markup' => theme('tripal_organism_base', array('node' => $node)),
  489. '#tripal_toc_id' => 'base',
  490. '#tripal_toc_title' => 'Overview',
  491. '#weight' => -100,
  492. );
  493. $node->content['tripal_organism_properties'] = array(
  494. '#markup' => theme('tripal_organism_properties', array('node' => $node)),
  495. '#tripal_toc_id' => 'properties',
  496. '#tripal_toc_title' => 'Properties',
  497. );
  498. $node->content['tripal_organism_references'] = array(
  499. '#markup' => theme('tripal_organism_references', array('node' => $node)),
  500. '#tripal_toc_id' => 'references',
  501. '#tripal_toc_title' => 'Cross References',
  502. );
  503. }
  504. if ($view_mode == 'teaser') {
  505. $node->content['tripal_organism_teaser'] = array(
  506. '#markup' => theme('tripal_organism_teaser', array('node' => $node)),
  507. );
  508. }
  509. break;
  510. }
  511. }