tripal_cv.cvterm_form.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 a database. If the term you are adding is custom for this database, consider creating a database specific for your site and use that anytime you would like to add terms.'),
  216. '#options' => $dbs,
  217. '#default_value' => $db_id,
  218. '#required' => TRUE,
  219. );
  220. $form['fields']['accession']= array(
  221. '#type' => 'textfield',
  222. '#title' => t("Accession"),
  223. '#description' => t('If this term has an existing accession (unique identifier) in the database
  224. please enter that here. If the accession is numeric with a database prefix (e.g. GO:003023), please
  225. enter just the numeric value. The database prefix will be appended whenever the term is displayed.
  226. If the accession is not numeric then enter it as is. If no value is provied, the term name
  227. provided above will be used as the accession.'),
  228. '#required' => TRUE,
  229. '#default_value' => $accession,
  230. );
  231. }
  232. /**
  233. * Validate cvterm edit form
  234. *
  235. * @ingroup tripal_cv
  236. */
  237. function tripal_cv_cvterm_edit_form_validate($form, &$form_state) {
  238. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
  239. $db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
  240. $name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
  241. $cvterm_id = array_key_exists('cvterm_id', $form_state['values']) ? $form_state['values']['cvterm_id'] : '';
  242. $accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
  243. $step = $form_state['storage']['step'];
  244. // make sure the cv term name is unique for this vocabulary
  245. if ($step == 1) {
  246. $values = array('name' => $name, 'cv_id' => $cv_id);
  247. $results = chado_select_record('cvterm', array('cvterm_id'), $values);
  248. foreach ($results as $r) {
  249. if ($r->cvterm_id != $cvterm_id) {
  250. form_set_error('name', 'The term name must be unique for this vocabulary. Another term with this name already exists.');
  251. }
  252. }
  253. }
  254. }
  255. /**
  256. * Validate cv add form
  257. *
  258. * @ingroup tripal_cv
  259. */
  260. function tripal_cv_cvterm_add_form_validate($form, &$form_state) {
  261. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
  262. $db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
  263. $name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
  264. $accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
  265. $values = array('cv_id' => $cv_id);
  266. $results = chado_select_record('cv', array('name'), $values);
  267. if (!$results or count($results) == 0) {
  268. form_set_error('cv_id', 'The controlled vocabulary does not exist');
  269. }
  270. // make sure the DB exists
  271. $values = array('db_id' => $db_id);
  272. $results = chado_select_record('db', array('name'), $values);
  273. if (!$results or count($results) == 0) {
  274. form_set_error('db_id', 'The database name does not exist');
  275. }
  276. // make sure the cv term name is unique for this vocabulary
  277. $values = array('name' => $name, 'cv_id' => $cv_id);
  278. $results = chado_select_record('cvterm', array('cvterm_id'), $values);
  279. if (count($results) > 0) {
  280. form_set_error('name', 'The term name must be unique for this vocabulary. Another term with this name already exists.');
  281. }
  282. // make sure this accession is unique for the database
  283. $values = array('accession' => $accession, 'db_id' => $db_id);
  284. $results = chado_select_record('dbxref', array('dbxref_id'), $values);
  285. if (count($results) > 0 ) {
  286. form_set_error('accession', 'The accession is not uniuqe for this vocabulary\'s database.');
  287. }
  288. }
  289. /**
  290. * Edits existing controlled vocabulary terms
  291. *
  292. * @ingroup tripal_cv
  293. */
  294. function tripal_cv_cvterm_edit_form_submit($form, &$form_state) {
  295. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
  296. $name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
  297. $definition = array_key_exists('definition', $form_state['values']) ? $form_state['values']['definition'] : '';
  298. $is_relationship = array_key_exists('is_relationship', $form_state['values']) ? $form_state['values']['is_relationship'] : '';
  299. $is_obsolete = array_key_exists('is_obsolete', $form_state['values']) ? $form_state['values']['is_obsolete'] : '';
  300. $cvterm_id = array_key_exists('cvterm_id', $form_state['values']) ? $form_state['values']['cvterm_id'] : '';
  301. $db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
  302. $accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
  303. $op = array_key_exists('op', $form_state['values']) ? trim($form_state['values']['op']) : '';
  304. $step = $form_state['storage']['step'];
  305. switch ($step) {
  306. case 0: // a cvterm name has been selected
  307. $cv_id = array_key_exists('cv_id', $form_state['values']) ? trim($form_state['values']['cv_id']) : '';
  308. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  309. // get the original cvterm_id
  310. $values = array('name' => $name, 'cv_id' => $cv_id);
  311. $results = chado_select_record('cvterm', array('cvterm_id'), $values);
  312. $cvterm = $results[0];
  313. $form_state['storage']['cv_id'] = $cv_id;
  314. $form_state['storage']['name'] = $name;
  315. $form_state['storage']['step'] = 1;
  316. $form_state['storage']['cvterm_id'] = $cvterm->cvterm_id;
  317. $form_state['rebuild'] = TRUE;
  318. break;
  319. case 1: // update/delete button has been clicked
  320. if ($op == 'Update') {
  321. // get the cv
  322. $values = array('cv_id' => $cv_id);
  323. $results = chado_select_record('cv', array('name'), $values);
  324. $cv = $results[0];
  325. // get the db
  326. $values = array('db_id' => $db_id);
  327. $results = chado_select_record('db', array('name'), $values);
  328. $db = $results[0];
  329. // now add the term
  330. $term = array(
  331. 'name' => $name,
  332. 'namespace' => $cv->name,
  333. 'id' => $accession,
  334. 'def' => $definition,
  335. 'is_obsolete' => $is_obsolete,
  336. );
  337. $cvterm = tripal_cv_add_cvterm($term, $cv->name, $is_relationship, TRUE, $db->name);
  338. if ($cvterm) {
  339. drupal_set_message('Term updated successfully.');
  340. }
  341. else {
  342. drupal_set_message('Could not add term. Check Drupal recent logs for error messages.', 'error');
  343. }
  344. }
  345. if ($op == 'Delete') {
  346. $values = array('cvterm_id' => $cvterm_id);
  347. $success = chado_delete_record('cvterm', $values);
  348. if ($success) {
  349. drupal_set_message('Term deleted successfully.');
  350. }
  351. else {
  352. drupal_set_message('Could not delete term term. Check Drupal recent logs for error messages.', 'error');
  353. }
  354. }
  355. break;
  356. }
  357. }
  358. /**
  359. * Adds new terms to an existing cv
  360. *
  361. * @ingroup tripal_cv
  362. */
  363. function tripal_cv_cvterm_add_form_submit($form, &$form_state) {
  364. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
  365. $name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
  366. $definition = array_key_exists('definition', $form_state['values']) ? $form_state['values']['definition'] : '';
  367. $is_relationship = array_key_exists('is_relationship', $form_state['values']) ? $form_state['values']['is_relationship'] : '';
  368. $is_obsolete = array_key_exists('is_obsolete', $form_state['values']) ? $form_state['values']['is_obsolete'] : '';
  369. $db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
  370. $accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
  371. // get the database
  372. $values = array('db_id' => $db_id);
  373. $results = chado_select_record('db', array('name'), $values);
  374. $db = $results[0];
  375. // get the cv
  376. $values = array('cv_id' => $cv_id);
  377. $results = chado_select_record('cv', array('name'), $values);
  378. $cv = $results[0];
  379. // now add the term
  380. $term = array(
  381. 'name' => $name,
  382. 'namespace' => $cv->name,
  383. 'id' => $accession,
  384. 'def' => $definition,
  385. 'is_obsolete' => $is_obsolete,
  386. );
  387. $cvterm = tripal_cv_add_cvterm($term, $cv->name, $is_relationship, TRUE, $db->name);
  388. if ($cvterm) {
  389. drupal_set_message('Term added successfully.');
  390. }
  391. else {
  392. drupal_set_message('Could not add term. Check Drupal recent logs for error messages.', 'error');
  393. }
  394. }
  395. /**
  396. * Ajax callback for the tripal_cv_form
  397. *
  398. * @ingroup tripal_cv
  399. */
  400. function tripal_cv_cvterm_edit_form_ajax($form, $form_state) {
  401. $elements = array();
  402. $elements['name'] = $form['name'];
  403. $elements['continue'] = $form['continue'];
  404. // add back in the cv-edit-div that is used for the next round of AJAX
  405. $elements['name']['#prefix'] = '<div id="cvterm-edit-div">';
  406. $elements['name']['#suffix'] = '</div">';
  407. return $elements;
  408. }