tripal_cv.cvterm_form.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. '#description' => t('The term must be unique within the database selected below.'),
  186. '#required' => TRUE,
  187. );
  188. $form['fields']['definition']= array(
  189. '#type' => 'textarea',
  190. '#title' => t('Description'),
  191. '#description' => t('Please enter a description for this term'),
  192. '#default_value' => $definition,
  193. );
  194. $form['fields']['is_relationship'] = array(
  195. '#type' => 'checkbox',
  196. '#title' => t('This term describes a relationship?'),
  197. '#default_value' => $is_relationship,
  198. );
  199. $form['fields']['is_obsolete'] = array(
  200. '#type' => 'checkbox',
  201. '#title' => t('This term is obsolete?'),
  202. '#default_value' => $is_obsolete,
  203. );
  204. $values = array();
  205. $columns = array('db_id', 'name');
  206. $options = array('order_by' => array('name' => 'ASC'));
  207. $results = chado_select_record('db', $columns, $values, $options);
  208. $dbs = array();
  209. $dbs[] = '';
  210. foreach ($results as $db) {
  211. $dbs[$db->db_id] = $db->name;
  212. }
  213. $form['fields']['db_id'] = array(
  214. '#type' => 'select',
  215. '#title' => t('Database'),
  216. '#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.'),
  217. '#options' => $dbs,
  218. '#default_value' => $db_id,
  219. '#required' => TRUE,
  220. );
  221. $form['fields']['accession']= array(
  222. '#type' => 'textfield',
  223. '#title' => t("Accession"),
  224. '#description' => t('If this term has an existing accession (unique identifier) in the database
  225. please enter that here. If the accession is numeric with a database prefix (e.g. GO:003023), please
  226. enter just the numeric value. The database prefix will be appended whenever the term is displayed.
  227. If you do not have a numeric value consider entering the term name 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']) ? trim($form_state['values']['cv_id']) : '';
  296. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  297. $definition = array_key_exists('definition', $form_state['values']) ? trim($form_state['values']['definition']) : '';
  298. $is_relationship = array_key_exists('is_relationship', $form_state['values']) ? trim($form_state['values']['is_relationship']) : '';
  299. $is_obsolete = array_key_exists('is_obsolete', $form_state['values']) ? trim($form_state['values']['is_obsolete']) : '';
  300. $cvterm_id = array_key_exists('cvterm_id', $form_state['values']) ? trim($form_state['values']['cvterm_id']) : '';
  301. $db_id = array_key_exists('db_id', $form_state['values']) ? trim($form_state['values']['db_id']) : '';
  302. $accession = array_key_exists('accession', $form_state['values']) ? trim($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. 'definition' => $definition,
  335. 'is_obsolete' => $is_obsolete,
  336. 'cv_name' => $cv->name,
  337. 'is_relationship' => $is_relationship,
  338. 'db_name' => $db_name
  339. );
  340. $cvterm = tripal_insert_cvterm($term, array('update_existing' => TRUE));
  341. if ($cvterm) {
  342. drupal_set_message('Term updated successfully.');
  343. }
  344. else {
  345. drupal_set_message('Could not add term. Check Drupal recent logs for error messages.', 'error');
  346. }
  347. }
  348. if ($op == 'Delete') {
  349. $values = array('cvterm_id' => $cvterm_id);
  350. $success = chado_delete_record('cvterm', $values);
  351. if ($success) {
  352. drupal_set_message('Term deleted successfully.');
  353. }
  354. else {
  355. drupal_set_message('Could not delete term term. Check Drupal recent logs for error messages.', 'error');
  356. }
  357. }
  358. break;
  359. }
  360. }
  361. /**
  362. * Adds new terms to an existing cv
  363. *
  364. * @ingroup tripal_cv
  365. */
  366. function tripal_cv_cvterm_add_form_submit($form, &$form_state) {
  367. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
  368. $name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
  369. $definition = array_key_exists('definition', $form_state['values']) ? $form_state['values']['definition'] : '';
  370. $is_relationship = array_key_exists('is_relationship', $form_state['values']) ? $form_state['values']['is_relationship'] : '';
  371. $is_obsolete = array_key_exists('is_obsolete', $form_state['values']) ? $form_state['values']['is_obsolete'] : '';
  372. $db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
  373. $accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
  374. // get the database
  375. $values = array('db_id' => $db_id);
  376. $results = chado_select_record('db', array('name'), $values);
  377. $db = $results[0];
  378. // get the cv
  379. $values = array('cv_id' => $cv_id);
  380. $results = chado_select_record('cv', array('name'), $values);
  381. $cv = $results[0];
  382. // now add the term
  383. $term = array(
  384. 'name' => $name,
  385. 'namespace' => $cv->name,
  386. 'id' => $accession,
  387. 'definition' => $definition,
  388. 'is_obsolete' => $is_obsolete,
  389. 'cv_name' => $cv->name,
  390. 'is_relationship' => $is_relationship,
  391. 'db_name' => $db->name
  392. );
  393. $cvterm = tripal_insert_cvterm($term, array('update_existing' => TRUE));
  394. if ($cvterm) {
  395. drupal_set_message('Term added successfully.');
  396. }
  397. else {
  398. drupal_set_message('Could not add term. Check Drupal recent logs for error messages.', 'error');
  399. }
  400. }
  401. /**
  402. * Ajax callback for the tripal_cv_form
  403. *
  404. * @ingroup tripal_cv
  405. */
  406. function tripal_cv_cvterm_edit_form_ajax($form, $form_state) {
  407. $elements = array();
  408. $elements['name'] = $form['name'];
  409. $elements['continue'] = $form['continue'];
  410. // add back in the cv-edit-div that is used for the next round of AJAX
  411. $elements['name']['#prefix'] = '<div id="cvterm-edit-div">';
  412. $elements['name']['#suffix'] = '</div">';
  413. return $elements;
  414. }