tripal_contact.chado_node.inc 21 KB

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