tripal_contact.form.inc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. }