tripal_chado.cv.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. <?php
  2. /**
  3. * Provide landing page to the new admin pages
  4. *
  5. * @ingroup tripal_cv
  6. */
  7. function tripal_cv_admin_cv_listing() {
  8. $output = '';
  9. // set the breadcrumb
  10. $breadcrumb = array();
  11. $breadcrumb[] = l('Home', '<front>');
  12. $breadcrumb[] = l('Administration', 'admin');
  13. $breadcrumb[] = l('Tripal', 'admin/tripal');
  14. $breadcrumb[] = l('Chado Modules', 'admin/tripal/legacy');
  15. $breadcrumb[] = l('Vocabularies', 'admin/tripal/legacy/vocab');
  16. drupal_set_breadcrumb($breadcrumb);
  17. // Add the view
  18. $cvs_view = views_embed_view('tripal_cv_admin_cvs','default');
  19. $cvterms_view = views_embed_view('tripal_cv_admin_cvterms','default');
  20. if (isset($cvs_view) && isset($cvterms_view)) {
  21. $output .= $cvs_view;
  22. }
  23. else {
  24. $output .= '<p>The Tripal Controlled Vocabulary module uses primarily views to provide an '
  25. . 'administrative interface. Currently one or more views needed for this '
  26. . 'administrative interface are disabled. <strong>Click each of the following links to '
  27. . 'enable the pertinent views</strong>:</p>';
  28. $output .= '<ul>';
  29. if (!isset($cvs_view)) {
  30. $output .= '<li>'.l('Tripal Vocabularies', 'admin/tripal/vocab/views/cvs/enable').'</li>';
  31. }
  32. if (!isset($cvterm_view)) {
  33. $output .= '<li>'.l('Tripal Vocabulary Terms', 'admin/tripal/vocab/views/cvterms/enable').'</li>';
  34. }
  35. $output .= '</ul>';
  36. }
  37. return $output;
  38. }
  39. /**
  40. * Provides the actual "Select CV" form on the Update/Delete Controlled
  41. * Vocabulary page. This form also triggers the edit javascript
  42. * @todo Modify this form to use Drupal AJAX
  43. *
  44. * @ingroup tripal_cv
  45. */
  46. function tripal_cv_cv_edit_form($form, &$form_state) {
  47. // get the cv_d if form was submitted via AJAX
  48. $cv_id = 0;
  49. if (array_key_exists('values', $form_state)) {
  50. $cv_id = $form_state['values']['cv_id'];
  51. }
  52. elseif (isset($form_state['build_info']['args'][0])) {
  53. $cv_id = $form_state['build_info']['args'][0];
  54. }
  55. // get a list of db from chado for user to choose
  56. $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
  57. $results = chado_query($sql);
  58. $cvs = array();
  59. $cvs[] = 'Select a vocabulary';
  60. foreach ($results as $cv) {
  61. $cvs[$cv->cv_id] = $cv->name;
  62. }
  63. $form['cv_id'] = array(
  64. '#title' => t('Controlled Vocabulary Name'),
  65. '#type' => 'select',
  66. '#options' => $cvs,
  67. '#ajax' => array(
  68. 'callback' => 'tripal_cv_edit_form_ajax',
  69. 'wrapper' => 'cv-edit-div',
  70. 'effect' => 'fade',
  71. 'event' => 'change',
  72. 'method' => 'replace',
  73. ),
  74. '#default_value' => $cv_id,
  75. );
  76. // if we don't have a db_id then we can return the form, otherwise
  77. // add in the other fields
  78. if ($cv_id) {
  79. tripal_cv_add_cv_form_fields($form, $form_state, $cv_id);
  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. else {
  91. // if we don't have a dbid 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="cv-edit-div">',
  96. '#suffix' => '</div>',
  97. );
  98. }
  99. return $form;
  100. }
  101. /**
  102. * Form to add contolled vocabularies
  103. *
  104. * @ingroup tripal_cv
  105. */
  106. function tripal_cv_cv_add_form($form, &$form_state) {
  107. // add in the form fields to this form
  108. tripal_cv_add_cv_form_fields($form, $form_state);
  109. $form['add'] = array(
  110. '#type' => 'submit',
  111. '#value' => t('Add'),
  112. '#weight' => 5,
  113. );
  114. return $form;
  115. }
  116. /**
  117. * Form fields in common between the cv add & edit form.
  118. *
  119. * @ingroup tripal_cv
  120. */
  121. function tripal_cv_add_cv_form_fields(&$form, $form_state, $cv_id = NULL) {
  122. $default_name = '';
  123. $default_desc = '';
  124. if ($cv_id) {
  125. $values = array('cv_id' => $cv_id);
  126. $result = chado_select_record('cv', array('*'), $values);
  127. $cv = $result[0];
  128. $default_name = $cv->name;
  129. $default_desc = $cv->definition;
  130. }
  131. // add a fieldset for the Drupal Schema API
  132. $form = array(
  133. '#type' => 'fieldset',
  134. '#title' => 'Controlled Vocabulary Details',
  135. '#collapsible' => 0,
  136. );
  137. $form['name']= array(
  138. '#type' => 'textfield',
  139. '#title' => t("Controlled Vocabulary name"),
  140. '#description' => t('Please enter the name for this vocabulary.'),
  141. '#required' => TRUE,
  142. '#default_value' => $default_name,
  143. '#maxlength' => 255,
  144. );
  145. $form['definition']= array(
  146. '#type' => 'textarea',
  147. '#title' => t('Description'),
  148. '#description' => t('Please enter a definition for this vocabulary'),
  149. '#default_value' => $default_desc,
  150. );
  151. return $form;
  152. }
  153. /**
  154. * Validation fucntion for tripal_cv_cv_add_form
  155. *
  156. * @ingroup tripal_cv
  157. */
  158. function tripal_cv_cv_add_form_validate($form, &$form_state) {
  159. tripal_cv_form_fields_validate($form, $form_state);
  160. }
  161. /**
  162. * Validation fucntion for tripal_cv_cv_edit_form
  163. *
  164. * @ingroup tripal_cv
  165. */
  166. function tripal_cv_cv_edit_form_validate($form, &$form_state) {
  167. tripal_cv_form_fields_validate($form, $form_state);
  168. }
  169. /**
  170. * Generic validation form for shared fields of both the edit and add forms
  171. *
  172. * @ingroup tripal_cv
  173. */
  174. function tripal_cv_form_fields_validate($form, &$form_state) {
  175. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  176. $desc = array_key_exists('definition', $form_state['values']) ? trim($form_state['values']['definition']) : '';
  177. $cv_id = array_key_exists('cv_id', $form_state['values']) ? trim($form_state['values']['cv_id']) : '';
  178. // make sure the cv name is unique
  179. $values = array('name' => $name);
  180. $results = chado_select_record('cv', array('cv_id'), $values);
  181. if (count($results) > 0 and $results[0]->cv_id != $cv_id) {
  182. form_set_error('name', 'The vocabulary name must be unique');
  183. }
  184. }
  185. /**
  186. * Submit cv add form
  187. *
  188. * @ingroup tripal_cv
  189. */
  190. function tripal_cv_cv_add_form_submit($form, &$form_state) {
  191. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  192. $desc = array_key_exists('definition', $form_state['values']) ? trim($form_state['values']['definition']) : '';
  193. $values = array(
  194. 'name' => $name,
  195. 'definition' => $desc,
  196. );
  197. $success = chado_insert_record('cv', $values);
  198. if ($success) {
  199. drupal_set_message(t("Controlled vocabulary added"));
  200. }
  201. else {
  202. drupal_set_message(t("Failed to add controlled vocabulary."));
  203. }
  204. }
  205. /**
  206. * Submit cv edit form
  207. *
  208. * @ingroup tripal_cv
  209. */
  210. function tripal_cv_cv_edit_form_submit($form, &$form_state) {
  211. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  212. $desc = array_key_exists('definition', $form_state['values']) ? trim($form_state['values']['definition']) : '';
  213. $cv_id = array_key_exists('cv_id', $form_state['values']) ? trim($form_state['values']['cv_id']) : '';
  214. $op = array_key_exists('op', $form_state['values']) ? trim($form_state['values']['op']) : '';
  215. $values = array(
  216. 'name' => $name,
  217. 'definition' => $desc,
  218. );
  219. if (strcmp($op, 'Update')==0) {
  220. $match = array('cv_id' => $cv_id);
  221. $success = chado_update_record('cv', $match, $values);
  222. if ($success) {
  223. drupal_set_message(t("Controlled vocabulary updated"));
  224. }
  225. else {
  226. drupal_set_message(t("Failed to update controlled vocabulary."));
  227. }
  228. }
  229. if (strcmp($op, 'Delete')==0) {
  230. $match = array('cv_id' => $cv_id);
  231. $success = chado_delete_record('cv', $match);
  232. if ($success) {
  233. drupal_set_message(t("Controlled vocabulary deleted"));
  234. }
  235. else {
  236. drupal_set_message(t("Failed to delete controlled vocabulary."));
  237. }
  238. }
  239. }
  240. /**
  241. * Ajax callback for the tripal_cv_form
  242. *
  243. * @ingroup tripal_cv
  244. */
  245. function tripal_cv_edit_form_ajax($form, $form_state) {
  246. $elements = array();
  247. // add in the form fields and the buttons
  248. if (array_key_exists('cv_id', $form_state['values'])) {
  249. $elements['fields'] = $form;
  250. $elements['update'] = $form['update'];
  251. $elements['delete'] = $form['delete'];
  252. }
  253. // add back in the cv-edit-div that is used for the next round of AJAX
  254. $elements['fields']['#prefix'] = '<div id="cv-edit-div">';
  255. $elements['fields']['#suffix'] = '</div">';
  256. // reset the values for the fields to the defaults
  257. $elements['fields']['name']['#value'] = $elements['fields']['name']['#default_value'];
  258. $elements['fields']['definition']['#value'] = $elements['fields']['definition']['#default_value'];
  259. //drupal_set_message('<pre>' . print_r($elements, TRUE) . '</pre>', "status");
  260. return $elements;
  261. }
  262. /**
  263. * Form for editing cvterms
  264. *
  265. * @ingroup tripal_cv
  266. */
  267. function tripal_cv_cvterm_edit_form($form, &$form_state) {
  268. $step = 0;
  269. if (empty($form_state['storage']['step'])) {
  270. $form_state['storage']['step'] = 0;
  271. }
  272. else {
  273. $step = $form_state['storage']['step'];
  274. }
  275. $cv_id = 0;
  276. if ($step == 1) {
  277. $cv_id = $form_state['storage']['cv_id'];
  278. $cvterm_name = $form_state['storage']['name'];
  279. $cvterm_id = $form_state['storage']['cvterm_id'];
  280. }
  281. // get the cv if form was submitted via AJAX
  282. $cvterm = '';
  283. if (array_key_exists('values', $form_state)) {
  284. $cv_id = $form_state['values']['cv_id'];
  285. if (array_key_exists('cvterm', $form_state['values'])) {
  286. $cvterm = $form_state['values']['cvterm'];
  287. }
  288. }
  289. elseif (isset($form_state['build_info']['args'][0])) {
  290. $cv_id = $form_state['build_info']['args'][0];
  291. $cvterm_id = $form_state['build_info']['args'][1];
  292. if ($form_state['build_info']['args'][1]) {
  293. $cvterm_name = chado_query('SELECT name FROM {cvterm} WHERE cvterm_id = :id',
  294. array(':id' => $cvterm_id))->fetchField();
  295. $step = 1;
  296. }
  297. }
  298. // get a list of CVs
  299. $cvs = array();
  300. $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
  301. $results = chado_query($sql);
  302. $cvs[] = 'Select a vocabulary';
  303. foreach ($results as $cv) {
  304. $cvs[$cv->cv_id] = $cv->name;
  305. }
  306. $form['cv_id'] = array(
  307. '#title' => t('Controlled Vocabulary Name'),
  308. '#type' => 'select',
  309. '#options' => $cvs,
  310. '#required' => TRUE,
  311. '#default_value' => $cv_id,
  312. '#ajax' => array(
  313. 'callback' => 'tripal_cv_cvterm_edit_form_ajax',
  314. 'wrapper' => 'cvterm-edit-div',
  315. 'event' => 'change',
  316. 'method' => 'replace',
  317. 'event' => 'change',
  318. ),
  319. );
  320. if ($cv_id and $step == 0) {
  321. $form['name']= array(
  322. '#type' => 'textfield',
  323. '#title' => t("Term Name"),
  324. '#default_value' => $cvterm,
  325. '#required' => TRUE,
  326. '#autocomplete_path' => "admin/tripal/tripal_cv/cvterm/auto_name/$cv_id",
  327. '#description' => t('Enter the term to edit.')
  328. );
  329. $form['continue']= array(
  330. '#type' => 'submit',
  331. '#value' => 'continue',
  332. );
  333. }
  334. elseif ($step == 1) {
  335. tripal_cv_add_cvterm_form_fields($form, $form_state, $cv_id, $cvterm_name);
  336. // when editing there are certain fields the user should not change for a term
  337. // let's mark those as disabled
  338. $form['cv_id']['#disabled'] = TRUE;
  339. $form['db_id']['#disabled'] = TRUE;
  340. $form['accession']['#disabled'] = TRUE;
  341. // add in the div for replacing the fields if needed
  342. $form['#prefix'] = '<div id="cvterm-edit-div">';
  343. $form['#suffix'] = '</div>';
  344. // add in the cvterm id
  345. $form['cvterm_id'] = array(
  346. '#type' => 'hidden',
  347. '#value' => $cvterm_id,
  348. );
  349. $form['update'] = array(
  350. '#type' => 'submit',
  351. '#value' => t('Update'),
  352. );
  353. $form['delete'] = array(
  354. '#type' => 'submit',
  355. '#value' => t('Delete'),
  356. '#attributes' => array('onclick' => 'if(!confirm("Really Delete?")){return false;}'),
  357. );
  358. }
  359. if ($step == 0) {
  360. // if we don't have a cv_id then this is the first time the form has
  361. // benn loaded and we need to create the div where ajax replacement elements get stored
  362. $form['div_replace'] = array(
  363. '#type' => 'item',
  364. '#prefix' => '<div id="cvterm-edit-div">',
  365. '#suffix' => '</div>',
  366. );
  367. }
  368. return $form;
  369. }
  370. /**
  371. * Form for adding cvterms
  372. *
  373. * @ingroup tripal_cv
  374. */
  375. function tripal_cv_cvterm_add_form($form, &$form_state) {
  376. $cv_id = 0;
  377. if (array_key_exists('values', $form_state)) {
  378. $cv_id = $form_state['values']['cv_id'];
  379. }
  380. elseif (isset($form_state['build_info']['args'][0])) {
  381. $cv_id = $form_state['build_info']['args'][0];
  382. }
  383. // get a list of CVs
  384. $cvs = array();
  385. $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
  386. $results = chado_query($sql);
  387. $cvs[] = 'Select a vocabulary';
  388. foreach ($results as $cv) {
  389. $cvs[$cv->cv_id] = $cv->name;
  390. }
  391. $form['cv_id'] = array(
  392. '#title' => t('Controlled Vocabulary (Ontology) Name'),
  393. '#type' => 'select',
  394. '#options' => $cvs,
  395. '#required' => TRUE,
  396. '#default_value' => $cv_id,
  397. );
  398. tripal_cv_add_cvterm_form_fields($form, $form_state);
  399. $form['add'] = array(
  400. '#type' => 'submit',
  401. '#value' => t('Add Term'),
  402. );
  403. return $form;
  404. }
  405. /**
  406. * Form fields in common between add/edit forms
  407. *
  408. * @ingroup tripal_cv
  409. */
  410. function tripal_cv_add_cvterm_form_fields(&$form, $form_state, $cv_id = 0, $cvterm_name = '') {
  411. $name = '';
  412. $definition = '';
  413. $is_relationship = '';
  414. $is_obsolete = '';
  415. $db_id = '';
  416. $accession = '';
  417. $cvterm = NULL;
  418. // get default values
  419. if ($cvterm_name) {
  420. $values = array('cv_id' => $cv_id, 'name' => $cvterm_name);
  421. $cvterm = chado_generate_var('cvterm', $values);
  422. $name = $cvterm->name;
  423. $definition = $cvterm->definition;
  424. $is_relationship = $cvterm->is_relationshiptype;
  425. $is_obsolete = $cvterm->is_obsolete;
  426. $db_id = $cvterm->dbxref_id->db_id->db_id;
  427. $accession = $cvterm->dbxref_id->accession;
  428. }
  429. $form['name']= array(
  430. '#type' => 'textfield',
  431. '#title' => t("Term Name"),
  432. '#default_value' => $name,
  433. '#description' => t('The term must be unique within the database selected below.'),
  434. '#required' => TRUE,
  435. );
  436. $form['internal_id']= array(
  437. '#type' => 'item',
  438. '#title' => t("Internal ID"),
  439. '#markup' => $cvterm ? $cvterm->cvterm_id : '',
  440. );
  441. $form['definition']= array(
  442. '#type' => 'textarea',
  443. '#title' => t('Description'),
  444. '#description' => t('Please enter a description for this term'),
  445. '#default_value' => $definition,
  446. );
  447. $form['is_relationship'] = array(
  448. '#type' => 'checkbox',
  449. '#title' => t('This term describes a relationship?'),
  450. '#default_value' => $is_relationship,
  451. );
  452. $form['is_obsolete'] = array(
  453. '#type' => 'checkbox',
  454. '#title' => t('This term is obsolete?'),
  455. '#default_value' => $is_obsolete,
  456. );
  457. $values = array();
  458. $columns = array('db_id', 'name');
  459. $options = array('order_by' => array('name' => 'ASC'));
  460. $results = chado_select_record('db', $columns, $values, $options);
  461. $dbs = array();
  462. $dbs[] = '';
  463. foreach ($results as $db) {
  464. $dbs[$db->db_id] = $db->name;
  465. }
  466. $form['db_id'] = array(
  467. '#type' => 'select',
  468. '#title' => t('Database'),
  469. '#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.'),
  470. '#options' => $dbs,
  471. '#default_value' => $db_id,
  472. '#required' => TRUE,
  473. );
  474. $form['accession']= array(
  475. '#type' => 'textfield',
  476. '#title' => t("Accession"),
  477. '#description' => t('If this term has an existing accession (unique identifier) in the database
  478. please enter that here. If the accession is numeric with a database prefix (e.g. GO:003023), please
  479. enter just the numeric value. The database prefix will be appended whenever the term is displayed.
  480. If you do not have a numeric value consider entering the term name as the accession.'),
  481. '#required' => TRUE,
  482. '#default_value' => $accession,
  483. );
  484. }
  485. /**
  486. * Validate cvterm edit form
  487. *
  488. * @ingroup tripal_cv
  489. */
  490. function tripal_cv_cvterm_edit_form_validate($form, &$form_state) {
  491. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
  492. $db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
  493. $name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
  494. $cvterm_id = array_key_exists('cvterm_id', $form_state['values']) ? $form_state['values']['cvterm_id'] : '';
  495. $accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
  496. $step = $form_state['storage']['step'];
  497. // make sure the cv term name is unique for this vocabulary
  498. if ($step == 1) {
  499. $values = array('name' => $name, 'cv_id' => $cv_id);
  500. $results = chado_select_record('cvterm', array('cvterm_id'), $values);
  501. foreach ($results as $r) {
  502. if ($r->cvterm_id != $cvterm_id) {
  503. form_set_error('name', 'The term name must be unique for this vocabulary. Another term with this name already exists.');
  504. }
  505. }
  506. }
  507. }
  508. /**
  509. * Validate cv add form
  510. *
  511. * @ingroup tripal_cv
  512. */
  513. function tripal_cv_cvterm_add_form_validate($form, &$form_state) {
  514. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
  515. $db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
  516. $name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
  517. $accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
  518. $values = array('cv_id' => $cv_id);
  519. $results = chado_select_record('cv', array('name'), $values);
  520. if (!$results or count($results) == 0) {
  521. form_set_error('cv_id', 'The controlled vocabulary does not exist');
  522. }
  523. // make sure the DB exists
  524. $values = array('db_id' => $db_id);
  525. $results = chado_select_record('db', array('name'), $values);
  526. if (!$results or count($results) == 0) {
  527. form_set_error('db_id', 'The database name does not exist');
  528. }
  529. // make sure the cv term name is unique for this vocabulary
  530. $values = array('name' => $name, 'cv_id' => $cv_id);
  531. $results = chado_select_record('cvterm', array('cvterm_id'), $values);
  532. if (count($results) > 0) {
  533. form_set_error('name', 'The term name must be unique for this vocabulary. Another term with this name already exists.');
  534. }
  535. // make sure this accession is unique for the database
  536. $values = array('accession' => $accession, 'db_id' => $db_id);
  537. $results = chado_select_record('dbxref', array('dbxref_id'), $values);
  538. if (count($results) > 0 ) {
  539. form_set_error('accession', 'The accession is not uniuqe for this vocabulary\'s database.');
  540. }
  541. }
  542. /**
  543. * Edits existing controlled vocabulary terms
  544. *
  545. * @ingroup tripal_cv
  546. */
  547. function tripal_cv_cvterm_edit_form_submit($form, &$form_state) {
  548. $cv_id = array_key_exists('cv_id', $form_state['values']) ? trim($form_state['values']['cv_id']) : '';
  549. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  550. $definition = array_key_exists('definition', $form_state['values']) ? trim($form_state['values']['definition']) : '';
  551. $is_relationship = array_key_exists('is_relationship', $form_state['values']) ? trim($form_state['values']['is_relationship']) : '';
  552. $is_obsolete = array_key_exists('is_obsolete', $form_state['values']) ? trim($form_state['values']['is_obsolete']) : '';
  553. $cvterm_id = array_key_exists('cvterm_id', $form_state['values']) ? trim($form_state['values']['cvterm_id']) : '';
  554. $db_id = array_key_exists('db_id', $form_state['values']) ? trim($form_state['values']['db_id']) : '';
  555. $accession = array_key_exists('accession', $form_state['values']) ? trim($form_state['values']['accession']) : '';
  556. $op = array_key_exists('op', $form_state['values']) ? trim($form_state['values']['op']) : '';
  557. $step = $form_state['storage']['step'];
  558. switch ($step) {
  559. case 0: // a cvterm name has been selected
  560. $cv_id = array_key_exists('cv_id', $form_state['values']) ? trim($form_state['values']['cv_id']) : '';
  561. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  562. // get the original cvterm_id
  563. $values = array('name' => $name, 'cv_id' => $cv_id);
  564. $results = chado_select_record('cvterm', array('cvterm_id'), $values);
  565. $cvterm = $results[0];
  566. $form_state['storage']['cv_id'] = $cv_id;
  567. $form_state['storage']['name'] = $name;
  568. $form_state['storage']['step'] = 1;
  569. $form_state['storage']['cvterm_id'] = $cvterm->cvterm_id;
  570. $form_state['rebuild'] = TRUE;
  571. break;
  572. case 1: // update/delete button has been clicked
  573. if ($op == 'Update') {
  574. // get the cv
  575. $values = array('cv_id' => $cv_id);
  576. $results = chado_select_record('cv', array('name'), $values);
  577. $cv = $results[0];
  578. // get the db
  579. $values = array('db_id' => $db_id);
  580. $results = chado_select_record('db', array('name'), $values);
  581. $db = $results[0];
  582. // now add the term
  583. $term = array(
  584. 'name' => $name,
  585. 'namespace' => $cv->name,
  586. 'id' => $accession,
  587. 'definition' => $definition,
  588. 'is_obsolete' => $is_obsolete,
  589. 'cv_name' => $cv->name,
  590. 'is_relationship' => $is_relationship,
  591. 'db_name' => $db_name
  592. );
  593. $cvterm = tripal_insert_cvterm($term, array('update_existing' => TRUE));
  594. if ($cvterm) {
  595. drupal_set_message('Term updated successfully.');
  596. }
  597. else {
  598. drupal_set_message('Could not add term. Check Drupal recent logs for error messages.', 'error');
  599. }
  600. }
  601. if ($op == 'Delete') {
  602. $values = array('cvterm_id' => $cvterm_id);
  603. $success = chado_delete_record('cvterm', $values);
  604. if ($success) {
  605. drupal_set_message('Term deleted successfully.');
  606. }
  607. else {
  608. drupal_set_message('Could not delete term term. Check Drupal recent logs for error messages.', 'error');
  609. }
  610. }
  611. break;
  612. }
  613. }
  614. /**
  615. * Adds new terms to an existing cv
  616. *
  617. * @ingroup tripal_cv
  618. */
  619. function tripal_cv_cvterm_add_form_submit($form, &$form_state) {
  620. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
  621. $name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
  622. $definition = array_key_exists('definition', $form_state['values']) ? $form_state['values']['definition'] : '';
  623. $is_relationship = array_key_exists('is_relationship', $form_state['values']) ? $form_state['values']['is_relationship'] : '';
  624. $is_obsolete = array_key_exists('is_obsolete', $form_state['values']) ? $form_state['values']['is_obsolete'] : '';
  625. $db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
  626. $accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
  627. // get the database
  628. $values = array('db_id' => $db_id);
  629. $results = chado_select_record('db', array('name'), $values);
  630. $db = $results[0];
  631. // get the cv
  632. $values = array('cv_id' => $cv_id);
  633. $results = chado_select_record('cv', array('name'), $values);
  634. $cv = $results[0];
  635. // now add the term
  636. $term = array(
  637. 'name' => $name,
  638. 'namespace' => $cv->name,
  639. 'id' => $accession,
  640. 'definition' => $definition,
  641. 'is_obsolete' => $is_obsolete,
  642. 'cv_name' => $cv->name,
  643. 'is_relationship' => $is_relationship,
  644. 'db_name' => $db->name
  645. );
  646. $cvterm = tripal_insert_cvterm($term, array('update_existing' => TRUE));
  647. if ($cvterm) {
  648. drupal_set_message('Term added successfully.');
  649. }
  650. else {
  651. drupal_set_message('Could not add term. Check Drupal recent logs for error messages.', 'error');
  652. }
  653. }
  654. /**
  655. * Ajax callback for the tripal_cv_form
  656. *
  657. * @ingroup tripal_cv
  658. */
  659. function tripal_cv_cvterm_edit_form_ajax($form, $form_state) {
  660. $elements = array();
  661. $elements['name'] = $form['name'];
  662. $elements['continue'] = $form['continue'];
  663. // add back in the cv-edit-div that is used for the next round of AJAX
  664. $elements['name']['#prefix'] = '<div id="cvterm-edit-div">';
  665. $elements['name']['#suffix'] = '</div">';
  666. return $elements;
  667. }