tripal_db.admin.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. /**
  3. * Purpose: Provide Guidance to new Tripal Admin
  4. *
  5. * @return HTML Formatted text
  6. *
  7. * @ingroup tripal_db
  8. */
  9. function tripal_db_module_description_page() {
  10. $text = '';
  11. $text = '<h3>Tripal External Database Administrative Tools Quick Links</h3>';
  12. $text .= '<ul>';
  13. $text .= '<li>' . l('Add an external database for cross-refernces.', 'admin/tripal/tripal_db/add_db') . '</li>';
  14. $text .= '<li>' . l('Update or delete an external database.', 'admin/tripal/tripal_db/edit_db') . '</li>';
  15. $text .= '</ul><br>';
  16. $text .= '<h3>Module Description:</h3>';
  17. $text .= '<p>The Tripal DB Module provides the ability to add database cross reference to the
  18. data in your Tripal Website. Typically an external database (such as NCBI Genbank, Gene Ontology (GO),
  19. stocks database) contains a collection of objects (genomic sequences, vocabulary terms, stocks) that are
  20. uniquely identified using an accession number (or identifier). Data loaded into Tripal can be a
  21. associated with these objects in remote databases, and links can appear on pages allowing site visitors
  22. to view the associated objects on the remote database\'s website </p>';
  23. $text .= '<h3>Setup Instructions:</h3>';
  24. $text .= '<ol>';
  25. $text .= '<li><b>Set Permissions</b>: This module supports the Drupal user permissions interface for
  26. controlling administrative access for creating, editing and deleting database cross-reference resources.
  27. The default is that only the site administrator has these
  28. permissions. Best practice is to create <a href="' . url('admin/user/roles') . '">a new role</a>
  29. for administrative tasks, (such as a webmaster role),
  30. and then <a href="' . url('admin/user/user') . '">assign users to the role</a>. Finally,
  31. <a href="' . url('admin/user/permissions') . '">assign the permission</a> titled "administer tripal db cross-reference".
  32. to the new role.</li>';
  33. $text .= '<li><b>Adding or Editing an External Databases</b>. Many resources such as NCBI nr or ExPASy SwissProt (to name a few)
  34. come pre-loaded with Chado. However, you can add new entries or edit existing entries. Also, when loading
  35. ontologies (controlled vocabularies) using the Tripal CV module new databases are added automaticaly for
  36. each ontology. To enable linking of accession on a page to the page for that accession on the external
  37. database, simply add the URL and the URL prefix when adding or editing a database.</li>';
  38. $text .= '<li><b>Associate Data with Accessions</b>. The Tripal loaders (e.g. GFF, OBO) can associate accessions from
  39. remote data to genomic features and controlled vocabularies automatically. Use the loaders to load genomic
  40. features and controlled vocabularies respectively. Additionally, the bulk loader can be used to create
  41. loading templates for associating external database accessions.
  42. </li>';
  43. $text .= '</ol>';
  44. $text .= '<br><h3>Features of this Module:</h3>';
  45. $text .= '<ul>';
  46. $text .= '<li><b>Add an External Databases</b>:';
  47. $text .= 'By entering the name and any additional details into the <a href="tripal_db/add_db">add database form</a> you register an external database with your website. This allows you to specify that a sequence feature or other data is also stored in an external database. This is escpecially useful if the external database may contain additional details not stored in yours. If the external database is online you can even provide a URL prefix which will automatically link any data in your website to theirs via a web link.</li>';
  48. $text .= '<li><b>Update or Delete and External Databases</b>';
  49. $text .= 'To edit the details of an external database record or to delete an already existing external database, go to the <a href="tripal_db/edit_db">Update/Delete DBs form</a>. This will allow you to change details or enter new details.</li>';
  50. $text .= '</ul>';
  51. return $text;
  52. }
  53. /**
  54. *
  55. *
  56. * @ingroup tripal_db
  57. */
  58. function tripal_db_admin_page() {
  59. $add_url = url("admin/tripal/tripal_db/add_db");
  60. $output = "<a href=\"$add_url\">Add a new external database</a>";
  61. $output .= drupal_get_form('tripal_db_select_form');
  62. $output .= '<div id="db-edit-div">Please select a database above to view or edit</div>';
  63. return $output;
  64. }
  65. /**
  66. *
  67. *
  68. * @ingroup tripal_db
  69. */
  70. function tripal_db_select_form() {
  71. return $form;
  72. }
  73. /**
  74. *
  75. * @ingroup tripal_db
  76. */
  77. function tripal_ajax_db_edit() {
  78. $status = TRUE;
  79. // prepare and render the form
  80. $form = tripal_core_ahah_prepare_form();
  81. $data = drupal_render($form);
  82. // bind javascript events to the new objects that will be returned
  83. // so that AHAH enabled elements will work.
  84. $settings = tripal_core_ahah_bind_events();
  85. // return the updated JSON
  86. drupal_json(
  87. array(
  88. 'status' => $status,
  89. 'data' => $data,
  90. 'settings' => $settings,
  91. )
  92. );
  93. }
  94. /**
  95. *
  96. * @ingroup tripal_db
  97. */
  98. function tripal_db_form(&$form_state = NULL, $action = 'Update') {
  99. $dbid = $form_state['values']['dbid'];
  100. if (strcmp($action,'Update')==0) {
  101. // get a list of db from chado for user to choose
  102. $sql = "SELECT * FROM {db} WHERE NOT name = 'tripal' ORDER BY name ";
  103. $results = chado_query($sql);
  104. $dbs = array();
  105. $dbs[] = '';
  106. while ($db = db_fetch_object($results)) {
  107. $dbs[$db->db_id] = $db->name;
  108. }
  109. $form['dbid'] = array(
  110. '#title' => t('External Database Name'),
  111. '#type' => 'select',
  112. '#options' => $dbs,
  113. '#ahah' => array(
  114. 'path' => 'admin/tripal/tripal_db/edit/js',
  115. 'wrapper' => 'db-edit-div',
  116. 'effect' => 'fade',
  117. 'event' => 'change',
  118. 'method' => 'replace',
  119. ),
  120. '#prefix' => '<div id="db-edit-div">',
  121. '#suffix' => '</div>',
  122. '#default_value' => $dbid,
  123. '#description' => t('Please select a database to edit'),
  124. );
  125. }
  126. else {
  127. $default_db = $form_state['values']['name'];
  128. $default_desc = $form_state['values']['description'];
  129. $default_url = $form_state['values']['url'];
  130. $default_urlprefix = $form_state['values']['urlprefix'];
  131. }
  132. // get this requested database
  133. if ($dbid) {
  134. $values = array('db_id' => $dbid);
  135. $result = tripal_core_chado_select('db', array('*'), $values);
  136. $db = $result[0];
  137. $prev_dbid = $form_state['values']['prev_dbid'];
  138. // if the database has changed then repopulate the fields with the databaes values
  139. if ($prev_dbid != $dbid) {
  140. $default_db = $db->name;
  141. $default_desc = $db->description;
  142. $default_url = $db->url;
  143. $default_urlprefix = $db->urlprefix;
  144. }
  145. // if the database did not change then keep the values in the form values
  146. else {
  147. $default_db = $form_state['values']['name'];
  148. $default_desc = $form_state['values']['description'];
  149. $default_url = $form_state['values']['url'];
  150. $default_urlprefix = $form_state['values']['urlprefix'];
  151. }
  152. }
  153. $form['form_action'] = array(
  154. '#type' => 'hidden',
  155. '#value' => $action,
  156. );
  157. // we need to distinguish between edits in a field that may have failed
  158. // and when the user selects a different database from the list.
  159. $form['prev_dbid'] = array(
  160. '#type' => 'hidden',
  161. '#value' => $dbid,
  162. );
  163. // if we want to update a database but the user has not
  164. // yet selected a database then return so we don't show the other fields
  165. // the rest of the fields will be added with the AHAH callback.
  166. if (strcmp($action,'Update')==0 and !$dbid) {
  167. return $form;
  168. }
  169. $form['name']= array(
  170. '#type' => 'textfield',
  171. '#title' => t("Database Name"),
  172. '#description' => t('Please enter the name for this external database.'),
  173. '#required' => TRUE,
  174. '#default_value' => $default_db,
  175. '#weight' => 1
  176. );
  177. $form['description']= array(
  178. '#type' => 'textarea',
  179. '#title' => t('Description'),
  180. '#description' => t('Please enter a description for this database'),
  181. '#default_value' => $default_desc,
  182. '#weight' => 2
  183. );
  184. $form['url']= array(
  185. '#type' => 'textfield',
  186. '#title' => t('URL'),
  187. '#description' => t('Please enter the web address for this database.'),
  188. '#default_value' => $default_url,
  189. '#weight' => 3
  190. );
  191. $form['urlprefix']= array(
  192. '#type' => 'textfield',
  193. '#title' => t('URL prefix'),
  194. '#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.'),
  195. '#default_value' => $default_urlprefix,
  196. '#weight' => 4
  197. );
  198. if (strcmp($action, 'Update')==0) {
  199. $form['update'] = array(
  200. '#type' => 'submit',
  201. '#value' => t('Update'),
  202. '#weight' => 5,
  203. '#executes_submit_callback' => TRUE,
  204. );
  205. $form['delete'] = array(
  206. '#type' => 'submit',
  207. '#value' => t('Delete'),
  208. '#weight' => 6,
  209. '#executes_submit_callback' => TRUE,
  210. );
  211. }
  212. else {
  213. $form['add'] = array(
  214. '#type' => 'submit',
  215. '#value' => t('Add'),
  216. '#weight' => 5,
  217. '#executes_submit_callback' => TRUE,
  218. );
  219. }
  220. return $form;
  221. }
  222. /**
  223. *
  224. * @ingroup tripal_db
  225. */
  226. function tripal_db_form_validate($form, &$form_state) {
  227. $name = trim($form_state['values']['name']);
  228. $desc = trim($form_state['values']['description']);
  229. $url = trim($form_state['values']['url']);
  230. $urlp = trim($form_state['values']['urlprefix']);
  231. $dbid = trim($form_state['values']['dbid']);
  232. $op = trim($form_state['values']['op']);
  233. $action = $form_state['values']['form_action'];
  234. // make sure the database name is unique
  235. $values = array('name' => $name);
  236. $results = tripal_core_chado_select('db', array('db_id'), $values);
  237. if (count($results) > 0 and $results[0]->db_id != $dbid) {
  238. form_set_error('name', 'The database name must be unique');
  239. }
  240. }
  241. /**
  242. *
  243. * @ingroup tripal_db
  244. */
  245. function tripal_db_form_submit($form, &$form_state) {
  246. $name = trim($form_state['values']['name']);
  247. $desc = trim($form_state['values']['description']);
  248. $url = trim($form_state['values']['url']);
  249. $urlp = trim($form_state['values']['urlprefix']);
  250. $dbid = trim($form_state['values']['dbid']);
  251. $op = trim($form_state['values']['op']);
  252. $values = array(
  253. 'name' => $name,
  254. 'description' => $desc,
  255. 'url' => $url,
  256. 'urlprefix' => $urlp,
  257. );
  258. if ($dbid) {
  259. if (strcmp($op, 'Update')==0) {
  260. $match = array('db_id' => $dbid);
  261. $success = tripal_core_chado_update('db', $match, $values);
  262. if ($success) {
  263. drupal_set_message(t("External database updated"));
  264. }
  265. else {
  266. drupal_set_message(t("Failed to update external database."));
  267. }
  268. }
  269. if (strcmp($op, 'Delete')==0) {
  270. $match = array('db_id' => $dbid);
  271. $success = tripal_core_chado_delete('db', $match);
  272. if ($success) {
  273. drupal_set_message(t("External database deleted"));
  274. }
  275. else {
  276. drupal_set_message(t("Failed to delete external database."));
  277. }
  278. }
  279. }
  280. else {
  281. $success = tripal_core_chado_insert('db', $values);
  282. if ($success) {
  283. drupal_set_message(t("External database added"));
  284. }
  285. else {
  286. drupal_set_message(t("Failed to add external database."));
  287. }
  288. }
  289. }