tripal_cv.module 37 KB

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