|
1
|
|
|
<?php |
|
2
|
|
|
// $Id$ |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* @file |
|
6
|
|
|
* Administration page callbacks for the boinctranslate module. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* The BOINC translation settings form allows configuration of BTS integration |
|
11
|
|
|
* (and other translation related settings) |
|
12
|
|
|
*/ |
|
13
|
|
|
function boinctranslate_admin_settings(&$form_state) { |
|
14
|
|
|
$form = array(); |
|
15
|
|
|
$initialized = FALSE; |
|
16
|
|
|
$import_enabled = FALSE; |
|
17
|
|
|
$default = array( |
|
18
|
|
|
'transifex_user' => variable_get('boinc_translate_transifex_user', ''), |
|
19
|
|
|
'transifex_pass' => variable_get('boinc_translate_transifex_pass', ''), |
|
20
|
|
|
'transifex_boinc_name' => variable_get( |
|
21
|
|
|
'boinc_translate_transifex_standard_name', 'boinc' |
|
22
|
|
|
), |
|
23
|
|
|
'transifex_boinc_resources' => variable_get( |
|
24
|
|
|
'boinc_translate_transifex_standard_resources', |
|
25
|
|
|
"project-generic\nweb" |
|
26
|
|
|
), |
|
27
|
|
|
'transifex_boinc_drupal_resource' => variable_get( |
|
28
|
|
|
'boinc_translate_transifex_boinc_drupal_resource', |
|
29
|
|
|
'drupal' |
|
30
|
|
|
), |
|
31
|
|
|
'transifex_project_name' => variable_get( |
|
32
|
|
|
'boinc_translate_transifex_project_name', '' |
|
33
|
|
|
), |
|
34
|
|
|
'transifex_project_resources' => variable_get( |
|
35
|
|
|
'boinc_translate_transifex_project_resources', '' |
|
36
|
|
|
), |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
if ($default['transifex_user'] AND $default['transifex_pass']) { |
|
|
|
|
|
|
40
|
|
|
if (trim($default['transifex_boinc_resources']) |
|
41
|
|
|
OR trim($default['transifex_project_resources'])) { |
|
|
|
|
|
|
42
|
|
|
$import_enabled = TRUE; |
|
43
|
|
|
} |
|
44
|
|
|
if ($default['transifex_boinc_name'] |
|
45
|
|
|
AND $default['transifex_project_name'] |
|
|
|
|
|
|
46
|
|
|
AND trim($default['transifex_boinc_resources']) |
|
|
|
|
|
|
47
|
|
|
AND trim($default['transifex_project_resources'])) { |
|
|
|
|
|
|
48
|
|
|
$initialized = TRUE; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// Define the form |
|
53
|
|
|
|
|
54
|
|
|
$form['transifex'] = array( |
|
55
|
|
|
'#title' => t('Transifex settings'), |
|
56
|
|
|
'#type' => 'fieldset', |
|
57
|
|
|
'#description' => '', |
|
58
|
|
|
'#collapsible' => TRUE, |
|
59
|
|
|
'#collapsed' => $initialized, |
|
60
|
|
|
'#attributes' => array('class' => '') |
|
61
|
|
|
); |
|
62
|
|
|
$form['transifex']['boinc_translate_transifex_user'] = array( |
|
63
|
|
|
'#type' => 'textfield', |
|
64
|
|
|
'#title' => t('User name'), |
|
65
|
|
|
'#default_value' => $default['transifex_user'], |
|
66
|
|
|
); |
|
67
|
|
|
$form['transifex']['boinc_translate_transifex_pass'] = array( |
|
68
|
|
|
'#type' => 'password', |
|
69
|
|
|
'#title' => t('Password'), |
|
70
|
|
|
'#attributes' => array( |
|
71
|
|
|
'placeholder' => $default['transifex_pass'] ? '********' : '', |
|
72
|
|
|
), |
|
73
|
|
|
); |
|
74
|
|
|
$form['transifex']['boinc_translate_transifex_standard_name'] = array( |
|
75
|
|
|
'#type' => 'textfield', |
|
76
|
|
|
'#title' => t('BOINC Transifex project'), |
|
77
|
|
|
'#default_value' => $default['transifex_boinc_name'], |
|
78
|
|
|
'#description' => t('The portion of the Transifex URL that identifies BOINC.'), |
|
79
|
|
|
); |
|
80
|
|
|
$form['transifex']['boinc_translate_transifex_boinc_drupal_resource'] = array( |
|
81
|
|
|
'#type' => 'textfield', |
|
82
|
|
|
'#title' => t('Official BOINC Drupal Resource'), |
|
83
|
|
|
'#default_value' => $default['transifex_boinc_drupal_resource'], |
|
84
|
|
|
'#description' => t('The portion of the Transifex URL that identifies |
|
85
|
|
|
the standard BOINC Resource to use for translation of the public |
|
86
|
|
|
content defined in the Drupal system.'), |
|
87
|
|
|
); |
|
88
|
|
|
$form['transifex']['boinc_translate_transifex_standard_resources'] = array( |
|
89
|
|
|
'#type' => 'textarea', |
|
90
|
|
|
'#title' => t('Additional BOINC Resources'), |
|
91
|
|
|
'#default_value' => $default['transifex_boinc_resources'], |
|
92
|
|
|
'#description' => t('List additional BOINC Transifex Resources to be used |
|
93
|
|
|
for translating strings on this site (one Resource string per line). |
|
94
|
|
|
Resources will be imported in the order they are given, so |
|
95
|
|
|
translations from the Resources at the top of the list will be overridden |
|
96
|
|
|
by any matching translations found in Resources further down the list. |
|
97
|
|
|
Note that strings from the Official BOINC Drupal Resource will override |
|
98
|
|
|
any strings from these additional Resources.'), |
|
99
|
|
|
); |
|
100
|
|
|
$form['transifex']['boinc_translate_transifex_project_name'] = array( |
|
101
|
|
|
'#type' => 'textfield', |
|
102
|
|
|
'#title' => t('Project-specific Transifex project'), |
|
103
|
|
|
'#default_value' => $default['transifex_project_name'], |
|
104
|
|
|
'#description' => t('The portion of the Transifex URL that identifies this project.'), |
|
105
|
|
|
); |
|
106
|
|
|
$form['transifex']['boinc_translate_transifex_project_resources'] = array( |
|
107
|
|
|
'#type' => 'textarea', |
|
108
|
|
|
'#title' => t('Project-specific Resources'), |
|
109
|
|
|
'#default_value' => $default['transifex_project_resources'], |
|
110
|
|
|
'#description' => t('List project-specific Transifex Resources to be used |
|
111
|
|
|
for translating strings on this site (one Resource string per line). |
|
112
|
|
|
Resources will be imported in the order they are given, so |
|
113
|
|
|
translations from the Resources at the top of the list will be overridden |
|
114
|
|
|
by any matching translations found in Resources further down the list. |
|
115
|
|
|
<br/> |
|
116
|
|
|
NOTE: The first Resource listed here is considered the primary Resource |
|
117
|
|
|
for project-specific translations and will be updated by the "Update |
|
118
|
|
|
project-specific Resources" button below!'), |
|
119
|
|
|
); |
|
120
|
|
|
$form['transifex']['buttons']['submit'] = array( |
|
121
|
|
|
'#type' => 'submit', |
|
122
|
|
|
'#value' => t('Save configuration'), |
|
123
|
|
|
); |
|
124
|
|
|
|
|
125
|
|
|
$form['tools'] = array( |
|
126
|
|
|
'#title' => t('Tools'), |
|
127
|
|
|
'#type' => 'fieldset', |
|
128
|
|
|
'#description' => '', |
|
129
|
|
|
'#collapsible' => TRUE, |
|
130
|
|
|
'#collapsed' => FALSE, |
|
131
|
|
|
'#attributes' => array('class' => '') |
|
132
|
|
|
); |
|
133
|
|
|
$form['tools']['initialize_languages_text'] = array( |
|
134
|
|
|
'#type' => 'item', |
|
135
|
|
|
'#title' => t('Install official BOINC languages'), |
|
136
|
|
|
'#value' => t('Installs all languages from the BOINC Transifex project. Also installs missing languages in Drupal. <br>Languages must be enabled manually in Administer > Site configuration > Languages.'), |
|
137
|
|
|
); |
|
138
|
|
|
$form['tools']['initialize_languages'] = array( |
|
139
|
|
|
'#type' => 'button', |
|
140
|
|
|
'#value' => t('Install official BOINC languages'), |
|
141
|
|
|
'#executes_submit_callback' => TRUE, |
|
142
|
|
|
'#submit' => array('boinctranslate_admin_settings_initialize_languages'), |
|
143
|
|
|
'#disabled' => !$initialized, |
|
144
|
|
|
); |
|
145
|
|
|
$form['tools']['import_now_text'] = array( |
|
146
|
|
|
'#type' => 'item', |
|
147
|
|
|
'#title' => t('Import all translations'), |
|
148
|
|
|
'#value' => t('Imports from Tranifex all configured Resources into their respective text groups.'), |
|
149
|
|
|
); |
|
150
|
|
|
$form['tools']['import_now'] = array( |
|
151
|
|
|
'#type' => 'button', |
|
152
|
|
|
'#value' => t('Import all translations'), |
|
153
|
|
|
'#executes_submit_callback' => TRUE, |
|
154
|
|
|
'#submit' => array('boinctranslate_admin_settings_import_now'), |
|
155
|
|
|
'#disabled' => !$import_enabled, |
|
156
|
|
|
); |
|
157
|
|
|
$form['tools']['download_boinc_pot_text'] = array( |
|
158
|
|
|
'#type' => 'item', |
|
159
|
|
|
'#title' => t('Download official BOINC-Drupal .pot'), |
|
160
|
|
|
'#value' => t('Downloads the generic BOINC-Drupal .pot file. This is the template file for the boinc text group.'), |
|
161
|
|
|
); |
|
162
|
|
|
$form['tools']['download_boinc_pot'] = array( |
|
163
|
|
|
'#type' => 'button', |
|
164
|
|
|
'#value' => t('Download official BOINC-Drupal .pot'), |
|
165
|
|
|
'#executes_submit_callback' => TRUE, |
|
166
|
|
|
'#submit' => array('boinctranslate_admin_settings_download_boinc_pot'), |
|
167
|
|
|
'#disabled' => !$import_enabled, |
|
168
|
|
|
); |
|
169
|
|
|
$form['tools']['download_project_pot_text'] = array( |
|
170
|
|
|
'#type' => 'item', |
|
171
|
|
|
'#title' => t('Download project-specific .pot'), |
|
172
|
|
|
'#value' => t('Downloads the project-specific .pot file. This is the template file for the project text group.'), |
|
173
|
|
|
); |
|
174
|
|
|
$form['tools']['download_project_pot'] = array( |
|
175
|
|
|
'#type' => 'button', |
|
176
|
|
|
'#value' => t('Download project-specific .pot'), |
|
177
|
|
|
'#executes_submit_callback' => TRUE, |
|
178
|
|
|
'#submit' => array('boinctranslate_admin_settings_download_project_pot'), |
|
179
|
|
|
'#disabled' => !$import_enabled, |
|
180
|
|
|
); |
|
181
|
|
|
if (user_access('update official BOINC translations')) { |
|
182
|
|
|
$form['tools']['update_official_boinc_text'] = array( |
|
183
|
|
|
'#type' => 'item', |
|
184
|
|
|
'#title' => t('Update official BOINC translations'), |
|
185
|
|
|
'#value' => t('Updates the official BOINC-Drupal Resource (translatable strings templates) to the BOINC Transifex project, based on the consolidated imported translations.<br><b>WARNING: Do not use unless you have write-access to the BOINC Transifex project.</b>'), |
|
186
|
|
|
); |
|
187
|
|
|
$form['tools']['update_official_boinc'] = array( |
|
188
|
|
|
'#type' => 'button', |
|
189
|
|
|
'#value' => t('Update official BOINC translations'), |
|
190
|
|
|
'#executes_submit_callback' => TRUE, |
|
191
|
|
|
'#submit' => array('boinctranslate_admin_settings_update_official_boinc'), |
|
192
|
|
|
'#disabled' => !$import_enabled, |
|
193
|
|
|
); |
|
194
|
|
|
} |
|
195
|
|
|
$form['tools']['export_now_text'] = array( |
|
196
|
|
|
'#type' => 'item', |
|
197
|
|
|
'#title' => t('Update project-specific Resources'), |
|
198
|
|
|
'#value' => t('Updates the configured Resources (translatable strings templates) to the project\'s Transifex project.<br><b>WARNING: Do not use unless you have write-access to your project\'s Transifex project.</b>'), |
|
199
|
|
|
); |
|
200
|
|
|
$form['tools']['export_now'] = array( |
|
201
|
|
|
'#type' => 'button', |
|
202
|
|
|
'#value' => t('Update project-specific Resources'), |
|
203
|
|
|
'#executes_submit_callback' => TRUE, |
|
204
|
|
|
'#submit' => array('boinctranslate_admin_settings_export_now'), |
|
205
|
|
|
'#disabled' => !$import_enabled, |
|
206
|
|
|
); |
|
207
|
|
|
|
|
208
|
|
|
$form['#submit'][] = 'system_settings_form_submit'; |
|
209
|
|
|
$form['#theme'] = 'system_settings_form'; |
|
210
|
|
|
|
|
211
|
|
|
return $form; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
function boinctranslate_admin_settings_export_now() { |
|
215
|
|
|
drupal_goto('admin/boinc/translation/export'); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
function boinctranslate_admin_settings_import_now() { |
|
219
|
|
|
drupal_goto('admin/boinc/translation/import'); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
function boinctranslate_admin_settings_initialize_languages() { |
|
223
|
|
|
drupal_goto('admin/boinc/translation/initialize-languages'); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
function boinctranslate_admin_settings_update_official_boinc() { |
|
227
|
|
|
drupal_goto('admin/boinc/translation/update-official-boinc'); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
function boinctranslate_admin_settings_download_boinc_pot() { |
|
231
|
|
|
drupal_goto('admin/boinc/translation/download-pot/boinc'); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
function boinctranslate_admin_settings_download_project_pot() { |
|
235
|
|
|
drupal_goto('admin/boinc/translation/download-pot/project'); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Validate the BOINC translation settings form. |
|
241
|
|
|
*/ |
|
242
|
|
|
function boinctranslate_admin_settings_validate($form, &$form_state) { |
|
243
|
|
|
$values = $form_state['values']; |
|
244
|
|
|
$errors = array(); |
|
245
|
|
|
$api_base_url = 'https://www.transifex.com/api/2'; |
|
246
|
|
|
|
|
247
|
|
|
if (!$values['boinc_translate_transifex_user']) { |
|
248
|
|
|
form_set_error('boinc_translate_transifex_user', t('User name is required.')); |
|
249
|
|
|
} |
|
250
|
|
|
if (!$values['boinc_translate_transifex_pass']) { |
|
251
|
|
|
if (!variable_get('boinc_translate_transifex_pass', '')) { |
|
252
|
|
|
form_set_error('boinc_translate_transifex_pass', t('Password is required.')); |
|
253
|
|
|
} |
|
254
|
|
|
else { |
|
255
|
|
|
unset($form_state['values']['boinc_translate_transifex_pass']); |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
if (!$values['boinc_translate_transifex_standard_name']) { |
|
259
|
|
|
form_set_error('boinc_translate_transifex_standard_name', |
|
260
|
|
|
t('BOINC Transifex project name is required.') |
|
261
|
|
|
); |
|
262
|
|
|
} |
|
263
|
|
|
if (!$values['boinc_translate_transifex_standard_resources']) { |
|
264
|
|
|
form_set_error('boinc_translate_transifex_standard_resources', |
|
265
|
|
|
t('At least one BOINC Transifex project Resource is required.') |
|
266
|
|
|
); |
|
267
|
|
|
} |
|
268
|
|
|
if (!$values['boinc_translate_transifex_project_resources']) { |
|
269
|
|
|
form_set_error('boinc_translate_transifex_project_resources', |
|
270
|
|
|
t('At least one project-specific Transifex Resource is required.') |
|
271
|
|
|
); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
$username = $values['boinc_translate_transifex_user']; |
|
275
|
|
|
$password = ($values['boinc_translate_transifex_pass']) ? $values['boinc_translate_transifex_pass'] : variable_get('boinc_translate_transifex_pass', ''); |
|
276
|
|
|
$boinc_name = $values['boinc_translate_transifex_standard_name']; |
|
277
|
|
|
$boinc_resources = boinctranslate_parse_resources( |
|
278
|
|
|
$values['boinc_translate_transifex_standard_resources'] |
|
279
|
|
|
); |
|
280
|
|
|
|
|
281
|
|
|
if ($username AND $password AND $boinc_name AND $boinc_resources) { |
|
|
|
|
|
|
282
|
|
|
// Test authentication |
|
283
|
|
|
$authenticated = FALSE; |
|
284
|
|
|
$path = "project/{$boinc_name}/resource/{$boinc_resources[0]}/translation/en"; |
|
285
|
|
|
$response = boinctranslate_transifex_request($path, NULL, TRUE, FALSE, $username, $password); |
|
286
|
|
|
|
|
287
|
|
|
if ($response) { |
|
288
|
|
|
if ($response == '401 UNAUTHORIZED') { |
|
289
|
|
|
form_set_error( |
|
290
|
|
|
'boinc_translate_transifex_pass', |
|
291
|
|
|
t('Transifex authentication failed.') |
|
292
|
|
|
); |
|
293
|
|
|
} |
|
294
|
|
|
else { |
|
295
|
|
|
$authenticated = TRUE; |
|
296
|
|
|
} |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
if ($authenticated) { |
|
300
|
|
|
// Prepare list of resources to validate |
|
301
|
|
|
$transifex_resources = array( |
|
302
|
|
|
'boinc' => $boinc_resources, |
|
303
|
|
|
); |
|
304
|
|
|
// Parse project-specific resources |
|
305
|
|
|
$project_name = trim($values['boinc_translate_transifex_project_name']); |
|
306
|
|
|
$project_resources = boinctranslate_parse_resources( |
|
307
|
|
|
$values['boinc_translate_transifex_project_resources'] |
|
308
|
|
|
); |
|
309
|
|
|
if ($project_name AND $project_resources) { |
|
|
|
|
|
|
310
|
|
|
$transifex_resources[$project_name] = $project_resources; |
|
311
|
|
|
} |
|
312
|
|
|
elseif ($project_name AND !$project_resources) { |
|
|
|
|
|
|
313
|
|
|
drupal_set_message( |
|
314
|
|
|
t('No project-specific resources were provided'), |
|
315
|
|
|
'warning' |
|
316
|
|
|
); |
|
317
|
|
|
} |
|
318
|
|
|
elseif ($project_resources AND !$project_name) { |
|
|
|
|
|
|
319
|
|
|
drupal_set_message( |
|
320
|
|
|
t('No project-specific Transifex project name was provided'), |
|
321
|
|
|
'warning' |
|
322
|
|
|
); |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
// Try to access the given resources |
|
326
|
|
|
foreach ($transifex_resources as $project => $resources) { |
|
327
|
|
|
foreach ($resources as $resource) { |
|
328
|
|
|
|
|
329
|
|
|
$path = "project/{$project}/resource/{$resource}/translation/en"; |
|
330
|
|
|
$response = boinctranslate_transifex_request($path); |
|
331
|
|
|
|
|
332
|
|
|
if ($response == '404 NOT FOUND') { |
|
333
|
|
|
form_set_error( |
|
334
|
|
|
'boinc_translate_transifex_' . ($project == $boinc_name ? 'standard' : 'project') . '_resources', |
|
335
|
|
|
t('Resource %name not found in %project.', |
|
336
|
|
|
array( |
|
337
|
|
|
'%name' => $resource, |
|
338
|
|
|
'%project' => $project, |
|
339
|
|
|
) |
|
340
|
|
|
) |
|
341
|
|
|
); |
|
342
|
|
|
} |
|
343
|
|
|
} |
|
344
|
|
|
} |
|
345
|
|
|
} |
|
346
|
|
|
} |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
/** |
|
350
|
|
|
* Handle post-validation submission of BOINC translation settings form. |
|
351
|
|
|
*/ |
|
352
|
|
|
function boinctranslate_admin_settings_submit($form, &$form_state) { |
|
353
|
|
|
drupal_set_message(t('BOINC translation settings have been updated.')); |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
|
|
357
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.