tripal_cv.module 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 tables',
  27. 'description' => 'The Chado cvtermpath table provides lineage for terms and is useful for quickly finding any ancestor parent of a term. However, this table must be populated. This page allows for populating of this table one vocabulary at a time',
  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_CALLBACK,
  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_CALLBACK
  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. $browser_url = url("cv_browser");
  157. $output = "<a href=\"$add_url\">Add a new controlled vocabulary</a> | ";
  158. $output .= "<a href=\"$cvtermpath_url\">Update the cvtermpath table</a> | ";
  159. $output .= "<a href=\"$browser_url\">Browse a vocabulary</a> ";
  160. $output .= drupal_get_form('tripal_cv_select_form');
  161. $output .= '<div id="db-edit-div">Please select a vocabulary above to view or edit</div>';
  162. return $output;
  163. }
  164. /*************************************************************************
  165. *
  166. */
  167. function tripal_cv_select_form(){
  168. $previous_db = db_set_active('chado'); // use chado database
  169. // get a list of db from chado for user to choose
  170. $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
  171. $results = db_query ($sql);
  172. db_set_active($previous_db); // use drupal database
  173. $cvs = array();
  174. $cvs[] = '';
  175. while ($cv = db_fetch_object($results)){
  176. $cvs[$cv->cv_id] = $cv->name;
  177. }
  178. $form['cvid'] = array(
  179. '#title' => t('Controlled Vocabulary/Ontology Name'),
  180. '#type' => 'select',
  181. '#options' => $cvs,
  182. '#ahah' => array(
  183. 'path' => 'admin/tripal/tripal_cv/edit/js',
  184. 'wrapper' => 'db-edit-div',
  185. 'effect' => 'fade',
  186. 'event' => 'change',
  187. 'method' => 'replace',
  188. ),
  189. );
  190. return $form;
  191. }
  192. /*************************************************************************
  193. *
  194. */
  195. function tripal_ajax_cv_edit (){
  196. // get the database id, build the form and then return the JSON object
  197. $cvid = $_POST['cvid'];
  198. $form = drupal_get_form('tripal_cv_form',$cvid);
  199. drupal_json(array('status' => TRUE, 'data' => $form));
  200. }
  201. /*************************************************************************
  202. *
  203. */
  204. function tripal_cv_form(&$form_state = NULL,$cvid = NULL){
  205. // get this requested database
  206. if($cvid){
  207. $sql = "SELECT * FROM {cv} WHERE cv_id = %d ";
  208. $previous_db = db_set_active('chado');
  209. $cv = db_fetch_object(db_query($sql,$cvid));
  210. db_set_active($previous_db);
  211. # set the default values. If there is a value set in the
  212. # form_state then let's use that, otherwise, we'll pull
  213. # the values from the database
  214. $default_db = $form_state['values']['name'];
  215. $default_desc = $form_state['values']['description'];
  216. $default_url = $form_state['values']['url'];
  217. $default_urlprefix = $form_state['values']['urlprefix'];
  218. if(!$default_db){
  219. $default_cv = $cv->name;
  220. }
  221. if(!$default_desc){
  222. $default_desc = $cv->definition;
  223. }
  224. $action = 'Update';
  225. } else {
  226. $action = 'Add';
  227. }
  228. $form['cvid'] = array(
  229. '#type' => 'hidden',
  230. '#value' => $cvid
  231. );
  232. $form['name']= array(
  233. '#type' => 'textfield',
  234. '#title' => t("Controlled Vocabulary name"),
  235. '#description' => t('Please enter the name for this vocabulary.'),
  236. '#required' => TRUE,
  237. '#default_value' => $default_cv,
  238. '#weight' => 1
  239. );
  240. $form['definition']= array(
  241. '#type' => 'textarea',
  242. '#title' => t('Description'),
  243. '#description' => t('Please enter a description for this vocabulary'),
  244. '#default_value' => $default_desc,
  245. '#weight' => 2
  246. );
  247. if(strcmp($action,'Update')==0){
  248. $form['update'] = array (
  249. '#type' => 'submit',
  250. '#value' => t('Update'),
  251. '#weight' => 5,
  252. '#executes_submit_callback' => TRUE,
  253. );
  254. $form['delete'] = array (
  255. '#type' => 'submit',
  256. '#value' => t('Delete'),
  257. '#weight' => 6,
  258. '#executes_submit_callback' => TRUE,
  259. );
  260. } else {
  261. $form['add'] = array (
  262. '#type' => 'submit',
  263. '#value' => t('Add'),
  264. '#weight' => 5,
  265. '#executes_submit_callback' => TRUE,
  266. );
  267. }
  268. $form['#redirect'] = 'admin/tripal/tripal_cv';
  269. return $form;
  270. }
  271. /************************************************************************
  272. *
  273. */
  274. function tripal_cv_form_submit($form, &$form_state){
  275. $name = $form_state['values']['name'];
  276. $desc = $form_state['values']['definition'];
  277. $cvid = $form_state['values']['cvid'];
  278. $op = $form_state['values']['op'];
  279. if($cvid){
  280. if(strcmp($op,'Update')==0){
  281. $sql = "
  282. UPDATE {cv} SET
  283. name = '%s',
  284. definition = '%s'
  285. WHERE cv_id = %d
  286. ";
  287. $previous_db = db_set_active('chado');
  288. $db = db_query($sql,$name,$desc,$cvid);
  289. db_set_active($previous_db);
  290. if($db){
  291. drupal_set_message("Controlled vocabulary updated");
  292. } else {
  293. drupal_set_message("Failed to update controlled vocabulary.");
  294. }
  295. }
  296. if(strcmp($op,'Delete')==0){
  297. $sql = "
  298. DELETE FROM {cv}
  299. WHERE cv_id = %d
  300. ";
  301. $previous_db = db_set_active('chado');
  302. $db = db_query($sql,$cvid);
  303. db_set_active($previous_db);
  304. if($db){
  305. drupal_set_message("Controlled vocabulary deleted");
  306. } else {
  307. drupal_set_message("Failed to delete controlled vocabulary.");
  308. }
  309. }
  310. }
  311. else {
  312. $sql = "
  313. INSERT INTO {cv}
  314. (name,definition)
  315. VALUES
  316. ('%s','%s')
  317. ";
  318. $previous_db = db_set_active('chado');
  319. $db = db_query($sql,$name,$desc);
  320. db_set_active($previous_db);
  321. if($db){
  322. drupal_set_message("External database added");
  323. } else {
  324. drupal_set_message("Failed to add external database.");
  325. }
  326. }
  327. return '';
  328. }
  329. /*************************************************************************
  330. *
  331. */
  332. function tripal_cv_cvtermpath_form () {
  333. $previous_db = db_set_active('chado'); // use chado database
  334. // get a list of db from chado for user to choose
  335. $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
  336. $results = db_query ($sql);
  337. db_set_active($previous_db); // use drupal database
  338. $cvs = array();
  339. $cvs[] = '';
  340. while ($cv = db_fetch_object($results)){
  341. $cvs[$cv->cv_id] = $cv->name;
  342. }
  343. $form['cvid'] = array(
  344. '#title' => t('Controlled Vocabulary/Ontology Name'),
  345. '#type' => 'select',
  346. '#options' => $cvs,
  347. '#description' => t('Select a controlled vocabulary for which you would like to upate the cvtermpath.'),
  348. );
  349. $form['description'] = array(
  350. '#type' => 'item',
  351. '#value' => t("Submit a job to update chado cvtermpath table."),
  352. '#weight' => 1,
  353. );
  354. $form['button'] = array(
  355. '#type' => 'submit',
  356. '#value' => t('Update cvtermpath'),
  357. '#weight' => 2,
  358. );
  359. return $form;
  360. }
  361. /*************************************************************************
  362. *
  363. */
  364. function tripal_cv_cvtermpath_form_validate($form, &$form_state) {
  365. global $user;
  366. $cvid = $form_state['values']['cvid'];
  367. // first get the controlled vocabulary name:
  368. $previous_db = db_set_active('chado');
  369. $cv = db_fetch_object(db_query("SELECT * FROM {cv} WHERE cv_id = %d",$cvid));
  370. db_set_active($previous_db);
  371. // Submit a job to update cvtermpath
  372. $job_args = array($cvid);
  373. if ($form_state['values']['op'] == t('Update cvtermpath')) {
  374. tripal_add_job("Update cvtermpath: $cv->name",'tripal_cv',
  375. 'tripal_cv_update_cvtermpath',$job_args,$user->uid);
  376. }
  377. }
  378. /***********************************************************
  379. * Update the cvtermpath table
  380. */
  381. function tripal_cv_update_cvtermpath($cvid = NULL, $job_id = NULL) {
  382. // first get the controlled vocabulary name:
  383. $previous_db = db_set_active('chado');
  384. $cv = db_fetch_object(db_query("SELECT * FROM {cv} WHERE cv_id = %d",$cvid));
  385. print "\nUpdating cvtermpath for $cv->name...\n";
  386. // now fill the cvtermpath table
  387. $sql = "SELECT * FROM fill_cvtermpath('%s')";
  388. db_query($sql,$cv->name);
  389. db_set_active($previous_db);
  390. return;
  391. }
  392. /*******************************************************************************
  393. * We need to let drupal know about our theme functions and their arguments.
  394. * We create theme functions to allow users of the module to customize the
  395. * look and feel of the output generated in this module
  396. */
  397. function tripal_cv_theme () {
  398. return array(
  399. 'tripal_cv_cvterm_edit' => array (
  400. 'arguments' => array('cvterm'),
  401. ),
  402. );
  403. }
  404. /*************************************************************************
  405. */
  406. function tripal_cv_get_cv_id($cv_name){
  407. $sql = "
  408. SELECT cv_id FROM {cv} WHERE name = '%s'
  409. ";
  410. $previous_db = db_set_active('chado');
  411. $cv = db_fetch_object(db_query($sql,$cv_name));
  412. db_set_active($previous_db);
  413. return $cv->cv_id;
  414. }
  415. /*************************************************************************
  416. *
  417. */
  418. function tripal_cv_cvterm_edit($cvterm_id){
  419. $sql = "
  420. SELECT CVT.name as cvtermname, CVT.definition, CV.name as cvname
  421. FROM {CVTerm} CVT
  422. INNER JOIN CV on CVT.cv_id = CV.cv_id
  423. WHERE CVT.cvterm_id = %d
  424. ";
  425. $previous_db = db_set_active('chado');
  426. $cvterm = db_fetch_object(db_query($sql,$cvterm_id));
  427. db_set_active($previous_db);
  428. return theme('tripal_cv_cvterm_edit',$cvterm);
  429. }
  430. /*************************************************************************
  431. *
  432. */
  433. function theme_tripal_cv_cvterm_edit(&$cvterm){
  434. $output = "
  435. <div id=\"cvterm\">
  436. <table>
  437. <tr><th>Term</th><td>$cvterm->cvtermname</td></tr>
  438. <tr><th>Vocabulary</th><td>$cvterm->cvname</td></tr>
  439. <tr><th>Definition</th><td>$cvterm->definition</td></tr>
  440. </table>
  441. </div>
  442. ";
  443. return $output;
  444. }