cv_form.inc 7.5 KB

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