i18n_path.admin.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * @file
  4. * Administration pages for path translation.
  5. */
  6. /**
  7. * Path overview page
  8. */
  9. function i18n_path_admin_overview($type = NULL) {
  10. module_load_include('admin.inc', 'i18n_translation');
  11. return i18n_translation_admin_overview('path');
  12. }
  13. /**
  14. * Path add/edit form
  15. */
  16. function i18n_path_admin_form($form, $form_state, $translation_set = NULL) {
  17. $form['translation_set'] = array('#type' => 'value', '#value' => $translation_set);
  18. if ($translation_set) {
  19. $paths = $translation_set->get_translations();
  20. }
  21. else {
  22. $paths = array();
  23. }
  24. $form['title'] = array(
  25. '#title' => t('Title'),
  26. '#type' => 'textfield',
  27. '#default_value' => $translation_set ? $translation_set->title : '',
  28. '#description' => t('Optional descriptive name for this set.'),
  29. );
  30. $form['translations'] = array(
  31. '#type' => 'fieldset',
  32. '#title' => t('Translations'),
  33. '#tree' => TRUE,
  34. '#description' => t('The path for this menu link. This can be an internal Drupal path such as %add-node or an external URL such as %drupal. Enter %front to link to the front page.', array('%front' => '<front>', '%add-node' => 'node/add', '%drupal' => 'http://drupal.org')),
  35. );
  36. foreach (i18n_language_list() as $langcode => $name) {
  37. $form['translations'][$langcode] = array(
  38. '#type' => 'textfield',
  39. '#title' => check_plain($name),
  40. '#default_value' => !empty($paths[$langcode]) ? $paths[$langcode]->path : '',
  41. );
  42. }
  43. $form['controls']['update'] = array(
  44. '#type' => 'submit',
  45. '#value' => t('Save'),
  46. '#name' => 'save',
  47. );
  48. if ($translation_set) {
  49. $form['controls']['delete'] = array(
  50. '#type' => 'submit',
  51. '#value' => t('Delete'),
  52. '#name' => 'delete',
  53. );
  54. }
  55. return $form;
  56. }
  57. /**
  58. * Process form validation
  59. */
  60. function i18n_path_admin_form_validate($form, &$form_state) {
  61. if ($form_state['triggering_element']['#name'] == 'save') {
  62. $paths = &$form_state['values']['translations'];
  63. if ($paths = array_filter($paths)) {
  64. module_load_include('inc', 'menu', 'menu.admin');
  65. foreach ($paths as $language => &$link_path) {
  66. $link_path = i18n_prepare_normal_path($link_path, $language);
  67. $validation_form_state = array(
  68. 'values' => array(
  69. 'link_path' => $link_path,
  70. ),
  71. );
  72. menu_edit_item_validate(array(), $validation_form_state);
  73. }
  74. }
  75. else {
  76. form_set_error('paths', t('There are no path translations to save.'));
  77. }
  78. }
  79. }
  80. /**
  81. * Process form submission
  82. */
  83. function i18n_path_admin_form_submit($form, &$form_state) {
  84. $translation_set = $form_state['values']['translation_set'];
  85. switch ($form_state['triggering_element']['#name']) {
  86. case 'save':
  87. $paths = array_filter($form_state['values']['translations']);
  88. $translation_set = $translation_set ? $translation_set : i18n_translation_set_create('path');
  89. $translation_set->title = '';
  90. $translations = $translation_set->get_translations();
  91. foreach ($paths as $lang => $path) {
  92. if (isset($translations[$lang])) {
  93. $translations[$lang]->path = $path;
  94. }
  95. else {
  96. $translation_set->add_item($path, $lang);
  97. }
  98. }
  99. foreach (array_diff(array_keys($translation_set->get_translations()), array_keys($paths)) as $language) {
  100. $translation_set->remove_language($language);
  101. unset($translations[$language]);
  102. }
  103. if (!empty($form_state['values']['title'])) {
  104. $translation_set->title = $form_state['values']['title'];
  105. }
  106. $translation_set->save(TRUE);
  107. drupal_set_message(t('The path translation has been saved.'));
  108. break;
  109. case 'delete':
  110. $destination = array();
  111. if (isset($_GET['destination'])) {
  112. $destination = drupal_get_destination();
  113. unset($_GET['destination']);
  114. }
  115. $form_state['redirect'] = array($translation_set->get_delete_path(), array('query' => $destination));
  116. return;
  117. }
  118. $form_state['redirect'] = 'admin/config/regional/i18n_translation/path';
  119. }
  120. /**
  121. * Save path translation set.
  122. */
  123. function i18n_path_save_translations($paths, $tpid = NULL) {
  124. $paths = array_filter($paths);
  125. if (lock_acquire('i18n_path')) {
  126. if ($tpid) {
  127. db_delete('i18n_path')->condition('tpid', $tpid)->execute();
  128. }
  129. else {
  130. $tpid = 1 + (int)db_query('SELECT MAX(tpid) FROM {i18n_path}')->fetchField();
  131. }
  132. foreach ($paths as $langcode => $path) {
  133. db_insert('i18n_path')
  134. ->fields(array('tpid' => $tpid, 'language' => $langcode, 'path' => $path))
  135. ->execute();
  136. }
  137. lock_release('i18n_path');
  138. return $tpid;
  139. }
  140. }