tripal_cv.module 37 KB

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