tripal_db.admin.inc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. /**
  3. *
  4. *
  5. * @ingroup tripal_db
  6. */
  7. function tripal_db_admin_page() {
  8. $add_url = url("admin/tripal/tripal_db/add_db");
  9. $output = "<a href=\"$add_url\">Add a new external database</a>";
  10. $output .= drupal_get_form('tripal_db_select_form');
  11. $output .= '<div id="db-edit-div">Please select a database above to view or edit</div>';
  12. return $output;
  13. }
  14. /**
  15. *
  16. */
  17. function tripal_db_select_form() {
  18. return $form;
  19. }
  20. /**
  21. *
  22. * @param $form
  23. * @param $form_state
  24. *
  25. * @ingroup tripal_db
  26. */
  27. function tripal_db_db_edit_form($form, &$form_state) {
  28. // get the dbid if form was submitted via AJAX
  29. $dbid = 0;
  30. if (array_key_exists('values', $form_state)) {
  31. $dbid = $form_state['values']['dbid'];
  32. }
  33. // get a list of db from chado for user to choose
  34. $sql = "SELECT * FROM {db} WHERE NOT name = 'tripal' ORDER BY name ";
  35. $results = chado_query($sql);
  36. $dbs = array();
  37. $dbs[] = 'Select a database';
  38. foreach ($results as $db) {
  39. $dbs[$db->db_id] = $db->name;
  40. }
  41. $form['dbid'] = array(
  42. '#title' => t('External Database Name'),
  43. '#type' => 'select',
  44. '#options' => $dbs,
  45. '#ajax' => array(
  46. 'callback' => 'tripal_db_edit_form_ajax',
  47. 'wrapper' => 'db-edit-div',
  48. 'effect' => 'fade',
  49. 'event' => 'change',
  50. 'method' => 'replace',
  51. ),
  52. '#default_value' => $dbid,
  53. );
  54. // if we don't have a db_id then we can return the form, otherwise
  55. // add in the other fields
  56. if ($dbid) {
  57. tripal_db_add_db_form_fields($form, $form_state, $dbid);
  58. $form['update'] = array(
  59. '#type' => 'submit',
  60. '#value' => t('Update'),
  61. '#weight' => 5,
  62. );
  63. $form['delete'] = array(
  64. '#type' => 'submit',
  65. '#value' => t('Delete'),
  66. '#weight' => 6,
  67. );
  68. }
  69. else {
  70. // if we don't have a dbid then this is the first time the form has
  71. // benn loaded and we need to create the div where ajax replacement elements get stored
  72. $form['div_replace'] = array(
  73. '#type' => 'item',
  74. '#prefix' => '<div id="db-edit-div">',
  75. '#suffix' => '</div>',
  76. );
  77. }
  78. return $form;
  79. }
  80. /**
  81. *
  82. * @param $form
  83. * @param $form_state
  84. *
  85. * @ingroup tripal_db
  86. */
  87. function tripal_db_db_add_form($form, $form_state) {
  88. // add in the form fields to this form
  89. tripal_db_add_db_form_fields($form, $form_state);
  90. $form['add'] = array(
  91. '#type' => 'submit',
  92. '#value' => t('Add'),
  93. '#weight' => 5,
  94. );
  95. return $form;
  96. }
  97. /**
  98. *
  99. * @param $form
  100. * @param $form_state
  101. * @param $dbid
  102. *
  103. * @ingroup tripal_db
  104. */
  105. function tripal_db_add_db_form_fields(&$form, $form_state, $dbid = NULL) {
  106. $default_db = '';
  107. $default_desc = '';
  108. $default_url = '';
  109. $default_urlprefix = '';
  110. // get the default values from the database first
  111. if ($dbid) {
  112. $values = array('db_id' => $dbid);
  113. $result = tripal_core_chado_select('db', array('*'), $values);
  114. $db = $result[0];
  115. $default_db = $db->name;
  116. $default_desc = $db->description;
  117. $default_url = $db->url;
  118. $default_urlprefix = $db->urlprefix;
  119. }
  120. // add a fieldset for the Drupal Schema API
  121. $form['fields'] = array(
  122. '#type' => 'fieldset',
  123. '#title' => 'Database Details',
  124. '#collapsible' => 0,
  125. );
  126. $form['fields']['name']= array(
  127. '#type' => 'textfield',
  128. '#title' => t("Database Name"),
  129. '#description' => t('Please enter the name for this external database.'),
  130. '#required' => TRUE,
  131. '#default_value' => $default_db,
  132. '#maxlength' => 255,
  133. );
  134. $form['fields']['description']= array(
  135. '#type' => 'textarea',
  136. '#title' => t('Description'),
  137. '#description' => t('Please enter a description for this database'),
  138. '#default_value' => $default_desc,
  139. '#maxlength' => 255,
  140. );
  141. $form['fields']['url']= array(
  142. '#type' => 'textfield',
  143. '#title' => t('URL'),
  144. '#description' => t('Please enter the web address for this database.'),
  145. '#default_value' => $default_url,
  146. '#maxlength' => 255,
  147. );
  148. $form['fields']['urlprefix']= array(
  149. '#type' => 'textfield',
  150. '#title' => t('URL prefix'),
  151. '#description' => t('Tripal can provide links to external databases when accession numbers or unique identifiers are known. Typically, a database will provide a unique web address for each accession and the accession usually is the last component of the page address. Please enter the web address, minus the accession number for this database. When an accession number is present, Tripal will combine this web address with the accession and provide a link to the external site.'),
  152. '#default_value' => $default_urlprefix,
  153. '#maxlength' => 255,
  154. );
  155. return $form;
  156. }
  157. /**
  158. * Validation fucntion for tripal_db_db_add_form
  159. * @param $form
  160. * @param $form_state
  161. *
  162. * @ingroup tripal_db
  163. */
  164. function tripal_db_db_add_form_validate($form, &$form_state) {
  165. tripal_db_form_fields_validate($form, $form_state);
  166. }
  167. /**
  168. * Validation fucntion for tripal_db_db_edit_form
  169. * @param unknown_type $form
  170. * @param unknown_type $form_state
  171. *
  172. * @ingroup tripal_db
  173. */
  174. function tripal_db_db_edit_form_validate($form, &$form_state) {
  175. tripal_db_form_fields_validate($form, $form_state);
  176. }
  177. /**
  178. * Genetic validation form for shared fields of both the edit and add forms
  179. * @param $form
  180. * @param $form_state
  181. *
  182. * @ingroup tripal_db
  183. */
  184. function tripal_db_form_fields_validate($form, &$form_state) {
  185. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  186. $desc = array_key_exists('description', $form_state['values']) ? trim($form_state['values']['description']) : '';
  187. $url = array_key_exists('url', $form_state['values']) ? trim($form_state['values']['url']) : '';
  188. $urlp = array_key_exists('urlprefix', $form_state['values']) ? trim($form_state['values']['urlprefix']) : '';
  189. $dbid = array_key_exists('dbid', $form_state['values']) ? trim($form_state['values']['dbid']) : '';
  190. // make sure the database name is unique
  191. $values = array('name' => $name);
  192. $results = tripal_core_chado_select('db', array('db_id'), $values);
  193. if (count($results) > 0 and $results[0]->db_id != $dbid) {
  194. form_set_error('name', 'The database name must be unique');
  195. }
  196. }
  197. /**
  198. *
  199. * @param $form
  200. * @param $form_state
  201. *
  202. * @ingroup tripal_db
  203. */
  204. function tripal_db_db_add_form_submit($form, &$form_state) {
  205. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  206. $desc = array_key_exists('description', $form_state['values']) ? trim($form_state['values']['description']) : '';
  207. $url = array_key_exists('url', $form_state['values']) ? trim($form_state['values']['url']) : '';
  208. $urlp = array_key_exists('urlprefix', $form_state['values']) ? trim($form_state['values']['urlprefix']) : '';
  209. $values = array(
  210. 'name' => $name,
  211. 'description' => $desc,
  212. 'url' => $url,
  213. 'urlprefix' => $urlp,
  214. );
  215. $success = tripal_core_chado_insert('db', $values);
  216. if ($success) {
  217. drupal_set_message(t("External database added"));
  218. }
  219. else {
  220. drupal_set_message(t("Failed to add external database."));
  221. }
  222. }
  223. /**
  224. *
  225. * @param $form
  226. * @param $form_state
  227. *
  228. * @ingroup tripal_db
  229. */
  230. function tripal_db_db_edit_form_submit($form, &$form_state) {
  231. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  232. $desc = array_key_exists('description', $form_state['values']) ? trim($form_state['values']['description']) : '';
  233. $url = array_key_exists('url', $form_state['values']) ? trim($form_state['values']['url']) : '';
  234. $urlp = array_key_exists('urlprefix', $form_state['values']) ? trim($form_state['values']['urlprefix']) : '';
  235. $dbid = array_key_exists('dbid', $form_state['values']) ? trim($form_state['values']['dbid']) : '';
  236. $op = trim($form_state['values']['op']);
  237. $values = array(
  238. 'name' => $name,
  239. 'description' => $desc,
  240. 'url' => $url,
  241. 'urlprefix' => $urlp,
  242. );
  243. if (strcmp($op, 'Update')==0) {
  244. $match = array('db_id' => $dbid);
  245. $success = tripal_core_chado_update('db', $match, $values);
  246. if ($success) {
  247. drupal_set_message(t("External database updated"));
  248. }
  249. else {
  250. drupal_set_message(t("Failed to update external database."));
  251. }
  252. }
  253. if (strcmp($op, 'Delete')==0) {
  254. $match = array('db_id' => $dbid);
  255. $success = tripal_core_chado_delete('db', $match);
  256. if ($success) {
  257. drupal_set_message(t("External database deleted"));
  258. }
  259. else {
  260. drupal_set_message(t("Failed to delete external database."));
  261. }
  262. }
  263. }
  264. /**
  265. * Ajax callback for the tripal_db_form
  266. * @ingroup tripal_db
  267. */
  268. function tripal_db_edit_form_ajax($form, $form_state) {
  269. $elements = array();
  270. // add in the form fields and the buttons
  271. if (array_key_exists('dbid', $form_state['values'])) {
  272. $elements['fields'] = $form['fields'];
  273. $elements['update'] = $form['update'];
  274. $elements['delete'] = $form['delete'];
  275. }
  276. // add back in the db-edit-div that is used for the next round of AJAX
  277. $elements['fields']['#prefix'] = '<div id="db-edit-div">';
  278. $elements['fields']['#suffix'] = '</div">';
  279. // reset the values for the fields to the defaults
  280. $elements['fields']['name']['#value'] = $elements['fields']['name']['#default_value'];
  281. $elements['fields']['description']['#value'] = $elements['fields']['description']['#default_value'];
  282. $elements['fields']['url']['#value'] = $elements['fields']['url']['#default_value'];
  283. $elements['fields']['urlprefix']['#value'] = $elements['fields']['urlprefix']['#default_value'];
  284. //drupal_set_message('<pre>' . print_r($elements, TRUE) . '</pre>', "status");
  285. return $elements;
  286. }