tripal_db.module 8.8 KB

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