tripal_contact.chado_node.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. <?php
  2. /**
  3. * @file
  4. * Implements drupal node hooks.
  5. *
  6. * @ingroup tripal_legacy_contact
  7. */
  8. /**
  9. * Implementation of hook_node_info().
  10. * This node_info, is a simple node that describes the functionallity of the
  11. * module.
  12. *
  13. * @ingroup tripal_legacy_contact
  14. */
  15. function tripal_contact_node_info() {
  16. return [
  17. 'chado_contact' => [
  18. 'name' => t('Contact (Tripal v2 legacy)'),
  19. 'base' => 'chado_contact',
  20. 'description' => t('A contact from the Chado database'),
  21. 'has_title' => TRUE,
  22. 'locked' => TRUE,
  23. 'chado_node_api' => [
  24. 'base_table' => 'contact',
  25. 'hook_prefix' => 'chado_contact',
  26. 'record_type_title' => [
  27. 'singular' => t('Contact'),
  28. 'plural' => t('Contacts'),
  29. ],
  30. 'sync_filters' => [
  31. 'type_id' => FALSE,
  32. 'organism_id' => FALSE,
  33. ],
  34. ],
  35. ],
  36. ];
  37. }
  38. /**
  39. * Implementation of hook_form().
  40. *
  41. * @parm $node
  42. * The node that is created when the database is initialized
  43. *
  44. * @parm $form_state
  45. * The state of the form, that has the user entered information that is
  46. * neccessary for, setting up the database tables for the contact
  47. *
  48. * @return $form
  49. * The information that was enterd allong with
  50. *
  51. * @ingroup tripal_legacy_contact
  52. */
  53. function chado_contact_form(&$node, $form_state) {
  54. $form = [];
  55. // Default values can come in the following ways:
  56. //
  57. // 1) as elements of the $node object. This occurs when editing an existing contact
  58. // 2) in the $form_state['values'] array which occurs on a failed validation or
  59. // ajax callbacks from non submit form elements
  60. // 3) in the $form_state['input'[ array which occurs on ajax callbacks from submit
  61. // form elements and the form is being rebuilt
  62. //
  63. // set form field defaults
  64. $contact_id = NULL;
  65. $type_id = 0;
  66. $contactname = '';
  67. $description = '';
  68. // if we are editing an existing node then the contact is already part of the node
  69. if (property_exists($node, 'contact')) {
  70. $contact = $node->contact;
  71. $contact_id = $contact->contact_id;
  72. // get form defaults
  73. $type_id = $contact->type_id->cvterm_id;
  74. $contactname = $contact->name;
  75. // get the contact default values. When this module was first created
  76. // the contact description was incorrectly stored in the $node->body field.
  77. // It is better to store it in the Chado tables. However, the 'description'
  78. // field of the contact table is only 255 characters. So, we are going
  79. // to follow the same as the contact module and store the description in
  80. // the contactprop table and leave the contact.description field blank.
  81. // however, for backwards compatibitily, we check to see if the description
  82. // is in the $node->body field. If it is we'll use that. When the node is
  83. // edited the text will be moved out of the body and into the contactprop
  84. // table where it should belong.
  85. $description = '';
  86. if (property_exists($node, 'body')) {
  87. $description = $node->body;
  88. }
  89. else {
  90. $description = $contact->description;
  91. }
  92. if (!$description) {
  93. $contactprop = chado_get_property(
  94. ['table' => 'contact', 'id' => $contact->contact_id],
  95. ['type_name' => 'contact_description', 'cv_name' => 'tripal_contact']
  96. );
  97. $description = (isset($contactprop->value)) ? $contactprop->value : '';
  98. }
  99. // set the contact_id in the form
  100. $form['contact_id'] = [
  101. '#type' => 'value',
  102. '#value' => $contact->contact_id,
  103. ];
  104. }
  105. // if we are re constructing the form from a failed validation or ajax callback
  106. // then use the $form_state['values'] values
  107. if (array_key_exists('values', $form_state)) {
  108. $type_id = $form_state['values']['type_id'];
  109. $contactname = $form_state['values']['contactname'];
  110. $description = $form_state['values']['description'];
  111. }
  112. // if we are re building the form from after submission (from ajax call) then
  113. // the values are in the $form_state['input'] array
  114. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  115. $type_id = $form_state['input']['type_id'];
  116. $contactname = $form_state['input']['contactname'];
  117. $description = $form_state['input']['description'];
  118. }
  119. // get the contact type
  120. $type_cv = tripal_get_default_cv('contact', 'type_id');
  121. if ($type_cv->name == 'tripal_contact') {
  122. // get the contact types. If the default is the 'tripal_contact' vocabulary,
  123. // then we want terms that are part of the tripal_contact
  124. // vocabulary and are children of the term 'Contact Type', so we need
  125. // to join on the cvtermpath table and select those with a distance of 1
  126. $sql = "
  127. SELECT CVTS.cvterm_id, CVTS.name
  128. FROM {cvtermpath} CVTP
  129. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  130. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  131. INNER JOIN {cv} CV ON CVTO.cv_id = CV.cv_id
  132. WHERE
  133. CV.name = 'tripal_contact' AND
  134. CVTO.name = 'Contact Type' AND
  135. CVTP.pathdistance = 1
  136. ORDER BY CVTS.name ASC
  137. ";
  138. $results = chado_query($sql);
  139. while ($contact_type = $results->fetchObject()) {
  140. $contact_types[$contact_type->cvterm_id] = $contact_type->name;
  141. if (strcmp($contact_type->name, "Person") == 0 and !$type_id) {
  142. $type_id = $contact_type->cvterm_id;
  143. }
  144. }
  145. }
  146. else {
  147. $contact_types = tripal_get_cvterm_default_select_options('contact', 'type_id', 'contact types');
  148. }
  149. $form['type_id'] = [
  150. '#type' => 'select',
  151. '#title' => t('Contact Type'),
  152. '#options' => $contact_types,
  153. '#required' => TRUE,
  154. '#default_value' => $type_id,
  155. ];
  156. $form['contactname'] = [
  157. '#type' => 'textfield',
  158. '#title' => t('Contact Name'),
  159. '#description' => t('Enter the name of this contact'),
  160. '#required' => TRUE,
  161. '#default_value' => $contactname,
  162. '#maxlength' => 255,
  163. ];
  164. $form['description'] = [
  165. '#type' => 'text_format',
  166. '#title' => t('Contact Description'),
  167. '#description' => t('A brief description of the contact'),
  168. '#required' => TRUE,
  169. '#default_value' => $description,
  170. ];
  171. // Properties Form
  172. // ----------------------------------
  173. $prop_cv = tripal_get_default_cv('contactprop', 'type_id');
  174. $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
  175. $select_options = [];
  176. // the Tripal contact vocabulary is heirarchical so if that vocab is default we
  177. // want to use the subset of terms not under the type 'Contact Type' for our
  178. // properties list.
  179. if ($prop_cv->name == 'tripal_contact') {
  180. // Need to pass in our own select_options since we use cvtermpath to filter ours
  181. $select_options[] = 'Select a Property';
  182. $sql = "
  183. SELECT CVTS.cvterm_id, CVTS.name
  184. FROM {cvtermpath} CVTP
  185. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  186. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  187. INNER JOIN {cv} CV ON CVTO.cv_id = CV.cv_id
  188. WHERE
  189. CV.name = 'tripal_contact' AND
  190. NOT CVTO.name = 'Contact Type'
  191. ORDER BY CVTS.name ASC";
  192. $prop_types = chado_query($sql);
  193. while ($prop = $prop_types->fetchObject()) {
  194. // add all properties except the Citation. That property is set via the uniquename field
  195. if ($prop->name != 'Citation') {
  196. if (!isset($select_options[$prop->cvterm_id])) {
  197. $select_options[$prop->cvterm_id] = $prop->name;
  198. }
  199. }
  200. }
  201. }
  202. $details = [
  203. 'property_table' => 'contactprop',
  204. 'chado_id' => $contact_id,
  205. 'cv_id' => $cv_id,
  206. 'select_options' => $select_options,
  207. ];
  208. chado_add_node_form_properties($form, $form_state, $details);
  209. // RELATIONSHIPS FORM
  210. //---------------------------------------------
  211. $relationship_cv = tripal_get_default_cv('contact_relationship', 'type_id');
  212. $cv_id = $relationship_cv ? $relationship_cv->cv_id : NULL;
  213. $details = [
  214. 'relationship_table' => 'contact_relationship',
  215. // the name of the _relationship table
  216. 'base_table' => 'contact',
  217. // the name of your chado base table
  218. 'base_foreign_key' => 'contact_id',
  219. // the name of the key in your base chado table
  220. 'base_key_value' => $contact_id,
  221. // the value of example_id for this record
  222. 'nodetype' => 'contact',
  223. // the human-readable name of your node type
  224. 'cv_id' => $cv_id,
  225. // the cv.cv_id of the cv governing contact_relationship.type_id
  226. 'base_name_field' => 'name',
  227. // the base table field you want to be used as the name
  228. ];
  229. // Adds the form elements to your current form
  230. chado_add_node_form_relationships($form, $form_state, $details);
  231. return $form;
  232. }
  233. /**
  234. * Implements hook_validate().
  235. * Validates submission of form when adding or updating a contact node.
  236. *
  237. * @ingroup tripal_legacy_contact
  238. */
  239. function chado_contact_validate($node, $form, &$form_state) {
  240. // We only want to validate when the node is saved.
  241. // Since this validate can be called on AJAX and Deletion of the node
  242. // we need to make this check to ensure queries are not executed
  243. // without the proper values.
  244. if (property_exists($node, "op") and $node->op != 'Save') {
  245. return;
  246. }
  247. // we are syncing if we do not have a node ID but we do have a contact_id. We don't
  248. // need to validate during syncing so just skip it.
  249. if (!property_exists($node, 'nid') and property_exists($node, 'contact_id') and $node->contact_id != 0) {
  250. return;
  251. }
  252. // remove surrounding white-space on submitted values
  253. $node->contactname = property_exists($node, 'contactname') ? trim($node->contactname) : '';
  254. // Validating for an update
  255. if (!is_null($node->nid)) {
  256. // get the existing node
  257. $values = ['contact_id' => $node->contact_id];
  258. $result = chado_select_record('contact', ['*'], $values);
  259. $contact = $result[0];
  260. // if the name has changed make sure it doesn't conflict with an existing name
  261. if ($contact->name != $node->contactname) {
  262. $values = ['name' => $node->contactname];
  263. $result = chado_select_record('contact', ['contact_id'], $values);
  264. if ($result and count($result) > 0) {
  265. form_set_error('contactname', 'Cannot update the contact with this contact name. A contact with this name already exists.');
  266. return;
  267. }
  268. }
  269. }
  270. // Validating for an insert
  271. else {
  272. // The unique constraint for the chado contact table is: name
  273. $values = [
  274. 'name' => $node->contactname,
  275. ];
  276. $contact = chado_select_record('contact', ['contact_id'], $values);
  277. if ($contact and count($contact) > 0) {
  278. form_set_error('contactname', 'Cannot add the contact with this name. A contact with these values already exists.');
  279. return;
  280. }
  281. }
  282. }
  283. /**
  284. * Implements hook_access().
  285. *
  286. * This hook allows node modules to limit access to the node types they define.
  287. *
  288. * @param $node
  289. * The node on which the operation is to be performed, or, if it does not yet
  290. * exist, the type of node to be created
  291. *
  292. * @param $op
  293. * The operation to be performed
  294. *
  295. * @param $account
  296. * A user object representing the user for whom the operation is to be
  297. * performed
  298. *
  299. * @return
  300. * If the permission for the specified operation is not set then return FALSE.
  301. * If the permission is set then return NULL as this allows other modules to
  302. * disable access. The only exception is when the $op == 'create'. We will
  303. * always return TRUE if the permission is set.
  304. *
  305. * @ingroup tripal_legacy_contact
  306. */
  307. function tripal_contact_node_access($node, $op, $account) {
  308. $node_type = $node;
  309. if (is_object($node)) {
  310. $node_type = $node->type;
  311. }
  312. if ($node_type == 'chado_contact') {
  313. if ($op == 'create') {
  314. if (!user_access('create chado_contact content', $account)) {
  315. return NODE_ACCESS_DENY;
  316. }
  317. return NODE_ACCESS_ALLOW;
  318. }
  319. if ($op == 'update') {
  320. if (!user_access('edit chado_contact content', $account)) {
  321. return NODE_ACCESS_DENY;
  322. }
  323. }
  324. if ($op == 'delete') {
  325. if (!user_access('delete chado_contact content', $account)) {
  326. return NODE_ACCESS_DENY;
  327. }
  328. }
  329. if ($op == 'view') {
  330. if (!user_access('access chado_contact content', $account)) {
  331. return NODE_ACCESS_DENY;
  332. }
  333. }
  334. }
  335. return NODE_ACCESS_IGNORE;
  336. }
  337. /**
  338. * Implements of hook_insert().
  339. *
  340. * This function inserts user entered information pertaining to the contact
  341. * instance into the
  342. * 'contactauthor', 'contactprop', 'chado_contact', 'contact' talble of the
  343. * database.
  344. *
  345. * @parm $node
  346. * Then node which contains the information stored within the node-ID
  347. *
  348. * @ingroup tripal_legacy_contact
  349. */
  350. function chado_contact_insert($node) {
  351. $contact_id = '';
  352. // if there is a contact_id in the $node object then this must be a sync so
  353. // we can skip adding the contact as it is already there, although
  354. // we do need to proceed with insertion into the chado/drupal linking table.
  355. if (!property_exists($node, 'contact_id')) {
  356. // remove surrounding white-space on submitted values
  357. $node->contactname = trim($node->contactname);
  358. $node->description = trim($node->description['value']);
  359. // insert and then get the newly inserted contact record
  360. $values = [
  361. 'name' => $node->contactname,
  362. 'description' => '',
  363. 'type_id' => $node->type_id,
  364. ];
  365. $contact = chado_insert_record('contact', $values);
  366. if (!$contact) {
  367. drupal_set_message(t('Unable to add contact.', 'warning'));
  368. tripal_report_error('tripal_contact', TRIPAL_ERROR,
  369. 'Insert contact: Unable to create contact where values: %values',
  370. ['%values' => print_r($values, TRUE)]);
  371. return;
  372. }
  373. $contact_id = $contact['contact_id'];
  374. // Add the description property
  375. $properties = chado_retrieve_node_form_properties($node);
  376. $contact_descrip_id = tripal_get_cvterm([
  377. 'name' => 'contact_description',
  378. 'cv_id' => ['name' => 'tripal_contact'],
  379. ]);
  380. $properties[$contact_descrip_id->cvterm_id][0] = $node->description;
  381. // * Properties Form *
  382. $details = [
  383. 'property_table' => 'contactprop',
  384. 'base_table' => 'contact',
  385. 'foreignkey_name' => 'contact_id',
  386. 'foreignkey_value' => $contact_id,
  387. ];
  388. chado_update_node_form_properties($node, $details, $properties);
  389. // * Relationships Form *
  390. $details = [
  391. 'relationship_table' => 'contact_relationship',
  392. // name of the _relationship table
  393. 'foreignkey_value' => $contact_id
  394. // value of the contact_id key
  395. ];
  396. chado_update_node_form_relationships($node, $details);
  397. }
  398. else {
  399. $contact_id = $node->contact_id;
  400. }
  401. // Make sure the entry for this contact doesn't already exist in the
  402. // chado_contact table if it doesn't exist then we want to add it.
  403. $check_org_id = chado_get_id_from_nid('contact', $node->nid);
  404. if (!$check_org_id) {
  405. $record = new stdClass();
  406. $record->nid = $node->nid;
  407. $record->vid = $node->vid;
  408. $record->contact_id = $contact_id;
  409. drupal_write_record('chado_contact', $record);
  410. }
  411. return TRUE;
  412. }
  413. /**
  414. * Implements hook_update
  415. *
  416. * The purpose of the function is to allow the module to take action when an
  417. * edited node is being updated. It updates any name changes to the database
  418. * tables that were created upon registering a contact.
  419. * As well, the database will be changed, so the user changed information will
  420. * be saved to the database.
  421. *
  422. * @param $node
  423. * The node being updated
  424. *
  425. * @ingroup tripal_legacy_contact
  426. */
  427. function chado_contact_update($node) {
  428. // remove surrounding white-space on submitted values
  429. $node->contactname = trim($node->contactname);
  430. $node->description = trim($node->description['value']);
  431. $contact_id = chado_get_id_from_nid('contact', $node->nid);
  432. // update the contact record
  433. $match = [
  434. 'contact_id' => $contact_id,
  435. ];
  436. $values = [
  437. 'name' => $node->contactname,
  438. 'description' => '',
  439. 'type_id' => $node->type_id,
  440. ];
  441. $status = chado_update_record('contact', $match, $values);
  442. if (!$status) {
  443. drupal_set_message("Error updating contact", "error");
  444. tripal_report_error('tripal_contact', TRIPAL_ERROR,
  445. "Error updating contact", []);
  446. return;
  447. }
  448. // Add the description property
  449. $properties = chado_retrieve_node_form_properties($node);
  450. $contact_descrip_id = tripal_get_cvterm([
  451. 'name' => 'contact_description',
  452. 'cv_id' => ['name' => 'tripal_contact'],
  453. ]);
  454. $properties[$contact_descrip_id->cvterm_id][0] = $node->description;
  455. // now add in the properties by first removing any the contact
  456. // already has and adding the ones we have
  457. $details = [
  458. 'property_table' => 'contactprop',
  459. 'base_table' => 'contact',
  460. 'foreignkey_name' => 'contact_id',
  461. 'foreignkey_value' => $contact_id,
  462. ];
  463. chado_update_node_form_properties($node, $details, $properties);
  464. // * Relationships Form *
  465. $details = [
  466. 'relationship_table' => 'contact_relationship',
  467. // name of the _relationship table
  468. 'foreignkey_value' => $contact_id
  469. // value of the contact_id key
  470. ];
  471. chado_update_node_form_relationships($node, $details);
  472. }
  473. /**
  474. * Implements hook_load().
  475. *
  476. * @param $node
  477. * The node that is to be accessed from the database
  478. *
  479. * @return $node
  480. * The node with the information to be loaded into the database
  481. *
  482. * @ingroup tripal_legacy_contact
  483. */
  484. function chado_contact_load($nodes) {
  485. foreach ($nodes as $nid => $node) {
  486. // find the contact and add in the details
  487. $contact_id = chado_get_id_from_nid('contact', $nid);
  488. // if the nid does not have a matching record then skip this node.
  489. // this can happen with orphaned nodes.
  490. if (!$contact_id) {
  491. continue;
  492. }
  493. // get the contact
  494. $values = ['contact_id' => $contact_id];
  495. $contact = chado_generate_var('contact', $values);
  496. // get the contact description from the contactprop table and replace
  497. // the contact.description field with this one (we don't use the contact.description
  498. // field because it is only 255 characters (too small)).
  499. $values = [
  500. 'contact_id' => $contact->contact_id,
  501. 'type_id' => [
  502. 'name' => 'contact_description',
  503. ],
  504. ];
  505. $options = [
  506. 'return_array' => 1,
  507. 'include_fk' => ['type_id' => 1],
  508. ];
  509. $description = chado_generate_var('contactprop', $values, $options);
  510. if (count($description) == 1) {
  511. $description = chado_expand_var($description, 'field', 'contactprop.value');
  512. $contact->description = $description[0]->value;
  513. }
  514. $nodes[$nid]->contact = $contact;
  515. // Now get the title
  516. $node->title = chado_get_node_title($node);
  517. }
  518. }
  519. /**
  520. * Implements hook_delete().
  521. *
  522. * This function takes a node and if the delete button has been chosen by the
  523. * user, the contact and it's details will be removed.Following,given the
  524. * node-ID, the instance will be deleted from the 'chado_contact' table.
  525. *
  526. * @parm $node
  527. * Then node which contains the information stored within the node-ID
  528. *
  529. * @ingroup tripal_legacy_contact
  530. */
  531. function chado_contact_delete(&$node) {
  532. $contact_id = chado_get_id_from_nid('contact', $node->nid);
  533. // if we don't have a contact id for this node then this isn't a node of
  534. // type chado_contact or the entry in the chado_contact table was lost.
  535. if (!$contact_id) {
  536. return;
  537. }
  538. // Remove data from {chado_contact}, {node} and {node_revisions} tables of
  539. // drupal database
  540. $sql_del = "DELETE FROM {chado_contact} WHERE nid = :nid AND vid = :vid";
  541. db_query($sql_del, [':nid' => $node->nid, ':vid' => $node->vid]);
  542. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  543. db_query($sql_del, [':nid' => $node->nid, ':vid' => $node->vid]);
  544. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  545. db_query($sql_del, [':nid' => $node->nid, ':vid' => $node->vid]);
  546. // Remove data from contact and contactprop tables of chado database as well
  547. chado_query("DELETE FROM {contactprop} WHERE contact_id = :contact_id", [':contact_id' => $contact_id]);
  548. chado_query("DELETE FROM {contact} WHERE contact_id = :contact_id", [':contact_id' => $contact_id]);
  549. }
  550. /**
  551. * Implements hook_node_view().
  552. *
  553. * @ingroup tripal_legacy_contact
  554. */
  555. function tripal_contact_node_view($node, $view_mode, $langcode) {
  556. switch ($node->type) {
  557. case 'chado_contact':
  558. // Show feature browser and counts
  559. if ($view_mode == 'full') {
  560. $node->content['tripal_contact_base'] = [
  561. '#theme' => 'tripal_contact_base',
  562. '#node' => $node,
  563. '#tripal_toc_id' => 'base',
  564. '#tripal_toc_title' => 'Overview',
  565. '#weight' => -100,
  566. ];
  567. $node->content['tripal_contact_properties'] = [
  568. '#theme' => 'tripal_contact_properties',
  569. '#node' => $node,
  570. '#tripal_toc_id' => 'properties',
  571. '#tripal_toc_title' => 'Properties',
  572. ];
  573. if (module_exists('tripal_pub')) {
  574. $node->content['tripal_contact_publications'] = [
  575. '#theme' => 'tripal_contact_publications',
  576. '#node' => $node,
  577. '#tripal_toc_id' => 'publications',
  578. '#tripal_toc_title' => 'Publications',
  579. ];
  580. }
  581. $node->content['tripal_contact_relationships'] = [
  582. '#theme' => 'tripal_contact_relationships',
  583. '#node' => $node,
  584. '#tripal_toc_id' => 'relationships',
  585. '#tripal_toc_title' => 'Relationships',
  586. ];
  587. }
  588. if ($view_mode == 'teaser') {
  589. $node->content['tripal_contact_teaser'] = [
  590. '#theme' => 'tripal_contact_teaser',
  591. '#node' => $node,
  592. ];
  593. }
  594. break;
  595. }
  596. }
  597. /**
  598. * Implements hook_node_presave().
  599. *
  600. * @ingroup tripal_legacy_contact
  601. */
  602. function tripal_contact_node_presave($node) {
  603. switch ($node->type) {
  604. case 'chado_contact':
  605. // for a form submission the 'contactname' field will be set,
  606. // for a sync, we must pull from the contact object
  607. if (property_exists($node, 'contactname')) {
  608. // set the title
  609. $node->title = $node->contactname;
  610. }
  611. else {
  612. if (property_exists($node, 'contact')) {
  613. $node->title = $node->contact->name;
  614. }
  615. }
  616. break;
  617. }
  618. }
  619. /**
  620. * Implements hook_node_insert().
  621. * Acts on all content types.
  622. *
  623. * @ingroup tripal_legacy_contact
  624. */
  625. function tripal_contact_node_insert($node) {
  626. switch ($node->type) {
  627. case 'chado_contact':
  628. // find the contact and add in the details
  629. $contact_id = chado_get_id_from_nid('contact', $node->nid);
  630. // get the contact
  631. $values = ['contact_id' => $contact_id];
  632. $contact = chado_generate_var('contact', $values);
  633. $node->contact = $contact;
  634. // Now get the title
  635. $node->title = chado_get_node_title($node);
  636. // Now use the API to set the path.
  637. chado_set_node_url($node);
  638. break;
  639. }
  640. }
  641. /**
  642. * Implements hook_node_update().
  643. * Acts on all content types.
  644. *
  645. * @ingroup tripal_legacy_contact
  646. */
  647. function tripal_contact_node_update($node) {
  648. switch ($node->type) {
  649. case 'chado_contact':
  650. // Set the title
  651. $node->title = chado_get_node_title($node);
  652. // Now use the API to set the path.
  653. chado_set_node_url($node);
  654. break;
  655. }
  656. }
  657. /**
  658. * Implements [content_type]_chado_node_default_title_format().
  659. *
  660. * Defines a default title format for the Chado Node API to set the titles on
  661. * Chado contact nodes based on chado fields.
  662. */
  663. function chado_contact_chado_node_default_title_format() {
  664. return '[contact.name]';
  665. }
  666. /**
  667. * Implements hook_chado_node_default_url_format().
  668. *
  669. * Designates a default URL format for contact nodes.
  670. */
  671. function chado_contact_chado_node_default_url_format() {
  672. return '/contact/[contact.name]';
  673. }
  674. /**
  675. * Implements [content_type]_chado_node_sync_select_query().
  676. *
  677. * Adds a where clause to the query to exclude the NULL contact.
  678. */
  679. function chado_contact_chado_node_sync_select_query($query) {
  680. $query['where_clauses']['title'][] = 'contact.name <> :contact_name_null1';
  681. $query['where_clauses']['title'][] = 'contact.name <> :contact_name_null2';
  682. $query['where_args']['title'][':contact_name_null1'] = 'null';
  683. $query['where_args']['title'][':contact_name_null2'] = 'NULL';
  684. return $query;
  685. }