tripal_db.admin.inc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. '#suffix' => '<div id="db-edit-div"></div>',
  53. '#default_value' => $dbid,
  54. );
  55. // if we don't have a db_id then we can safetly return the form
  56. if ($dbid) {
  57. // if we are here because of an AJAX calback, that means the database select
  58. // box was changed and the form is being rebuilt on an AJAX call not on a
  59. // form validation error. Therefore, we want to provide the dbid
  60. if (array_key_exists('triggering_element', $form_state) and
  61. $form_statte['triggerming_element']['#ajax']['callback'] = tripal_db_edit_form_ajax) {
  62. tripal_db_add_db_form_fields($form, $form_state, $dbid);
  63. }
  64. else {
  65. tripal_db_add_db_form_fields($form, $form_state);
  66. }
  67. $form['update'] = array(
  68. '#type' => 'submit',
  69. '#value' => t('Update'),
  70. '#weight' => 5,
  71. );
  72. $form['delete'] = array(
  73. '#type' => 'submit',
  74. '#value' => t('Delete'),
  75. '#weight' => 6,
  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 databae first
  111. if ($dbid) {
  112. $values = array('db_id' => $dbid);
  113. $db = tripal_core_chado_select('db', array('*'), $values);
  114. $default_db = $db[0]->name;
  115. $default_desc = $db[0]->description;
  116. $default_url = $db[0]->url;
  117. $default_urlprefix = $db[0]->urlprefix;
  118. }
  119. // set the default value to the form_state if those exist
  120. elseif (array_key_exists('values', $form_state)) {
  121. $default_db = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : $default_db;
  122. $default_desc = array_key_exists('description', $form_state['values']) ? $form_state['values']['description'] : $default_desc;
  123. $default_url = array_key_exists('url', $form_state['values']) ? $form_state['values']['url'] : $default_url;
  124. $default_urlprefix = array_key_exists('urlprefix', $form_state['values']) ? $form_state['values']['urlprefix'] : $default_urlprefix;
  125. }
  126. // add a fieldset for the Drupal Schema API
  127. $form['fields'] = array(
  128. '#type' => 'fieldset',
  129. '#title' => 'Database Details',
  130. '#collapsible' => 0,
  131. );
  132. $form['fields']['name']= array(
  133. '#type' => 'textfield',
  134. '#title' => t("Database Name"),
  135. '#description' => t('Please enter the name for this external database.'),
  136. '#required' => TRUE,
  137. '#default_value' => $default_db,
  138. '#weight' => 1,
  139. '#maxlength' => 255,
  140. );
  141. $form['fields']['description']= array(
  142. '#type' => 'textarea',
  143. '#title' => t('Description'),
  144. '#description' => t('Please enter a description for this database'),
  145. '#default_value' => $default_desc,
  146. '#weight' => 2,
  147. '#maxlength' => 255,
  148. );
  149. $form['fields']['url']= array(
  150. '#type' => 'textfield',
  151. '#title' => t('URL'),
  152. '#description' => t('Please enter the web address for this database.'),
  153. '#default_value' => $default_url,
  154. '#weight' => 3,
  155. '#maxlength' => 255,
  156. );
  157. $form['fields']['urlprefix']= array(
  158. '#type' => 'textfield',
  159. '#title' => t('URL prefix'),
  160. '#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.'),
  161. '#default_value' => $default_urlprefix,
  162. '#weight' => 4,
  163. '#maxlength' => 255,
  164. );
  165. return $form;
  166. }
  167. /**
  168. * Validation fucntion for tripal_db_db_add_form
  169. * @param $form
  170. * @param $form_state
  171. *
  172. * @ingroup tripal_db
  173. */
  174. function tripal_db_db_add_form_validate($form, &$form_state) {
  175. tripal_db_form_fields_validate($form, $form_state);
  176. }
  177. /**
  178. * Validation fucntion for tripal_db_db_edit_form
  179. * @param unknown_type $form
  180. * @param unknown_type $form_state
  181. *
  182. * @ingroup tripal_db
  183. */
  184. function tripal_db_db_edit_form_validate($form, &$form_state) {
  185. tripal_db_form_fields_validate($form, $form_state);
  186. }
  187. /**
  188. * Genetic validation form for shared fields of both the edit and add forms
  189. * @param $form
  190. * @param $form_state
  191. *
  192. * @ingroup tripal_db
  193. */
  194. function tripal_db_form_fields_validate($form, &$form_state) {
  195. $name = trim($form_state['values']['name']);
  196. $desc = trim($form_state['values']['description']);
  197. $url = trim($form_state['values']['url']);
  198. $urlp = trim($form_state['values']['urlprefix']);
  199. // make sure the database name is unique
  200. $values = array('name' => $name);
  201. $results = tripal_core_chado_select('db', array('db_id'), $values);
  202. if (count($results) > 0 and $results[0]->db_id != $dbid) {
  203. form_set_error('name', 'The database name must be unique');
  204. }
  205. }
  206. /**
  207. *
  208. * @param $form
  209. * @param $form_state
  210. *
  211. * @ingroup tripal_db
  212. */
  213. function tripal_db_db_add_form_submit($form, &$form_state) {
  214. $name = trim($form_state['values']['name']);
  215. $desc = trim($form_state['values']['description']);
  216. $url = trim($form_state['values']['url']);
  217. $urlp = trim($form_state['values']['urlprefix']);
  218. $values = array(
  219. 'name' => $name,
  220. 'description' => $desc,
  221. 'url' => $url,
  222. 'urlprefix' => $urlp,
  223. );
  224. $success = tripal_core_chado_insert('db', $values);
  225. if ($success) {
  226. drupal_set_message(t("External database added"));
  227. }
  228. else {
  229. drupal_set_message(t("Failed to add external database."));
  230. }
  231. }
  232. /**
  233. *
  234. * @param $form
  235. * @param $form_state
  236. *
  237. * @ingroup tripal_db
  238. */
  239. function tripal_db_db_edit_form_submit($form, &$form_state) {
  240. $name = trim($form_state['values']['name']);
  241. $desc = trim($form_state['values']['description']);
  242. $url = trim($form_state['values']['url']);
  243. $urlp = trim($form_state['values']['urlprefix']);
  244. $dbid = trim($form_state['values']['dbid']);
  245. $op = trim($form_state['values']['op']);
  246. $values = array(
  247. 'name' => $name,
  248. 'description' => $desc,
  249. 'url' => $url,
  250. 'urlprefix' => $urlp,
  251. );
  252. if (strcmp($op, 'Update')==0) {
  253. $match = array('db_id' => $dbid);
  254. $success = tripal_core_chado_update('db', $match, $values);
  255. if ($success) {
  256. drupal_set_message(t("External database updated"));
  257. }
  258. else {
  259. drupal_set_message(t("Failed to update external database."));
  260. }
  261. }
  262. if (strcmp($op, 'Delete')==0) {
  263. $match = array('db_id' => $dbid);
  264. $success = tripal_core_chado_delete('db', $match);
  265. if ($success) {
  266. drupal_set_message(t("External database deleted"));
  267. }
  268. else {
  269. drupal_set_message(t("Failed to delete external database."));
  270. }
  271. }
  272. }
  273. /**
  274. * Ajax callback for the tripal_db_form
  275. * @ingroup tripal_db
  276. */
  277. function tripal_db_edit_form_ajax($form, $form_state) {
  278. unset($form['dbid']);
  279. return $form;
  280. }