tripal_cv.module 37 KB

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