tripal_cv.module 36 KB

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