tripal_chado.db.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 = array();
  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 = array();
  59. $dbs[] = 'Select a database';
  60. foreach ($results as $db) {
  61. $dbs[$db->db_id] = $db->name;
  62. }
  63. $form['dbid'] = array(
  64. '#title' => t('External Database Name'),
  65. '#type' => 'select',
  66. '#options' => $dbs,
  67. '#ajax' => array(
  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'] = array(
  81. '#type' => 'submit',
  82. '#value' => t('Update'),
  83. );
  84. $form['delete'] = array(
  85. '#type' => 'submit',
  86. '#value' => t('Delete'),
  87. '#attributes' => array('onclick' => 'if(!confirm("Really Delete?")){return false;}'),
  88. );
  89. }
  90. else {
  91. // if we don't have a dbid then this is the first time the form has
  92. // benn loaded and we need to create the div where ajax replacement elements get stored
  93. $form['div_replace'] = array(
  94. '#type' => 'item',
  95. '#prefix' => '<div id="db-edit-div">',
  96. '#suffix' => '</div>',
  97. );
  98. }
  99. return $form;
  100. }
  101. /**
  102. * Form to add new databases.
  103. * Implements hook_form().
  104. *
  105. * @ingroup tripal_db
  106. */
  107. function tripal_chado_db_add_form($form, &$form_state) {
  108. // add in the form fields to this form
  109. tripal_chado_add_db_form_fields($form, $form_state);
  110. $form['add'] = array(
  111. '#type' => 'submit',
  112. '#value' => t('Add'),
  113. '#weight' => 5,
  114. );
  115. return $form;
  116. }
  117. /**
  118. * Fields commmon between the add/edit forms
  119. *
  120. * @ingroup tripal_db
  121. */
  122. function tripal_chado_add_db_form_fields(&$form, $form_state, $dbid = NULL) {
  123. $default_db = '';
  124. $default_desc = '';
  125. $default_url = '';
  126. $default_urlprefix = '';
  127. // get the default values from the database first
  128. if ($dbid) {
  129. $values = array('db_id' => $dbid);
  130. $result = chado_select_record('db', array('*'), $values);
  131. $db = $result[0];
  132. $default_db = $db->name;
  133. $default_desc = $db->description;
  134. $default_url = $db->url;
  135. $default_urlprefix = $db->urlprefix;
  136. $form['dbid'] = array(
  137. '#type' => 'value',
  138. '#value' => $dbid,
  139. );
  140. }
  141. // add a fieldset for the Drupal Schema API
  142. $form['fields'] = array(
  143. '#type' => 'fieldset',
  144. '#title' => 'Database Details',
  145. '#collapsible' => 0,
  146. );
  147. $form['fields']['name']= array(
  148. '#type' => 'textfield',
  149. '#title' => t("Database Name"),
  150. '#description' => t('Please enter the name for this external database.'),
  151. '#required' => TRUE,
  152. '#default_value' => $default_db,
  153. '#maxlength' => 255,
  154. );
  155. $form['fields']['description']= array(
  156. '#type' => 'textarea',
  157. '#title' => t('Description'),
  158. '#description' => t('Please enter a description for this database'),
  159. '#default_value' => $default_desc,
  160. '#maxlength' => 255,
  161. );
  162. $form['fields']['url']= array(
  163. '#type' => 'textfield',
  164. '#title' => t('URL'),
  165. '#description' => t('Please enter the web address for this database.'),
  166. '#default_value' => $default_url,
  167. '#maxlength' => 255,
  168. );
  169. $form['fields']['urlprefix']= array(
  170. '#type' => 'textfield',
  171. '#title' => t('URL prefix'),
  172. '#description' => t('Tripal can provide links to external databases
  173. when accession numbers or unique identifiers are known. Typically,
  174. a database will provide a unique web address for each accession and
  175. the accession usually is the last component of the page address.
  176. Please enter the web address, minus the accession number for this
  177. database. By default, Tripal will
  178. combine this "URL prefix" with the database short name and
  179. accession to create a link to the external site. But, you can
  180. also use the tokens {db} and {accession} within the URL prefix
  181. to specify exactly where the database short name and the accession
  182. should be added. Tripal will substitute the actual values in
  183. place of these tokens to create the link. (e.g. URL prefix: https://phytozome.jgi.doe.gov/phytomine/portal.do?externalid=PAC:{accession}).'),
  184. '#default_value' => $default_urlprefix,
  185. '#maxlength' => 255,
  186. );
  187. return $form;
  188. }
  189. /**
  190. * Validation fucntion for tripal_db_db_add_form
  191. *
  192. * @ingroup tripal_db
  193. */
  194. function tripal_chado_db_add_form_validate($form, &$form_state) {
  195. tripal_chado_db_form_fields_validate($form, $form_state);
  196. }
  197. /**
  198. * Validation fucntion for tripal_db_db_edit_form
  199. *
  200. * @ingroup tripal_db
  201. */
  202. function tripal_chado_db_edit_form_validate($form, &$form_state) {
  203. tripal_chado_db_form_fields_validate($form, $form_state);
  204. }
  205. /**
  206. * Generic form validation for shared fields of both the edit and add forms
  207. *
  208. * @ingroup tripal_db
  209. */
  210. function tripal_chado_db_form_fields_validate($form, &$form_state) {
  211. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  212. $desc = array_key_exists('description', $form_state['values']) ? trim($form_state['values']['description']) : '';
  213. $url = array_key_exists('url', $form_state['values']) ? trim($form_state['values']['url']) : '';
  214. $urlp = array_key_exists('urlprefix', $form_state['values']) ? trim($form_state['values']['urlprefix']) : '';
  215. $dbid = array_key_exists('dbid', $form_state['values']) ? trim($form_state['values']['dbid']) : '';
  216. // make sure the database name is unique
  217. $values = array('name' => $name);
  218. $results = chado_select_record('db', array('db_id'), $values);
  219. if (count($results) > 0 and $results[0]->db_id != $dbid) {
  220. form_set_error('name', 'The database name must be unique');
  221. }
  222. }
  223. /**
  224. * Submit for add db form
  225. *
  226. */
  227. function tripal_chado_db_add_form_submit($form, &$form_state) {
  228. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  229. $desc = array_key_exists('description', $form_state['values']) ? trim($form_state['values']['description']) : '';
  230. $url = array_key_exists('url', $form_state['values']) ? trim($form_state['values']['url']) : '';
  231. $urlp = array_key_exists('urlprefix', $form_state['values']) ? trim($form_state['values']['urlprefix']) : '';
  232. $values = array(
  233. 'name' => $name,
  234. 'description' => $desc,
  235. 'url' => $url,
  236. 'urlprefix' => $urlp,
  237. );
  238. $success = chado_insert_record('db', $values);
  239. if ($success) {
  240. drupal_set_message(t("External database added"));
  241. drupal_goto('admin/tripal/loaders/chado_db');
  242. }
  243. else {
  244. drupal_set_message(t("Failed to add external database."));
  245. }
  246. }
  247. /**
  248. * Submit for edit db form
  249. *
  250. */
  251. function tripal_chado_db_edit_form_submit($form, &$form_state) {
  252. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  253. $desc = array_key_exists('description', $form_state['values']) ? trim($form_state['values']['description']) : '';
  254. $url = array_key_exists('url', $form_state['values']) ? trim($form_state['values']['url']) : '';
  255. $urlp = array_key_exists('urlprefix', $form_state['values']) ? trim($form_state['values']['urlprefix']) : '';
  256. $dbid = array_key_exists('dbid', $form_state['values']) ? trim($form_state['values']['dbid']) : '';
  257. $op = trim($form_state['values']['op']);
  258. $values = array(
  259. 'name' => $name,
  260. 'description' => $desc,
  261. 'url' => $url,
  262. 'urlprefix' => $urlp,
  263. );
  264. if (strcmp($op, 'Update')==0) {
  265. $match = array('db_id' => $dbid);
  266. $success = chado_update_record('db', $match, $values);
  267. if ($success) {
  268. drupal_set_message(t("External database updated"));
  269. drupal_goto('admin/tripal/loaders/chado_db');
  270. }
  271. else {
  272. drupal_set_message(t("Failed to update external database."));
  273. }
  274. }
  275. if (strcmp($op, 'Delete')==0) {
  276. $match = array('db_id' => $dbid);
  277. $success = chado_delete_record('db', $match);
  278. if ($success) {
  279. drupal_set_message(t("External database deleted"));
  280. drupal_goto('admin/tripal/loaders/chado_db');
  281. }
  282. else {
  283. drupal_set_message(t("Failed to delete external database."));
  284. }
  285. }
  286. }
  287. /**
  288. * Ajax callback for the tripal_chado_db_form
  289. *
  290. */
  291. function tripal_chado_db_edit_form_ajax($form, $form_state) {
  292. $elements = array();
  293. // add in the form fields and the buttons
  294. if (array_key_exists('dbid', $form_state['values'])) {
  295. $elements['fields'] = $form['fields'];
  296. $elements['update'] = $form['update'];
  297. $elements['delete'] = $form['delete'];
  298. }
  299. // add back in the db-edit-div that is used for the next round of AJAX
  300. $elements['fields']['#prefix'] = '<div id="db-edit-div">';
  301. $elements['fields']['#suffix'] = '</div">';
  302. // reset the values for the fields to the defaults
  303. $elements['fields']['name']['#value'] = $elements['fields']['name']['#default_value'];
  304. $elements['fields']['description']['#value'] = $elements['fields']['description']['#default_value'];
  305. $elements['fields']['url']['#value'] = $elements['fields']['url']['#default_value'];
  306. $elements['fields']['urlprefix']['#value'] = $elements['fields']['urlprefix']['#default_value'];
  307. //drupal_set_message('<pre>' . print_r($elements, TRUE) . '</pre>', "status");
  308. return $elements;
  309. }