cvterm_form.inc 15 KB

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