tripal_cv.cvterm_form.inc

  1. 2.x tripal_cv/includes/tripal_cv.cvterm_form.inc
  2. 3.x legacy/tripal_cv/includes/tripal_cv.cvterm_form.inc

Provides a form for creating & editing chado controlled vocabularies

File

tripal_cv/includes/tripal_cv.cvterm_form.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Provides a form for creating & editing chado controlled vocabularies
  5. */
  6. /**
  7. * Form for editing cvterms
  8. *
  9. * @ingroup tripal_cv
  10. */
  11. function tripal_cv_cvterm_edit_form($form, &$form_state) {
  12. $step = 0;
  13. if (empty($form_state['storage']['step'])) {
  14. $form_state['storage']['step'] = 0;
  15. }
  16. else {
  17. $step = $form_state['storage']['step'];
  18. }
  19. $cv_id = 0;
  20. if ($step == 1) {
  21. $cv_id = $form_state['storage']['cv_id'];
  22. $cvterm_name = $form_state['storage']['name'];
  23. $cvterm_id = $form_state['storage']['cvterm_id'];
  24. }
  25. // get the cv if form was submitted via AJAX
  26. $cvterm = '';
  27. if (array_key_exists('values', $form_state)) {
  28. $cv_id = $form_state['values']['cv_id'];
  29. if (array_key_exists('cvterm', $form_state['values'])) {
  30. $cvterm = $form_state['values']['cvterm'];
  31. }
  32. }
  33. elseif (isset($form_state['build_info']['args'][0])) {
  34. $cv_id = $form_state['build_info']['args'][0];
  35. $cvterm_id = $form_state['build_info']['args'][1];
  36. if ($form_state['build_info']['args'][1]) {
  37. $cvterm_name = chado_query('SELECT name FROM {cvterm} WHERE cvterm_id=:id', array(':id' => $cvterm_id))->fetchField();
  38. $step = 1;
  39. }
  40. }
  41. // get a list of CVs
  42. $cvs = array();
  43. $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
  44. $results = chado_query($sql);
  45. $cvs[] = 'Select a vocabulary';
  46. foreach ($results as $cv) {
  47. $cvs[$cv->cv_id] = $cv->name;
  48. }
  49. $form['cv_id'] = array(
  50. '#title' => t('Controlled Vocabulary (Ontology) Name'),
  51. '#type' => 'select',
  52. '#options' => $cvs,
  53. '#required' => TRUE,
  54. '#default_value' => $cv_id,
  55. '#ajax' => array(
  56. 'callback' => 'tripal_cv_cvterm_edit_form_ajax',
  57. 'wrapper' => 'cvterm-edit-div',
  58. 'event' => 'change',
  59. 'method' => 'replace',
  60. 'event' => 'change',
  61. ),
  62. );
  63. if ($cv_id and $step == 0) {
  64. $form['name']= array(
  65. '#type' => 'textfield',
  66. '#title' => t("Term Name"),
  67. '#default_value' => $cvterm,
  68. '#required' => TRUE,
  69. '#autocomplete_path' => "admin/tripal/tripal_cv/cvterm/auto_name/$cv_id",
  70. '#description' => t('Enter the term to edit.')
  71. );
  72. $form['continue']= array(
  73. '#type' => 'submit',
  74. '#value' => 'continue',
  75. );
  76. }
  77. elseif ($step == 1) {
  78. tripal_cv_add_cvterm_form_fields($form, $form_state, $cv_id, $cvterm_name);
  79. // when editing there are certain fields the user should not change for a term
  80. // let's mark those as disabled
  81. $form['cv_id']['#disabled'] = TRUE;
  82. $form['fields']['db_id']['#disabled'] = TRUE;
  83. $form['fields']['accession']['#disabled'] = TRUE;
  84. // add in the div for replacing the fields if needed
  85. $form['fields']['#prefix'] = '<div id="cvterm-edit-div">';
  86. $form['fields']['#suffix'] = '</div>';
  87. // add in the cvterm id
  88. $form['fields']['cvterm_id'] = array(
  89. '#type' => 'hidden',
  90. '#value' => $cvterm_id,
  91. );
  92. $form['update'] = array(
  93. '#type' => 'submit',
  94. '#value' => t('Update'),
  95. );
  96. $form['delete'] = array(
  97. '#type' => 'submit',
  98. '#value' => t('Delete'),
  99. '#attributes' => array('onclick' => 'if(!confirm("Really Delete?")){return false;}'),
  100. );
  101. }
  102. if ($step == 0) {
  103. // if we don't have a cv_id then this is the first time the form has
  104. // benn loaded and we need to create the div where ajax replacement elements get stored
  105. $form['div_replace'] = array(
  106. '#type' => 'item',
  107. '#prefix' => '<div id="cvterm-edit-div">',
  108. '#suffix' => '</div>',
  109. );
  110. }
  111. return $form;
  112. }
  113. /**
  114. * Form for adding cvterms
  115. *
  116. * @ingroup tripal_cv
  117. */
  118. function tripal_cv_cvterm_add_form($form, &$form_state) {
  119. $cv_id = 0;
  120. if (array_key_exists('values', $form_state)) {
  121. $cv_id = $form_state['values']['cv_id'];
  122. }
  123. elseif (isset($form_state['build_info']['args'][0])) {
  124. $cv_id = $form_state['build_info']['args'][0];
  125. }
  126. // get a list of CVs
  127. $cvs = array();
  128. $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
  129. $results = chado_query($sql);
  130. $cvs[] = 'Select a vocabulary';
  131. foreach ($results as $cv) {
  132. $cvs[$cv->cv_id] = $cv->name;
  133. }
  134. $form['cv_id'] = array(
  135. '#title' => t('Controlled Vocabulary (Ontology) Name'),
  136. '#type' => 'select',
  137. '#options' => $cvs,
  138. '#required' => TRUE,
  139. '#default_value' => $cv_id,
  140. );
  141. tripal_cv_add_cvterm_form_fields($form, $form_state);
  142. $form['add'] = array(
  143. '#type' => 'submit',
  144. '#value' => t('Add Term'),
  145. );
  146. return $form;
  147. }
  148. /**
  149. * Form fields in common between add/edit forms
  150. *
  151. * @ingroup tripal_cv
  152. */
  153. function tripal_cv_add_cvterm_form_fields(&$form, $form_state, $cv_id = 0, $cvterm_name = '') {
  154. $name = '';
  155. $definition = '';
  156. $is_relationship = '';
  157. $is_obsolete = '';
  158. $db_id = '';
  159. $accession = '';
  160. // get default values
  161. if ($cvterm_name) {
  162. $values = array('cv_id' => $cv_id, 'name' => $cvterm_name);
  163. $cvterm = chado_generate_var('cvterm', $values);
  164. $name = $cvterm->name;
  165. $definition = $cvterm->definition;
  166. $is_relationship = $cvterm->is_relationshiptype;
  167. $is_obsolete = $cvterm->is_obsolete;
  168. $db_id = $cvterm->dbxref_id->db_id->db_id;
  169. $accession = $cvterm->dbxref_id->accession;
  170. }
  171. // add a fieldset for the Drupal Schema API
  172. $form['fields'] = array(
  173. '#type' => 'fieldset',
  174. '#title' => 'Term Details',
  175. '#collapsible' => 0,
  176. );
  177. $form['fields']['name']= array(
  178. '#type' => 'textfield',
  179. '#title' => t("Term Name"),
  180. '#default_value' => $name,
  181. '#description' => t('The term must be unique within the database selected below.'),
  182. '#required' => TRUE,
  183. );
  184. $form['fields']['definition']= array(
  185. '#type' => 'textarea',
  186. '#title' => t('Description'),
  187. '#description' => t('Please enter a description for this term'),
  188. '#default_value' => $definition,
  189. );
  190. $form['fields']['is_relationship'] = array(
  191. '#type' => 'checkbox',
  192. '#title' => t('This term describes a relationship?'),
  193. '#default_value' => $is_relationship,
  194. );
  195. $form['fields']['is_obsolete'] = array(
  196. '#type' => 'checkbox',
  197. '#title' => t('This term is obsolete?'),
  198. '#default_value' => $is_obsolete,
  199. );
  200. $values = array();
  201. $columns = array('db_id', 'name');
  202. $options = array('order_by' => array('name' => 'ASC'));
  203. $results = chado_select_record('db', $columns, $values, $options);
  204. $dbs = array();
  205. $dbs[] = '';
  206. foreach ($results as $db) {
  207. $dbs[$db->db_id] = $db->name;
  208. }
  209. $form['fields']['db_id'] = array(
  210. '#type' => 'select',
  211. '#title' => t('Database'),
  212. '#description' => t('All terms must be assocated with a database. If there is no database for this term (e.g. it is a custom term specific to this site) then select the database \'null\' or consider creating a database specific for your site and use that anytime you would like to add terms.'),
  213. '#options' => $dbs,
  214. '#default_value' => $db_id,
  215. '#required' => TRUE,
  216. );
  217. $form['fields']['accession']= array(
  218. '#type' => 'textfield',
  219. '#title' => t("Accession"),
  220. '#description' => t('If this term has an existing accession (unique identifier) in the database
  221. please enter that here. If the accession is numeric with a database prefix (e.g. GO:003023), please
  222. enter just the numeric value. The database prefix will be appended whenever the term is displayed.
  223. If you do not have a numeric value consider entering the term name as the accession.'),
  224. '#required' => TRUE,
  225. '#default_value' => $accession,
  226. );
  227. }
  228. /**
  229. * Validate cvterm edit form
  230. *
  231. * @ingroup tripal_cv
  232. */
  233. function tripal_cv_cvterm_edit_form_validate($form, &$form_state) {
  234. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
  235. $db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
  236. $name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
  237. $cvterm_id = array_key_exists('cvterm_id', $form_state['values']) ? $form_state['values']['cvterm_id'] : '';
  238. $accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
  239. $step = $form_state['storage']['step'];
  240. // make sure the cv term name is unique for this vocabulary
  241. if ($step == 1) {
  242. $values = array('name' => $name, 'cv_id' => $cv_id);
  243. $results = chado_select_record('cvterm', array('cvterm_id'), $values);
  244. foreach ($results as $r) {
  245. if ($r->cvterm_id != $cvterm_id) {
  246. form_set_error('name', 'The term name must be unique for this vocabulary. Another term with this name already exists.');
  247. }
  248. }
  249. }
  250. }
  251. /**
  252. * Validate cv add form
  253. *
  254. * @ingroup tripal_cv
  255. */
  256. function tripal_cv_cvterm_add_form_validate($form, &$form_state) {
  257. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
  258. $db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
  259. $name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
  260. $accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
  261. $values = array('cv_id' => $cv_id);
  262. $results = chado_select_record('cv', array('name'), $values);
  263. if (!$results or count($results) == 0) {
  264. form_set_error('cv_id', 'The controlled vocabulary does not exist');
  265. }
  266. // make sure the DB exists
  267. $values = array('db_id' => $db_id);
  268. $results = chado_select_record('db', array('name'), $values);
  269. if (!$results or count($results) == 0) {
  270. form_set_error('db_id', 'The database name does not exist');
  271. }
  272. // make sure the cv term name is unique for this vocabulary
  273. $values = array('name' => $name, 'cv_id' => $cv_id);
  274. $results = chado_select_record('cvterm', array('cvterm_id'), $values);
  275. if (count($results) > 0) {
  276. form_set_error('name', 'The term name must be unique for this vocabulary. Another term with this name already exists.');
  277. }
  278. // make sure this accession is unique for the database
  279. $values = array('accession' => $accession, 'db_id' => $db_id);
  280. $results = chado_select_record('dbxref', array('dbxref_id'), $values);
  281. if (count($results) > 0 ) {
  282. form_set_error('accession', 'The accession is not uniuqe for this vocabulary\'s database.');
  283. }
  284. }
  285. /**
  286. * Edits existing controlled vocabulary terms
  287. *
  288. * @ingroup tripal_cv
  289. */
  290. function tripal_cv_cvterm_edit_form_submit($form, &$form_state) {
  291. $cv_id = array_key_exists('cv_id', $form_state['values']) ? trim($form_state['values']['cv_id']) : '';
  292. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  293. $definition = array_key_exists('definition', $form_state['values']) ? trim($form_state['values']['definition']) : '';
  294. $is_relationship = array_key_exists('is_relationship', $form_state['values']) ? trim($form_state['values']['is_relationship']) : '';
  295. $is_obsolete = array_key_exists('is_obsolete', $form_state['values']) ? trim($form_state['values']['is_obsolete']) : '';
  296. $cvterm_id = array_key_exists('cvterm_id', $form_state['values']) ? trim($form_state['values']['cvterm_id']) : '';
  297. $db_id = array_key_exists('db_id', $form_state['values']) ? trim($form_state['values']['db_id']) : '';
  298. $accession = array_key_exists('accession', $form_state['values']) ? trim($form_state['values']['accession']) : '';
  299. $op = array_key_exists('op', $form_state['values']) ? trim($form_state['values']['op']) : '';
  300. $step = $form_state['storage']['step'];
  301. switch ($step) {
  302. case 0: // a cvterm name has been selected
  303. $cv_id = array_key_exists('cv_id', $form_state['values']) ? trim($form_state['values']['cv_id']) : '';
  304. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  305. // get the original cvterm_id
  306. $values = array('name' => $name, 'cv_id' => $cv_id);
  307. $results = chado_select_record('cvterm', array('cvterm_id'), $values);
  308. $cvterm = $results[0];
  309. $form_state['storage']['cv_id'] = $cv_id;
  310. $form_state['storage']['name'] = $name;
  311. $form_state['storage']['step'] = 1;
  312. $form_state['storage']['cvterm_id'] = $cvterm->cvterm_id;
  313. $form_state['rebuild'] = TRUE;
  314. break;
  315. case 1: // update/delete button has been clicked
  316. if ($op == 'Update') {
  317. // get the cv
  318. $values = array('cv_id' => $cv_id);
  319. $results = chado_select_record('cv', array('name'), $values);
  320. $cv = $results[0];
  321. // get the db
  322. $values = array('db_id' => $db_id);
  323. $results = chado_select_record('db', array('name'), $values);
  324. $db = $results[0];
  325. // now add the term
  326. $term = array(
  327. 'name' => $name,
  328. 'namespace' => $cv->name,
  329. 'id' => $accession,
  330. 'definition' => $definition,
  331. 'is_obsolete' => $is_obsolete,
  332. 'cv_name' => $cv->name,
  333. 'is_relationship' => $is_relationship,
  334. 'db_name' => $db_name
  335. );
  336. $cvterm = tripal_insert_cvterm($term, array('update_existing' => TRUE));
  337. if ($cvterm) {
  338. drupal_set_message('Term updated successfully.');
  339. }
  340. else {
  341. drupal_set_message('Could not add term. Check Drupal recent logs for error messages.', 'error');
  342. }
  343. }
  344. if ($op == 'Delete') {
  345. $values = array('cvterm_id' => $cvterm_id);
  346. $success = chado_delete_record('cvterm', $values);
  347. if ($success) {
  348. drupal_set_message('Term deleted successfully.');
  349. }
  350. else {
  351. drupal_set_message('Could not delete term term. Check Drupal recent logs for error messages.', 'error');
  352. }
  353. }
  354. break;
  355. }
  356. }
  357. /**
  358. * Adds new terms to an existing cv
  359. *
  360. * @ingroup tripal_cv
  361. */
  362. function tripal_cv_cvterm_add_form_submit($form, &$form_state) {
  363. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
  364. $name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
  365. $definition = array_key_exists('definition', $form_state['values']) ? $form_state['values']['definition'] : '';
  366. $is_relationship = array_key_exists('is_relationship', $form_state['values']) ? $form_state['values']['is_relationship'] : '';
  367. $is_obsolete = array_key_exists('is_obsolete', $form_state['values']) ? $form_state['values']['is_obsolete'] : '';
  368. $db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
  369. $accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
  370. // get the database
  371. $values = array('db_id' => $db_id);
  372. $results = chado_select_record('db', array('name'), $values);
  373. $db = $results[0];
  374. // get the cv
  375. $values = array('cv_id' => $cv_id);
  376. $results = chado_select_record('cv', array('name'), $values);
  377. $cv = $results[0];
  378. // now add the term
  379. $term = array(
  380. 'name' => $name,
  381. 'namespace' => $cv->name,
  382. 'id' => $accession,
  383. 'definition' => $definition,
  384. 'is_obsolete' => $is_obsolete,
  385. 'cv_name' => $cv->name,
  386. 'is_relationship' => $is_relationship,
  387. 'db_name' => $db->name
  388. );
  389. $cvterm = tripal_insert_cvterm($term, array('update_existing' => TRUE));
  390. if ($cvterm) {
  391. drupal_set_message('Term added successfully.');
  392. }
  393. else {
  394. drupal_set_message('Could not add term. Check Drupal recent logs for error messages.', 'error');
  395. }
  396. }
  397. /**
  398. * Ajax callback for the tripal_cv_form
  399. *
  400. * @ingroup tripal_cv
  401. */
  402. function tripal_cv_cvterm_edit_form_ajax($form, $form_state) {
  403. $elements = array();
  404. $elements['name'] = $form['name'];
  405. $elements['continue'] = $form['continue'];
  406. // add back in the cv-edit-div that is used for the next round of AJAX
  407. $elements['name']['#prefix'] = '<div id="cvterm-edit-div">';
  408. $elements['name']['#suffix'] = '</div">';
  409. return $elements;
  410. }