tripal_db.module 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 References',
  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 External 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 an External 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 External 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>Description:</h3>';
  75. $text .= '<p>TODO: Basic Description of this module including mention/link to the chado module</p>';
  76. $text .= '<h3>Post Installation Instructions:</h3>';
  77. $text .= '<p>TODO: Describe any post installation intructions here. You shouldalways include setting user permissions.</p>';
  78. $text .= '<h3>Features of this Module:</h3>';
  79. $text .= '<p>TODO: Discuss the Features of this module including links. Some features to consider are creating content, details pages/node content, editing/deleteing, basic listings and vies integration. See admin/tripal/tripal_stock for an example.</p>';
  80. return $text;
  81. }
  82. /*************************************************************************
  83. *
  84. */
  85. function tripal_db_admin_page(){
  86. $add_url = url("admin/tripal/tripal_db/new");
  87. $output = "<a href=\"$add_url\">Add a new external database</a>";
  88. $output .= drupal_get_form('tripal_db_select_form');
  89. $output .= '<div id="db-edit-div">Please select a database above to view or edit</div>';
  90. return $output;
  91. }
  92. /*************************************************************************
  93. *
  94. */
  95. function tripal_db_select_form(){
  96. $previous_db = tripal_db_set_active('chado'); // use chado database
  97. // get a list of db from chado for user to choose
  98. $sql = "SELECT * FROM {db} WHERE NOT name = 'tripal' ORDER BY name ";
  99. $results = db_query ($sql);
  100. tripal_db_set_active($previous_db); // use drupal database
  101. $dbs = array();
  102. $dbs[] = '';
  103. while ($db = db_fetch_object($results)){
  104. $dbs[$db->db_id] = $db->name;
  105. }
  106. $form['dbid'] = array(
  107. '#title' => t('External Database Name'),
  108. '#type' => 'select',
  109. '#options' => $dbs,
  110. '#ahah' => array(
  111. 'path' => 'admin/tripal/tripal_db/edit/js',
  112. 'wrapper' => 'db-edit-div',
  113. 'effect' => 'fade',
  114. 'event' => 'change',
  115. 'method' => 'replace',
  116. ),
  117. );
  118. return $form;
  119. }
  120. /*************************************************************************
  121. *
  122. */
  123. function tripal_ajax_db_edit (){
  124. // get the database id, build the form and then return the JSON object
  125. $dbid = $_POST['dbid'];
  126. $form = drupal_get_form('tripal_db_form',$dbid);
  127. drupal_json(array('status' => TRUE, 'data' => $form));
  128. }
  129. /*************************************************************************
  130. *
  131. */
  132. function tripal_db_form(&$form_state = NULL,$dbid = NULL){
  133. // get this requested database
  134. if($dbid){
  135. $sql = "SELECT * FROM {db} WHERE db_id = $dbid ";
  136. $previous_db = tripal_db_set_active('chado');
  137. $db = db_fetch_object(db_query($sql));
  138. tripal_db_set_active($previous_db);
  139. # set the default values. If there is a value set in the
  140. # form_state then let's use that, otherwise, we'll pull
  141. # the values from the database
  142. $default_db = $form_state['values']['name'];
  143. $default_desc = $form_state['values']['description'];
  144. $default_url = $form_state['values']['url'];
  145. $default_urlprefix = $form_state['values']['urlprefix'];
  146. if(!$default_db){
  147. $default_db = $db->name;
  148. }
  149. if(!$default_desc){
  150. $default_desc = $db->description;
  151. }
  152. if(!$default_url){
  153. $default_url = $db->url;
  154. }
  155. if(!$default_urlprefix){
  156. $default_urlprefix = $db->urlprefix;
  157. }
  158. $action = 'Update';
  159. } else {
  160. $action = 'Add';
  161. }
  162. $form['dbid'] = array(
  163. '#type' => 'hidden',
  164. '#value' => $dbid
  165. );
  166. $form['name']= array(
  167. '#type' => 'textfield',
  168. '#title' => t("Database Name"),
  169. '#description' => t('Please enter the name for this external database.'),
  170. '#required' => TRUE,
  171. '#default_value' => $default_db,
  172. '#weight' => 1
  173. );
  174. $form['description']= array(
  175. '#type' => 'textarea',
  176. '#title' => t('Description'),
  177. '#description' => t('Please enter a description for this database'),
  178. '#default_value' => $default_desc,
  179. '#weight' => 2
  180. );
  181. $form['url']= array(
  182. '#type' => 'textfield',
  183. '#title' => t('URL'),
  184. '#description' => t('Please enter the web address for this database.'),
  185. '#default_value' => $default_url,
  186. '#weight' => 3
  187. );
  188. $form['urlprefix']= array(
  189. '#type' => 'textfield',
  190. '#title' => t('URL prefix'),
  191. '#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.'),
  192. '#default_value' => $default_urlprefix,
  193. '#weight' => 4
  194. );
  195. if(strcmp($action,'Update')==0){
  196. $form['update'] = array (
  197. '#type' => 'submit',
  198. '#value' => t('Update'),
  199. '#weight' => 5,
  200. '#executes_submit_callback' => TRUE,
  201. );
  202. $form['delete'] = array (
  203. '#type' => 'submit',
  204. '#value' => t('Delete'),
  205. '#weight' => 6,
  206. '#executes_submit_callback' => TRUE,
  207. );
  208. } else {
  209. $form['add'] = array (
  210. '#type' => 'submit',
  211. '#value' => t('Add'),
  212. '#weight' => 5,
  213. '#executes_submit_callback' => TRUE,
  214. );
  215. }
  216. $form['#redirect'] = 'admin/tripal/tripal_db';
  217. return $form;
  218. }
  219. /************************************************************************
  220. *
  221. */
  222. function tripal_db_form_submit($form, &$form_state){
  223. $name = $form_state['values']['name'];
  224. $desc = $form_state['values']['description'];
  225. $url = $form_state['values']['url'];
  226. $urlp = $form_state['values']['urlprefix'];
  227. $dbid = $form_state['values']['dbid'];
  228. $op = $form_state['values']['op'];
  229. if($dbid){
  230. if(strcmp($op,'Update')==0){
  231. $sql = "
  232. UPDATE {db} SET
  233. name = '%s',
  234. description = '%s',
  235. url = '%s',
  236. urlprefix = '%s'
  237. WHERE db_id = %d
  238. ";
  239. $previous_db = tripal_db_set_active('chado');
  240. $db = db_query($sql,$name,$desc,$url,$urlp,$dbid);
  241. tripal_db_set_active($previous_db);
  242. if($db){
  243. drupal_set_message("External database updated");
  244. } else {
  245. drupal_set_message("Failed to update external database.");
  246. }
  247. }
  248. if(strcmp($op,'Delete')==0){
  249. $sql = "
  250. DELETE FROM {db}
  251. WHERE db_id = %d
  252. ";
  253. $previous_db = tripal_db_set_active('chado');
  254. $db = db_query($sql,$dbid);
  255. tripal_db_set_active($previous_db);
  256. if($db){
  257. drupal_set_message("External database deleted");
  258. } else {
  259. drupal_set_message("Failed to delete external database.");
  260. }
  261. }
  262. }
  263. else {
  264. $sql = "
  265. INSERT INTO {db}
  266. (name,description,url,urlprefix)
  267. VALUES
  268. ('%s','%s','%s','%s')
  269. ";
  270. $previous_db = tripal_db_set_active('chado');
  271. $db = db_query($sql,$name,$desc,$url,$urlp);
  272. tripal_db_set_active($previous_db);
  273. if($db){
  274. drupal_set_message("External database added");
  275. } else {
  276. drupal_set_message("Failed to add external database.");
  277. }
  278. }
  279. return '';
  280. }