tripal_cv.module 36 KB

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