tripal_db.module 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. //
  3. // Copyright 2009 Clemson University
  4. //
  5. /*************************************************************************
  6. *
  7. */
  8. function tripal_db_init(){
  9. // add the tripal_db JS and CSS
  10. drupal_add_css(drupal_get_path('theme', 'tripal').
  11. '/css/tripal_db.css');
  12. drupal_add_js(drupal_get_path('theme', 'tripal').'/js/tripal_db.js');
  13. }
  14. /*************************************************************************
  15. *
  16. */
  17. function tripal_db_menu() {
  18. $items = array();
  19. $items['admin/tripal/tripal_db'] = array(
  20. 'title' => 'DB',
  21. 'description' => 'Manage External Databases ',
  22. 'page callback' => 'tripal_db_list',
  23. 'access arguments' => array('administer site configuration'),
  24. 'type' => MENU_NORMAL_ITEM,
  25. );
  26. $items['admin/tripal/tripal_db/new'] = array(
  27. 'title' => 'Add DB',
  28. 'page callback' => 'drupal_get_form',
  29. 'page arguments' => array('tripal_db_form'),
  30. 'access arguments' => array('access administration pages'),
  31. 'type' => MENU_NORMAL_ITEM,
  32. );
  33. $items['admin/tripal/tripal_db/edit/%'] = array(
  34. 'title' => 'Edit DB',
  35. 'page callback' => 'drupal_get_form',
  36. 'page arguments' => array('tripal_db_form',4),
  37. 'access arguments' => array('access administration pages'),
  38. 'type' => MENU_NORMAL_ITEM,
  39. );
  40. return $items;
  41. }
  42. /*******************************************************************************
  43. * Set the permission types that the chado module uses. Essentially we
  44. * want permissionis that protect creation, editing and deleting of chado
  45. * data objects
  46. */
  47. function tripal_db_perm(){
  48. return array(
  49. 'access chado_db content',
  50. 'create chado_db content',
  51. 'delete chado_db content',
  52. 'edit chado_db content',
  53. );
  54. }
  55. /*************************************************************************
  56. *
  57. */
  58. function tripal_db_list () {
  59. $previous_db = db_set_active('chado');
  60. $dbs = pager_query("SELECT * FROM {db} ORDER BY db_id",30,0,
  61. "SELECT count(*) FROM {db}");
  62. db_set_active($previous_db);
  63. // build the URLs using the url function so we can handle installations where
  64. // clean URLs are or are not used
  65. $new_url = url("admin/tripal/tripal_db/new");
  66. $output .= "Below is the list of all external databases.";
  67. $output .= "<br><a href=\"$new_url\">Add a new external database</a>";
  68. $output .= "<table class=\"border-table\">".
  69. " <tr>".
  70. " <th></th>".
  71. " <th>ID</th>".
  72. " <th>Name</th>".
  73. " <th>Description</th>".
  74. " <th>URL</th>".
  75. " <th>URL Prefix</th>".
  76. " </tr>";
  77. while($db = db_fetch_object($dbs)){
  78. $edit_url = url("admin/tripal/tripal_db/edit/$db->db_id");
  79. $output .= " <tr>".
  80. " <td><a href=\"$edit_url\">edit</a></td>".
  81. " <td>$db->db_id</td>".
  82. " <td>$db->name</td>".
  83. " <td>$db->description</td>".
  84. " <td>$db->url</td>".
  85. " <td>$db->urlprefix</td>".
  86. " </tr>";
  87. }
  88. $output .= "</table>";
  89. $output .= theme_pager();
  90. return $output;
  91. }
  92. /*************************************************************************
  93. *
  94. */
  95. function tripal_db_form(&$form_state = NULL,$db_id = NULL){
  96. if(!$db_id){
  97. $action = 'Add';
  98. } else {
  99. $action = 'Update';
  100. }
  101. // get this requested database
  102. if(strcmp($action,'Update')==0){
  103. $sql = "SELECT * FROM {db} WHERE db_id = $db_id ";
  104. $previous_db = db_set_active('chado');
  105. $db = db_fetch_object(db_query($sql));
  106. db_set_active($previous_db);
  107. # set the default values. If there is a value set in the
  108. # form_state then let's use that, otherwise, we'll pull
  109. # the values from the database
  110. $default_db = $form_state['values']['name'];
  111. $default_desc = $form_state['values']['description'];
  112. $default_url = $form_state['values']['url'];
  113. $default_urlprefix = $form_state['values']['urlprefix'];
  114. if(!$default_db){
  115. $default_db = $db->name;
  116. }
  117. if(!$default_desc){
  118. $default_desc = $db->description;
  119. }
  120. if(!$default_url){
  121. $default_url = $db->url;
  122. }
  123. if(!$default_urlprefix){
  124. $default_urlprefix = $db->urlprefix;
  125. }
  126. }
  127. // Build the form
  128. $form['action'] = array(
  129. '#type' => 'value',
  130. '#value' => $action
  131. );
  132. $form['db_id'] = array(
  133. '#type' => 'value',
  134. '#value' => $db_id
  135. );
  136. $form['name']= array(
  137. '#type' => 'textfield',
  138. '#title' => t('Database Name'),
  139. '#description' => t('Please enter the name for this external database.'),
  140. '#required' => TRUE,
  141. '#default_value' => $default_db,
  142. '#weight' => 1
  143. );
  144. $form['description']= array(
  145. '#type' => 'textarea',
  146. '#title' => t('Description'),
  147. '#description' => t('Please enter a description for this database'),
  148. '#default_value' => $default_desc,
  149. '#weight' => 2
  150. );
  151. $form['url']= array(
  152. '#type' => 'textfield',
  153. '#title' => t('URL'),
  154. '#description' => t('Please enter the web address for this database.'),
  155. '#default_value' => $default_url,
  156. '#weight' => 3
  157. );
  158. $form['urlprefix']= array(
  159. '#type' => 'textfield',
  160. '#title' => t('URL prefix'),
  161. '#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.'),
  162. '#default_value' => $default_urlprefix,
  163. '#weight' => 4
  164. );
  165. $form['submit'] = array (
  166. '#type' => 'submit',
  167. '#value' => t($action),
  168. '#weight' => 5,
  169. '#executes_submit_callback' => TRUE,
  170. );
  171. $form['#redirect'] = 'admin/tripal/tripal_db';
  172. return $form;
  173. }
  174. /************************************************************************
  175. *
  176. */
  177. function tripal_db_form_submit($form, &$form_state){
  178. $action = $form_state['values']['action'];
  179. $db_id = $form_state['values']['db_id'];
  180. $name = $form_state['values']['name'];
  181. $desc = $form_state['values']['description'];
  182. $url = $form_state['values']['url'];
  183. $urlp = $form_state['values']['urlprefix'];
  184. if(strcmp($action,'Update')==0){
  185. $sql = "
  186. UPDATE {db} SET
  187. name = '$name',
  188. description = '$desc',
  189. url = '$url',
  190. urlprefix = '$urlp'
  191. WHERE db_id = $db_id
  192. ";
  193. $previous_db = db_set_active('chado');
  194. $db = db_query($sql);
  195. db_set_active($previous_db);
  196. if($db){
  197. drupal_set_message("Database updated");
  198. } else {
  199. drupal_set_message("Failed to update database.");
  200. }
  201. }
  202. else if(strcmp($action,'Add')==0){
  203. $sql = "
  204. INSERT INTO {db}
  205. (name,description,url,urlprefix)
  206. VALUES
  207. ('$name','$desc','$url','$urlp')
  208. ";
  209. $previous_db = db_set_active('chado');
  210. $db = db_query($sql);
  211. db_set_active($previous_db);
  212. if($db){
  213. drupal_set_message("Database added");
  214. } else {
  215. drupal_set_message("Failed to add database.");
  216. }
  217. }
  218. else {
  219. drupal_set_message("No action performed.");
  220. }
  221. return '';
  222. }