tripal_cv_admin.inc 16 KB

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