cvterm_form.inc 16 KB

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