tripal_organism.chado_node.inc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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_legacy_organism
  13. */
  14. function tripal_organism_node_info() {
  15. $nodes = [];
  16. $nodes['chado_organism'] = [
  17. 'name' => t('Organism (Tripal v2 legacy)'),
  18. 'base' => 'chado_organism',
  19. 'description' => t('An organism'),
  20. 'has_title' => TRUE,
  21. 'locked' => TRUE,
  22. 'chado_node_api' => [
  23. 'base_table' => 'organism',
  24. 'hook_prefix' => 'chado_organism',
  25. 'record_type_title' => [
  26. 'singular' => t('Organism'),
  27. 'plural' => t('Organisms'),
  28. ],
  29. 'sync_filters' => [
  30. 'type_id' => FALSE,
  31. 'organism_id' => FALSE,
  32. 'checkboxes' => ['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
  45. * exist, the 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
  53. * performed
  54. *
  55. * @return
  56. * If the permission for the specified operation is not set then return FALSE.
  57. * If the permission is set then return NULL as this allows other modules to
  58. * disable access. The only exception is when the $op == 'create'. We will
  59. * always return TRUE if the permission is set.
  60. *
  61. * @ingroup tripal_legacy_organism
  62. */
  63. function tripal_organism_node_access($node, $op, $account) {
  64. $node_type = $node;
  65. if (is_object($node)) {
  66. $node_type = $node->type;
  67. }
  68. if ($node_type == 'chado_organism') {
  69. if ($op == 'create') {
  70. if (!user_access('create chado_organism content', $account)) {
  71. return NODE_ACCESS_DENY;
  72. }
  73. return NODE_ACCESS_ALLOW;
  74. }
  75. if ($op == 'update') {
  76. if (!user_access('edit chado_organism content', $account)) {
  77. return NODE_ACCESS_DENY;
  78. }
  79. }
  80. if ($op == 'delete') {
  81. if (!user_access('delete chado_organism content', $account)) {
  82. return NODE_ACCESS_DENY;
  83. }
  84. }
  85. if ($op == 'view') {
  86. if (!user_access('access chado_organism content', $account)) {
  87. return NODE_ACCESS_DENY;
  88. }
  89. }
  90. return NODE_ACCESS_IGNORE;
  91. }
  92. }
  93. /**
  94. * Implement hook_form().
  95. *
  96. * When editing or creating a new node of type 'chado_organism' we need
  97. * a form. This function creates the form that will be used for this.
  98. *
  99. * @ingroup tripal_legacy_organism
  100. */
  101. function chado_organism_form($node, $form_state) {
  102. $form = [];
  103. $chado_version = chado_get_version(TRUE);
  104. // Default values can come in the following ways:
  105. //
  106. // 1) As elements of the $node object. This occurs when editing an existing
  107. // organism.
  108. // 2) In the $form_state['values'] array which occurs on a failed validation
  109. // or ajax callbacks from non submit form elements
  110. // 3) In the $form_state['input'[ array which occurs on ajax callbacks from
  111. // submit form elements and the form is being rebuilt
  112. //
  113. // Set form field defaults.
  114. $organism = NULL;
  115. $organism_id = NULL;
  116. $abbreviation = '';
  117. $genus = '';
  118. $species = '';
  119. $common_name = '';
  120. $description = '';
  121. $infraspecific_name = '';
  122. $type_id = '';
  123. // We have a file upload element on the form soe we need the multipart
  124. // encoding type
  125. $form['#attributes']['enctype'] = 'multipart/form-data';
  126. // If the organism is part of the node object then we are editing. If not
  127. // we are inserting
  128. if (property_exists($node, 'organism')) {
  129. $organism = $node->organism;
  130. // Add in the comment since it is a text field and may not be included if
  131. // too big
  132. $organism = chado_expand_var($organism, 'field', 'organism.comment');
  133. // Get form defaults.
  134. $abbreviation = $organism->abbreviation;
  135. $genus = $organism->genus;
  136. $species = $organism->species;
  137. $common_name = $organism->common_name;
  138. $description = $organism->comment;
  139. // The infraspecific and type_id fields are new to Chado v1.3
  140. if ($chado_version > 1.2) {
  141. $infraspecific_name = $organism->infraspecific_name;
  142. $type_id = $organism->type_id->cvterm_id;
  143. }
  144. // Set the organism_id in the form.
  145. $form['organism_id'] = [
  146. '#type' => 'value',
  147. '#value' => $organism->organism_id,
  148. ];
  149. $organism_id = $organism->organism_id;
  150. }
  151. // If we are re constructing the form from a failed validation or ajax
  152. // callback then use the $form_state['values'] values.
  153. if (array_key_exists('values', $form_state) and isset($form_state['values']['genus'])) {
  154. $abbreviation = $form_state['values']['abbreviation'];
  155. $genus = $form_state['values']['genus'];
  156. $species = $form_state['values']['species'];
  157. $common_name = $form_state['values']['common_name'];
  158. $description = $form_state['values']['comment'];
  159. if ($chado_version > 1.2) {
  160. $infraspecific_name = $form_state['values']['infraspecific_name'];
  161. $type_id = $form_state['values']['type_id'];
  162. }
  163. }
  164. // If we are re building the form from after submission (from ajax call) then
  165. // the values are in the $form_state['input'] array.
  166. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  167. $abbreviation = $form_state['input']['abbreviation'];
  168. $genus = $form_state['input']['genus'];
  169. $species = $form_state['input']['species'];
  170. $common_name = $form_state['input']['common_name'];
  171. $description = $form_state['input']['comment'];
  172. if ($chado_version > 1.2) {
  173. $infraspecific_name = $form_state['input']['infraspecific_name'];
  174. $type_id = $form_state['input']['type_id'];
  175. }
  176. }
  177. $form['genus'] = [
  178. '#type' => 'textfield',
  179. '#title' => t('Genus'),
  180. '#required' => TRUE,
  181. '#default_value' => $genus,
  182. ];
  183. $form['species'] = [
  184. '#type' => 'textfield',
  185. '#title' => t('Species'),
  186. '#required' => TRUE,
  187. '#default_value' => $species,
  188. ];
  189. // The infraspecific and type_id fields are new to Chado v1.3.
  190. if ($chado_version > 1.2) {
  191. $options = ['0' => 'Select a rank'];
  192. $cv = tripal_get_cv(['name' => 'taxonomic_rank']);
  193. if (!$cv) {
  194. drupal_set_message('The taxonomic_rank vocabulary cannot be found, thus selects for "rank" are not available.', 'warning');
  195. }
  196. else {
  197. $terms = tripal_get_cvterm_select_options($cv->cv_id);
  198. // Unfortunately the taxonomic_rank vocabulary is not properly organized
  199. // such that we only include terms below 'species'. Therefore we will
  200. // just list them here and hope we haven't missed one.
  201. $valid_terms = [
  202. 'subspecies',
  203. 'varietas',
  204. 'subvariety',
  205. 'forma',
  206. 'subforma',
  207. ];
  208. foreach ($terms as $cvterm_id => $name) {
  209. if (in_array($name, $valid_terms)) {
  210. $options[$cvterm_id] = $name;
  211. }
  212. }
  213. }
  214. $form['type_id'] = [
  215. '#type' => 'select',
  216. '#title' => t('Infraspecific Rank'),
  217. '#options' => $options,
  218. '#default_value' => $type_id,
  219. '#description' => t('The scientific name for any taxon
  220. below the rank of species. This field is used for constructing the
  221. full infraspecific name for the organism.'),
  222. ];
  223. $form['infraspecific_name'] = [
  224. '#type' => 'textfield',
  225. '#title' => t('Infraspecific Name'),
  226. '#default_value' => $infraspecific_name,
  227. '#description' => t("The infraspecific name for this organism. When
  228. diplaying the full taxonomic name, this field is appended to the
  229. genus, species and rank."),
  230. ];
  231. }
  232. $form['abbreviation'] = [
  233. '#type' => 'textfield',
  234. '#title' => t('Abbreviation'),
  235. '#default_value' => $abbreviation,
  236. '#descriptoin' => t('A short abbreviation for this species (e.g. O.sativa)'),
  237. ];
  238. $form['common_name'] = [
  239. '#type' => 'textfield',
  240. '#title' => t('Common Name'),
  241. '#default_value' => $common_name,
  242. ];
  243. $form['description'] = [
  244. '#type' => 'text_format',
  245. '#rows' => 15,
  246. '#title' => t('Description'),
  247. '#default_value' => $description,
  248. ];
  249. $form['organism_image'] = [
  250. '#type' => 'managed_file',
  251. '#title' => t('Organism Image'),
  252. '#description' => t('Add an image to display for this organism.'),
  253. '#progress_indicator' => 'bar',
  254. '#upload_location' => 'public://tripal/tripal_organism/images/',
  255. ];
  256. // PROPERTIES FORM
  257. //---------------------------------------------
  258. $prop_cv = tripal_get_default_cv('organismprop', 'type_id');
  259. $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
  260. $details = [
  261. 'property_table' => 'organismprop',
  262. 'chado_id' => $organism_id,
  263. 'cv_id' => $cv_id,
  264. ];
  265. // Adds the form elements to your current form
  266. chado_add_node_form_properties($form, $form_state, $details);
  267. // ADDITIONAL DBXREFS FORM
  268. //---------------------------------------------
  269. $details = [
  270. 'linking_table' => 'organism_dbxref',
  271. 'base_foreign_key' => 'organism_id',
  272. 'base_key_value' => $organism_id,
  273. ];
  274. // Adds the form elements to your current form.
  275. chado_add_node_form_dbxrefs($form, $form_state, $details);
  276. return $form;
  277. }
  278. /**
  279. * Implementation of hook_validate().
  280. *
  281. * @param $node
  282. * @param $form
  283. * @param $form_state
  284. *
  285. * @ingroup tripal_legacy_organism
  286. */
  287. function chado_organism_validate($node, $form, &$form_state) {
  288. // We only want to validate when the node is saved.
  289. // Since this validate can be called on AJAX and Deletion of the node
  290. // we need to make this check to ensure queries are not executed
  291. // without the proper values.
  292. if (property_exists($node, "op") and $node->op != 'Save') {
  293. return;
  294. }
  295. // we are syncing if we do not have a node ID but we do have a organism_id. We don't
  296. // need to validate during syncing so just skip it.
  297. if (!property_exists($node, 'nid') and property_exists($node, 'organism_id') and $node->organism_id != 0) {
  298. return;
  299. }
  300. // remove any white space around values
  301. $node->genus = property_exists($node, 'genus') ? trim($node->genus) : '';
  302. $node->species = property_exists($node, 'species') ? trim($node->species) : '';
  303. $node->abbreviation = property_exists($node, 'abbreviation') ? trim($node->abbreviation) : '';
  304. $node->common_name = property_exists($node, 'common_name') ? trim($node->common_name) : '';
  305. $node->type_id = property_exists($node, 'type_id') ? trim($node->type_id) : '';
  306. $node->infraspecific_name = property_exists($node, 'infraspecific_name') ? trim($node->infraspecific_name) : '';
  307. if ($node->type_id and !$node->infraspecific_name) {
  308. form_set_error('infraspecific_name', "If a rank is provided an infraspecific name must also be provided.");
  309. }
  310. if (!$node->type_id and $node->infraspecific_name) {
  311. form_set_error('type_id', "Please provide a rank for the infraspecific name.");
  312. }
  313. // Validating for an update
  314. if (property_exists($node, 'organism_id')) {
  315. $sql = "
  316. SELECT *
  317. FROM {organism} O
  318. WHERE
  319. genus = :genus AND
  320. species = :species AND NOT
  321. organism_id = :organism_id
  322. ";
  323. $args = [
  324. ':genus' => $node->genus,
  325. ':species' => $node->species,
  326. ':organism_id' => $node->organism_id,
  327. ];
  328. $result = chado_query($sql, $args)->fetchObject();
  329. if ($result) {
  330. form_set_error('genus', t("Update cannot proceed. The organism genus
  331. '$node->genus' and species '$node->species' is already present in the database."));
  332. tripal_report_error('tripal_organism', TRIPAL_WARNING,
  333. 'Update organism: genus and species already exists: %values',
  334. ['%values' => "genus = $node->genus, species = $node->species"]);
  335. }
  336. }
  337. // Validating for an insert
  338. else {
  339. $values = [
  340. 'genus' => $node->genus,
  341. 'species' => $node->species,
  342. ];
  343. $organism = chado_select_record('organism', ['organism_id'], $values);
  344. if (sizeof($organism) > 0) {
  345. form_set_error('genus', 'Cannot add the organism with this genus and species.
  346. The organism already exists.');
  347. tripal_report_error('tripal_organism', TRIPAL_WARNING,
  348. 'Insert organism: genus and species already exists: %values',
  349. ['%values' => "genus = $node->genus, species = $node->species"]);
  350. }
  351. }
  352. }
  353. /**
  354. * Implements hook_insert().
  355. *
  356. * When a new chado_organism node is created we also need to add information
  357. * to our chado_organism table. This function is called on insert of a new
  358. * node of type 'chado_organism' and inserts the necessary information.
  359. *
  360. * @ingroup tripal_legacy_organism
  361. */
  362. function chado_organism_insert($node) {
  363. $chado_version = chado_get_version(TRUE);
  364. $organism_id = '';
  365. // if there is an organism_id in the $node object then this must be a sync so
  366. // we can skip adding the organism as it is already there, although
  367. // we do need to proceed with insertion into the chado/drupal linking table.
  368. if (!property_exists($node, 'organism_id')) {
  369. // remove any white space around values
  370. $node->genus = trim($node->genus);
  371. $node->species = trim($node->species);
  372. $node->abbreviation = trim($node->abbreviation);
  373. $node->common_name = trim($node->common_name);
  374. $node->description = trim($node->description['value']);
  375. if ($chado_version > 1.2) {
  376. $node->type_id = trim($node->type_id);
  377. $node->infraspecific_name = trim($node->infraspecific_name);
  378. }
  379. $values = [
  380. 'genus' => $node->genus,
  381. 'species' => $node->species,
  382. 'abbreviation' => $node->abbreviation,
  383. 'common_name' => $node->common_name,
  384. 'comment' => $node->description,
  385. ];
  386. if ($chado_version > 1.2) {
  387. if ($node->type_id) {
  388. $values['type_id'] = $node->type_id;
  389. }
  390. if ($node->infraspecific_name) {
  391. $values['infraspecific_name'] = $node->infraspecific_name;
  392. }
  393. }
  394. $organism = chado_insert_record('organism', $values);
  395. if (!$organism) {
  396. drupal_set_message(t('Unable to add organism.', 'warning'));
  397. tripal_report_error('tripal_organism', TRIPAL_ERROR, 'Insert Organism: Unable to create organism where values:%values',
  398. ['%values' => print_r($values, TRUE)]);
  399. return;
  400. }
  401. $organism_id = $organism['organism_id'];
  402. if ($organism_id) {
  403. // * Properties Form *
  404. $details = [
  405. 'property_table' => 'organismprop',
  406. // the name of the prop table
  407. 'base_table' => 'organism',
  408. // the name of your chado base table
  409. 'foreignkey_name' => 'organism_id',
  410. // the name of the key in your base table
  411. 'foreignkey_value' => $organism_id
  412. // the value of the example_id key
  413. ];
  414. chado_update_node_form_properties($node, $details);
  415. // * Additional DBxrefs Form *
  416. $details = [
  417. 'linking_table' => 'organism_dbxref',
  418. // the name of your _dbxref table
  419. 'foreignkey_name' => 'organism_id',
  420. // the name of the key in your base table
  421. 'foreignkey_value' => $organism_id
  422. // the value of the organism_id key
  423. ];
  424. chado_update_node_form_dbxrefs($node, $details);
  425. }
  426. }
  427. else {
  428. $organism_id = $node->organism_id;
  429. }
  430. // Make sure the entry for this organism doesn't already exist in the
  431. // chado_organism table if it doesn't exist then we want to add it.
  432. $check_org_id = chado_get_id_from_nid('organism', $node->nid);
  433. if (!$check_org_id) {
  434. $record = new stdClass();
  435. $record->nid = $node->nid;
  436. $record->vid = $node->vid;
  437. $record->organism_id = $organism_id;
  438. drupal_write_record('chado_organism', $record);
  439. }
  440. // add the image
  441. if (property_exists($node, 'organism_image')) {
  442. chado_organism_add_image($node);
  443. }
  444. }
  445. /**
  446. * Implements hook_update().
  447. *
  448. * @ingroup tripal_legacy_organism
  449. */
  450. function chado_organism_update($node) {
  451. $chado_version = chado_get_version(TRUE);
  452. // remove any white space around values
  453. $node->genus = trim($node->genus);
  454. $node->species = trim($node->species);
  455. $node->abbreviation = trim($node->abbreviation);
  456. $node->common_name = trim($node->common_name);
  457. $node->description = trim($node->description['value']);
  458. if ($chado_version > 1.2) {
  459. $node->type_id = trim($node->type_id);
  460. $node->infraspecific_name = trim($node->infraspecific_name);
  461. }
  462. $organism_id = chado_get_id_from_nid('organism', $node->nid);
  463. if ($node->revision) {
  464. // there is no way to handle revisions in Chado but leave
  465. // this here just to make not we've addressed it.
  466. }
  467. $match = [
  468. 'organism_id' => $organism_id,
  469. ];
  470. $values = [
  471. 'genus' => $node->genus,
  472. 'species' => $node->species,
  473. 'abbreviation' => $node->abbreviation,
  474. 'common_name' => $node->common_name,
  475. 'comment' => $node->description,
  476. ];
  477. if ($chado_version > 1.2) {
  478. if ($node->type_id) {
  479. $values['type_id'] = $node->type_id;
  480. }
  481. else {
  482. $values['type_id'] = '__NULL__';
  483. }
  484. if ($node->infraspecific_name) {
  485. $values['infraspecific_name'] = $node->infraspecific_name;
  486. }
  487. else {
  488. $values['infraspecific_name'] = '__NULL__';
  489. }
  490. }
  491. $org_status = chado_update_record('organism', $match, $values);
  492. if ($node->organism_image != '') {
  493. chado_organism_add_image($node);
  494. }
  495. // * Properties Form *
  496. $details = [
  497. 'property_table' => 'organismprop',
  498. // the name of the prop table
  499. 'base_table' => 'organism',
  500. // the name of your chado base table
  501. 'foreignkey_name' => 'organism_id',
  502. // the name of the key in your base table
  503. 'foreignkey_value' => $organism_id
  504. // the value of the example_id key
  505. ];
  506. chado_update_node_form_properties($node, $details);
  507. // * Additional DBxrefs Form *
  508. $details = [
  509. 'linking_table' => 'organism_dbxref',
  510. // the name of your _dbxref table
  511. 'foreignkey_name' => 'organism_id',
  512. // the name of the key in your base table
  513. 'foreignkey_value' => $organism_id
  514. // the value of the organism_id key
  515. ];
  516. chado_update_node_form_dbxrefs($node, $details);
  517. }
  518. /**
  519. * Adds the image to the organism node and cleans up any old images.
  520. *
  521. * @param $node
  522. * The node object.
  523. */
  524. function chado_organism_add_image($node) {
  525. // If there is already an organism image, then remove it it if
  526. // no other modules are using it
  527. $fid = db_select('file_usage', 'fu')
  528. ->fields('fu', ['fid'])
  529. ->condition('module', 'tripal_organism')
  530. ->condition('type', 'organism_image')
  531. ->condition('id', $node->nid)
  532. ->execute()
  533. ->fetchField();
  534. if ($fid) {
  535. $file = file_load($fid);
  536. file_usage_delete($file, 'tripal_organism', 'organism_image', $node->nid);
  537. file_delete($file);
  538. }
  539. // Save the uploaded file
  540. $file = file_load($node->organism_image);
  541. if ($file) {
  542. $file->status = FILE_STATUS_PERMANENT;
  543. file_save($file);
  544. file_usage_add($file, 'tripal_organism', 'organism_image', $node->nid);
  545. }
  546. }
  547. /**
  548. * Implements hook_delete().
  549. *
  550. * Delete organism from both drupal and chado databases. Check dependency before
  551. * deleting from chado.
  552. *
  553. * @ingroup tripal_legacy_organism
  554. */
  555. function chado_organism_delete($node) {
  556. $organism_id = chado_get_id_from_nid('organism', $node->nid);
  557. // if we don't have an organism id for this node then this isn't a node of
  558. // type chado_organism or the entry in the chado_organism table was lost.
  559. if (!$organism_id) {
  560. return;
  561. }
  562. // Remove data from the {chado_organism}, {node}, and {node_revisions} tables
  563. $sql_del = "DELETE FROM {chado_organism} WHERE nid = :nid AND vid = :vid";
  564. db_query($sql_del, [':nid' => $node->nid, ':vid' => $node->vid]);
  565. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  566. db_query($sql_del, [':nid' => $node->nid, ':vid' => $node->vid]);
  567. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  568. db_query($sql_del, [':nid' => $node->nid, ':vid' => $node->vid]);
  569. // Test dependency before deleting from chado database. If a library or
  570. // feature depends on this organism, don't delete it
  571. $sql = "SELECT feature_id FROM {feature} WHERE organism_id = :organism_id";
  572. $check_feature = chado_query($sql, [':organism_id' => $organism_id])->fetchObject();
  573. $sql = "SELECT library_id FROM {library} WHERE organism_id = :organism_id";
  574. $check_lib = chado_query($sql, [':organism_id' => $organism_id])->fetchObject();
  575. $sql = "SELECT stock_id FROM {stock} WHERE organism_id = :organism_id";
  576. $check_stock = chado_query($sql, [':organism_id' => $organism_id])->fetchObject();
  577. if (!$check_lib && !$check_feature && !$check_stock) {
  578. chado_delete_record('organism', ['organism_id' => $organism_id]);
  579. }
  580. else {
  581. 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');
  582. }
  583. }
  584. /**
  585. * Implements hook_load().
  586. *
  587. * When a node is requested by the user this function is called to allow us
  588. * to add auxiliary data to the node object.
  589. *
  590. * @ingroup tripal_legacy_organism
  591. */
  592. function chado_organism_load($nodes) {
  593. foreach ($nodes as $nid => $node) {
  594. // find the organism and add in the details
  595. $organism_id = chado_get_id_from_nid('organism', $nid);
  596. // if the nid does not have a matching record then skip this node.
  597. // this can happen with orphaned nodes.
  598. if (!$organism_id) {
  599. continue;
  600. }
  601. // build the organism variable
  602. $values = ['organism_id' => $organism_id];
  603. $organism = chado_generate_var('organism', $values);
  604. // add in the description field
  605. $organism = chado_expand_var($organism, 'field', 'organism.comment');
  606. $nodes[$nid]->organism = $organism;
  607. // Now get the title
  608. $node->title = chado_get_node_title($node);
  609. }
  610. }
  611. /**
  612. * Implements hook_node_presave(). Acts on all content types.
  613. *
  614. * @param $node
  615. * The node to be saved
  616. *
  617. * @ingroup tripal_legacy_organism
  618. */
  619. function tripal_organism_node_presave($node) {
  620. switch ($node->type) {
  621. // This step is for setting the title for the Drupal node. This title
  622. // is permanent and thus is created to be unique. Title changes provided
  623. // by tokens are generated on the fly dynamically, but the node title
  624. // seen in the content listing needs to be set here. Do not call
  625. // the chado_get_node_title() function here to set the title as the node
  626. // object isn't properly filled out and the function will fail.
  627. case 'chado_organism':
  628. // when syncing the details are not present in the $node object
  629. // as they are when submitted via the form. Therefore, if we do
  630. // not see any field values from the form, we assume this function
  631. // is being called for syncing, so we must set the title accordingly
  632. if (property_exists($node, 'genus')) {
  633. $node->title = $node->genus . " " . $node->species;
  634. if (property_exists($node, 'type_id')) {
  635. $cvterm = tripal_get_cvterm(['cvterm_id' => $node->type_id]);
  636. if ($cvterm) {
  637. $node->title .= $cvterm->name . " " . $node->infraspecific_name;
  638. }
  639. }
  640. }
  641. elseif (property_exists($node, 'organism')) {
  642. $node->title = $node->organism->genus . " " . $node->organism->species;
  643. if (property_exists($node, 'type_id')) {
  644. $node->title .= $node->organism->type_id->name . " " . $node->organism->infraspecific_name;
  645. }
  646. }
  647. break;
  648. }
  649. }
  650. /**
  651. * Implements hook_node_view().
  652. *
  653. * @ingroup tripal_legacy_organism
  654. */
  655. function tripal_organism_node_view($node, $view_mode, $langcode) {
  656. switch ($node->type) {
  657. case 'chado_organism':
  658. // Show feature browser and counts
  659. if ($view_mode == 'full') {
  660. $node->content['tripal_organism_base'] = [
  661. '#theme' => 'tripal_organism_base',
  662. '#node' => $node,
  663. '#tripal_toc_id' => 'base',
  664. '#tripal_toc_title' => 'Overview',
  665. '#weight' => -100,
  666. ];
  667. $node->content['tripal_organism_properties'] = [
  668. '#theme' => 'tripal_organism_properties',
  669. '#node' => $node,
  670. '#tripal_toc_id' => 'properties',
  671. '#tripal_toc_title' => 'Properties',
  672. ];
  673. $node->content['tripal_organism_references'] = [
  674. '#theme' => 'tripal_organism_references',
  675. '#node' => $node,
  676. '#tripal_toc_id' => 'references',
  677. '#tripal_toc_title' => 'Cross References',
  678. ];
  679. }
  680. if ($view_mode == 'teaser') {
  681. $node->content['tripal_organism_teaser'] = [
  682. '#theme' => 'tripal_organism_teaser',
  683. '#node' => $node,
  684. ];
  685. }
  686. break;
  687. }
  688. }
  689. /**
  690. * Implements hook_node_insert().
  691. * Acts on all content types.
  692. *
  693. * @ingroup tripal_legacy_organism
  694. */
  695. function tripal_organism_node_insert($node) {
  696. switch ($node->type) {
  697. case 'chado_organism':
  698. // find the organism and add in the details
  699. $organism_id = chado_get_id_from_nid('organism', $node->nid);
  700. $values = ['organism_id' => $organism_id];
  701. $organism = chado_generate_var('organism', $values);
  702. $node->organism = $organism;
  703. // Now get the title
  704. $node->title = chado_get_node_title($node);
  705. // Now use the API to set the path.
  706. chado_set_node_url($node);
  707. break;
  708. }
  709. }
  710. /**
  711. * Implements hook_node_update().
  712. * Acts on all content types.
  713. *
  714. * @ingroup tripal_legacy_organism
  715. */
  716. function tripal_organism_node_update($node) {
  717. switch ($node->type) {
  718. case 'chado_organism':
  719. // Now get the title.
  720. $node->title = chado_get_node_title($node);
  721. // Now use the API to set the path.
  722. chado_set_node_url($node);
  723. break;
  724. }
  725. }
  726. /**
  727. * Implements [content_type]_chado_node_default_title_format().
  728. *
  729. * Defines a default title format for the Chado Node API to set the titles on
  730. * Chado organism nodes based on chado fields.
  731. */
  732. function chado_organism_chado_node_default_title_format() {
  733. return '[organism.genus] [organism.species]';
  734. }
  735. /**
  736. * Implements hook_chado_node_default_url_format().
  737. *
  738. * Designates a default URL format for organism nodes.
  739. */
  740. function chado_organism_chado_node_default_url_format() {
  741. return '/organism/[organism.genus]/[organism.species]';
  742. }