tripal_organism.chado_node.inc 26 KB

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