tripal_contact.chado_node.inc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. <?php
  2. /**
  3. * @file
  4. * Implements drupal node hooks.
  5. *
  6. * @ingroup tripal_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_contact
  13. */
  14. function tripal_contact_node_info() {
  15. return array(
  16. 'chado_contact' => array(
  17. 'name' => t('Contact'),
  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_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 = tripal_contact_get_property($contact->contact_id, 'contact_description');
  93. $description = (isset($contactprop->value)) ? $contactprop->value : '';
  94. }
  95. // set the contact_id in the form
  96. $form['contact_id'] = array(
  97. '#type' => 'value',
  98. '#value' => $contact->contact_id,
  99. );
  100. }
  101. // if we are re constructing the form from a failed validation or ajax callback
  102. // then use the $form_state['values'] values
  103. if (array_key_exists('values', $form_state)) {
  104. $type_id = $form_state['values']['type_id'];
  105. $contactname = $form_state['values']['contactname'];
  106. $description = $form_state['values']['description'];
  107. }
  108. // if we are re building the form from after submission (from ajax call) then
  109. // the values are in the $form_state['input'] array
  110. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  111. $type_id = $form_state['input']['type_id'];
  112. $contactname = $form_state['input']['contactname'];
  113. $description = $form_state['input']['description'];
  114. }
  115. // get the contact type
  116. $type_cv = tripal_get_default_cv('contact', 'type_id');
  117. if ($type_cv->name == 'tripal_contact') {
  118. // get the contact types. If the default is the 'tripal_contact' vocabulary,
  119. // then we want terms that are part of the tripal_contact
  120. // vocabulary and are children of the term 'Contact Type', so we need
  121. // to join on the cvtermpath table and select those with a distance of 1
  122. $sql = "
  123. SELECT CVTS.cvterm_id, CVTS.name
  124. FROM {cvtermpath} CVTP
  125. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  126. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  127. INNER JOIN {cv} CV ON CVTO.cv_id = CV.cv_id
  128. WHERE
  129. CV.name = 'tripal_contact' AND
  130. CVTO.name = 'Contact Type' AND
  131. CVTP.pathdistance = 1
  132. ORDER BY CVTS.name ASC
  133. ";
  134. $results = chado_query($sql);
  135. while ($contact_type = $results->fetchObject()) {
  136. $contact_types[$contact_type->cvterm_id] = $contact_type->name;
  137. if (strcmp($contact_type->name, "Person") == 0 and !$type_id) {
  138. $type_id = $contact_type->cvterm_id;
  139. }
  140. }
  141. }
  142. else {
  143. $contact_types = tripal_get_cvterm_default_select_options('contact', 'type_id', 'contact types');
  144. $contact_types[0] = 'Select a Type';
  145. }
  146. $form['type_id'] = array(
  147. '#type' => 'select',
  148. '#title' => t('Contact Type'),
  149. '#options' => $contact_types,
  150. '#required' => TRUE,
  151. '#default_value' => $type_id,
  152. );
  153. $form['contactname']= array(
  154. '#type' => 'textfield',
  155. '#title' => t('Contact Name'),
  156. '#description' => t('Enter the name of this contact'),
  157. '#required' => TRUE,
  158. '#default_value' => $contactname,
  159. '#maxlength' => 255,
  160. );
  161. $form['description']= array(
  162. '#type' => 'textarea',
  163. '#title' => t('Contact Description'),
  164. '#description' => t('A brief description of the contact'),
  165. '#required' => TRUE,
  166. '#default_value' => $description,
  167. );
  168. // Properties Form
  169. // ----------------------------------
  170. $prop_cv = tripal_get_default_cv('contactprop', 'type_id');
  171. $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
  172. $select_options = array();
  173. // the Tripal contact vocabulary is heirarchical so if that vocab is default we
  174. // want to use the subset of terms not under the type 'Contact Type' for our
  175. // properties list.
  176. if($prop_cv->name == 'tripal_contact') {
  177. // Need to pass in our own select_options since we use cvtermpath to filter ours
  178. $select_options[] = 'Select a Property';
  179. $sql = "
  180. SELECT CVTS.cvterm_id, CVTS.name
  181. FROM {cvtermpath} CVTP
  182. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  183. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  184. INNER JOIN {cv} CV ON CVTO.cv_id = CV.cv_id
  185. WHERE
  186. CV.name = 'tripal_contact' AND
  187. NOT CVTO.name = 'Contact Type'
  188. ORDER BY CVTS.name ASC";
  189. $prop_types = chado_query($sql);
  190. while ($prop = $prop_types->fetchObject()) {
  191. // add all properties except the Citation. That property is set via the uniquename field
  192. if ($prop->name != 'Citation') {
  193. if (!isset($select_options[$prop->cvterm_id])) {
  194. $select_options[$prop->cvterm_id] = $prop->name;
  195. }
  196. }
  197. }
  198. }
  199. $details = array(
  200. 'property_table' => 'contactprop',
  201. 'chado_id' => $contact_id,
  202. 'cv_id' => $cv_id,
  203. 'select_options' => $select_options,
  204. );
  205. chado_add_node_form_properties($form, $form_state, $details);
  206. // RELATIONSHIPS FORM
  207. //---------------------------------------------
  208. $relationship_cv = tripal_get_default_cv('contact_relationship', 'type_id');
  209. $cv_id = $relationship_cv ? $relationship_cv->cv_id : NULL;
  210. $details = array(
  211. 'relationship_table' => 'contact_relationship', // the name of the _relationship table
  212. 'base_table' => 'contact', // the name of your chado base table
  213. 'base_foreign_key' => 'contact_id', // the name of the key in your base chado table
  214. 'base_key_value' => $contact_id, // the value of example_id for this record
  215. 'nodetype' => 'contact', // the human-readable name of your node type
  216. 'cv_id' => $cv_id, // the cv.cv_id of the cv governing contact_relationship.type_id
  217. 'base_name_field' => 'name', // the base table field you want to be used as the name
  218. );
  219. // Adds the form elements to your current form
  220. chado_add_node_form_relationships($form, $form_state, $details);
  221. return $form;
  222. }
  223. /**
  224. * Implements hook_validate().
  225. * Validates submission of form when adding or updating a contact node.
  226. *
  227. * @ingroup tripal_contact
  228. */
  229. function chado_contact_validate($node, $form, &$form_state) {
  230. // if this is a delete then don't validate
  231. if($node->op == 'Delete') {
  232. return;
  233. }
  234. // we are syncing if we do not have a node ID but we do have a contact_id. We don't
  235. // need to validate during syncing so just skip it.
  236. if (is_null($node->nid) and property_exists($node, 'contact_id') and $node->contact_id != 0) {
  237. return;
  238. }
  239. // remove surrounding white-space on submitted values
  240. $node->contactname = trim($node->contactname);
  241. $node->description = trim($node->description);
  242. // Validating for an update
  243. if (!is_null($node->nid)) {
  244. // get the existing node
  245. $values = array('contact_id' => $node->contact_id);
  246. $result = chado_select_record('contact', array('*'), $values);
  247. $contact = $result[0];
  248. // if the name has changed make sure it doesn't conflict with an existing name
  249. if ($contact->name != $node->contactname) {
  250. $values = array('name' => $node->contactname);
  251. $result = chado_select_record('contact', array('contact_id'), $values);
  252. if ($result and count($result) > 0) {
  253. form_set_error('contactname', 'Cannot update the contact with this contact name. A contact with this name already exists.');
  254. return;
  255. }
  256. }
  257. }
  258. // Validating for an insert
  259. else {
  260. // The unique constraint for the chado contact table is: name
  261. $values = array(
  262. 'name' => $node->contactname,
  263. );
  264. $contact = chado_select_record('contact', array('contact_id'), $values);
  265. if ($contact and count($contact) > 0) {
  266. form_set_error('contactname', 'Cannot add the contact with this name. A contact with these values already exists.');
  267. return;
  268. }
  269. }
  270. }
  271. /**
  272. * Implements hook_access().
  273. *
  274. * This hook allows node modules to limit access to the node types they define.
  275. *
  276. * @param $node
  277. * The node on which the operation is to be performed, or, if it does not yet exist, the
  278. * type of node to be created
  279. *
  280. * @param $op
  281. * The operation to be performed
  282. *
  283. * @param $account
  284. * A user object representing the user for whom the operation is to be performed
  285. *
  286. * @return
  287. * If the permission for the specified operation is not set then return FALSE. If the
  288. * permission is set then return NULL as this allows other modules to disable
  289. * access. The only exception is when the $op == 'create'. We will always
  290. * return TRUE if the permission is set.
  291. *
  292. * @ingroup tripal_contact
  293. */
  294. function chado_contact_node_access($node, $op, $account ) {
  295. $node_type = $node;
  296. if (is_object($node)) {
  297. $node_type = $node->type;
  298. }
  299. if($node_type == 'chado_contact') {
  300. if ($op == 'create') {
  301. if (!user_access('create chado_contact content', $account)) {
  302. return NODE_ACCESS_DENY;
  303. }
  304. return NODE_ACCESS_ALLOW;
  305. }
  306. if ($op == 'update') {
  307. if (!user_access('edit chado_contact content', $account)) {
  308. return NODE_ACCESS_DENY;
  309. }
  310. }
  311. if ($op == 'delete') {
  312. if (!user_access('delete chado_contact content', $account)) {
  313. return NODE_ACCESS_DENY;
  314. }
  315. }
  316. if ($op == 'view') {
  317. if (!user_access('access chado_contact content', $account)) {
  318. return NODE_ACCESS_DENY;
  319. }
  320. }
  321. }
  322. return NODE_ACCESS_IGNORE;
  323. }
  324. /**
  325. * Implements of hook_insert().
  326. *
  327. * This function inserts user entered information pertaining to the contact instance into the
  328. * 'contactauthor', 'contactprop', 'chado_contact', 'contact' talble of the database.
  329. *
  330. * @parm $node
  331. * Then node which contains the information stored within the node-ID
  332. *
  333. * @ingroup tripal_contact
  334. */
  335. function chado_contact_insert($node) {
  336. // remove surrounding white-space on submitted values
  337. $node->contactname = trim($node->contactname);
  338. $node->description = trim($node->description);
  339. // if there is a contact_id in the $node object then this must be a sync so
  340. // we can skip adding the contact as it is already there, although
  341. // we do need to proceed with the rest of the insert
  342. if (!property_exists($node, 'contact_id')) {
  343. // insert and then get the newly inserted contact record
  344. $values = array(
  345. 'name' => $node->contactname,
  346. 'description' => '',
  347. 'type_id' => $node->type_id,
  348. );
  349. $contact = chado_insert_record('contact', $values);
  350. if (!$contact) {
  351. drupal_set_message(t('Unable to add contact.', 'warning'));
  352. tripal_report_error('tripal_contact', TRIPAL_ERROR,
  353. 'Insert contact: Unable to create contact where values: %values',
  354. array('%values' => print_r($values, TRUE)));
  355. return;
  356. }
  357. $contact_id = $contact['contact_id'];
  358. // Add the description property
  359. $properties = chado_retrieve_node_form_properties($node);
  360. $contact_descrip_id = tripal_cv_get_cvterm_by_name('contact_description', NULL, 'tripal_contact');
  361. $properties[$contact_descrip_id->cvterm_id][0] = $node->description;
  362. // * Properties Form *
  363. $details = array(
  364. 'property_table' => 'contactprop',
  365. 'base_table' => 'contact',
  366. 'foreignkey_name' => 'contact_id',
  367. 'foreignkey_value' => $contact_id
  368. );
  369. chado_update_node_form_properties($node, $details, $properties);
  370. // * Relationships Form *
  371. $details = array(
  372. 'relationship_table' => 'contact_relationship', // name of the _relationship table
  373. 'foreignkey_value' => $contact_id // value of the contact_id key
  374. );
  375. chado_update_node_form_relationships($node, $details);
  376. }
  377. else {
  378. $contact_id = $node->contact_id;
  379. }
  380. // Make sure the entry for this contact doesn't already exist in the
  381. // chado_contact table if it doesn't exist then we want to add it.
  382. $check_org_id = chado_get_id_from_nid('contact', $node->nid);
  383. if (!$check_org_id) {
  384. $record = new stdClass();
  385. $record->nid = $node->nid;
  386. $record->vid = $node->vid;
  387. $record->contact_id = $contact_id;
  388. drupal_write_record('chado_contact', $record);
  389. }
  390. return TRUE;
  391. }
  392. /**
  393. * Implements hook_update
  394. *
  395. * The purpose of the function is to allow the module to take action when an edited node is being
  396. * updated. It updates any name changes to the database tables that werec reated upon registering a contact.
  397. * As well, the database will be changed, so the user changed information will be saved to the database.
  398. *
  399. * @param $node
  400. * The node being updated
  401. *
  402. * @ingroup tripal_contact
  403. */
  404. function chado_contact_update($node) {
  405. // remove surrounding white-space on submitted values
  406. $node->contactname = trim($node->contactname);
  407. $node->description = trim($node->description);
  408. $contact_id = chado_get_id_from_nid('contact', $node->nid) ;
  409. // update the contact record
  410. $match = array(
  411. 'contact_id' => $contact_id,
  412. );
  413. $values = array(
  414. 'name' => $node->contactname,
  415. 'description' => '',
  416. 'type_id' => $node->type_id
  417. );
  418. $status = chado_update_record('contact', $match, $values);
  419. if (!$status) {
  420. drupal_set_message("Error updating contact", "error");
  421. tripal_report_error('tripal_contact', TRIPAL_ERROR,
  422. "Error updating contact", array());
  423. return;
  424. }
  425. // Add the description property
  426. $properties = chado_retrieve_node_form_properties($node);
  427. $contact_descrip_id = tripal_cv_get_cvterm_by_name('contact_description', NULL, 'tripal_contact');
  428. $properties[$contact_descrip_id->cvterm_id][0] = $node->description;
  429. // now add in the properties by first removing any the contact
  430. // already has and adding the ones we have
  431. $details = array(
  432. 'property_table' => 'contactprop',
  433. 'base_table' => 'contact',
  434. 'foreignkey_name' => 'contact_id',
  435. 'foreignkey_value' => $contact_id
  436. );
  437. chado_update_node_form_properties($node, $details, $properties);
  438. // * Relationships Form *
  439. $details = array(
  440. 'relationship_table' => 'contact_relationship', // name of the _relationship table
  441. 'foreignkey_value' => $contact_id // value of the contact_id key
  442. );
  443. chado_update_node_form_relationships($node, $details);
  444. }
  445. /**
  446. * Implements hook_load().
  447. *
  448. * @param $node
  449. * The node that is to be accessed from the database
  450. *
  451. * @return $node
  452. * The node with the information to be loaded into the database
  453. *
  454. * @ingroup tripal_contact
  455. */
  456. function chado_contact_load($nodes) {
  457. foreach ($nodes as $nid => $node) {
  458. // find the contact and add in the details
  459. $contact_id = chado_get_id_from_nid('contact', $nid);
  460. // if the nid does not have a matching record then skip this node.
  461. // this can happen with orphaned nodes.
  462. if (!$contact_id) {
  463. continue;
  464. }
  465. // get the contact
  466. $values = array('contact_id' => $contact_id);
  467. $contact = chado_generate_var('contact', $values);
  468. // get the contact description from the contactprop table and replace
  469. // the contact.description field with this one (we don't use the contact.description
  470. // field because it is only 255 characters (too small)).
  471. $values = array(
  472. 'contact_id' => $contact->contact_id,
  473. 'type_id' => array(
  474. 'name' => 'contact_description',
  475. ),
  476. );
  477. $options = array(
  478. 'return_array' => 1,
  479. 'include_fk' => array('type_id' => 1),
  480. );
  481. $description = chado_generate_var('contactprop', $values, $options);
  482. if (count($description) == 1) {
  483. $description = chado_expand_var($description, 'field', 'contactprop.value');
  484. $contact->description = $description[0]->value;
  485. }
  486. $nodes[$nid]->contact = $contact;
  487. }
  488. }
  489. /**
  490. * Implements hook_delete().
  491. *
  492. * This function takes a node and if the delete button has been chosen by the user, the contact
  493. * and it's details will be removed.Following,given the node-ID, the instance will be deleted from
  494. * the 'chado_contact' table.
  495. *
  496. * @parm $node
  497. * Then node which contains the information stored within the node-ID
  498. *
  499. * @ingroup tripal_contact
  500. */
  501. function chado_contact_delete(&$node) {
  502. $contact_id = chado_get_id_from_nid('contact', $node->nid);
  503. // if we don't have a contact id for this node then this isn't a node of
  504. // type chado_contact or the entry in the chado_contact table was lost.
  505. if (!$contact_id) {
  506. return;
  507. }
  508. // Remove data from {chado_contact}, {node} and {node_revisions} tables of
  509. // drupal database
  510. $sql_del = "DELETE FROM {chado_contact} WHERE nid = :nid AND vid = :vid";
  511. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  512. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  513. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  514. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  515. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  516. // Remove data from contact and contactprop tables of chado database as well
  517. chado_query("DELETE FROM {contactprop} WHERE contact_id = :contact_id", array(':contact_id' => $contact_id));
  518. chado_query("DELETE FROM {contact} WHERE contact_id = :contact_id", array(':contact_id' => $contact_id));
  519. }
  520. /**
  521. * Implements hook_node_view().
  522. *
  523. * @ingroup tripal_contact
  524. */
  525. function tripal_contact_node_view($node, $view_mode, $langcode) {
  526. switch ($node->type) {
  527. case 'chado_contact':
  528. // Show feature browser and counts
  529. if ($view_mode == 'full') {
  530. $node->content['tripal_contact_base'] = array(
  531. '#markup' => theme('tripal_contact_base', array('node' => $node)),
  532. '#tripal_toc_id' => 'base',
  533. '#tripal_toc_title' => 'Overview',
  534. '#weight' => -100,
  535. );
  536. $node->content['tripal_contact_properties'] = array(
  537. '#markup' => theme('tripal_contact_properties', array('node' => $node)),
  538. '#tripal_toc_id' => 'properties',
  539. '#tripal_toc_title' => 'Properties',
  540. );
  541. $node->content['tripal_contact_publications'] = array(
  542. '#markup' => theme('tripal_contact_publications', array('node' => $node)),
  543. '#tripal_toc_id' => 'publications',
  544. '#tripal_toc_title' => 'Publications',
  545. );
  546. $node->content['tripal_contact_relationships'] = array(
  547. '#markup' => theme('tripal_contact_relationships', array('node' => $node)),
  548. '#tripal_toc_id' => 'relationships',
  549. '#tripal_toc_title' => 'Relationships',
  550. );
  551. }
  552. if ($view_mode == 'teaser') {
  553. $node->content['tripal_contact_teaser'] = array(
  554. '#markup' => theme('tripal_contact_teaser', array('node' => $node)),
  555. );
  556. }
  557. break;
  558. }
  559. }
  560. /**
  561. * Implements hook_node_presave().
  562. *
  563. * @ingroup tripal_contact
  564. */
  565. function tripal_contact_node_presave($node) {
  566. switch ($node->type) {
  567. case 'chado_contact':
  568. // for a form submission the 'contactname' field will be set,
  569. // for a sync, we must pull from the contact object
  570. if (property_exists($node, 'contactname')) {
  571. // set the title
  572. $node->title = $node->contactname;
  573. }
  574. else if (property_exists($node, 'contact')) {
  575. $node->title = $node->contact->name;
  576. }
  577. break;
  578. }
  579. }