cvterm_form.inc 15 KB

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