tripal_db.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. require_once "tripal_db.api.inc";
  3. /**
  4. * @defgroup tripal_db External Database Module
  5. * @ingroup tripal_modules
  6. */
  7. /**
  8. *
  9. * @ingroup tripal_db
  10. */
  11. function tripal_db_init(){
  12. // add the tripal_db JS and CSS
  13. drupal_add_css(drupal_get_path('theme', 'tripal').'/css/tripal_db.css');
  14. drupal_add_js(drupal_get_path('theme', 'tripal').'/js/tripal_db.js');
  15. }
  16. /**
  17. *
  18. * @ingroup tripal_db
  19. */
  20. function tripal_db_menu() {
  21. $items = array();
  22. $items['admin/tripal/tripal_db'] = array(
  23. 'title' => 'Databases',
  24. 'description' => 'Basic Description of Tripal DB Module Functionality',
  25. 'page callback' => 'tripal_db_module_description_page',
  26. 'access arguments' => array('administer site configuration'),
  27. 'type' => MENU_NORMAL_ITEM,
  28. );
  29. $items['admin/tripal/tripal_db/edit_db'] = array(
  30. 'title' => 'Update/Delete Database References',
  31. 'description' => 'Manage Databases ',
  32. 'page callback' => 'tripal_db_admin_page',
  33. 'access arguments' => array('administer site configuration'),
  34. 'type' => MENU_NORMAL_ITEM,
  35. );
  36. $items['admin/tripal/tripal_db/add_db'] = array(
  37. 'title' => 'Add a Database',
  38. 'page callback' => 'drupal_get_form',
  39. 'page arguments' => array('tripal_db_form'),
  40. 'access arguments' => array('access administration pages'),
  41. 'type' => MENU_NORMAL_ITEM,
  42. );
  43. $items['admin/tripal/tripal_db/edit/js'] = array(
  44. 'title' => 'Edit Databases',
  45. 'page callback' => 'tripal_ajax_db_edit',
  46. 'access arguments' => array('access administration pages'),
  47. 'type' => MENU_CALLBACK,
  48. );
  49. return $items;
  50. }
  51. /**
  52. * Set the permission types that the chado module uses. Essentially we
  53. * want permissionis that protect creation, editing and deleting of chado
  54. * data objects
  55. *
  56. * @ingroup tripal_db
  57. */
  58. function tripal_db_perm(){
  59. return array(
  60. 'access chado_db content',
  61. 'create chado_db content',
  62. 'delete chado_db content',
  63. 'edit chado_db content',
  64. );
  65. }
  66. /**
  67. * Implements hook_views_api()
  68. * Purpose: Essentially this hook tells drupal that there is views support for
  69. * for this module which then includes tripal_db.views.inc where all the
  70. * views integration code is
  71. *
  72. * @ingroup tripal_db
  73. */
  74. function tripal_db_views_api() {
  75. return array('api' => 2.0);
  76. }
  77. /**
  78. * Purpose: Provide Guidance to new Tripal Admin
  79. *
  80. * @return HTML Formatted text
  81. *
  82. * @ingroup tripal_db
  83. */
  84. function tripal_db_module_description_page() {
  85. $text = '';
  86. $text = '<h3>Tripal External Database Administrative Tools Quick Links</h3>';
  87. $text .= '<ul>';
  88. $text .= '<li>'.l('Add External DB', 'admin/tripal/tripal_db/add_db').'</li>';
  89. $text .= '<li>'.l('Update/Delete External DBs', 'admin/tripal/tripal_db/edit_db').'</li>';
  90. $text .= '<li>'.l('Database References Listing', 'admin/tripal/tripal_db/list_dbxrefs').'</li>';
  91. $text .= '</ul>';
  92. $text .= '<h3>Module Description:</h3>';
  93. $text .= '<p>The Tripal DB Module provides functionality for linking the data in your Tripal Website with other biological websites out there. Essentially you register an enternal database with your website and then associate any of your data (usually sequence features) with that external database by providing the accession for your data in the other database. If the other database is online and you provided a URL prefix when you registered the external database with your site then there will be a link on the details page for your data that takes the user to the same record in the external database.</p>';
  94. $text .= '<h3>Setup Instructions:</h3>';
  95. $text .= '<ol>';
  96. $text .= '<li><p><b>Set Permissions</b>: The feature module supports the Drupal user permissions interface for
  97. controlling access to feature content and functions. These permissions include viewing,
  98. creating, editing or administering of
  99. feature content. The default is that only the original site administrator has these
  100. permissions. You can <a href="'.url('admin/user/roles').'">add roles</a> for classifying users,
  101. <a href="'.url('admin/user/user').'">assign users to roles</a> and
  102. <a href="'.url('admin/user/permissions').'">assign permissions</a> for the feature content to
  103. those roles. For a simple setup, allow anonymous users access to view organism content and
  104. allow the site administrator all other permissions.</p></li>';
  105. $text .= '<li><b>Register any external databases</b> with data pertinent to your site.</li>';
  106. $text .= '<li><b>Create Database References</b>: Then as you load in your data, create database references linking your data to the external database.</li>';
  107. $text .= '</ol>';
  108. $text .= '<h3>Features of this Module:</h3>';
  109. $text .= '<ul>';
  110. $text .= '<li><b>Add/Register External Databases</b>:';
  111. $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>';
  112. $text .= '<li><b>Update/Delete External Databases</b>';
  113. $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>';
  114. $text .= '<li><b>List all External Database References</b>';
  115. $text .= 'If you have views installed, there will be a link to a default listing of all database references currently in your database. This listing can be accessed <a href="tripal_db/list_dbxrefs">here</a>. It requires the Drupal Module Views version 2 to be installed (<a href="http://drupal.org/project/views">Drupal Views</a>)</li>';
  116. $text .= '</ul>';
  117. return $text;
  118. }
  119. /**
  120. *
  121. *
  122. * @ingroup tripal_db
  123. */
  124. function tripal_db_admin_page(){
  125. $add_url = url("admin/tripal/tripal_db/add_db");
  126. $output = "<a href=\"$add_url\">Add a new external database</a>";
  127. $output .= drupal_get_form('tripal_db_select_form');
  128. $output .= '<div id="db-edit-div">Please select a database above to view or edit</div>';
  129. return $output;
  130. }
  131. /**
  132. *
  133. *
  134. * @ingroup tripal_db
  135. */
  136. function tripal_db_select_form(){
  137. $previous_db = tripal_db_set_active('chado'); // use chado database
  138. // get a list of db from chado for user to choose
  139. $sql = "SELECT * FROM {db} WHERE NOT name = 'tripal' ORDER BY name ";
  140. $results = db_query ($sql);
  141. tripal_db_set_active($previous_db); // use drupal database
  142. $dbs = array();
  143. $dbs[] = '';
  144. while ($db = db_fetch_object($results)){
  145. $dbs[$db->db_id] = $db->name;
  146. }
  147. $form['dbid'] = array(
  148. '#title' => t('External Database Name'),
  149. '#type' => 'select',
  150. '#options' => $dbs,
  151. '#ahah' => array(
  152. 'path' => 'admin/tripal/tripal_db/edit/js',
  153. 'wrapper' => 'db-edit-div',
  154. 'effect' => 'fade',
  155. 'event' => 'change',
  156. 'method' => 'replace',
  157. ),
  158. );
  159. return $form;
  160. }
  161. /**
  162. *
  163. * @ingroup tripal_db
  164. */
  165. function tripal_ajax_db_edit (){
  166. // get the database id, build the form and then return the JSON object
  167. $dbid = $_POST['dbid'];
  168. $form = drupal_get_form('tripal_db_form',$dbid);
  169. drupal_json(array('status' => TRUE, 'data' => $form));
  170. }
  171. /**
  172. *
  173. * @ingroup tripal_db
  174. */
  175. function tripal_db_form(&$form_state = NULL,$dbid = NULL){
  176. // get this requested database
  177. if($dbid){
  178. $sql = "SELECT * FROM {db} WHERE db_id = $dbid ";
  179. $previous_db = tripal_db_set_active('chado');
  180. $db = db_fetch_object(db_query($sql));
  181. tripal_db_set_active($previous_db);
  182. # set the default values. If there is a value set in the
  183. # form_state then let's use that, otherwise, we'll pull
  184. # the values from the database
  185. $default_db = $form_state['values']['name'];
  186. $default_desc = $form_state['values']['description'];
  187. $default_url = $form_state['values']['url'];
  188. $default_urlprefix = $form_state['values']['urlprefix'];
  189. if(!$default_db){
  190. $default_db = $db->name;
  191. }
  192. if(!$default_desc){
  193. $default_desc = $db->description;
  194. }
  195. if(!$default_url){
  196. $default_url = $db->url;
  197. }
  198. if(!$default_urlprefix){
  199. $default_urlprefix = $db->urlprefix;
  200. }
  201. $action = 'Update';
  202. } else {
  203. $action = 'Add';
  204. }
  205. $form['dbid'] = array(
  206. '#type' => 'hidden',
  207. '#value' => $dbid
  208. );
  209. $form['name']= array(
  210. '#type' => 'textfield',
  211. '#title' => t("Database Name"),
  212. '#description' => t('Please enter the name for this external database.'),
  213. '#required' => TRUE,
  214. '#default_value' => $default_db,
  215. '#weight' => 1
  216. );
  217. $form['description']= array(
  218. '#type' => 'textarea',
  219. '#title' => t('Description'),
  220. '#description' => t('Please enter a description for this database'),
  221. '#default_value' => $default_desc,
  222. '#weight' => 2
  223. );
  224. $form['url']= array(
  225. '#type' => 'textfield',
  226. '#title' => t('URL'),
  227. '#description' => t('Please enter the web address for this database.'),
  228. '#default_value' => $default_url,
  229. '#weight' => 3
  230. );
  231. $form['urlprefix']= array(
  232. '#type' => 'textfield',
  233. '#title' => t('URL prefix'),
  234. '#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.'),
  235. '#default_value' => $default_urlprefix,
  236. '#weight' => 4
  237. );
  238. if(strcmp($action,'Update')==0){
  239. $form['update'] = array (
  240. '#type' => 'submit',
  241. '#value' => t('Update'),
  242. '#weight' => 5,
  243. '#executes_submit_callback' => TRUE,
  244. );
  245. $form['delete'] = array (
  246. '#type' => 'submit',
  247. '#value' => t('Delete'),
  248. '#weight' => 6,
  249. '#executes_submit_callback' => TRUE,
  250. );
  251. } else {
  252. $form['add'] = array (
  253. '#type' => 'submit',
  254. '#value' => t('Add'),
  255. '#weight' => 5,
  256. '#executes_submit_callback' => TRUE,
  257. );
  258. }
  259. $form['#redirect'] = 'admin/tripal/tripal_db';
  260. return $form;
  261. }
  262. /**
  263. *
  264. * @ingroup tripal_db
  265. */
  266. function tripal_db_form_submit($form, &$form_state){
  267. $name = $form_state['values']['name'];
  268. $desc = $form_state['values']['description'];
  269. $url = $form_state['values']['url'];
  270. $urlp = $form_state['values']['urlprefix'];
  271. $dbid = $form_state['values']['dbid'];
  272. $op = $form_state['values']['op'];
  273. if($dbid){
  274. if(strcmp($op,'Update')==0){
  275. $sql = "
  276. UPDATE {db} SET
  277. name = '%s',
  278. description = '%s',
  279. url = '%s',
  280. urlprefix = '%s'
  281. WHERE db_id = %d
  282. ";
  283. $previous_db = tripal_db_set_active('chado');
  284. $db = db_query($sql,$name,$desc,$url,$urlp,$dbid);
  285. tripal_db_set_active($previous_db);
  286. if($db){
  287. drupal_set_message("External database updated");
  288. } else {
  289. drupal_set_message("Failed to update external database.");
  290. }
  291. }
  292. if(strcmp($op,'Delete')==0){
  293. $sql = "
  294. DELETE FROM {db}
  295. WHERE db_id = %d
  296. ";
  297. $previous_db = tripal_db_set_active('chado');
  298. $db = db_query($sql,$dbid);
  299. tripal_db_set_active($previous_db);
  300. if($db){
  301. drupal_set_message("External database deleted");
  302. } else {
  303. drupal_set_message("Failed to delete external database.");
  304. }
  305. }
  306. }
  307. else {
  308. $sql = "
  309. INSERT INTO {db}
  310. (name,description,url,urlprefix)
  311. VALUES
  312. ('%s','%s','%s','%s')
  313. ";
  314. $previous_db = tripal_db_set_active('chado');
  315. $db = db_query($sql,$name,$desc,$url,$urlp);
  316. tripal_db_set_active($previous_db);
  317. if($db){
  318. drupal_set_message("External database added");
  319. } else {
  320. drupal_set_message("Failed to add external database.");
  321. }
  322. }
  323. return '';
  324. }