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