tripal_cv.module 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. <?php
  2. require_once "charts.php";
  3. require_once "trees.php";
  4. require_once "obo_loader.php";
  5. require_once "tripal_cv.api.inc";
  6. /*************************************************************************
  7. *
  8. */
  9. function tripal_cv_init(){
  10. // add the tripal_cv JS and CSS
  11. drupal_add_css(drupal_get_path('theme', 'tripal').
  12. '/css/tripal_cv.css');
  13. drupal_add_js(drupal_get_path('theme', 'tripal').'/js/tripal_cv.js');
  14. // add the jsTree JS and CSS
  15. drupal_add_css(drupal_get_path('theme', 'tripal').'/js/jsTree/source/tree_component.css');
  16. drupal_add_js (drupal_get_path('theme', 'tripal').'/js/jsTree/source/_lib.js');
  17. drupal_add_js (drupal_get_path('theme', 'tripal').'/js/jsTree/source/tree_component.js');
  18. }
  19. /*************************************************************************
  20. *
  21. */
  22. function tripal_cv_menu() {
  23. $items = array();
  24. $items['admin/tripal/tripal_cv'] = array(
  25. 'title' => 'Controlled Vocabularies',
  26. 'description' => 'Basic Description of Tripal CV Module Functionality',
  27. 'page callback' => 'tripal_cv_module_description_page',
  28. 'access arguments' => array('administer site configuration'),
  29. 'type' => MENU_NORMAL_ITEM,
  30. );
  31. $items['admin/tripal/tripal_cv/cvtermpath'] = array(
  32. 'title' => 'Update Chado cvtermpath tables',
  33. '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',
  34. 'page callback' => 'drupal_get_form',
  35. 'page arguments' => array('tripal_cv_cvtermpath_form'),
  36. 'access arguments' => array('administer site configuration'),
  37. 'type' => MENU_NORMAL_ITEM,
  38. );
  39. $items['admin/tripal/tripal_cv/edit_cv'] = array(
  40. 'title' => 'Update/Delete Controlled Vocabulary',
  41. 'description' => 'Manage controlled vocabularies/ontolgoies in Chado ',
  42. 'page callback' => 'tripal_cv_admin_page',
  43. 'access arguments' => array('administer site configuration'),
  44. 'type' => MENU_NORMAL_ITEM,
  45. );
  46. $items['admin/tripal/tripal_cv/add_cv'] = array(
  47. 'title' => 'Add a Controlled Vocabulary',
  48. 'page callback' => 'drupal_get_form',
  49. 'page arguments' => array('tripal_cv_add_form'),
  50. 'access arguments' => array('access administration pages'),
  51. 'type' => MENU_NORMAL_ITEM,
  52. );
  53. $items['admin/tripal/tripal_cv/add_cvterm'] = array(
  54. 'title' => 'Add Controlled Vocabulary Terms',
  55. 'description' => 'Manage controlled vocabulary/ontology terms in Chado ',
  56. 'page callback' => 'drupal_get_form',
  57. 'page arguments' => array('tripal_cv_add_cvterm_form'),
  58. 'access arguments' => array('administer site configuration'),
  59. 'type' => MENU_NORMAL_ITEM,
  60. );
  61. $items['admin/tripal/tripal_cv/add_cvterm/js'] = array(
  62. 'page callback' => 'tripal_cv_add_cvterm_callback',
  63. 'access arguments' => array('administer site configuration'),
  64. 'type' => MENU_CALLBACK,
  65. );
  66. $items['admin/tripal/tripal_cv/obo_loader'] = array(
  67. 'title' =>'Add/Update Ontology With OBO File',
  68. 'page callback' => 'drupal_get_form',
  69. 'page arguments' => array('tripal_cv_obo_form'),
  70. 'access arguments' => array('access administration pages'),
  71. 'type' => MENU_NORMAL_ITEM,
  72. );
  73. $items['admin/tripal/tripal_cv/edit/js'] = array(
  74. 'title' => 'Edit Controlled Vocabularies',
  75. 'page callback' => 'tripal_ajax_cv_edit',
  76. 'access arguments' => array('access administration pages'),
  77. 'type' => MENU_CALLBACK,
  78. );
  79. $items['tripal_cv_chart'] = array(
  80. 'path' => 'tripal_cv_chart',
  81. 'title' => t('CV Chart'),
  82. 'page callback' => 'tripal_cv_chart',
  83. 'page arguments' => array(1),
  84. 'access arguments' => array('access content'),
  85. 'type' => MENU_CALLBACK
  86. );
  87. $items['tripal_cv_tree'] = array(
  88. 'path' => 'tripal_cv_tree',
  89. 'title' => t('CV Term Viewer'),
  90. 'page callback' => 'tripal_cv_tree',
  91. 'page arguments' => array(1),
  92. 'access arguments' => array('access content'),
  93. 'type' => MENU_CALLBACK
  94. );
  95. // menu items for working with the CV module tree browser
  96. $items['cv_browser'] = array(
  97. 'title' => t('CV Relationship Browser'),
  98. 'page callback' => 'tripal_cv_show_browser',
  99. 'access arguments' => array('access chado_cv content'),
  100. 'type' => MENU_CALLBACK
  101. );
  102. $items['tripal_cv_init_browser'] = array(
  103. 'path' => 'tripal_cv_init_browser',
  104. 'title' => t('CV Browser'),
  105. 'page callback' => 'tripal_cv_init_browser',
  106. 'page arguments' => array(1),
  107. 'access arguments' => array('access content'),
  108. 'type' => MENU_CALLBACK
  109. );
  110. // menu item for interaction with the tree
  111. $items['tripal_cv_update_tree'] = array(
  112. 'path' => 'tripal_cv_update_tree',
  113. 'title' => t('CV Tree'),
  114. 'page callback' => 'tripal_cv_update_tree',
  115. 'page arguments' => array(2,3),
  116. 'access arguments' => array('access content'),
  117. 'type' => MENU_CALLBACK
  118. );
  119. // menu items for working with terms
  120. $items['tripal_cv_cvterm_info'] = array(
  121. 'path' => 'tripal_cv_cvterm_info',
  122. 'title' => t('CV Term Viewer'),
  123. 'page callback' => 'tripal_cv_cvterm_info',
  124. 'page arguments' => array(1),
  125. 'access arguments' => array('access content'),
  126. 'type' => MENU_CALLBACK
  127. );
  128. $items['tripal_cv_cvterm_edit'] = array(
  129. 'path' => 'tripal_cv_edit',
  130. 'title' => t('CV Term Editor'),
  131. 'page callback' => 'tripal_cv_cvterm_edit',
  132. 'page arguments' => array(1),
  133. 'access arguments' => array('edit chado_cv content'),
  134. 'type' => MENU_CALLBACK
  135. );
  136. return $items;
  137. }
  138. /*******************************************************************************
  139. * The following function proves access control for users trying to
  140. * perform actions on data managed by this module
  141. */
  142. function chado_cv_access($op, $node, $account){
  143. if ($op == 'create') {
  144. return user_access('create chado_cv content', $account);
  145. }
  146. if ($op == 'update') {
  147. if (user_access('edit chado_cv content', $account)) {
  148. return TRUE;
  149. }
  150. }
  151. if ($op == 'delete') {
  152. if (user_access('delete chado_cv content', $account)) {
  153. return TRUE;
  154. }
  155. }
  156. if ($op == 'view') {
  157. if (user_access('access chado_cv content', $account)) {
  158. return TRUE;
  159. }
  160. }
  161. return FALSE;
  162. }
  163. /*******************************************************************************
  164. * Set the permission types that the chado module uses. Essentially we
  165. * want permissionis that protect creation, editing and deleting of chado
  166. * data objects
  167. */
  168. function tripal_cv_perm(){
  169. return array(
  170. 'access chado_cv content',
  171. 'create chado_cv content',
  172. 'delete chado_cv content',
  173. 'edit chado_cv content',
  174. );
  175. }
  176. /*************************************************************************
  177. * Implements hook_views_api()
  178. * Purpose: Essentially this hook tells drupal that there is views support for
  179. * for this module which then includes tripal_cv.views.inc where all the
  180. * views integration code is
  181. */
  182. function tripal_cv_views_api() {
  183. return array('api' => 2.0);
  184. }
  185. /*******************************************************************************
  186. * We need to let drupal know about our theme functions and their arguments.
  187. * We create theme functions to allow users of the module to customize the
  188. * look and feel of the output generated in this module
  189. */
  190. function tripal_cv_theme () {
  191. return array(
  192. 'tripal_cv_cvterm_edit' => array (
  193. 'arguments' => array('cvterm'),
  194. ),
  195. );
  196. }
  197. /*************************************************************************
  198. * Purpose: Provide Guidance to new Tripal Admin
  199. *
  200. * @return HTML Formatted text
  201. */
  202. function tripal_cv_module_description_page() {
  203. $text = '';
  204. $text .= '<h3>Description:</h3>';
  205. $text .= '<p>The Tripal CV (Controlled Vocabularies) Module provides
  206. functionality for managing controlled vocabularies and the terms they are
  207. comprised of. The flexibility and extendibility of the chado schema depends
  208. on controlled vocabularies. For example, by using a controlled vocabulary for
  209. feature types the chado schema can describe features of any type, even those
  210. we have not concieved of yet.</p>';
  211. $text .= '<h3>Post Installation Instructions:</h3>';
  212. $text .= '<p>The main post installation step is <b>load any controlled
  213. vocabularies/ontologies needed for you content</b>. It is suggested that you
  214. use community developed ontologes whenever possible. These ontologies
  215. are often very well thought out and cover possibilities you may not have
  216. thought of yet.</p>';
  217. $text .= '<p>However, in some cases, community developed ontologies for your
  218. data may not yet be developed. In this case, it is suggested that you begin
  219. developement of an ontology using one of the online tools. You might find
  220. that many researchers are trying to deal with the same data and are willing
  221. to help you in this endevor. You can create a controlled vocabulary and
  222. add terms to it to provide functionality to your site while you are waiting
  223. for the ontology to be developed.</p>';
  224. $text .= '<h3>Features of this Module:</h3>';
  225. $text .= '<b>'.l('Load an already Existing Ontology/Controlled Vocabulary', 'admin/tripal/tripal_cv/obo_loader').'</b>';
  226. $text .= '<p>This module provides an ontology loader to load already existing
  227. ontologies into chado. You can access this loader at '.
  228. l('Admin->Tripal Management->Tripal CV->Add/Update Ontology With OBO File', 'admin/tripal/tripal_cv/obo_loader')
  229. .'. This loader allows you to choose from a list of common ontologies or
  230. enter the URL or location to an OBO file. Even the list of common
  231. ontologies is using a URL ensuring you get the most up to date ontology.</p>';
  232. $text .= '<p>This loader adds a Tripal Job which then waits in a queue to
  233. be launched. To launch Tripal Jobs either navidate to the root of your
  234. drupal installation and execute "php sites/all/modules/tripal/tripal_core/
  235. tripal_launch_jobs.php <drupal user>" or set up a cron job (See user manual
  236. for more details).</p>';
  237. $text .= '<b>Create/Update/Delete A Controlled Vocaulbulary</b>';
  238. $text .= '<p>As mentioned above, there are some cases where you might need
  239. to create your own controlled vocabulary. To do this go to '.
  240. l('Admin->Tripal Management->Tripal CV->Add a Controlled Vocabulary','admin/tripal/tripal_cv/add_cv')
  241. .' and fill out the form provided.</p>';
  242. $text .= '<p>Sometimes you might want to update or even delete an already
  243. existing controlled vocabulary. To Update/Delete a controlled vocabulary
  244. go to '.l('Admin->Tripal Management->Tripal CV->Update/Delete Controlled Vocabulary', 'admin/tripal/tripal_cv/edit_cv')
  245. .', select the existing controlled vocabulary you want to modify and then
  246. edit it as desired. This only modifies the name, description of a
  247. controlled vocabulary. See the next section for adding, removing, editing
  248. the term a controlled vocabulary contains.</p>';
  249. $text .= '<b>Create a Controlled Vocaulbulary Term</b>';
  250. $text .= '<p>To Add a term to an already existing controlled vocabulary
  251. go to '.l('Admin->Tripal Management->Tripal CV->Add a Controlled Vocabulary Term','admin/tripal/tripal_cv/add_cvterm')
  252. .', select the controlled vocabulary you want to add terms to and then fill
  253. out the form.</p>';
  254. return $text;
  255. }
  256. /*************************************************************************
  257. * @section Edit/Delete CVs
  258. *************************************************************************/
  259. /*************************************************************************
  260. * Purpose: Provides the form for Updating and Deleteing existing
  261. * chado controlled vocabularies (See chado cv table)
  262. */
  263. function tripal_cv_admin_page(){
  264. $add_url = url("admin/tripal/tripal_cv/new");
  265. $obo_url = url("admin/tripal/tripal_cv/obo");
  266. $cvtermpath_url = url("admin/tripal/tripal_cv/cvtermpath");
  267. $browser_url = url("cv_browser");
  268. $output = "<a href=\"$add_url\">Add a new controlled vocabulary</a> | ";
  269. $output .= "<a href=\"$browser_url\">Browse a vocabulary</a> | ";
  270. $output .= "<a href=\"$obo_url\">Add/Update Ontology With OBO File</a> | ";
  271. $output .= "<a href=\"$cvtermpath_url\">Update the cvtermpath table</a> ";
  272. $output .= drupal_get_form('tripal_cv_select_form');
  273. $output .= '<div id="db-edit-div">Please select a vocabulary above to view or edit</div>';
  274. return $output;
  275. }
  276. /*************************************************************************
  277. * Purpose: Provides the actual "Select CV" form on the Update/Delete Controlled
  278. * Vocabulary page. This form also triggers the edit javascript
  279. * @todo Modify this form to use Drupal AJAX
  280. */
  281. function tripal_cv_select_form(){
  282. $previous_db = tripal_db_set_active('chado'); // use chado database
  283. // get a list of db from chado for user to choose
  284. $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
  285. $results = db_query ($sql);
  286. tripal_db_set_active($previous_db); // use drupal database
  287. $cvs = array();
  288. $cvs[] = '';
  289. while ($cv = db_fetch_object($results)){
  290. $cvs[$cv->cv_id] = $cv->name;
  291. }
  292. $form['cvid'] = array(
  293. '#title' => t('Controlled Vocabulary/Ontology Name'),
  294. '#type' => 'select',
  295. '#options' => $cvs,
  296. '#ahah' => array(
  297. 'path' => 'admin/tripal/tripal_cv/edit/js',
  298. 'wrapper' => 'db-edit-div',
  299. 'effect' => 'fade',
  300. 'event' => 'change',
  301. 'method' => 'replace',
  302. ),
  303. );
  304. return $form;
  305. }
  306. /*************************************************************************
  307. * Purpose: The edit controlled vocabulary javascript
  308. */
  309. function tripal_ajax_cv_edit (){
  310. // get the database id, build the form and then return the JSON object
  311. $cvid = $_POST['cvid'];
  312. $form = drupal_get_form('tripal_cv_edit_form',$cvid);
  313. drupal_json(array('status' => TRUE, 'data' => $form));
  314. }
  315. /*************************************************************************
  316. * Purpose: Provides a form to allow updating/deleteing of controlled vocabularies
  317. */
  318. function tripal_cv_edit_form(&$form_state = NULL,$cvid = NULL){
  319. $sql = "SELECT * FROM {cv} WHERE cv_id = %d ";
  320. $previous_db = tripal_db_set_active('chado');
  321. $cv = db_fetch_object(db_query($sql,$cvid));
  322. tripal_db_set_active($previous_db);
  323. # set the default values. If there is a value set in the
  324. # form_state then let's use that, otherwise, we'll pull
  325. # the values from the database
  326. $default_db = $form_state['values']['name'];
  327. $default_desc = $form_state['values']['description'];
  328. $default_url = $form_state['values']['url'];
  329. $default_urlprefix = $form_state['values']['urlprefix'];
  330. if(!$default_db){
  331. $default_cv = $cv->name;
  332. }
  333. if(!$default_desc){
  334. $default_desc = $cv->definition;
  335. }
  336. $form['cvid'] = array(
  337. '#type' => 'hidden',
  338. '#value' => $cvid
  339. );
  340. $form['name']= array(
  341. '#type' => 'textfield',
  342. '#title' => t("Controlled Vocabulary name"),
  343. '#description' => t('Please enter the name for this vocabulary.'),
  344. '#required' => FALSE,
  345. '#default_value' => $default_cv,
  346. '#weight' => 1
  347. );
  348. $form['definition']= array(
  349. '#type' => 'textarea',
  350. '#title' => t('Description'),
  351. '#description' => t('Please enter a description for this vocabulary'),
  352. '#default_value' => $default_desc,
  353. '#weight' => 2
  354. );
  355. $form['update'] = array (
  356. '#type' => 'submit',
  357. '#value' => t('Update'),
  358. '#weight' => 5,
  359. '#executes_submit_callback' => TRUE,
  360. );
  361. $form['delete'] = array (
  362. '#type' => 'submit',
  363. '#value' => t('Delete'),
  364. '#weight' => 6,
  365. '#executes_submit_callback' => TRUE,
  366. );
  367. $form['#redirect'] = 'admin/tripal/tripal_cv';
  368. return $form;
  369. }
  370. /************************************************************************
  371. * Purpose: The submit function of the update/delete controlled vocabulary form
  372. */
  373. function tripal_cv_edit_form_submit($form, &$form_state){
  374. $name = $form_state['values']['name'];
  375. $desc = $form_state['values']['definition'];
  376. $cvid = $form_state['values']['cvid'];
  377. $op = $form_state['values']['op'];
  378. if(strcmp($op,'Update')==0){
  379. $sql = "
  380. UPDATE {cv} SET
  381. name = '%s',
  382. definition = '%s'
  383. WHERE cv_id = %d
  384. ";
  385. $previous_db = tripal_db_set_active('chado');
  386. $db = db_query($sql,$name,$desc,$cvid);
  387. tripal_db_set_active($previous_db);
  388. if($db){
  389. drupal_set_message("Controlled vocabulary updated");
  390. } else {
  391. drupal_set_message("Failed to update controlled vocabulary.");
  392. }
  393. }
  394. if(strcmp($op,'Delete')==0){
  395. $sql = "
  396. DELETE FROM {cv}
  397. WHERE cv_id = %d
  398. ";
  399. $previous_db = tripal_db_set_active('chado');
  400. $db = db_query($sql,$cvid);
  401. tripal_db_set_active($previous_db);
  402. if($db){
  403. drupal_set_message("Controlled vocabulary deleted");
  404. } else {
  405. drupal_set_message("Failed to delete controlled vocabulary.");
  406. }
  407. }
  408. return '';
  409. }
  410. /*************************************************************************
  411. * @section Add CVs
  412. *************************************************************************/
  413. /*************************************************************************
  414. * Purpose: Provides the Add controlled vocabulary form
  415. */
  416. function tripal_cv_add_form(&$form_state = NULL){
  417. $form['cvid'] = array(
  418. '#type' => 'hidden',
  419. '#value' => $cvid
  420. );
  421. $form['name']= array(
  422. '#type' => 'textfield',
  423. '#title' => t("Controlled Vocabulary name"),
  424. '#description' => t('Please enter the name for this vocabulary. This field will be ignored if an OBO file or URL is provided above'),
  425. '#required' => FALSE,
  426. '#default_value' => $default_cv,
  427. '#weight' => 1
  428. );
  429. $form['definition']= array(
  430. '#type' => 'textarea',
  431. '#title' => t('Description'),
  432. '#description' => t('Please enter a description for this vocabulary'),
  433. '#default_value' => $default_desc,
  434. '#weight' => 2
  435. );
  436. $form['add'] = array (
  437. '#type' => 'submit',
  438. '#value' => t('Add'),
  439. '#weight' => 5,
  440. '#executes_submit_callback' => TRUE,
  441. );
  442. $form['#redirect'] = 'admin/tripal/tripal_cv';
  443. return $form;
  444. }
  445. /************************************************************************
  446. * Purpose: The submit function for the add controlled vocabulary form
  447. */
  448. function tripal_cv_add_form_submit($form, &$form_state){
  449. $name = $form_state['values']['name'];
  450. $desc = $form_state['values']['definition'];
  451. $sql = "
  452. INSERT INTO {cv}
  453. (name,definition)
  454. VALUES
  455. ('%s','%s')
  456. ";
  457. $previous_db = tripal_db_set_active('chado');
  458. $db = db_query($sql,$name,$desc);
  459. tripal_db_set_active($previous_db);
  460. if($db){
  461. drupal_set_message("Controlled vocabulary added");
  462. } else {
  463. drupal_set_message("Failed to add controlled vocabulary.");
  464. }
  465. return '';
  466. }
  467. /*************************************************************************
  468. * @section Add Controlled Vocabulary Term
  469. *************************************************************************/
  470. /*************************************************************************
  471. * Purpose: Provides the form that allows adding of terms to an existing
  472. * controlled vocabulary
  473. */
  474. function tripal_cv_add_cvterm_form (&$form_state) {
  475. $form = array();
  476. $results = tripal_core_chado_select(
  477. 'cv',
  478. array('cv_id','name'),
  479. array()
  480. );
  481. $cvs = array();
  482. $cvs[] = '';
  483. foreach ($results as $cv) {
  484. $cvs[$cv->cv_id] = $cv->name;
  485. }
  486. $form['cv_id'] = array(
  487. '#title' => t('Controlled Vocabulary/Ontology Name'),
  488. '#type' => 'select',
  489. '#options' => $cvs,
  490. '#ahah' => array(
  491. 'path' => 'admin/tripal/tripal_cv/add_cvterm/js',
  492. 'wrapper' => 'cvterm-add-div',
  493. 'effect' => 'fade',
  494. 'event' => 'change',
  495. 'method' => 'replace',
  496. ),
  497. '#required' => TRUE,
  498. );
  499. $form['add_cvterm'] = array(
  500. '#type' => 'item',
  501. '#value' => t('Please select a vocabulary above to add a term to it'),
  502. '#prefix' => '<div id="cvterm-add-div">',
  503. '#suffix' => '</div>'
  504. );
  505. if ($form_state['values']['cv_id']) {
  506. $form['add_cvterm'] = array(
  507. '#type' => 'fieldset',
  508. '#title' => t('Add Term to the current Controlled Vocabulary'),
  509. '#prefix' => '<div id="cvterm-add-div">',
  510. '#suffix' => '</div>'
  511. );
  512. $form['add_cvterm']['name']= array(
  513. '#type' => 'textfield',
  514. '#title' => t("Term Name"),
  515. '#description' => t('Please enter the name for this vocabulary term.'),
  516. '#required' => FALSE,
  517. '#weight' => 1,
  518. '#required' => TRUE,
  519. );
  520. $form['add_cvterm']['definition']= array(
  521. '#type' => 'textarea',
  522. '#title' => t('Description'),
  523. '#description' => t('Please enter a description for this term'),
  524. '#weight' => 2
  525. );
  526. $form['add_cvterm']['is_relationshiptype'] = array(
  527. '#type' => 'checkbox',
  528. '#title' => t('This term describes a relationship?'),
  529. '#weight' => 3,
  530. );
  531. $form['add_cvterm']['is_obsolete'] = array(
  532. '#type' => 'checkbox',
  533. '#title' => t('This term is obsolete?'),
  534. '#weight' => 3,
  535. );
  536. $results = tripal_core_chado_select(
  537. 'db',
  538. array('db_id', 'name'),
  539. array()
  540. );
  541. $dbs = array();
  542. $dbs[] = '';
  543. foreach ($results as $db) {
  544. $dbs[$db->db_id] = $db->name;
  545. }
  546. $form['add_cvterm']['db_id'] = array(
  547. '#type' => 'select',
  548. '#title' => t('Database'),
  549. '#description' => t('All terms must be assocated with an external database.
  550. Please select the external database to associate with
  551. this term'),
  552. '#options' => $dbs,
  553. '#weight' => 4,
  554. '#required' => TRUE,
  555. );
  556. $form['add_cvterm']['submit'] = array(
  557. '#type' => 'submit',
  558. '#value' => 'Add Term',
  559. '#weight' => 5
  560. );
  561. } //end of if cv selected
  562. return $form;
  563. }
  564. /*************************************************************************
  565. * Purpose: Validates the input for adding a cvterm
  566. */
  567. function tripal_cv_add_cvterm_form_validate ($form, &$form_state) {
  568. if (!empty($form_state['ahah_submission'])) {
  569. return;
  570. }
  571. }
  572. /*************************************************************************
  573. * Purpose: Adds terms to an existing controlled vocabulary
  574. */
  575. function tripal_cv_add_cvterm_form_submit ($form, &$form_state) {
  576. if (!empty($form_state['ahah_submission'])) {
  577. return;
  578. }
  579. // Add dbxref for cvterm
  580. $dbxref_insert_values = array(
  581. 'db_id' => $form_state['values']['db_id'],
  582. 'accession' => $form_state['values']['name'],
  583. 'description' => 'cvterm reference',
  584. );
  585. $dbxref_results = tripal_core_chado_select(
  586. 'dbxref',
  587. array('dbxref_id'),
  588. $dbxref_insert_values
  589. );
  590. if (!$dbxref_results[0]->dbxref_id) {
  591. $dbxref_insert_values['version'] = '1';
  592. $dbxref_success = tripal_core_chado_insert(
  593. 'dbxref',
  594. $dbxref_insert_values
  595. );
  596. } else {
  597. $dbxref_success = true;
  598. }
  599. // Add cvterm
  600. if ($dbxref_success) {
  601. $insert_values = array(
  602. 'cv_id' => $form_state['values']['cv_id'],
  603. 'name' => $form_state['values']['name'],
  604. 'definition' => $form_state['values']['definition'],
  605. 'dbxref_id' => $dbxref_insert_values,
  606. 'is_obsolete' => (string) $form_state['values']['is_obsolete'],
  607. 'is_relationshiptype' => (string) $form_state['values']['is_relationshiptype'],
  608. );
  609. $success = tripal_core_chado_insert(
  610. 'cvterm',
  611. $insert_values
  612. );
  613. if ($success) {
  614. drupal_set_message('Successfully Added Term to Controlled Vocabulary');
  615. } else {
  616. drupal_set_message('Unable to add controlled vocabulary term', 'error');
  617. watchdog(
  618. 'tripal_cv',
  619. 'Cvterm Insert: Unable to insert cvterm where values: %values',
  620. array('%values' => print_r($insert_values,TRUE)),
  621. WATCHDOG_ERROR
  622. );
  623. }
  624. } else {
  625. drupal_set_message('Unable to add database reference for controlled vocabulary term', 'error');
  626. watchdog(
  627. 'tripal_cv',
  628. 'Cvterm Insert: Unable to insert dbxref for cvterm where values: %values',
  629. array('%values' => print_r($dbxref_insert_values,TRUE)),
  630. WATCHDOG_ERROR
  631. );
  632. }
  633. return;
  634. }
  635. /*************************************************************************
  636. * Purpose: This function gets called when the selecting of a cv from
  637. * the select list triggers it. This function simply rebuilds the form
  638. * with new information. No elements are created here
  639. */
  640. function tripal_cv_add_cvterm_callback () {
  641. // Retrieve the form from the cache
  642. $form_state = array('storage' => NULL);
  643. $form_build_id = $_POST['form_build_id'];
  644. $form = form_get_cache($form_build_id, $form_state);
  645. // Preparing to process the form
  646. $args = $form['#parameters'];
  647. $form_id = array_shift($args);
  648. $form_state['post'] = $form['#post'] = $_POST;
  649. $form['#programmed'] = $form['#redirect'] = FALSE;
  650. // Sets the form_state so that the validate and submit handlers can tell
  651. // when the form is submitted via AHAH
  652. $form_state['ahah_submission'] = TRUE;
  653. // Process the form with drupal_process_form. This function calls the submit
  654. // handlers, which put whatever was worthy of keeping into $form_state.
  655. drupal_process_form($form_id, $form, $form_state);
  656. // You call drupal_rebuild_form which destroys $_POST.
  657. // The form generator function is called and creates the form again but since
  658. // it knows to use $form_state, the form will be different.
  659. // The new form gets cached and processed again, but because $_POST is
  660. // destroyed, the submit handlers will not be called again.
  661. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  662. // This is the only piece specific to your form
  663. // Picks a piece of the form and renders it
  664. // Specifcally the add cvterm fieldset and all contained fields
  665. $add_cvterm_form = $form['add_cvterm'];
  666. unset($add_cvterm_form['#prefix'], $add_cvterm_form['#suffix']);
  667. $output = theme('status_messages') . drupal_render($add_cvterm_form);
  668. // Final rendering callback.
  669. drupal_json(array('status' => TRUE, 'data' => $output));
  670. }
  671. /*************************************************************************
  672. * @section Ontology Loader
  673. *************************************************************************/
  674. /*************************************************************************
  675. * Purpose: Provides the form to load an already existing controlled
  676. * Vocabulary into chado
  677. */
  678. function tripal_cv_obo_form(&$form_state = NULL){
  679. // get a list of db from chado for user to choose
  680. $sql = "SELECT * FROM {tripal_cv_obo} ORDER BY obo_id";
  681. $results = db_query ($sql);
  682. $obos = array();
  683. $obos[] = '';
  684. while ($obo = db_fetch_object($results)){
  685. $obos[$obo->obo_id] = "$obo->name | $obo->path";
  686. }
  687. $form['obo_existing'] = array(
  688. '#type' =>'fieldset',
  689. '#title' => t('Use a Saved Ontology OBO Reference')
  690. );
  691. $form['obo_new'] = array(
  692. '#type' =>'fieldset',
  693. '#title' => t('Use a New Ontology OBO Reference')
  694. );
  695. $form['obo_existing']['existing_instructions']= array(
  696. '#value' => t('The Ontology OBO files listed in the drop down below have been automatically added upon
  697. installation of the Tripal CV module or were added from a previous upload. Select
  698. an OBO, then click the submit button to load the vocabulary into the database. If the
  699. vocabularies already exist then the ontology will be updated.'),
  700. '#weight' => -1
  701. );
  702. $form['obo_existing']['obo_id'] = array(
  703. '#title' => t('Ontology OBO File Reference'),
  704. '#type' => 'select',
  705. '#options' => $obos,
  706. '#weight' => 0
  707. );
  708. $form['obo_new']['path_instructions']= array(
  709. '#value' => t('Provide the name and path for the OBO file. If the vocabulary OBO file
  710. is stored local to the server provide a file name. If the vocabulry is stored remotely,
  711. provide a URL. Only provide a URL or a local file, not both.'),
  712. '#weight' => 0
  713. );
  714. $form['obo_new']['obo_name']= array(
  715. '#type' => 'textfield',
  716. '#title' => t('New Vocabulary Name'),
  717. '#description' => t('Please provide a name for this vocabulary. After upload, this name will appear in the drop down
  718. list above for use again later.'),
  719. '#weight' => 1
  720. );
  721. $form['obo_new']['obo_url']= array(
  722. '#type' => 'textfield',
  723. '#title' => t('Remote URL'),
  724. '#description' => t('Please enter a URL for the online OBO file. The file will be downloaded and parsed.
  725. (e.g. http://www.obofoundry.org/ro/ro.obo'),
  726. '#default_value' => $default_desc,
  727. '#weight' => 2
  728. );
  729. $form['obo_new']['obo_file']= array(
  730. '#type' => 'textfield',
  731. '#title' => t('Local File'),
  732. '#description' => t('Please enter the full system path for an OBO definition file, or a path within the Drupal
  733. installation (e.g. /sites/default/files/xyz.obo). The path must be accessible to the
  734. server on which this Drupal instance is running.'),
  735. '#default_value' => $default_desc,
  736. '#weight' => 3
  737. );
  738. $form['submit'] = array (
  739. '#type' => 'submit',
  740. '#value' => t('Submit'),
  741. '#weight' => 5,
  742. '#executes_submit_callback' => TRUE,
  743. );
  744. $form['#redirect'] = 'admin/tripal/tripal_cv/obo';
  745. return $form;
  746. }
  747. /************************************************************************
  748. * Purpose: The submit function for the load ontology form. It registers a
  749. * tripal job to run the obo_loader.php script
  750. */
  751. function tripal_cv_obo_form_submit($form, &$form_state){
  752. global $user;
  753. $obo_id = $form_state['values']['obo_id'];
  754. $obo_name = $form_state['values']['obo_name'];
  755. $obo_url = $form_state['values']['obo_url'];
  756. $obo_file = $form_state['values']['obo_file'];
  757. $sql = "SELECT * FROM {tripal_cv_obo} WHERE obo_id = %d";
  758. $obo = db_fetch_object(db_query($sql,$obo_id));
  759. if($obo_id){
  760. $args = array($obo_id);
  761. tripal_add_job("Load OBO $obo->name",'tripal_cv',
  762. "tripal_cv_load_obo_v1_2_id",$args,$user->uid);
  763. }
  764. else {
  765. if($obo_url){
  766. $args = array($obo_name,$obo_url);
  767. tripal_add_job("Load OBO $obo_name",'tripal_cv',
  768. "tripal_cv_load_obo_v1_2_url",$args,$user->uid);
  769. }
  770. elseif($obo_file){
  771. $args = array($obo_name,$obo_file);
  772. tripal_add_job("Load OBO $obo_name",'tripal_cv',
  773. "tripal_cv_load_obo_v1_2_file",$args,$user->uid);
  774. }
  775. }
  776. return '';
  777. }
  778. /*************************************************************************
  779. * @section cvterm path management
  780. *************************************************************************/
  781. /*************************************************************************
  782. *
  783. */
  784. function tripal_cv_cvtermpath_form () {
  785. $previous_db = tripal_db_set_active('chado'); // use chado database
  786. // get a list of db from chado for user to choose
  787. $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
  788. $results = db_query ($sql);
  789. tripal_db_set_active($previous_db); // use drupal database
  790. $cvs = array();
  791. $cvs[] = '';
  792. while ($cv = db_fetch_object($results)){
  793. $cvs[$cv->cv_id] = $cv->name;
  794. }
  795. $form['cvid'] = array(
  796. '#title' => t('Controlled Vocabulary/Ontology Name'),
  797. '#type' => 'select',
  798. '#options' => $cvs,
  799. '#description' => t('Select a controlled vocabulary for which you would like to upate the cvtermpath.'),
  800. );
  801. $form['description'] = array(
  802. '#type' => 'item',
  803. '#value' => t("Submit a job to update chado cvtermpath table."),
  804. '#weight' => 1,
  805. );
  806. $form['button'] = array(
  807. '#type' => 'submit',
  808. '#value' => t('Update cvtermpath'),
  809. '#weight' => 2,
  810. );
  811. return $form;
  812. }
  813. /*************************************************************************
  814. *
  815. */
  816. function tripal_cv_cvtermpath_form_validate($form, &$form_state) {
  817. global $user;
  818. $cvid = $form_state['values']['cvid'];
  819. // first get the controlled vocabulary name:
  820. $previous_db = tripal_db_set_active('chado');
  821. $cv = db_fetch_object(db_query("SELECT * FROM {cv} WHERE cv_id = %d",$cvid));
  822. tripal_db_set_active($previous_db);
  823. // Submit a job to update cvtermpath
  824. $job_args = array($cvid);
  825. if ($form_state['values']['op'] == t('Update cvtermpath')) {
  826. tripal_add_job("Update cvtermpath: $cv->name",'tripal_cv',
  827. 'tripal_cv_update_cvtermpath',$job_args,$user->uid);
  828. }
  829. }
  830. /***********************************************************
  831. * Update the cvtermpath table
  832. */
  833. function tripal_cv_update_cvtermpath($cvid = NULL, $job_id = NULL) {
  834. // first get the controlled vocabulary name:
  835. $previous_db = tripal_db_set_active('chado');
  836. $cv = db_fetch_object(db_query("SELECT * FROM {cv} WHERE cv_id = %d",$cvid));
  837. print "\nUpdating cvtermpath for $cv->name...\n";
  838. // now fill the cvtermpath table
  839. $sql = "SELECT * FROM fill_cvtermpath('%s')";
  840. db_query($sql,$cv->name);
  841. tripal_db_set_active($previous_db);
  842. return;
  843. }
  844. /*************************************************************************
  845. * @section Miscellaneous
  846. * @todo check to see if these functions are still needed and/or if they
  847. * should be moved to the api file
  848. *************************************************************************/
  849. /*************************************************************************
  850. */
  851. function tripal_cv_get_cv_id($cv_name){
  852. $sql = "
  853. SELECT cv_id FROM {cv} WHERE name = '%s'
  854. ";
  855. $previous_db = tripal_db_set_active('chado');
  856. $cv = db_fetch_object(db_query($sql,$cv_name));
  857. tripal_db_set_active($previous_db);
  858. return $cv->cv_id;
  859. }
  860. /*************************************************************************
  861. *
  862. */
  863. function tripal_cv_cvterm_edit($cvterm_id){
  864. $sql = "
  865. SELECT CVT.name as cvtermname, CVT.definition, CV.name as cvname
  866. FROM {CVTerm} CVT
  867. INNER JOIN CV on CVT.cv_id = CV.cv_id
  868. WHERE CVT.cvterm_id = %d
  869. ";
  870. $previous_db = tripal_db_set_active('chado');
  871. $cvterm = db_fetch_object(db_query($sql,$cvterm_id));
  872. tripal_db_set_active($previous_db);
  873. return theme('tripal_cv_cvterm_edit',$cvterm);
  874. }
  875. /*************************************************************************
  876. *
  877. */
  878. function theme_tripal_cv_cvterm_edit(&$cvterm){
  879. $output = "
  880. <div id=\"cvterm\">
  881. <table>
  882. <tr><th>Term</th><td>$cvterm->cvtermname</td></tr>
  883. <tr><th>Vocabulary</th><td>$cvterm->cvname</td></tr>
  884. <tr><th>Definition</th><td>$cvterm->definition</td></tr>
  885. </table>
  886. </div>
  887. ";
  888. return $output;
  889. }