tripal_db.admin.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. * @param $form
  40. * @param $form_state
  41. *
  42. * @ingroup tripal_db
  43. */
  44. function tripal_db_db_edit_form($form, &$form_state) {
  45. // get the dbid if form was submitted via AJAX
  46. $dbid = 0;
  47. if (array_key_exists('values', $form_state)) {
  48. $dbid = $form_state['values']['dbid'];
  49. }
  50. elseif (isset($form_state['build_info']['args'][0])) {
  51. $dbid = $form_state['build_info']['args'][0];
  52. }
  53. // get a list of db from chado for user to choose
  54. $sql = "SELECT * FROM {db} WHERE NOT name = 'tripal' ORDER BY name ";
  55. $results = chado_query($sql);
  56. $dbs = array();
  57. $dbs[] = 'Select a database';
  58. foreach ($results as $db) {
  59. $dbs[$db->db_id] = $db->name;
  60. }
  61. $form['dbid'] = array(
  62. '#title' => t('External Database Name'),
  63. '#type' => 'select',
  64. '#options' => $dbs,
  65. '#ajax' => array(
  66. 'callback' => 'tripal_db_edit_form_ajax',
  67. 'wrapper' => 'db-edit-div',
  68. 'effect' => 'fade',
  69. 'event' => 'change',
  70. 'method' => 'replace',
  71. ),
  72. '#default_value' => $dbid,
  73. );
  74. // if we don't have a db_id then we can return the form, otherwise
  75. // add in the other fields
  76. if ($dbid) {
  77. tripal_db_add_db_form_fields($form, $form_state, $dbid);
  78. $form['update'] = array(
  79. '#type' => 'submit',
  80. '#value' => t('Update'),
  81. );
  82. $form['delete'] = array(
  83. '#type' => 'submit',
  84. '#value' => t('Delete'),
  85. '#attributes' => array('onclick' => 'if(!confirm("Really Delete?")){return false;}'),
  86. );
  87. }
  88. else {
  89. // if we don't have a dbid then this is the first time the form has
  90. // benn loaded and we need to create the div where ajax replacement elements get stored
  91. $form['div_replace'] = array(
  92. '#type' => 'item',
  93. '#prefix' => '<div id="db-edit-div">',
  94. '#suffix' => '</div>',
  95. );
  96. }
  97. return $form;
  98. }
  99. /**
  100. *
  101. * @param $form
  102. * @param $form_state
  103. *
  104. * @ingroup tripal_db
  105. */
  106. function tripal_db_db_add_form($form, $form_state) {
  107. // add in the form fields to this form
  108. tripal_db_add_db_form_fields($form, $form_state);
  109. $form['add'] = array(
  110. '#type' => 'submit',
  111. '#value' => t('Add'),
  112. '#weight' => 5,
  113. );
  114. return $form;
  115. }
  116. /**
  117. *
  118. * @param $form
  119. * @param $form_state
  120. * @param $dbid
  121. *
  122. * @ingroup tripal_db
  123. */
  124. function tripal_db_add_db_form_fields(&$form, $form_state, $dbid = NULL) {
  125. $default_db = '';
  126. $default_desc = '';
  127. $default_url = '';
  128. $default_urlprefix = '';
  129. // get the default values from the database first
  130. if ($dbid) {
  131. $values = array('db_id' => $dbid);
  132. $result = tripal_core_chado_select('db', array('*'), $values);
  133. $db = $result[0];
  134. $default_db = $db->name;
  135. $default_desc = $db->description;
  136. $default_url = $db->url;
  137. $default_urlprefix = $db->urlprefix;
  138. }
  139. // add a fieldset for the Drupal Schema API
  140. $form['fields'] = array(
  141. '#type' => 'fieldset',
  142. '#title' => 'Database Details',
  143. '#collapsible' => 0,
  144. );
  145. $form['fields']['name']= array(
  146. '#type' => 'textfield',
  147. '#title' => t("Database Name"),
  148. '#description' => t('Please enter the name for this external database.'),
  149. '#required' => TRUE,
  150. '#default_value' => $default_db,
  151. '#maxlength' => 255,
  152. );
  153. $form['fields']['description']= array(
  154. '#type' => 'textarea',
  155. '#title' => t('Description'),
  156. '#description' => t('Please enter a description for this database'),
  157. '#default_value' => $default_desc,
  158. '#maxlength' => 255,
  159. );
  160. $form['fields']['url']= array(
  161. '#type' => 'textfield',
  162. '#title' => t('URL'),
  163. '#description' => t('Please enter the web address for this database.'),
  164. '#default_value' => $default_url,
  165. '#maxlength' => 255,
  166. );
  167. $form['fields']['urlprefix']= array(
  168. '#type' => 'textfield',
  169. '#title' => t('URL prefix'),
  170. '#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.'),
  171. '#default_value' => $default_urlprefix,
  172. '#maxlength' => 255,
  173. );
  174. return $form;
  175. }
  176. /**
  177. * Validation fucntion for tripal_db_db_add_form
  178. * @param $form
  179. * @param $form_state
  180. *
  181. * @ingroup tripal_db
  182. */
  183. function tripal_db_db_add_form_validate($form, &$form_state) {
  184. tripal_db_form_fields_validate($form, $form_state);
  185. }
  186. /**
  187. * Validation fucntion for tripal_db_db_edit_form
  188. * @param unknown_type $form
  189. * @param unknown_type $form_state
  190. *
  191. * @ingroup tripal_db
  192. */
  193. function tripal_db_db_edit_form_validate($form, &$form_state) {
  194. tripal_db_form_fields_validate($form, $form_state);
  195. }
  196. /**
  197. * Genetic validation form for shared fields of both the edit and add forms
  198. * @param $form
  199. * @param $form_state
  200. *
  201. * @ingroup tripal_db
  202. */
  203. function tripal_db_form_fields_validate($form, &$form_state) {
  204. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  205. $desc = array_key_exists('description', $form_state['values']) ? trim($form_state['values']['description']) : '';
  206. $url = array_key_exists('url', $form_state['values']) ? trim($form_state['values']['url']) : '';
  207. $urlp = array_key_exists('urlprefix', $form_state['values']) ? trim($form_state['values']['urlprefix']) : '';
  208. $dbid = array_key_exists('dbid', $form_state['values']) ? trim($form_state['values']['dbid']) : '';
  209. // make sure the database name is unique
  210. $values = array('name' => $name);
  211. $results = tripal_core_chado_select('db', array('db_id'), $values);
  212. if (count($results) > 0 and $results[0]->db_id != $dbid) {
  213. form_set_error('name', 'The database name must be unique');
  214. }
  215. }
  216. /**
  217. *
  218. * @param $form
  219. * @param $form_state
  220. *
  221. * @ingroup tripal_db
  222. */
  223. function tripal_db_db_add_form_submit($form, &$form_state) {
  224. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  225. $desc = array_key_exists('description', $form_state['values']) ? trim($form_state['values']['description']) : '';
  226. $url = array_key_exists('url', $form_state['values']) ? trim($form_state['values']['url']) : '';
  227. $urlp = array_key_exists('urlprefix', $form_state['values']) ? trim($form_state['values']['urlprefix']) : '';
  228. $values = array(
  229. 'name' => $name,
  230. 'description' => $desc,
  231. 'url' => $url,
  232. 'urlprefix' => $urlp,
  233. );
  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. /**
  243. *
  244. * @param $form
  245. * @param $form_state
  246. *
  247. * @ingroup tripal_db
  248. */
  249. function tripal_db_db_edit_form_submit($form, &$form_state) {
  250. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  251. $desc = array_key_exists('description', $form_state['values']) ? trim($form_state['values']['description']) : '';
  252. $url = array_key_exists('url', $form_state['values']) ? trim($form_state['values']['url']) : '';
  253. $urlp = array_key_exists('urlprefix', $form_state['values']) ? trim($form_state['values']['urlprefix']) : '';
  254. $dbid = array_key_exists('dbid', $form_state['values']) ? trim($form_state['values']['dbid']) : '';
  255. $op = trim($form_state['values']['op']);
  256. $values = array(
  257. 'name' => $name,
  258. 'description' => $desc,
  259. 'url' => $url,
  260. 'urlprefix' => $urlp,
  261. );
  262. if (strcmp($op, 'Update')==0) {
  263. $match = array('db_id' => $dbid);
  264. $success = tripal_core_chado_update('db', $match, $values);
  265. if ($success) {
  266. drupal_set_message(t("External database updated"));
  267. }
  268. else {
  269. drupal_set_message(t("Failed to update external database."));
  270. }
  271. }
  272. if (strcmp($op, 'Delete')==0) {
  273. $match = array('db_id' => $dbid);
  274. $success = tripal_core_chado_delete('db', $match);
  275. if ($success) {
  276. drupal_set_message(t("External database deleted"));
  277. }
  278. else {
  279. drupal_set_message(t("Failed to delete external database."));
  280. }
  281. }
  282. }
  283. /**
  284. * Ajax callback for the tripal_db_form
  285. * @ingroup tripal_db
  286. */
  287. function tripal_db_edit_form_ajax($form, $form_state) {
  288. $elements = array();
  289. // add in the form fields and the buttons
  290. if (array_key_exists('dbid', $form_state['values'])) {
  291. $elements['fields'] = $form['fields'];
  292. $elements['update'] = $form['update'];
  293. $elements['delete'] = $form['delete'];
  294. }
  295. // add back in the db-edit-div that is used for the next round of AJAX
  296. $elements['fields']['#prefix'] = '<div id="db-edit-div">';
  297. $elements['fields']['#suffix'] = '</div">';
  298. // reset the values for the fields to the defaults
  299. $elements['fields']['name']['#value'] = $elements['fields']['name']['#default_value'];
  300. $elements['fields']['description']['#value'] = $elements['fields']['description']['#default_value'];
  301. $elements['fields']['url']['#value'] = $elements['fields']['url']['#default_value'];
  302. $elements['fields']['urlprefix']['#value'] = $elements['fields']['urlprefix']['#default_value'];
  303. //drupal_set_message('<pre>' . print_r($elements, TRUE) . '</pre>', "status");
  304. return $elements;
  305. }