tripal_db.admin.inc

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

Provide administration of dbs & dbxrefs

File

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