tripal_cv.module 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <?php
  2. require_once "charts.php";
  3. require_once "trees.php";
  4. //
  5. // Copyright 2009 Clemson University
  6. //
  7. /*************************************************************************
  8. *
  9. */
  10. function tripal_cv_init(){
  11. // add the tripal_cv JS and CSS
  12. drupal_add_css(drupal_get_path('theme', 'tripal').
  13. '/css/tripal_cv.css');
  14. drupal_add_js(drupal_get_path('theme', 'tripal').'/js/tripal_cv.js');
  15. // add the jsTree JS and CSS
  16. drupal_add_css(drupal_get_path('theme', 'tripal').'/js/jsTree/source/tree_component.css');
  17. drupal_add_js (drupal_get_path('theme', 'tripal').'/js/jsTree/source/_lib.js');
  18. drupal_add_js (drupal_get_path('theme', 'tripal').'/js/jsTree/source/tree_component.js');
  19. }
  20. /*************************************************************************
  21. *
  22. */
  23. function tripal_cv_menu() {
  24. $items = array();
  25. $items['admin/tripal/tripal_cv/cvtermpath'] = array(
  26. 'title' => 'Update Chado cvtermpath table for GO terms',
  27. 'description' => 'Manage integration of Chado controlled vocabularies',
  28. 'page callback' => 'drupal_get_form',
  29. 'page arguments' => array('tripal_cv_cvtermpath_form'),
  30. 'access arguments' => array('administer site configuration'),
  31. 'type' => MENU_NORMAL_ITEM,
  32. );
  33. $items['admin/tripal/tripal_cv'] = array(
  34. 'title' => 'Controlled Vocabulary Management',
  35. 'description' => 'Manage controlled vocabularies/ontolgoies in Chado ',
  36. 'page callback' => 'tripal_cv_admin_page',
  37. 'access arguments' => array('administer site configuration'),
  38. 'type' => MENU_NORMAL_ITEM,
  39. );
  40. $items['admin/tripal/tripal_cv/new'] = array(
  41. 'title' => 'Add a Controlled Vocabulary',
  42. 'page callback' => 'drupal_get_form',
  43. 'page arguments' => array('tripal_cv_form'),
  44. 'access arguments' => array('access administration pages'),
  45. 'type' => MENU_NORMAL_ITEM,
  46. );
  47. $items['admin/tripal/tripal_cv/edit/js'] = array(
  48. 'title' => 'Edit Controlled Vocabularies',
  49. 'page callback' => 'tripal_ajax_cv_edit',
  50. 'access arguments' => array('access administration pages'),
  51. 'type' => MENU_NORMAL_ITEM,
  52. );
  53. $items['tripal_cv_chart'] = array(
  54. 'path' => 'tripal_cv_chart',
  55. 'title' => t('CV Chart'),
  56. 'page callback' => 'tripal_cv_chart',
  57. 'page arguments' => array(1),
  58. 'access arguments' => array('access content'),
  59. 'type' => MENU_CALLBACK
  60. );
  61. $items['tripal_cv_tree'] = array(
  62. 'path' => 'tripal_cv_tree',
  63. 'title' => t('CV Term Viewer'),
  64. 'page callback' => 'tripal_cv_tree',
  65. 'page arguments' => array(1),
  66. 'access arguments' => array('access content'),
  67. 'type' => MENU_CALLBACK
  68. );
  69. // menu items for working with the CV module tree browser
  70. $items['cv_browser'] = array(
  71. 'title' => t('CV Relationship Browser'),
  72. 'page callback' => 'tripal_cv_show_browser',
  73. 'access arguments' => array('access chado_cv content'),
  74. 'type' => MENU_NORMAL_ITEM
  75. );
  76. $items['tripal_cv_init_browser'] = array(
  77. 'path' => 'tripal_cv_init_browser',
  78. 'title' => t('CV Browser'),
  79. 'page callback' => 'tripal_cv_init_browser',
  80. 'page arguments' => array(1),
  81. 'access arguments' => array('access content'),
  82. 'type' => MENU_CALLBACK
  83. );
  84. // menu item for interaction with the tree
  85. $items['tripal_cv_update_tree'] = array(
  86. 'path' => 'tripal_cv_update_tree',
  87. 'title' => t('CV Tree'),
  88. 'page callback' => 'tripal_cv_update_tree',
  89. 'page arguments' => array(2,3),
  90. 'access arguments' => array('access content'),
  91. 'type' => MENU_CALLBACK
  92. );
  93. // menu items for working with terms
  94. $items['tripal_cv_cvterm_info'] = array(
  95. 'path' => 'tripal_cv_cvterm_info',
  96. 'title' => t('CV Term Viewer'),
  97. 'page callback' => 'tripal_cv_cvterm_info',
  98. 'page arguments' => array(1),
  99. 'access arguments' => array('access content'),
  100. 'type' => MENU_CALLBACK
  101. );
  102. $items['tripal_cv_cvterm_edit'] = array(
  103. 'path' => 'tripal_cv_edit',
  104. 'title' => t('CV Term Editor'),
  105. 'page callback' => 'tripal_cv_cvterm_edit',
  106. 'page arguments' => array(1),
  107. 'access arguments' => array('edit chado_cv content'),
  108. 'type' => MENU_CALLBACK
  109. );
  110. return $items;
  111. }
  112. /*******************************************************************************
  113. * The following function proves access control for users trying to
  114. * perform actions on data managed by this module
  115. */
  116. function chado_cv_access($op, $node, $account){
  117. if ($op == 'create') {
  118. return user_access('create chado_cv content', $account);
  119. }
  120. if ($op == 'update') {
  121. if (user_access('edit chado_cv content', $account)) {
  122. return TRUE;
  123. }
  124. }
  125. if ($op == 'delete') {
  126. if (user_access('delete chado_cv content', $account)) {
  127. return TRUE;
  128. }
  129. }
  130. if ($op == 'view') {
  131. if (user_access('access chado_cv content', $account)) {
  132. return TRUE;
  133. }
  134. }
  135. return FALSE;
  136. }
  137. /*******************************************************************************
  138. * Set the permission types that the chado module uses. Essentially we
  139. * want permissionis that protect creation, editing and deleting of chado
  140. * data objects
  141. */
  142. function tripal_cv_perm(){
  143. return array(
  144. 'access chado_cv content',
  145. 'create chado_cv content',
  146. 'delete chado_cv content',
  147. 'edit chado_cv content',
  148. );
  149. }
  150. /*************************************************************************
  151. *
  152. */
  153. function tripal_cv_admin_page(){
  154. $add_url = url("admin/tripal/tripal_cv/new");
  155. $cvtermpath_url = url("admin/tripal/tripal_cv/cvtermpath");
  156. $output = "<a href=\"$add_url\">Add a new controlled vocabulary</a> | ";
  157. $output .= "<a href=\"$cvtermpath_url\">Update the cvtermpath table</a> ";
  158. $output .= drupal_get_form('tripal_cv_select_form');
  159. $output .= '<div id="db-edit-div">Please select a database above to view or edit</div>';
  160. return $output;
  161. }
  162. /*************************************************************************
  163. *
  164. */
  165. function tripal_cv_select_form(){
  166. $previous_db = db_set_active('chado'); // use chado database
  167. // get a list of db from chado for user to choose
  168. $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
  169. $results = db_query ($sql);
  170. db_set_active($previous_db); // use drupal database
  171. $cvs = array();
  172. $cvs[] = '';
  173. while ($cv = db_fetch_object($results)){
  174. $cvs[$cv->cv_id] = $cv->name;
  175. }
  176. $form['cvid'] = array(
  177. '#title' => t('Controlled Vocabulary/Ontology Name'),
  178. '#type' => 'select',
  179. '#options' => $cvs,
  180. '#ahah' => array(
  181. 'path' => 'admin/tripal/tripal_cv/edit/js',
  182. 'wrapper' => 'db-edit-div',
  183. 'effect' => 'fade',
  184. 'event' => 'change',
  185. 'method' => 'replace',
  186. ),
  187. );
  188. return $form;
  189. }
  190. /*************************************************************************
  191. *
  192. */
  193. function tripal_ajax_cv_edit (){
  194. // get the database id, build the form and then return the JSON object
  195. $cvid = $_POST['cvid'];
  196. $form = drupal_get_form('tripal_cv_form',$cvid);
  197. drupal_json(array('status' => TRUE, 'data' => $form));
  198. }
  199. /*************************************************************************
  200. *
  201. */
  202. function tripal_cv_form(&$form_state = NULL,$cvid = NULL){
  203. // get this requested database
  204. if($cvid){
  205. $sql = "SELECT * FROM {cv} WHERE cv_id = %d ";
  206. $previous_db = db_set_active('chado');
  207. $cv = db_fetch_object(db_query($sql,$cvid));
  208. db_set_active($previous_db);
  209. # set the default values. If there is a value set in the
  210. # form_state then let's use that, otherwise, we'll pull
  211. # the values from the database
  212. $default_db = $form_state['values']['name'];
  213. $default_desc = $form_state['values']['description'];
  214. $default_url = $form_state['values']['url'];
  215. $default_urlprefix = $form_state['values']['urlprefix'];
  216. if(!$default_db){
  217. $default_cv = $cv->name;
  218. }
  219. if(!$default_desc){
  220. $default_desc = $cv->definition;
  221. }
  222. $action = 'Update';
  223. } else {
  224. $action = 'Add';
  225. }
  226. $form['cvid'] = array(
  227. '#type' => 'hidden',
  228. '#value' => $cvid
  229. );
  230. $form['name']= array(
  231. '#type' => 'textfield',
  232. '#title' => t("Controlled Vocabulary name"),
  233. '#description' => t('Please enter the name for this vocabulary.'),
  234. '#required' => TRUE,
  235. '#default_value' => $default_cv,
  236. '#weight' => 1
  237. );
  238. $form['definition']= array(
  239. '#type' => 'textarea',
  240. '#title' => t('Description'),
  241. '#description' => t('Please enter a description for this vocabulary'),
  242. '#default_value' => $default_desc,
  243. '#weight' => 2
  244. );
  245. if(strcmp($action,'Update')==0){
  246. $form['update'] = array (
  247. '#type' => 'submit',
  248. '#value' => t('Update'),
  249. '#weight' => 5,
  250. '#executes_submit_callback' => TRUE,
  251. );
  252. $form['delete'] = array (
  253. '#type' => 'submit',
  254. '#value' => t('Delete'),
  255. '#weight' => 6,
  256. '#executes_submit_callback' => TRUE,
  257. );
  258. } else {
  259. $form['add'] = array (
  260. '#type' => 'submit',
  261. '#value' => t('Add'),
  262. '#weight' => 5,
  263. '#executes_submit_callback' => TRUE,
  264. );
  265. }
  266. $form['#redirect'] = 'admin/tripal/tripal_cv';
  267. return $form;
  268. }
  269. /************************************************************************
  270. *
  271. */
  272. function tripal_cv_form_submit($form, &$form_state){
  273. $name = $form_state['values']['name'];
  274. $desc = $form_state['values']['definition'];
  275. $cvid = $form_state['values']['cvid'];
  276. $op = $form_state['values']['op'];
  277. if($cvid){
  278. if(strcmp($op,'Update')==0){
  279. $sql = "
  280. UPDATE {cv} SET
  281. name = '%s',
  282. definition = '%s'
  283. WHERE cv_id = %d
  284. ";
  285. $previous_db = db_set_active('chado');
  286. $db = db_query($sql,$name,$desc,$cvid);
  287. db_set_active($previous_db);
  288. if($db){
  289. drupal_set_message("Controlled vocabulary updated");
  290. } else {
  291. drupal_set_message("Failed to update controlled vocabulary.");
  292. }
  293. }
  294. if(strcmp($op,'Delete')==0){
  295. $sql = "
  296. DELETE FROM {cv}
  297. WHERE cv_id = %d
  298. ";
  299. $previous_db = db_set_active('chado');
  300. $db = db_query($sql,$cvid);
  301. db_set_active($previous_db);
  302. if($db){
  303. drupal_set_message("Controlled vocabulary deleted");
  304. } else {
  305. drupal_set_message("Failed to delete controlled vocabulary.");
  306. }
  307. }
  308. }
  309. else {
  310. $sql = "
  311. INSERT INTO {cv}
  312. (name,definition)
  313. VALUES
  314. ('%s','%s')
  315. ";
  316. $previous_db = db_set_active('chado');
  317. $db = db_query($sql,$name,$desc);
  318. db_set_active($previous_db);
  319. if($db){
  320. drupal_set_message("External database added");
  321. } else {
  322. drupal_set_message("Failed to add external database.");
  323. }
  324. }
  325. return '';
  326. }
  327. /*************************************************************************
  328. *
  329. */
  330. function tripal_cv_cvtermpath_form () {
  331. $previous_db = db_set_active('chado'); // use chado database
  332. // get a list of db from chado for user to choose
  333. $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
  334. $results = db_query ($sql);
  335. db_set_active($previous_db); // use drupal database
  336. $cvs = array();
  337. $cvs[] = '';
  338. while ($cv = db_fetch_object($results)){
  339. $cvs[$cv->cv_id] = $cv->name;
  340. }
  341. $form['cvid'] = array(
  342. '#title' => t('Controlled Vocabulary/Ontology Name'),
  343. '#type' => 'select',
  344. '#options' => $cvs,
  345. '#description' => t('Select a controlled vocabulary for which you would like to upate the cvtermpath.'),
  346. );
  347. $form['description'] = array(
  348. '#type' => 'item',
  349. '#value' => t("Submit a job to update chado cvtermpath table."),
  350. '#weight' => 1,
  351. );
  352. $form['button'] = array(
  353. '#type' => 'submit',
  354. '#value' => t('Update cvtermpath'),
  355. '#weight' => 2,
  356. );
  357. return $form;
  358. }
  359. /*************************************************************************
  360. *
  361. */
  362. function tripal_cv_cvtermpath_form_validate($form, &$form_state) {
  363. global $user;
  364. $cvid = $form_state['values']['cvid'];
  365. // first get the controlled vocabulary name:
  366. $previous_db = db_set_active('chado');
  367. $cv = db_fetch_object(db_query("SELECT * FROM {cv} WHERE cv_id = %d",$cvid));
  368. db_set_active($previous_db);
  369. // Submit a job to update cvtermpath
  370. $job_args = array($cvid);
  371. if ($form_state['values']['op'] == t('Update cvtermpath')) {
  372. tripal_add_job("Update cvtermpath: $cv->name",'tripal_cv',
  373. 'tripal_cv_update_cvtermpath',$job_args,$user->uid);
  374. }
  375. }
  376. /***********************************************************
  377. * Update the cvtermpath table
  378. */
  379. function tripal_cv_update_cvtermpath($cvid = NULL, $job_id = NULL) {
  380. // first get the controlled vocabulary name:
  381. $previous_db = db_set_active('chado');
  382. $cv = db_fetch_object(db_query("SELECT * FROM {cv} WHERE cv_id = %d",$cvid));
  383. print "\nUpdating cvtermpath for $cv->name...\n";
  384. // now fill the cvtermpath table
  385. $sql = "SELECT * FROM fill_cvtermpath('%s')";
  386. db_query($sql,$cv->name);
  387. db_set_active($previous_db);
  388. return;
  389. }
  390. /*******************************************************************************
  391. * We need to let drupal know about our theme functions and their arguments.
  392. * We create theme functions to allow users of the module to customize the
  393. * look and feel of the output generated in this module
  394. */
  395. function tripal_cv_theme () {
  396. return array(
  397. 'tripal_cv_cvterm_edit' => array (
  398. 'arguments' => array('cvterm'),
  399. ),
  400. );
  401. }
  402. /*************************************************************************
  403. */
  404. function tripal_cv_get_cv_id($cv_name){
  405. $sql = "
  406. SELECT cv_id FROM {cv} WHERE name = '%s'
  407. ";
  408. $previous_db = db_set_active('chado');
  409. $cv = db_fetch_object(db_query($sql,$cv_name));
  410. db_set_active($previous_db);
  411. return $cv->cv_id;
  412. }
  413. /*************************************************************************
  414. *
  415. */
  416. function tripal_cv_cvterm_edit($cvterm_id){
  417. $sql = "
  418. SELECT CVT.name as cvtermname, CVT.definition, CV.name as cvname
  419. FROM {CVTerm} CVT
  420. INNER JOIN CV on CVT.cv_id = CV.cv_id
  421. WHERE CVT.cvterm_id = %d
  422. ";
  423. $previous_db = db_set_active('chado');
  424. $cvterm = db_fetch_object(db_query($sql,$cvterm_id));
  425. db_set_active($previous_db);
  426. return theme('tripal_cv_cvterm_edit',$cvterm);
  427. }
  428. /*************************************************************************
  429. *
  430. */
  431. function theme_tripal_cv_cvterm_edit(&$cvterm){
  432. $output = "
  433. <div id=\"cvterm\">
  434. <table>
  435. <tr><th>Term</th><td>$cvterm->cvtermname</td></tr>
  436. <tr><th>Vocabulary</th><td>$cvterm->cvname</td></tr>
  437. <tr><th>Definition</th><td>$cvterm->definition</td></tr>
  438. </table>
  439. </div>
  440. ";
  441. return $output;
  442. }