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