tripal_db.admin.inc

  1. 2.x tripal_db/includes/tripal_db.admin.inc
  2. 1.x tripal_db/includes/tripal_db.admin.inc

File

tripal_db/includes/tripal_db.admin.inc
View source
  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. * @ingroup tripal_db
  18. */
  19. function tripal_db_select_form() {
  20. return $form;
  21. }
  22. /**
  23. *
  24. * @ingroup tripal_db
  25. */
  26. function tripal_ajax_db_edit() {
  27. $status = TRUE;
  28. // prepare and render the form
  29. $form = tripal_core_ahah_prepare_form();
  30. $data = drupal_render($form);
  31. // bind javascript events to the new objects that will be returned
  32. // so that AHAH enabled elements will work.
  33. $settings = tripal_core_ahah_bind_events();
  34. // return the updated JSON
  35. drupal_json(
  36. array(
  37. 'status' => $status,
  38. 'data' => $data,
  39. 'settings' => $settings,
  40. )
  41. );
  42. }
  43. /**
  44. *
  45. * @ingroup tripal_db
  46. */
  47. function tripal_db_form(&$form_state = NULL, $action = 'Update') {
  48. $dbid = $form_state['values']['dbid'];
  49. if (strcmp($action,'Update')==0) {
  50. // get a list of db from chado for user to choose
  51. $sql = "SELECT * FROM {db} WHERE NOT name = 'tripal' ORDER BY name ";
  52. $results = chado_query($sql);
  53. $dbs = array();
  54. $dbs[] = '';
  55. while ($db = db_fetch_object($results)) {
  56. $dbs[$db->db_id] = $db->name;
  57. }
  58. $form['dbid'] = array(
  59. '#title' => t('External Database Name'),
  60. '#type' => 'select',
  61. '#options' => $dbs,
  62. '#ahah' => array(
  63. 'path' => 'admin/tripal/tripal_db/edit/js',
  64. 'wrapper' => 'db-edit-div',
  65. 'effect' => 'fade',
  66. 'event' => 'change',
  67. 'method' => 'replace',
  68. ),
  69. '#prefix' => '<div id="db-edit-div">',
  70. '#suffix' => '</div>',
  71. '#default_value' => $dbid,
  72. '#description' => t('Please select a database to edit'),
  73. );
  74. }
  75. else {
  76. $default_db = $form_state['values']['name'];
  77. $default_desc = $form_state['values']['description'];
  78. $default_url = $form_state['values']['url'];
  79. $default_urlprefix = $form_state['values']['urlprefix'];
  80. }
  81. // get this requested database
  82. if ($dbid) {
  83. $values = array('db_id' => $dbid);
  84. $result = tripal_core_chado_select('db', array('*'), $values);
  85. $db = $result[0];
  86. $prev_dbid = $form_state['values']['prev_dbid'];
  87. // if the database has changed then repopulate the fields with the databaes values
  88. if ($prev_dbid != $dbid) {
  89. $default_db = $db->name;
  90. $default_desc = $db->description;
  91. $default_url = $db->url;
  92. $default_urlprefix = $db->urlprefix;
  93. }
  94. // if the database did not change then keep the values in the form values
  95. else {
  96. $default_db = $form_state['values']['name'];
  97. $default_desc = $form_state['values']['description'];
  98. $default_url = $form_state['values']['url'];
  99. $default_urlprefix = $form_state['values']['urlprefix'];
  100. }
  101. }
  102. $form['form_action'] = array(
  103. '#type' => 'hidden',
  104. '#value' => $action,
  105. );
  106. // we need to distinguish between edits in a field that may have failed
  107. // and when the user selects a different database from the list.
  108. $form['prev_dbid'] = array(
  109. '#type' => 'hidden',
  110. '#value' => $dbid,
  111. );
  112. // if we want to update a database but the user has not
  113. // yet selected a database then return so we don't show the other fields
  114. // the rest of the fields will be added with the AHAH callback.
  115. if (strcmp($action,'Update')==0 and !$dbid) {
  116. return $form;
  117. }
  118. $form['name']= array(
  119. '#type' => 'textfield',
  120. '#title' => t("Database Name"),
  121. '#description' => t('Please enter the name for this external database.'),
  122. '#required' => TRUE,
  123. '#default_value' => $default_db,
  124. '#weight' => 1,
  125. '#maxlength' => 255,
  126. );
  127. $form['description']= array(
  128. '#type' => 'textarea',
  129. '#title' => t('Description'),
  130. '#description' => t('Please enter a description for this database'),
  131. '#default_value' => $default_desc,
  132. '#weight' => 2,
  133. '#maxlength' => 255,
  134. );
  135. $form['url']= array(
  136. '#type' => 'textfield',
  137. '#title' => t('URL'),
  138. '#description' => t('Please enter the web address for this database.'),
  139. '#default_value' => $default_url,
  140. '#weight' => 3,
  141. '#maxlength' => 255,
  142. );
  143. $form['urlprefix']= array(
  144. '#type' => 'textfield',
  145. '#title' => t('URL prefix'),
  146. '#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.'),
  147. '#default_value' => $default_urlprefix,
  148. '#weight' => 4,
  149. '#maxlength' => 255,
  150. );
  151. if (strcmp($action, 'Update')==0) {
  152. $form['update'] = array(
  153. '#type' => 'submit',
  154. '#value' => t('Update'),
  155. '#weight' => 5,
  156. '#executes_submit_callback' => TRUE,
  157. );
  158. $form['delete'] = array(
  159. '#type' => 'submit',
  160. '#value' => t('Delete'),
  161. '#weight' => 6,
  162. '#executes_submit_callback' => TRUE,
  163. );
  164. }
  165. else {
  166. $form['add'] = array(
  167. '#type' => 'submit',
  168. '#value' => t('Add'),
  169. '#weight' => 5,
  170. '#executes_submit_callback' => TRUE,
  171. );
  172. }
  173. return $form;
  174. }
  175. /**
  176. *
  177. * @ingroup tripal_db
  178. */
  179. function tripal_db_form_validate($form, &$form_state) {
  180. $name = trim($form_state['values']['name']);
  181. $desc = trim($form_state['values']['description']);
  182. $url = trim($form_state['values']['url']);
  183. $urlp = trim($form_state['values']['urlprefix']);
  184. $dbid = trim($form_state['values']['dbid']);
  185. $op = trim($form_state['values']['op']);
  186. $action = $form_state['values']['form_action'];
  187. // make sure the database name is unique
  188. $values = array('name' => $name);
  189. $results = tripal_core_chado_select('db', array('db_id'), $values);
  190. if (count($results) > 0 and $results[0]->db_id != $dbid) {
  191. form_set_error('name', 'The database name must be unique');
  192. }
  193. }
  194. /**
  195. *
  196. * @ingroup tripal_db
  197. */
  198. function tripal_db_form_submit($form, &$form_state) {
  199. $name = trim($form_state['values']['name']);
  200. $desc = trim($form_state['values']['description']);
  201. $url = trim($form_state['values']['url']);
  202. $urlp = trim($form_state['values']['urlprefix']);
  203. $dbid = trim($form_state['values']['dbid']);
  204. $op = trim($form_state['values']['op']);
  205. $values = array(
  206. 'name' => $name,
  207. 'description' => $desc,
  208. 'url' => $url,
  209. 'urlprefix' => $urlp,
  210. );
  211. if ($dbid) {
  212. if (strcmp($op, 'Update')==0) {
  213. $match = array('db_id' => $dbid);
  214. $success = tripal_core_chado_update('db', $match, $values);
  215. if ($success) {
  216. drupal_set_message(t("External database updated"));
  217. }
  218. else {
  219. drupal_set_message(t("Failed to update external database."));
  220. }
  221. }
  222. if (strcmp($op, 'Delete')==0) {
  223. $match = array('db_id' => $dbid);
  224. $success = tripal_core_chado_delete('db', $match);
  225. if ($success) {
  226. drupal_set_message(t("External database deleted"));
  227. }
  228. else {
  229. drupal_set_message(t("Failed to delete external database."));
  230. }
  231. }
  232. }
  233. else {
  234. $success = tripal_core_chado_insert('db', $values);
  235. if ($success) {
  236. drupal_set_message(t("External database added"));
  237. }
  238. else {
  239. drupal_set_message(t("Failed to add external database."));
  240. }
  241. }
  242. }