tripal_cv.cv_form.inc

  1. 2.x tripal_cv/includes/tripal_cv.cv_form.inc
  2. 3.x legacy/tripal_cv/includes/tripal_cv.cv_form.inc

Provides a form for creating & editing chado controlled vocabularies

File

tripal_cv/includes/tripal_cv.cv_form.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Provides a form for creating & editing chado controlled vocabularies
  5. */
  6. /**
  7. * Provides the actual "Select CV" form on the Update/Delete Controlled
  8. * Vocabulary page. This form also triggers the edit javascript
  9. * @todo Modify this form to use Drupal AJAX
  10. *
  11. * @ingroup tripal_cv
  12. */
  13. function tripal_cv_cv_edit_form($form, &$form_state) {
  14. // get the cv_d if form was submitted via AJAX
  15. $cv_id = 0;
  16. if (array_key_exists('values', $form_state)) {
  17. $cv_id = $form_state['values']['cv_id'];
  18. }
  19. elseif (isset($form_state['build_info']['args'][0])) {
  20. $cv_id = $form_state['build_info']['args'][0];
  21. }
  22. // get a list of db from chado for user to choose
  23. $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
  24. $results = chado_query($sql);
  25. $cvs = array();
  26. $cvs[] = 'Select a vocabulary';
  27. foreach ($results as $cv) {
  28. $cvs[$cv->cv_id] = $cv->name;
  29. }
  30. $form['cv_id'] = array(
  31. '#title' => t('Controlled Vocabulary Name'),
  32. '#type' => 'select',
  33. '#options' => $cvs,
  34. '#ajax' => array(
  35. 'callback' => 'tripal_cv_edit_form_ajax',
  36. 'wrapper' => 'cv-edit-div',
  37. 'effect' => 'fade',
  38. 'event' => 'change',
  39. 'method' => 'replace',
  40. ),
  41. '#default_value' => $cv_id,
  42. );
  43. // if we don't have a db_id then we can return the form, otherwise
  44. // add in the other fields
  45. if ($cv_id) {
  46. tripal_cv_add_cv_form_fields($form, $form_state, $cv_id);
  47. $form['update'] = array(
  48. '#type' => 'submit',
  49. '#value' => t('Update'),
  50. );
  51. $form['delete'] = array(
  52. '#type' => 'submit',
  53. '#value' => t('Delete'),
  54. '#attributes' => array('onclick' => 'if(!confirm("Really Delete?")){return false;}'),
  55. );
  56. }
  57. else {
  58. // if we don't have a dbid then this is the first time the form has
  59. // benn loaded and we need to create the div where ajax replacement elements get stored
  60. $form['div_replace'] = array(
  61. '#type' => 'item',
  62. '#prefix' => '<div id="cv-edit-div">',
  63. '#suffix' => '</div>',
  64. );
  65. }
  66. return $form;
  67. }
  68. /**
  69. * Form to add contolled vocabularies
  70. *
  71. * @ingroup tripal_cv
  72. */
  73. function tripal_cv_cv_add_form($form, &$form_state) {
  74. // add in the form fields to this form
  75. tripal_cv_add_cv_form_fields($form, $form_state);
  76. $form['add'] = array(
  77. '#type' => 'submit',
  78. '#value' => t('Add'),
  79. '#weight' => 5,
  80. );
  81. return $form;
  82. }
  83. /**
  84. * Form fields in common between the cv add & edit form.
  85. *
  86. * @ingroup tripal_cv
  87. */
  88. function tripal_cv_add_cv_form_fields(&$form, $form_state, $cv_id = NULL) {
  89. $default_name = '';
  90. $default_desc = '';
  91. if ($cv_id) {
  92. $values = array('cv_id' => $cv_id);
  93. $result = chado_select_record('cv', array('*'), $values);
  94. $cv = $result[0];
  95. $default_name = $cv->name;
  96. $default_desc = $cv->definition;
  97. }
  98. // add a fieldset for the Drupal Schema API
  99. $form['fields'] = array(
  100. '#type' => 'fieldset',
  101. '#title' => 'Controlled Vocabulary Details',
  102. '#collapsible' => 0,
  103. );
  104. $form['fields']['name']= array(
  105. '#type' => 'textfield',
  106. '#title' => t("Controlled Vocabulary name"),
  107. '#description' => t('Please enter the name for this vocabulary.'),
  108. '#required' => TRUE,
  109. '#default_value' => $default_name,
  110. '#maxlength' => 255,
  111. );
  112. $form['fields']['definition']= array(
  113. '#type' => 'textarea',
  114. '#title' => t('Description'),
  115. '#description' => t('Please enter a definition for this vocabulary'),
  116. '#default_value' => $default_desc,
  117. );
  118. return $form;
  119. }
  120. /**
  121. * Validation fucntion for tripal_cv_cv_add_form
  122. *
  123. * @ingroup tripal_cv
  124. */
  125. function tripal_cv_cv_add_form_validate($form, &$form_state) {
  126. tripal_cv_form_fields_validate($form, $form_state);
  127. }
  128. /**
  129. * Validation fucntion for tripal_cv_cv_edit_form
  130. *
  131. * @ingroup tripal_cv
  132. */
  133. function tripal_cv_cv_edit_form_validate($form, &$form_state) {
  134. tripal_cv_form_fields_validate($form, $form_state);
  135. }
  136. /**
  137. * Generic validation form for shared fields of both the edit and add forms
  138. *
  139. * @ingroup tripal_cv
  140. */
  141. function tripal_cv_form_fields_validate($form, &$form_state) {
  142. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  143. $desc = array_key_exists('definition', $form_state['values']) ? trim($form_state['values']['definition']) : '';
  144. $cv_id = array_key_exists('cv_id', $form_state['values']) ? trim($form_state['values']['cv_id']) : '';
  145. // make sure the cv name is unique
  146. $values = array('name' => $name);
  147. $results = chado_select_record('cv', array('cv_id'), $values);
  148. if (count($results) > 0 and $results[0]->cv_id != $cv_id) {
  149. form_set_error('name', 'The vocabulary name must be unique');
  150. }
  151. }
  152. /**
  153. * Submit cv add form
  154. *
  155. * @ingroup tripal_cv
  156. */
  157. function tripal_cv_cv_add_form_submit($form, &$form_state) {
  158. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  159. $desc = array_key_exists('definition', $form_state['values']) ? trim($form_state['values']['definition']) : '';
  160. $values = array(
  161. 'name' => $name,
  162. 'definition' => $desc,
  163. );
  164. $success = chado_insert_record('cv', $values);
  165. if ($success) {
  166. drupal_set_message(t("Controlled vocabulary added"));
  167. }
  168. else {
  169. drupal_set_message(t("Failed to add controlled vocabulary."));
  170. }
  171. }
  172. /**
  173. * Submit cv edit form
  174. *
  175. * @ingroup tripal_cv
  176. */
  177. function tripal_cv_cv_edit_form_submit($form, &$form_state) {
  178. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  179. $desc = array_key_exists('definition', $form_state['values']) ? trim($form_state['values']['definition']) : '';
  180. $cv_id = array_key_exists('cv_id', $form_state['values']) ? trim($form_state['values']['cv_id']) : '';
  181. $op = array_key_exists('op', $form_state['values']) ? trim($form_state['values']['op']) : '';
  182. $values = array(
  183. 'name' => $name,
  184. 'definition' => $desc,
  185. );
  186. if (strcmp($op, 'Update')==0) {
  187. $match = array('cv_id' => $cv_id);
  188. $success = chado_update_record('cv', $match, $values);
  189. if ($success) {
  190. drupal_set_message(t("Controlled vocabulary updated"));
  191. }
  192. else {
  193. drupal_set_message(t("Failed to update controlled vocabulary."));
  194. }
  195. }
  196. if (strcmp($op, 'Delete')==0) {
  197. $match = array('cv_id' => $cv_id);
  198. $success = chado_delete_record('cv', $match);
  199. if ($success) {
  200. drupal_set_message(t("Controlled vocabulary deleted"));
  201. }
  202. else {
  203. drupal_set_message(t("Failed to delete controlled vocabulary."));
  204. }
  205. }
  206. }
  207. /**
  208. * Ajax callback for the tripal_cv_form
  209. *
  210. * @ingroup tripal_cv
  211. */
  212. function tripal_cv_edit_form_ajax($form, $form_state) {
  213. $elements = array();
  214. // add in the form fields and the buttons
  215. if (array_key_exists('cv_id', $form_state['values'])) {
  216. $elements['fields'] = $form['fields'];
  217. $elements['update'] = $form['update'];
  218. $elements['delete'] = $form['delete'];
  219. }
  220. // add back in the cv-edit-div that is used for the next round of AJAX
  221. $elements['fields']['#prefix'] = '<div id="cv-edit-div">';
  222. $elements['fields']['#suffix'] = '</div">';
  223. // reset the values for the fields to the defaults
  224. $elements['fields']['name']['#value'] = $elements['fields']['name']['#default_value'];
  225. $elements['fields']['definition']['#value'] = $elements['fields']['definition']['#default_value'];
  226. //drupal_set_message('<pre>' . print_r($elements, TRUE) . '</pre>', "status");
  227. return $elements;
  228. }