tripal_chado.cv.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. $result = db_select('chado.cvterm','c')
  294. ->fields('c', array('name'))
  295. ->condition('c.cvterm_id',$cvterm_id)
  296. ->execute();
  297. $cvterm_name = $result->fetchObject()->name;
  298. $step = 1;
  299. }
  300. }
  301. // get a list of CVs
  302. $cvs = array();
  303. $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
  304. $results = chado_query($sql);
  305. $cvs[] = 'Select a vocabulary';
  306. foreach ($results as $cv) {
  307. $cvs[$cv->cv_id] = $cv->name;
  308. }
  309. $form['cv_id'] = array(
  310. '#title' => t('Controlled Vocabulary Name'),
  311. '#type' => 'select',
  312. '#options' => $cvs,
  313. '#required' => TRUE,
  314. '#default_value' => $cv_id,
  315. '#ajax' => array(
  316. 'callback' => 'tripal_cv_cvterm_edit_form_ajax',
  317. 'wrapper' => 'cvterm-edit-div',
  318. 'event' => 'change',
  319. 'method' => 'replace',
  320. 'event' => 'change',
  321. ),
  322. );
  323. if ($cv_id and $step == 0) {
  324. $form['name']= array(
  325. '#type' => 'textfield',
  326. '#title' => t("Term Name"),
  327. '#default_value' => $cvterm,
  328. '#required' => TRUE,
  329. '#autocomplete_path' => "admin/tripal/tripal_cv/cvterm/auto_name/$cv_id",
  330. '#description' => t('Enter the term to edit.')
  331. );
  332. $form['continue']= array(
  333. '#type' => 'submit',
  334. '#value' => 'continue',
  335. );
  336. }
  337. elseif ($step == 1) {
  338. tripal_cv_add_cvterm_form_fields($form, $form_state, $cv_id, $cvterm_name);
  339. // when editing there are certain fields the user should not change for a term
  340. // let's mark those as disabled
  341. $form['cv_id']['#disabled'] = TRUE;
  342. $form['db_id']['#disabled'] = TRUE;
  343. $form['accession']['#disabled'] = TRUE;
  344. // add in the div for replacing the fields if needed
  345. $form['#prefix'] = '<div id="cvterm-edit-div">';
  346. $form['#suffix'] = '</div>';
  347. // add in the cvterm id
  348. $form['cvterm_id'] = array(
  349. '#type' => 'hidden',
  350. '#value' => $cvterm_id,
  351. );
  352. $form['update'] = array(
  353. '#type' => 'submit',
  354. '#value' => t('Update'),
  355. );
  356. $form['delete'] = array(
  357. '#type' => 'submit',
  358. '#value' => t('Delete'),
  359. '#attributes' => array('onclick' => 'if(!confirm("Really Delete?")){return false;}'),
  360. );
  361. }
  362. if ($step == 0) {
  363. // if we don't have a cv_id then this is the first time the form has
  364. // benn loaded and we need to create the div where ajax replacement elements get stored
  365. $form['div_replace'] = array(
  366. '#type' => 'item',
  367. '#prefix' => '<div id="cvterm-edit-div">',
  368. '#suffix' => '</div>',
  369. );
  370. }
  371. return $form;
  372. }
  373. /**
  374. * Form for adding cvterms
  375. *
  376. * @ingroup tripal_cv
  377. */
  378. function tripal_cv_cvterm_add_form($form, &$form_state) {
  379. $cv_id = 0;
  380. if (array_key_exists('values', $form_state)) {
  381. $cv_id = $form_state['values']['cv_id'];
  382. }
  383. elseif (isset($form_state['build_info']['args'][0])) {
  384. $cv_id = $form_state['build_info']['args'][0];
  385. }
  386. // get a list of CVs
  387. $cvs = array();
  388. $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
  389. $results = chado_query($sql);
  390. $cvs[] = 'Select a vocabulary';
  391. foreach ($results as $cv) {
  392. $cvs[$cv->cv_id] = $cv->name;
  393. }
  394. $form['cv_id'] = array(
  395. '#title' => t('Controlled Vocabulary (Ontology) Name'),
  396. '#type' => 'select',
  397. '#options' => $cvs,
  398. '#required' => TRUE,
  399. '#default_value' => $cv_id,
  400. );
  401. tripal_cv_add_cvterm_form_fields($form, $form_state);
  402. $form['add'] = array(
  403. '#type' => 'submit',
  404. '#value' => t('Add Term'),
  405. );
  406. return $form;
  407. }
  408. /**
  409. * Form fields in common between add/edit forms
  410. *
  411. * @ingroup tripal_cv
  412. */
  413. function tripal_cv_add_cvterm_form_fields(&$form, $form_state, $cv_id = 0, $cvterm_name = '') {
  414. $name = '';
  415. $definition = '';
  416. $is_relationship = '';
  417. $is_obsolete = '';
  418. $db_id = '';
  419. $accession = '';
  420. $cvterm = NULL;
  421. // get default values
  422. if ($cvterm_name) {
  423. $values = array('cv_id' => $cv_id, 'name' => $cvterm_name);
  424. $cvterm = chado_generate_var('cvterm', $values);
  425. $name = $cvterm->name;
  426. $definition = $cvterm->definition;
  427. $is_relationship = $cvterm->is_relationshiptype;
  428. $is_obsolete = $cvterm->is_obsolete;
  429. $db_id = $cvterm->dbxref_id->db_id->db_id;
  430. $accession = $cvterm->dbxref_id->accession;
  431. }
  432. $form['name']= array(
  433. '#type' => 'textfield',
  434. '#title' => t("Term Name"),
  435. '#default_value' => $name,
  436. '#description' => t('The term must be unique within the database selected below.'),
  437. '#required' => TRUE,
  438. );
  439. $form['internal_id']= array(
  440. '#type' => 'item',
  441. '#title' => t("Internal ID"),
  442. '#markup' => $cvterm ? $cvterm->cvterm_id : '',
  443. );
  444. $form['definition']= array(
  445. '#type' => 'textarea',
  446. '#title' => t('Description'),
  447. '#description' => t('Please enter a description for this term'),
  448. '#default_value' => $definition,
  449. );
  450. $form['is_relationship'] = array(
  451. '#type' => 'checkbox',
  452. '#title' => t('This term describes a relationship?'),
  453. '#default_value' => $is_relationship,
  454. );
  455. $form['is_obsolete'] = array(
  456. '#type' => 'checkbox',
  457. '#title' => t('This term is obsolete?'),
  458. '#default_value' => $is_obsolete,
  459. );
  460. $values = array();
  461. $columns = array('db_id', 'name');
  462. $options = array('order_by' => array('name' => 'ASC'));
  463. $results = chado_select_record('db', $columns, $values, $options);
  464. $dbs = array();
  465. $dbs[] = '';
  466. foreach ($results as $db) {
  467. $dbs[$db->db_id] = $db->name;
  468. }
  469. $form['db_id'] = array(
  470. '#type' => 'select',
  471. '#title' => t('Database'),
  472. '#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.'),
  473. '#options' => $dbs,
  474. '#default_value' => $db_id,
  475. '#required' => TRUE,
  476. );
  477. $form['accession']= array(
  478. '#type' => 'textfield',
  479. '#title' => t("Accession"),
  480. '#description' => t('If this term has an existing accession (unique identifier) in the database
  481. please enter that here. If the accession is numeric with a database prefix (e.g. GO:003023), please
  482. enter just the numeric value. The database prefix will be appended whenever the term is displayed.
  483. If you do not have a numeric value consider entering the term name as the accession.'),
  484. '#required' => TRUE,
  485. '#default_value' => $accession,
  486. );
  487. }
  488. /**
  489. * Validate cvterm edit form
  490. *
  491. * @ingroup tripal_cv
  492. */
  493. function tripal_cv_cvterm_edit_form_validate($form, &$form_state) {
  494. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
  495. $db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
  496. $name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
  497. $cvterm_id = array_key_exists('cvterm_id', $form_state['values']) ? $form_state['values']['cvterm_id'] : '';
  498. $accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
  499. $step = $form_state['storage']['step'];
  500. // make sure the cv term name is unique for this vocabulary
  501. if ($step == 1) {
  502. $values = array('name' => $name, 'cv_id' => $cv_id);
  503. $results = chado_select_record('cvterm', array('cvterm_id'), $values);
  504. foreach ($results as $r) {
  505. if ($r->cvterm_id != $cvterm_id) {
  506. form_set_error('name', 'The term name must be unique for this vocabulary. Another term with this name already exists.');
  507. }
  508. }
  509. }
  510. }
  511. /**
  512. * Validate cv add form
  513. *
  514. * @ingroup tripal_cv
  515. */
  516. function tripal_cv_cvterm_add_form_validate($form, &$form_state) {
  517. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
  518. $db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
  519. $name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
  520. $accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
  521. $values = array('cv_id' => $cv_id);
  522. $results = chado_select_record('cv', array('name'), $values);
  523. if (!$results or count($results) == 0) {
  524. form_set_error('cv_id', 'The controlled vocabulary does not exist');
  525. }
  526. // make sure the DB exists
  527. $values = array('db_id' => $db_id);
  528. $results = chado_select_record('db', array('name'), $values);
  529. if (!$results or count($results) == 0) {
  530. form_set_error('db_id', 'The database name does not exist');
  531. }
  532. // make sure the cv term name is unique for this vocabulary
  533. $values = array('name' => $name, 'cv_id' => $cv_id);
  534. $results = chado_select_record('cvterm', array('cvterm_id'), $values);
  535. if (count($results) > 0) {
  536. form_set_error('name', 'The term name must be unique for this vocabulary. Another term with this name already exists.');
  537. }
  538. // make sure this accession is unique for the database
  539. $values = array('accession' => $accession, 'db_id' => $db_id);
  540. $results = chado_select_record('dbxref', array('dbxref_id'), $values);
  541. if (count($results) > 0 ) {
  542. form_set_error('accession', 'The accession is not uniuqe for this vocabulary\'s database.');
  543. }
  544. }
  545. /**
  546. * Edits existing controlled vocabulary terms
  547. *
  548. * @ingroup tripal_cv
  549. */
  550. function tripal_cv_cvterm_edit_form_submit($form, &$form_state) {
  551. $cv_id = array_key_exists('cv_id', $form_state['values']) ? trim($form_state['values']['cv_id']) : '';
  552. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  553. $definition = array_key_exists('definition', $form_state['values']) ? trim($form_state['values']['definition']) : '';
  554. $is_relationship = array_key_exists('is_relationship', $form_state['values']) ? trim($form_state['values']['is_relationship']) : '';
  555. $is_obsolete = array_key_exists('is_obsolete', $form_state['values']) ? trim($form_state['values']['is_obsolete']) : '';
  556. $cvterm_id = array_key_exists('cvterm_id', $form_state['values']) ? trim($form_state['values']['cvterm_id']) : '';
  557. $db_id = array_key_exists('db_id', $form_state['values']) ? trim($form_state['values']['db_id']) : '';
  558. $accession = array_key_exists('accession', $form_state['values']) ? trim($form_state['values']['accession']) : '';
  559. $op = array_key_exists('op', $form_state['values']) ? trim($form_state['values']['op']) : '';
  560. $step = $form_state['storage']['step'];
  561. switch ($step) {
  562. case 0: // a cvterm name has been selected
  563. $cv_id = array_key_exists('cv_id', $form_state['values']) ? trim($form_state['values']['cv_id']) : '';
  564. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  565. // get the original cvterm_id
  566. $values = array('name' => $name, 'cv_id' => $cv_id);
  567. $results = chado_select_record('cvterm', array('cvterm_id'), $values);
  568. $cvterm = $results[0];
  569. $form_state['storage']['cv_id'] = $cv_id;
  570. $form_state['storage']['name'] = $name;
  571. $form_state['storage']['step'] = 1;
  572. $form_state['storage']['cvterm_id'] = $cvterm->cvterm_id;
  573. $form_state['rebuild'] = TRUE;
  574. break;
  575. case 1: // update/delete button has been clicked
  576. if ($op == 'Update') {
  577. // get the cv
  578. $values = array('cv_id' => $cv_id);
  579. $results = chado_select_record('cv', array('name'), $values);
  580. $cv = $results[0];
  581. // get the db
  582. $values = array('db_id' => $db_id);
  583. $results = chado_select_record('db', array('name'), $values);
  584. $db = $results[0];
  585. // now add the term
  586. $term = array(
  587. 'name' => $name,
  588. 'namespace' => $cv->name,
  589. 'id' => $accession,
  590. 'definition' => $definition,
  591. 'is_obsolete' => $is_obsolete,
  592. 'cv_name' => $cv->name,
  593. 'is_relationship' => $is_relationship,
  594. 'db_name' => $db_name
  595. );
  596. $cvterm = tripal_insert_cvterm($term, array('update_existing' => TRUE));
  597. if ($cvterm) {
  598. drupal_set_message('Term updated successfully.');
  599. }
  600. else {
  601. drupal_set_message('Could not add term. Check Drupal recent logs for error messages.', 'error');
  602. }
  603. }
  604. if ($op == 'Delete') {
  605. $values = array('cvterm_id' => $cvterm_id);
  606. $success = chado_delete_record('cvterm', $values);
  607. if ($success) {
  608. drupal_set_message('Term deleted successfully.');
  609. }
  610. else {
  611. drupal_set_message('Could not delete term term. Check Drupal recent logs for error messages.', 'error');
  612. }
  613. }
  614. break;
  615. }
  616. }
  617. /**
  618. * Adds new terms to an existing cv
  619. *
  620. * @ingroup tripal_cv
  621. */
  622. function tripal_cv_cvterm_add_form_submit($form, &$form_state) {
  623. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
  624. $name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
  625. $definition = array_key_exists('definition', $form_state['values']) ? $form_state['values']['definition'] : '';
  626. $is_relationship = array_key_exists('is_relationship', $form_state['values']) ? $form_state['values']['is_relationship'] : '';
  627. $is_obsolete = array_key_exists('is_obsolete', $form_state['values']) ? $form_state['values']['is_obsolete'] : '';
  628. $db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
  629. $accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
  630. // get the database
  631. $values = array('db_id' => $db_id);
  632. $results = chado_select_record('db', array('name'), $values);
  633. $db = $results[0];
  634. // get the cv
  635. $values = array('cv_id' => $cv_id);
  636. $results = chado_select_record('cv', array('name'), $values);
  637. $cv = $results[0];
  638. // now add the term
  639. $term = array(
  640. 'name' => $name,
  641. 'namespace' => $cv->name,
  642. 'id' => $accession,
  643. 'definition' => $definition,
  644. 'is_obsolete' => $is_obsolete,
  645. 'cv_name' => $cv->name,
  646. 'is_relationship' => $is_relationship,
  647. 'db_name' => $db->name
  648. );
  649. $cvterm = tripal_insert_cvterm($term, array('update_existing' => TRUE));
  650. if ($cvterm) {
  651. drupal_set_message('Term added successfully.');
  652. }
  653. else {
  654. drupal_set_message('Could not add term. Check Drupal recent logs for error messages.', 'error');
  655. }
  656. }
  657. /**
  658. * Ajax callback for the tripal_cv_form
  659. *
  660. * @ingroup tripal_cv
  661. */
  662. function tripal_cv_cvterm_edit_form_ajax($form, $form_state) {
  663. $elements = array();
  664. $elements['name'] = $form['name'];
  665. $elements['continue'] = $form['continue'];
  666. // add back in the cv-edit-div that is used for the next round of AJAX
  667. $elements['name']['#prefix'] = '<div id="cvterm-edit-div">';
  668. $elements['name']['#suffix'] = '</div">';
  669. return $elements;
  670. }