tripal_contact.chado_node.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <?php
  2. /**
  3. * Implementation of tripal_contact_form().
  4. *
  5. *
  6. *
  7. * @parm $node
  8. * The node that is created when the database is initialized
  9. *
  10. * @parm $form_state
  11. * The state of the form, that has the user entered information that is neccessary for, setting
  12. * up the database tables for the contact
  13. *
  14. * @return $form
  15. * The information that was enterd allong with
  16. *
  17. */
  18. function chado_contact_form(&$node, $form_state) {
  19. $form = array();
  20. // Default values can come in the following ways:
  21. //
  22. // 1) as elements of the $node object. This occurs when editing an existing contact
  23. // 2) in the $form_state['values'] array which occurs on a failed validation or
  24. // ajax callbacks from non submit form elements
  25. // 3) in the $form_state['input'[ array which occurs on ajax callbacks from submit
  26. // form elements and the form is being rebuilt
  27. //
  28. // set form field defaults
  29. $contact_id = null;
  30. $type_id = 0;
  31. $title = '';
  32. $description = '';
  33. // if we are editing an existing node then the contact is already part of the node
  34. if (property_exists($node, 'contact')) {
  35. $contact = $node->contact;
  36. $contact_id = $contact->contact_id;
  37. // get form defaults
  38. $type_id = $contact->type_id->cvterm_id;
  39. $title = $contact->name;
  40. // get the contact default values. When this module was first created
  41. // the contact description was incorrectly stored in the $node->body field.
  42. // It is better to store it in the Chado tables. However, the 'description'
  43. // field of the contact table is only 255 characters. So, we are going
  44. // to follow the same as the contact module and store the description in
  45. // the contactprop table and leave the contact.description field blank.
  46. // however, for backwards compatibitily, we check to see if the description
  47. // is in the $node->body field. If it is we'll use that. When the node is
  48. // edited the text will be moved out of the body and into the contactprop
  49. // table where it should belong.
  50. $description = '';
  51. if (property_exists($node, 'body')) {
  52. $description = $node->body;
  53. }
  54. else {
  55. $description = $contact->description;
  56. }
  57. if (!$description) {
  58. $contactprop = tripal_contact_get_property($contact->contact_id, 'contact_description');
  59. $description = $contactprop->value;
  60. }
  61. // set the contact_id in the form
  62. $form['contact_id'] = array(
  63. '#type' => 'value',
  64. '#value' => $contact->contact_id,
  65. );
  66. }
  67. // if we are re constructing the form from a failed validation or ajax callback
  68. // then use the $form_state['values'] values
  69. if (array_key_exists('values', $form_state)) {
  70. $type_id = $form_state['values']['type_id'];
  71. $title = $form_state['values']['title'];
  72. $description = $form_state['values']['description'];
  73. }
  74. // if we are re building the form from after submission (from ajax call) then
  75. // the values are in the $form_state['input'] array
  76. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  77. $type_id = $form_state['input']['type_id'];
  78. $title = $form_state['input']['title'];
  79. $description = $form_state['input']['description'];
  80. }
  81. // get the contact types. These are those that are part of the tripal_contact
  82. // vocabulary and are children of the term 'Contact Type', so we need
  83. // to join on the cvtermpath table and select those with a distance of 1
  84. $sql = "
  85. SELECT CVTS.cvterm_id, CVTS.name
  86. FROM {cvtermpath} CVTP
  87. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  88. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  89. INNER JOIN {cv} CV ON CVTO.cv_id = CV.cv_id
  90. WHERE
  91. CV.name = 'tripal_contact' AND
  92. CVTO.name = 'Contact Type' AND
  93. CVTP.pathdistance = 1
  94. ORDER BY CVTS.name ASC
  95. ";
  96. $results = chado_query($sql);
  97. $contact_types = array();
  98. while ($contact_type = $results->fetchObject()) {
  99. $contact_types[$contact_type->cvterm_id] = $contact_type->name;
  100. if (strcmp($contact_type->name, "Person") == 0 and !$type_id) {
  101. $type_id = $contact_type->cvterm_id;
  102. }
  103. }
  104. $form['type_id'] = array(
  105. '#type' => 'select',
  106. '#title' => t('Contact Type'),
  107. '#options' => $contact_types,
  108. '#required' => TRUE,
  109. '#default_value' => $type_id,
  110. );
  111. $form['title']= array(
  112. '#type' => 'textfield',
  113. '#title' => t('Contact Name'),
  114. '#description' => t('Enter the name of this contact'),
  115. '#required' => TRUE,
  116. '#default_value' => $title,
  117. '#maxlength' => 255,
  118. );
  119. $form['description']= array(
  120. '#type' => 'textarea',
  121. '#title' => t('Contact Description'),
  122. '#description' => t('A brief description of the contact'),
  123. '#required' => TRUE,
  124. '#default_value' => $description,
  125. );
  126. // get the contact properties that the user can use for this form
  127. $properties = array();
  128. $properties[] = 'Select a Property';
  129. $sql = "
  130. SELECT CVTS.cvterm_id, CVTS.name
  131. FROM {cvtermpath} CVTP
  132. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  133. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  134. INNER JOIN {cv} CV ON CVTO.cv_id = CV.cv_id
  135. WHERE
  136. CV.name = 'tripal_contact' AND
  137. NOT CVTO.name = 'Contact Type'
  138. ORDER BY CVTS.name ASC
  139. ";
  140. $prop_types = chado_query($sql);
  141. while ($prop = $prop_types->fetchObject()) {
  142. $properties[$prop->cvterm_id] = $prop->name;
  143. }
  144. $exclude = array('contact_description');
  145. $include = array();
  146. tripal_core_properties_form($form, $form_state, 'contactprop', 'contact_id', 'tripal_contact',
  147. $properties, $contact_id, $exclude, $include, '', 'Properties');
  148. return $form;
  149. }
  150. /**
  151. * validates submission of form when adding or updating a contact node
  152. *
  153. * @ingroup tripal_contact
  154. */
  155. function chado_contact_validate($node, $form, &$form_state) {
  156. // remove surrounding white-space on submitted values
  157. $node->title = trim($node->title);
  158. $node->description = trim($node->description);
  159. // if this is a delete then don't validate
  160. if($node->op == 'Delete') {
  161. return;
  162. }
  163. // we are syncing if we do not have a node ID but we do have a contact_id. We don't
  164. // need to validate during syncing so just skip it.
  165. if (is_null($node->nid) and property_exists($node, 'contact_id') and $node->contact_id != 0) {
  166. return;
  167. }
  168. // Validating for an update
  169. if (property_exists($node, 'nid')) {
  170. // get the existing node
  171. $values = array('contact_id' => $node->contact_id);
  172. $result = tripal_core_chado_select('contact', array('*'), $values);
  173. $contact = $result[0];
  174. // if the name has changed make sure it doesn't conflict with an existing name
  175. if ($contact->name != $node->title) {
  176. $values = array('name' => $node->title);
  177. $result = tripal_core_chado_select('contact', array('contact_id'), $values);
  178. if ($result and count($result) > 0) {
  179. form_set_error('title', 'Cannot update the contact with this contact name. A contact with this name already exists.');
  180. return;
  181. }
  182. }
  183. }
  184. // Validating for an insert
  185. else {
  186. // The unique constraint for the chado contact table is: name
  187. $values = array(
  188. 'name' => $node->title,
  189. );
  190. $contact = tripal_core_chado_select('contact', array('contact_id'), $values);
  191. if ($contact and count($contact) > 0) {
  192. form_set_error('title', 'Cannot add the contact with this name. A contact with these values already exists.');
  193. return;
  194. }
  195. }
  196. }
  197. /**
  198. * Implement hook_access().
  199. *
  200. * This hook allows node modules to limit access to the node types they define.
  201. *
  202. * @param $node
  203. * The node on which the operation is to be performed, or, if it does not yet exist, the
  204. * type of node to be created
  205. *
  206. * @param $op
  207. * The operation to be performed
  208. *
  209. * @param $account
  210. * A user object representing the user for whom the operation is to be performed
  211. *
  212. * @return
  213. * If the permission for the specified operation is not set then return FALSE. If the
  214. * permission is set then return NULL as this allows other modules to disable
  215. * access. The only exception is when the $op == 'create'. We will always
  216. * return TRUE if the permission is set.
  217. *
  218. */
  219. function chado_contact_node_access($node, $op, $account ) {
  220. if ($op == 'create') {
  221. if (!user_access('create chado_contact content', $account)) {
  222. return FALSE;
  223. }
  224. return TRUE;
  225. }
  226. if ($op == 'update') {
  227. if (!user_access('edit chado_contact content', $account)) {
  228. return FALSE;
  229. }
  230. }
  231. if ($op == 'delete') {
  232. if (!user_access('delete chado_contact content', $account)) {
  233. return FALSE;
  234. }
  235. }
  236. if ($op == 'view') {
  237. if (!user_access('access chado_contact content', $account)) {
  238. return FALSE;
  239. }
  240. }
  241. return NULL;
  242. }
  243. /**
  244. * Implementation of tripal_contact_insert().
  245. *
  246. * This function inserts user entered information pertaining to the contact instance into the
  247. * 'contactauthor', 'contactprop', 'chado_contact', 'contact' talble of the database.
  248. *
  249. * @parm $node
  250. * Then node which contains the information stored within the node-ID
  251. *
  252. *
  253. */
  254. function chado_contact_insert($node) {
  255. // remove surrounding white-space on submitted values
  256. $node->title = trim($node->title);
  257. $node->description = trim($node->description);
  258. // if there is a contact_id in the $node object then this must be a sync so
  259. // we can skip adding the contact as it is already there, although
  260. // we do need to proceed with the rest of the insert
  261. if (!property_exists($node, 'contact_id')) {
  262. // insert and then get the newly inserted contact record
  263. $values = array(
  264. 'name' => $node->title,
  265. 'description' => '',
  266. 'type_id' => $node->type_id,
  267. );
  268. $contact = tripal_core_chado_insert('contact', $values);
  269. if (!$contact) {
  270. drupal_set_message(t('Unable to add contact.', 'warning'));
  271. watchdog('tripal_contact', 'Insert contact: Unable to create contact where values: %values',
  272. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  273. return;
  274. }
  275. $contact_id = $contact['contact_id'];
  276. // now add in the properties
  277. $properties = tripal_core_properties_form_retreive($node, 'tripal_contact');
  278. foreach ($properties as $property => $elements) {
  279. foreach ($elements as $rank => $value) {
  280. $status = tripal_contact_insert_property($contact_id, $property, $value, FALSE);
  281. if (!$status) {
  282. drupal_set_message("Error cannot add property: $property", "error");
  283. watchdog('t_contact', "Error cannot add property: %prop",
  284. array('%property' => $property), WATCHDOG_ERROR);
  285. }
  286. }
  287. }
  288. // add in the description as a separate property
  289. tripal_contact_insert_property($contact_id, 'contact_description', $node->description, FALSE);
  290. }
  291. else {
  292. $contact_id = $node->contact_id;
  293. }
  294. // Make sure the entry for this contact doesn't already exist in the
  295. // chado_contact table if it doesn't exist then we want to add it.
  296. $check_org_id = chado_get_id_for_node('contact', $node->nid);
  297. if (!$check_org_id) {
  298. $record = new stdClass();
  299. $record->nid = $node->nid;
  300. $record->vid = $node->vid;
  301. $record->contact_id = $contact_id;
  302. drupal_write_record('chado_contact', $record);
  303. }
  304. return TRUE;
  305. }
  306. /*
  307. *
  308. * Implements hook_update
  309. *
  310. * The purpose of the function is to allow the module to take action when an edited node is being
  311. * updated. It updates any name changes to the database tables that werec reated upon registering a contact.
  312. * As well, the database will be changed, so the user changed information will be saved to the database.
  313. *
  314. * @param $node
  315. * The node being updated
  316. *
  317. * @ingroup tripal_contact
  318. */
  319. function chado_contact_update($node) {
  320. // remove surrounding white-space on submitted values
  321. $node->title = trim($node->title);
  322. $node->description = trim($node->description);
  323. $contact_id = chado_get_id_for_node('contact', $node->nid) ;
  324. // update the contact record
  325. $match = array(
  326. 'contact_id' => $contact_id,
  327. );
  328. $values = array(
  329. 'name' => $node->title,
  330. 'description' => '',
  331. 'type_id' => $node->type_id
  332. );
  333. $status = tripal_core_chado_update('contact', $match, $values);
  334. if (!$status) {
  335. drupal_set_message("Error updating contact", "error");
  336. watchdog('t_contact', "Error updating contact", array(), WATCHDOG_ERROR);
  337. return;
  338. }
  339. // now add in the properties by first removing any the contact
  340. // already has and adding the ones we have
  341. tripal_core_chado_delete('contactprop', array('contact_id' => $contact_id));
  342. $properties = tripal_core_properties_form_retreive($node, 'tripal_contact');
  343. foreach ($properties as $property => $elements) {
  344. foreach ($elements as $rank => $value) {
  345. $status = tripal_contact_insert_property($contact_id, $property, $value, FALSE);
  346. if (!$status) {
  347. drupal_set_message("Error cannot add property: '$property'", "error");
  348. watchdog('t_contact', "Error cannot add property: '%prop'",
  349. array('%prop' => $property), WATCHDOG_ERROR);
  350. }
  351. }
  352. }
  353. // add in the description as a separate property
  354. tripal_contact_update_property($contact_id, 'contact_description', $node->description, 1);
  355. }
  356. /**
  357. * Implementation of tripal_contact_load().
  358. *
  359. *
  360. * @param $node
  361. * The node that is to be accessed from the database
  362. *
  363. * @return $node
  364. * The node with the information to be loaded into the database
  365. *
  366. */
  367. function chado_contact_load($nodes) {
  368. foreach ($nodes as $nid => $node) {
  369. // find the contact and add in the details
  370. $contact_id = chado_get_id_for_node('contact', $nid);
  371. // get the contact
  372. $values = array('contact_id' => $contact_id);
  373. $contact = tripal_core_generate_chado_var('contact', $values);
  374. // get the contact description from the contactprop table and replace
  375. // the contact.description field with this one (we don't use the contact.description
  376. // field because it is only 255 characters (too small)).
  377. $values = array(
  378. 'contact_id' => $contact->contact_id,
  379. 'type_id' => array(
  380. 'name' => 'contact_description',
  381. ),
  382. );
  383. $options = array(
  384. 'return_array' => 1,
  385. 'include_fk' => array('type_id' => 1),
  386. );
  387. $description = tripal_core_generate_chado_var('contactprop', $values, $options);
  388. if (count($description) == 1) {
  389. $description = tripal_core_expand_chado_vars($description, 'field', 'contactprop.value');
  390. $contact->description = $description[0]->value;
  391. }
  392. $nodes[$nid]->contact = $contact;
  393. }
  394. }
  395. /**
  396. * Implementation of tripal_contact_delete().
  397. *
  398. * This function takes a node and if the delete button has been chosen by the user, the contact
  399. * and it's details will be removed.Following,given the node-ID, the instance will be deleted from
  400. * the 'chado_contact' table.
  401. *
  402. * @parm $node
  403. * Then node which contains the information stored within the node-ID
  404. *
  405. */
  406. function chado_contact_delete(&$node) {
  407. $contact_id = chado_get_id_for_node('contact', $node->nid);
  408. // if we don't have a contact id for this node then this isn't a node of
  409. // type chado_contact or the entry in the chado_contact table was lost.
  410. if (!$contact_id) {
  411. return;
  412. }
  413. // Remove data from {chado_contact}, {node} and {node_revisions} tables of
  414. // drupal database
  415. $sql_del = "DELETE FROM {chado_contact} WHERE nid = :nid AND vid = :vid";
  416. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  417. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  418. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  419. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  420. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  421. // Remove data from contact and contactprop tables of chado database as well
  422. chado_query("DELETE FROM {contactprop} WHERE contact_id = :contact_id", array(':contact_id' => $contact_id));
  423. chado_query("DELETE FROM {contact} WHERE contact_id = :contact_id", array(':contact_id' => $contact_id));
  424. }