tripal_chado.cv.inc 25 KB

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