tripal_chado.db.inc 11 KB

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