tripal_cv_admin.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. <?php
  2. /**
  3. * Purpose: Provides the form for Updating and Deleteing existing
  4. * chado controlled vocabularies (See chado cv table)
  5. *
  6. * @ingroup tripal_cv
  7. */
  8. function tripal_cv_edit_page() {
  9. $output .= drupal_get_form('tripal_cv_select_form');
  10. $output .= '<div id="cv-edit-div">Please select a vocabulary above to view or edit</div>';
  11. return $output;
  12. }
  13. /**
  14. * Purpose: Provides the actual "Select CV" form on the Update/Delete Controlled
  15. * Vocabulary page. This form also triggers the edit javascript
  16. * @todo Modify this form to use Drupal AJAX
  17. *
  18. * @ingroup tripal_cv
  19. */
  20. function tripal_cv_select_form() {
  21. // get a list of db from chado for user to choose
  22. $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
  23. $results = chado_query($sql);
  24. $cvs = array();
  25. $cvs[] = '';
  26. while ($cv = db_fetch_object($results)) {
  27. $cvs[$cv->cv_id] = $cv->name;
  28. }
  29. $form['cvid'] = array(
  30. '#title' => t('Controlled Vocabulary/Ontology Name'),
  31. '#type' => 'select',
  32. '#options' => $cvs,
  33. '#ahah' => array(
  34. 'path' => 'admin/tripal/tripal_cv/cv/edit/js',
  35. 'wrapper' => 'cv-edit-div',
  36. 'effect' => 'fade',
  37. 'event' => 'change',
  38. 'method' => 'replace',
  39. ),
  40. );
  41. return $form;
  42. }
  43. /**
  44. * Purpose: The edit controlled vocabulary javascript
  45. *
  46. * @ingroup tripal_cv
  47. */
  48. function tripal_ajax_cv_edit() {
  49. // get the database id, build the form and then return the JSON object
  50. $cvid = filter_xss($_POST['cvid']);
  51. $form = drupal_get_form('tripal_cv_edit_form', $cvid);
  52. drupal_json(array('status' => TRUE, 'data' => $form));
  53. }
  54. /**
  55. * Purpose: Provides a form to allow updating/deleteing of controlled vocabularies
  56. *
  57. * @ingroup tripal_cv
  58. */
  59. function tripal_cv_edit_form(&$form_state = NULL, $cvid = NULL) {
  60. $sql = "SELECT * FROM {cv} WHERE cv_id = %d ";
  61. $cv = db_fetch_object(chado_query($sql, $cvid));
  62. // set the default values. If there is a value set in the
  63. // form_state then let's use that, otherwise, we'll pull
  64. // the values from the database
  65. $default_db = $form_state['values']['name'];
  66. $default_desc = $form_state['values']['description'];
  67. $default_url = $form_state['values']['url'];
  68. $default_urlprefix = $form_state['values']['urlprefix'];
  69. if (!$default_db) {
  70. $default_cv = $cv->name;
  71. }
  72. if (!$default_desc) {
  73. $default_desc = $cv->definition;
  74. }
  75. $form['cvid'] = array(
  76. '#type' => 'hidden',
  77. '#value' => $cvid
  78. );
  79. $form['name']= array(
  80. '#type' => 'textfield',
  81. '#title' => t("Controlled Vocabulary name"),
  82. '#description' => t('Please enter the name for this vocabulary.'),
  83. '#required' => FALSE,
  84. '#default_value' => $default_cv,
  85. '#weight' => 1
  86. );
  87. $form['definition']= array(
  88. '#type' => 'textarea',
  89. '#title' => t('Description'),
  90. '#description' => t('Please enter a description for this vocabulary'),
  91. '#default_value' => $default_desc,
  92. '#weight' => 2
  93. );
  94. $form['update'] = array(
  95. '#type' => 'submit',
  96. '#value' => t('Update'),
  97. '#weight' => 5,
  98. '#executes_submit_callback' => TRUE,
  99. );
  100. $form['delete'] = array(
  101. '#type' => 'submit',
  102. '#value' => t('Delete'),
  103. '#weight' => 6,
  104. '#executes_submit_callback' => TRUE,
  105. );
  106. $form['#redirect'] = 'admin/tripal/tripal_cv';
  107. return $form;
  108. }
  109. /**
  110. * Purpose: The submit function of the update/delete controlled vocabulary form
  111. *
  112. * @ingroup tripal_cv
  113. */
  114. function tripal_cv_edit_form_submit($form, &$form_state) {
  115. $name = $form_state['values']['name'];
  116. $desc = $form_state['values']['definition'];
  117. $cvid = $form_state['values']['cvid'];
  118. $op = $form_state['values']['op'];
  119. if (strcmp($op, 'Update') == 0) {
  120. $sql = "
  121. UPDATE {cv} SET
  122. name = '%s',
  123. definition = '%s'
  124. WHERE cv_id = %d
  125. ";
  126. $db = chado_query($sql, $name, $desc, $cvid);
  127. if ($db) {
  128. drupal_set_message(t("Controlled vocabulary updated"));
  129. }
  130. else {
  131. drupal_set_message(t("Failed to update controlled vocabulary."), 'error');
  132. }
  133. }
  134. if (strcmp($op, 'Delete')==0) {
  135. $sql = "
  136. DELETE FROM {cv}
  137. WHERE cv_id = %d
  138. ";
  139. $db = chado_query($sql, $cvid);
  140. if ($db) {
  141. drupal_set_message(t("Controlled vocabulary deleted"));
  142. }
  143. else {
  144. drupal_set_message(t("Failed to delete controlled vocabulary."), 'error');
  145. }
  146. }
  147. }
  148. /**
  149. * Purpose: Provides the Add controlled vocabulary form
  150. *
  151. * @ingroup tripal_cv
  152. */
  153. function tripal_cv_add_form(&$form_state = NULL) {
  154. $form['cvid'] = array(
  155. '#type' => 'hidden',
  156. '#value' => $cvid
  157. );
  158. $form['name']= array(
  159. '#type' => 'textfield',
  160. '#title' => t("Controlled Vocabulary name"),
  161. '#description' => t('Please enter the name for this vocabulary. This field will be ignored if an OBO file or URL is provided above'),
  162. '#required' => FALSE,
  163. '#default_value' => $default_cv,
  164. '#weight' => 1
  165. );
  166. $form['definition']= array(
  167. '#type' => 'textarea',
  168. '#title' => t('Description'),
  169. '#description' => t('Please enter a description for this vocabulary'),
  170. '#default_value' => $default_desc,
  171. '#weight' => 2
  172. );
  173. $form['add'] = array(
  174. '#type' => 'submit',
  175. '#value' => t('Add'),
  176. '#weight' => 5,
  177. '#executes_submit_callback' => TRUE,
  178. );
  179. $form['#redirect'] = 'admin/tripal/tripal_cv';
  180. return $form;
  181. }
  182. /**
  183. * Purpose: The submit function for the add controlled vocabulary form
  184. *
  185. * @ingroup tripal_cv
  186. */
  187. function tripal_cv_add_form_submit($form, &$form_state) {
  188. $name = $form_state['values']['name'];
  189. $desc = $form_state['values']['definition'];
  190. $sql = "
  191. INSERT INTO {cv}
  192. (name,definition)
  193. VALUES
  194. ('%s','%s')
  195. ";
  196. $db = chado_query($sql, $name, $desc);
  197. if ($db) {
  198. drupal_set_message(t("Controlled vocabulary added"));
  199. }
  200. else {
  201. drupal_set_message(t("Failed to add controlled vocabulary."), 'error');
  202. }
  203. }
  204. /**
  205. * Purpose: Provides the form that allows adding of terms to an existing
  206. * controlled vocabulary
  207. *
  208. * @ingroup tripal_cv
  209. */
  210. function tripal_cv_cvterm_form(&$form_state, $action = 'add') {
  211. tripal_core_ahah_init_form();
  212. $form = array();
  213. // get defaults
  214. $cv_id = $form_state['values']['cv_id'] ? $form_state['values']['cv_id'] : FALSE;
  215. $name = $form_state['values']['name'] ? $form_state['values']['name'] : '';
  216. // if we have a cv_id and a term name then get the rest of the term details
  217. if ($cv_id and $name) {
  218. $values = array(
  219. 'cv_id' => $cv_id,
  220. 'name' => $name,
  221. );
  222. $results = tripal_core_chado_select('cvterm', array('*'), $values);
  223. if (!$results or count($results) == 0) {
  224. // we can't find the cvterm so reset the name to blank
  225. $name = '';
  226. }
  227. else {
  228. $cvterm = $results[0];
  229. $definition = $cvterm->definition;
  230. $is_relationshiptype = $cvterm->is_relationshiptype;
  231. $is_obsolete = $cvterm->is_obsolete;
  232. // now get the database
  233. $values = array('dbxref_id' => $cvterm->dbxref_id);
  234. $results = tripal_core_chado_select('dbxref', array('*'), $values);
  235. $dbxref = $results[0];
  236. $accession = $dbxref->accession;
  237. $db_id = $dbxref->db_id;
  238. }
  239. }
  240. $values = array();
  241. $columns = array('cv_id', 'name');
  242. $options = array('order_by' => array('name' => 'ASC'));
  243. $results = tripal_core_chado_select('cv', $columns, $values, $options);
  244. $cvs = array();
  245. $cvs[] = '';
  246. foreach ($results as $cv) {
  247. $cvs[$cv->cv_id] = $cv->name;
  248. }
  249. $form['wrapper-top'] = array(
  250. '#type' => 'markup',
  251. '#value' => '<div id="cvterm-form">',
  252. );
  253. $form['form_action'] = array(
  254. '#type' => 'hidden',
  255. '#value' => $action,
  256. );
  257. $form['cv_id'] = array(
  258. '#title' => t('Controlled Vocabulary (Ontology) Name'),
  259. '#type' => 'select',
  260. '#options' => $cvs,
  261. '#required' => TRUE,
  262. '#default_value' => $cv_id,
  263. '#ahah' => array(
  264. 'path' => 'admin/tripal/tripal_cv/cvterm/ahah',
  265. 'wrapper' => 'cvterm-form',
  266. 'event' => 'change',
  267. 'method' => 'replace',
  268. ),
  269. );
  270. if ($cv_id) {
  271. $form['add_cvterm'] = array(
  272. '#type' => 'fieldset',
  273. '#title' => t('Term Details'),
  274. '#prefix' => '<div id="cvterm-add-div">',
  275. '#suffix' => '</div>'
  276. );
  277. $description = t('Please enter the name for this vocabulary term.');
  278. if ($action == 'edit') {
  279. $description = t('Enter the name of the term to edit. This field will update automatically as you type. Click outside of the box after entering the term.');
  280. }
  281. $form['add_cvterm']['name']= array(
  282. '#type' => 'textfield',
  283. '#title' => t("Term Name"),
  284. '#description' => $description,
  285. '#default_value' => $name,
  286. '#required' => TRUE,
  287. );
  288. if ($action == 'edit') {
  289. if ($name) {
  290. $form['add_cvterm']['name']['#attributes'] = array('readonly' => 'readonly');
  291. $form['add_cvterm']['name']['#description'] = 'The term name cannot be changed. If the name is incorrect, please create a new term and make this one as obsolete.';
  292. } else {
  293. $form['add_cvterm']['name']['#autocomplete_path'] = "admin/tripal/tripal_cv/cvterm/auto_name/$cv_id";
  294. $form['add_cvterm']['name']['#ahah'] = array(
  295. 'path' => 'admin/tripal/tripal_cv/cvterm/ahah',
  296. 'wrapper' => 'cvterm-form',
  297. 'method' => 'replace',
  298. );
  299. }
  300. }
  301. if ($action == 'add' or $name) {
  302. $form['add_cvterm']['definition']= array(
  303. '#type' => 'textarea',
  304. '#title' => t('Description'),
  305. '#description' => t('Please enter a description for this term'),
  306. '#default_value' => $definition,
  307. );
  308. $form['add_cvterm']['is_relationshiptype'] = array(
  309. '#type' => 'checkbox',
  310. '#title' => t('This term describes a relationship?'),
  311. '#default_value' => $is_relationshiptype,
  312. );
  313. $form['add_cvterm']['is_obsolete'] = array(
  314. '#type' => 'checkbox',
  315. '#title' => t('This term is obsolete?'),
  316. '#default_value' => $is_obsolete,
  317. );
  318. $values = array();
  319. $columns = array('db_id', 'name');
  320. $options = array('order_by' => array('name' => 'ASC'));
  321. $results = tripal_core_chado_select('db', $columns, $values, $options);
  322. $dbs = array();
  323. $dbs[] = '';
  324. foreach ($results as $db) {
  325. $dbs[$db->db_id] = $db->name;
  326. }
  327. $form['add_cvterm']['db_id'] = array(
  328. '#type' => 'select',
  329. '#title' => t('Database'),
  330. '#description' => t('All terms must be assocated with an external database.
  331. Please select the external database to associate with
  332. this term'),
  333. '#options' => $dbs,
  334. '#default_value' => $db_id,
  335. '#required' => TRUE,
  336. );
  337. if ($action == 'edit') {
  338. // we don't want to allow the user to change the database on an edit.
  339. $form['add_cvterm']['db_id']['#disabled'] = TRUE;
  340. $form['add_cvterm']['db_id']['#description'] = 'The database to which this term belongs cannot be changed.';
  341. }
  342. $form['add_cvterm']['accession']= array(
  343. '#type' => 'textfield',
  344. '#title' => t("Accession"),
  345. '#description' => t('If this term has an existing accession (unique identifier) in the database
  346. please enter that here. If the accession is numeric with a database prefix (e.g. GO:003023), please
  347. enter just the numeric value. The database prefix will be appended whenever the term is displayed.
  348. If the accession is not numeric then enter it as is. If no value is provied, the term name
  349. provided above will be used as the accession.'),
  350. '#required' => FALSE,
  351. '#default_value' => $accession,
  352. );
  353. if ($action == 'edit') {
  354. $form['add_cvterm']['accession']['#attributes'] = array('readonly' => 'readonly');
  355. $form['add_cvterm']['accession']['#description'] = 'Cannot change the term accession.';
  356. }
  357. $button_text = 'Add Term';
  358. if ($action == 'edit') {
  359. $button_text = 'Update Term';
  360. }
  361. $form['add_cvterm']['submit'] = array(
  362. '#type' => 'submit',
  363. '#value' => $button_text,
  364. );
  365. } // end if name selected (or action == 'add')
  366. } //end of if cv selected
  367. $form['wrapper-bottom'] = array(
  368. '#type' => 'markup',
  369. '#value' => '</div>',
  370. );
  371. return $form;
  372. }
  373. /**
  374. * Purpose: Validates the input for adding a cvterm
  375. *
  376. * @ingroup tripal_cv
  377. */
  378. function tripal_cv_cvterm_form_validate($form, &$form_state) {
  379. // Ensure that submit does not get called unless the AHAH in the form was called
  380. if (!empty($form_state['ahah_submission'])) {
  381. return;
  382. }
  383. }
  384. /**
  385. * Purpose: Adds terms to an existing controlled vocabulary
  386. *
  387. * @ingroup tripal_cv
  388. */
  389. function tripal_cv_cvterm_form_submit($form, &$form_state) {
  390. // Ensure the AHAH in the form was called
  391. if (!empty($form_state['ahah_submission'])) {
  392. return;
  393. }
  394. // get the database
  395. $values = array('db_id' => $form_state['values']['db_id']);
  396. $results = tripal_core_chado_select('db', array('name'), $values);
  397. if (!$results or count($results) == 0){
  398. drupal_set_message(t('Unable to add term. Cannot find the database.'), 'error');
  399. return;
  400. }
  401. $db = $results[0];
  402. // get the cv
  403. $values = array('cv_id' => $form_state['values']['cv_id']);
  404. $results = tripal_core_chado_select('cv', array('name'), $values);
  405. if (!$results or count($results) == 0){
  406. drupal_set_message(t('Unable to add term. Cannot find the vocabulary.'), 'error');
  407. return;
  408. }
  409. $cv = $results[0];
  410. // get the accession for this term
  411. $accession = $form_state['values']['accession'];
  412. if (!$accession) {
  413. $accession = $form_state['values']['name'];
  414. }
  415. if (is_numeric($accession)) {
  416. $accession = $db->name . ":" . $accession;
  417. }
  418. $update = 0;
  419. if ($form_state['values']['form_action'] == 'edit') {
  420. $update = 1;
  421. }
  422. // now add the term
  423. $term = array(
  424. 'name' => $form_state['values']['name'],
  425. 'namespace' => $cv->name,
  426. 'id' => $accession,
  427. 'def' => $form_state['values']['definition'],
  428. 'is_obsolete' => $form_state['values']['is_obsolete'],
  429. );
  430. $is_relationship = $form_state['values']['is_relationshiptype'];
  431. $cvterm = tripal_cv_add_cvterm($term, $cv->name, $is_relationship, $update, $db->name);
  432. if ($cvterm) {
  433. if (!$update) {
  434. drupal_set_message('Term added successfully.');
  435. }
  436. else {
  437. drupal_set_message('Term updated successfully.');
  438. }
  439. }
  440. else {
  441. drupal_set_message('Could not add term. Check Drupal recent logs for error messages.', 'error');
  442. }
  443. }
  444. /**
  445. * Purpose: This function gets called when the selecting of a cv from
  446. * the select list triggers it. This function simply rebuilds the form
  447. * with new information. No elements are created here
  448. *
  449. * @ingroup tripal_cv
  450. */
  451. function tripal_cv_cvterm_callback() {
  452. $status = TRUE;
  453. // prepare and render the form
  454. $form = tripal_core_ahah_prepare_form();
  455. $data = drupal_render($form);
  456. // bind javascript events to the new objects that will be returned
  457. // so that AHAH enabled elements will work.
  458. $settings = tripal_core_ahah_bind_events();
  459. // return the updated JSON
  460. drupal_json(
  461. array(
  462. 'status' => $status,
  463. 'data' => $data,
  464. 'settings' => $settings,
  465. )
  466. );
  467. }
  468. /**
  469. * Cvterm path form submit
  470. *
  471. * @ingroup tripal_cv
  472. */
  473. function tripal_cv_cvtermpath_form_submit($form, &$form_state) {
  474. global $user;
  475. $cvid = $form_state['values']['cvid'];
  476. // first get the controlled vocabulary name:
  477. $cv = db_fetch_object(chado_query("SELECT * FROM {cv} WHERE cv_id = %d", $cvid));
  478. // Submit a job to update cvtermpath
  479. $job_args = array($cvid);
  480. if ($form_state['values']['op'] == t('Update cvtermpath')) {
  481. tripal_add_job("Update cvtermpath: $cv->name", 'tripal_cv',
  482. 'tripal_cv_update_cvtermpath', $job_args, $user->uid);
  483. }
  484. }