tripal_cv.module 37 KB

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