tripal_cv_admin.inc 18 KB

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