tripal_db.module 12 KB

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