cv_form.inc 7.5 KB

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