tripal_contact.chado_node.inc 21 KB

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