tripal_db.admin.inc 11 KB

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