admin.inc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * @file
  4. * The administration interface.
  5. */
  6. /**
  7. * Header of the translation table.
  8. *
  9. * @param $languages
  10. * languages to translate to
  11. *
  12. * @return
  13. * header row for theme_table
  14. */
  15. function _translation_table_get_header($languages) {
  16. // FIXME: this sortable header won't work, why?
  17. // $header = array(array('data' => t('Original'), 'sort' => 'asc', 'field' => 'source'));
  18. $header = array(array('data' => t('Original')));
  19. foreach ($languages as $lang_code => $lang_name) {
  20. $header[] = array('data' => $lang_name);
  21. }
  22. $header[] = array('data' => t('Operations'), 'colspan' => 3);
  23. return $header;
  24. }
  25. /**
  26. * One row of the translation table.
  27. *
  28. * @param $source
  29. * source string record
  30. * @param $languages
  31. * languages to translate to
  32. * @return
  33. * table row for theme_table
  34. */
  35. function _translation_table_row($source, $languages) {
  36. $form['source'] = array(
  37. '#markup' => check_plain($source->source),
  38. );
  39. $form['location'] = array(
  40. '#type' => 'value',
  41. '#value' => check_plain($source->location),
  42. );
  43. foreach ($languages as $lang_code => $lang_name) {
  44. $translation = db_query("SELECT lt.translation FROM {locales_target} lt WHERE lt.lid = :lid AND lt.language = :lang", array('lid' => $source->lid, 'lang' => $lang_code))->fetchField();
  45. $form[$lang_code] = array(
  46. '#type' => 'textfield',
  47. '#default_value' => $translation,
  48. '#size' => 35,
  49. '#maxlength' => NULL,
  50. );
  51. }
  52. return $form;
  53. }
  54. /**
  55. * Submit handler for the translation table.
  56. */
  57. function translation_table_submit_translations($form, &$form_state) {
  58. switch ($form_state['clicked_button']['#id']) {
  59. case 'edit-submit':
  60. $language_list = locale_language_list('language', TRUE);
  61. foreach ($form_state['values']['strings'] as $lid => $values) {
  62. foreach ($values as $lang_code => $translation) {
  63. if (in_array($lang_code, $language_list)) {
  64. _translation_table_update_translation($lid, $lang_code, $translation);
  65. }
  66. }
  67. }
  68. break;
  69. }
  70. // Redirect to current page.
  71. $query = isset($_GET['page']) ? array('page' => $_GET['page']) : array();
  72. $form_state['redirect'] = array($_GET['q'], $query);
  73. }
  74. /**
  75. * Update, create or delete translation as needed.
  76. */
  77. function _translation_table_update_translation($lid, $lang_code, $translation) {
  78. if ($translation == '') {
  79. db_query("DELETE FROM {locales_target} WHERE lid = :lid AND language = :lang", array('lid' => $lid, 'lang' => $lang_code));
  80. return;
  81. }
  82. db_merge("locales_target")
  83. ->key(array('language' => $lang_code, 'lid' => $lid, 'plural' => 0))
  84. ->fields(array('translation' => $translation))
  85. ->execute();
  86. }