tripal_cv.cvterm_form.inc 16 KB

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