tripal_contact.chado_node.inc 24 KB

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