tripal_organism.chado_node.inc 19 KB

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