-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n_access.module
333 lines (285 loc) · 10.2 KB
/
i18n_access.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
/**
* @file
* file_description
*/
define('I18N_ACCESS_LANGUAGE_NEUTRAL', 'NEUTRAL');
/**
* Implements hook_user_insert().
*/
function i18n_access_user_insert(&$edit, &$account, $category = NULL) {
if ($category == 'account') {
// see user_admin_perm_submit()
if (isset($edit['i18n_access'])) {
db_delete('i18n_access')
->condition('uid', $account->uid)
->execute();
$edit['i18n_access'] = array_filter($edit['i18n_access']);
if (count($edit['i18n_access'])) {
db_insert('i18n_access')
->fields(array(
'uid' => $account->uid,
'perm' => implode(', ', array_keys($edit['i18n_access'])),
))->execute();
}
unset($edit['i18n_access']);
}
}
}
/**
* Implements hook_user_update().
*/
function i18n_access_user_update(&$edit, &$account, $category = NULL) {
if ($category == 'account') {
// see user_admin_perm_submit()
if (isset($edit['i18n_access'])) {
db_delete('i18n_access')
->condition('uid', $account->uid)
->execute();
$edit['i18n_access'] = array_filter($edit['i18n_access']);
if (count($edit['i18n_access'])) {
db_insert('i18n_access')
->fields(array(
'uid' => $account->uid,
'perm' => implode(', ', array_keys($edit['i18n_access'])),
))->execute();
}
unset($edit['i18n_access']);
}
}
}
/**
* Load the language permissions for a given user
*/
function i18n_access_load_permissions($uid = NULL) {
static $perms = array();
// use the global user id if none is passed
if (!isset($uid)) {
$uid = $GLOBALS['user']->uid;
$account = NULL;
}
else {
$account = user_load($uid);
}
if (!isset($perms[$uid])) {
$perm_string = db_query('SELECT perm FROM {i18n_access} WHERE uid = :uid', array(':uid' => $uid))->fetchField();
if ($perm_string) {
$perms[$uid] = drupal_map_assoc(explode(', ', $perm_string));
}
else {
$perms[$uid] = array();
}
}
// adding the default languages if permission has been granted
if (user_access('access selected languages', $account)) {
$perms[$uid] = array_merge($perms[$uid], drupal_map_assoc(variable_get('i18n_access_languages', array())));
}
return $perms[$uid];
}
/**
* Implements hook_permission().
*/
function i18n_access_permission() {
return array(
'access selected languages' => array(
'title' => t('Access selected languages'),
'description' => t('access selected languages.'),
),
);
}
/**
* Implements hook_form_node_form_alter().
*/
function i18n_access_form_node_form_alter(&$form, &$form_state, $form_id) {
if (isset($form['language']['#options'])) {
// Remove inaccessible languages from the select box
// don't do it for admininstrators
if (!user_access('administer nodes')) {
$perms = i18n_access_load_permissions();
foreach ($form['language']['#options'] as $key => $value) {
$perm_key = ($key == '') ? I18N_ACCESS_LANGUAGE_NEUTRAL : $key;
if ($key!='en' && empty($perms[$perm_key])) {
unset($form['language']['#options']["$key"]);
}
}
}
unset($form['#after_build']['0']);
}
}
/**
* Implements hook_form_alter().
*/
function i18n_access_form_alter(&$form, &$form_state, $form_id) {
//Configuring translation edit form to limit it to allowed language
if ($form_id == 'i18n_node_select_translation' && !user_access('administer nodes')) {
$perms = i18n_access_load_permissions();
foreach ($form['translations']['nid'] as $language => $translation) {
if (!isset($perms[$language]) && $language != '#tree') {
unset($form['translations']['nid'][$language]);
}
}
foreach ($form['translations']['language'] as $language => $translation) {
if (!isset($perms[$language]) && $language != '#tree') {
unset($form['translations']['language'][$language]);
}
}
foreach ($form['translations']['node'] as $language => $translation) {
if (!isset($perms[$language]) && $language != '#tree') {
unset($form['translations']['node'][$language]);
}
}
}
// Add i18n_access things to user/edit /user/add
if ($form_id == 'user_register_form' || $form_id == 'user_profile_form' ) {
$form['i18n_access'] = array(
'#type' => 'fieldset',
'#title' => t('Translation access'),
'#tree' => 0,
'#access' => user_access('administer users'),
);
$form['i18n_access']['i18n_access'] = array(
'#type' => 'checkboxes',
'#options' => array(I18N_ACCESS_LANGUAGE_NEUTRAL => t('Language neutral')) + locale_language_list('name'),
'#default_value' => i18n_access_load_permissions($form['#user']->uid),
'#description' => t('Select the languages that this user should have permission to create and edit content for.'),
);
}
}
/**
* Wrapper around node_access() with additional checks for language permissions.
*
* @see node_access()
*/
function i18n_access_node_access($node, $op, $account = NULL) {
if (is_object($node)) {
global $user;
// If no user object is supplied, the access check is for the current user.
if (empty($account)) {
$account = $user;
}
// Bypass completely if node_access returns false.
//TODO $access = node_access($node, $op, $account);
/* TODO if (!$access) {
return FALSE;
} */
// This module doesn't deal with view permissions
if ($op == 'view') {
return NODE_ACCESS_IGNORE;
}
// make sure that administrators always have access
if (user_access('administer nodes', $account)) {
return TRUE;
}
$perms = i18n_access_load_permissions($account->uid);
// Make sure to use the language neutral constant if node language is empty
$langcode = $node->language ? $node->language : I18N_ACCESS_LANGUAGE_NEUTRAL;
//return isset($perms[$langcode]) ? (bool) $perms[$langcode] : NODE_ACCESS_DENY;
return isset($perms[$langcode]) ? NODE_ACCESS_ALLOW : NODE_ACCESS_DENY;
}
}
/**
* Implements hook_menu_alter().
*/
function i18n_access_menu_alter(&$items) {
// Replace the translation overview page since we can't hook it.
$items['node/%node/translate']['page callback'] = 'i18n_access_translation_node_overview';
}
function i18n_access_translation_node_overview($node) {
include_once DRUPAL_ROOT . '/includes/language.inc';
if (!empty($node->tnid)) {
// Already part of a set, grab that set.
$tnid = $node->tnid;
$translations = translation_node_get_translations($node->tnid);
}
else {
// We have no translation source nid, this could be a new set, emulate that.
$tnid = $node->nid;
$translations = array($node->language => $node);
}
$type = variable_get('translation_language_type', LANGUAGE_TYPE_INTERFACE);
$header = array(t('Language'), t('Title'), t('Status'), t('Operations'));
//added from i18n/i18n_node/i18n_node.pages.inc function
global $user;
$account = $user;
$perms = i18n_access_load_permissions($account->uid);
//end
// Modes have different allowed languages
foreach (i18n_node_language_list($node) as $langcode => $language_name) {
if ($langcode == LANGUAGE_NONE) {
// Never show language neutral on the overview.
continue;
}
$options = array();
if (isset($translations[$langcode])) {
// Existing translation in the translation set: display status.
// We load the full node to check whether the user can edit it.
$translation_node = node_load($translations[$langcode]->nid);
$path = 'node/' . $translation_node->nid;
$title = i18n_node_translation_link($translation_node->title, $path, $langcode);
if (node_access('update', $translation_node)) {
$text = t('edit');
$path = 'node/' . $translation_node->nid . '/edit';
$options[] = i18n_node_translation_link($text, $path, $langcode);
}
$status = $translation_node->status ? t('Published') : t('Not published');
$status .= $translation_node->translate ? ' - <span class="marker">' . t('outdated') . '</span>' : '';
if ($translation_node->nid == $tnid) {
$language_name = t('<strong>@language_name</strong> (source)', array('@language_name' => $language_name));
}
}
else {
// No such translation in the set yet: help user to create it.
$title = t('n/a');
if (node_access('create', $node)) {
$text = t('add translation');
$path = 'node/add/' . str_replace('_', '-', $node->type);
$query = array('query' => array('translation' => $node->nid, 'target' => $langcode));
//condition added from i18n/i18n_node/i18n_node.pages.inc
if (in_array($langcode, $perms)) {
$options[] = i18n_node_translation_link($text, $path, $langcode, $query);
}
}
$status = t('Not translated');
}
$rows[] = array($language_name, $title, $status, implode(" | ", $options));
}
drupal_set_title(t('Translations of %title', array('%title' => $node->title)), PASS_THROUGH);
$build['translation_node_overview'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
);
if (user_access('administer content translations')) {
$build['translation_node_select'] = drupal_get_form('i18n_node_select_translation', $node, $translations);
}
return $build;
}
/**
* Implements hook_menu().
*/
function i18n_access_menu() {
$items = array();
$items['admin/settings/language/access'] = array(
'title' => 'Access',
'page callback' => 'drupal_get_form',
'page arguments' => array('i18n_access_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
);
return $items;
}
/**
* Admin settings form
*/
function i18n_access_admin_settings() {
$form['i18n_access_languages'] = array(
'#title' => t('Select the default access languages'),
'#type' => 'select',
'#multiple' => 'true',
'#options' => array(I18N_ACCESS_LANGUAGE_NEUTRAL => t('Language neutral')) + locale_language_list('name'),
'#default_value' => variable_get('i18n_access_languages', array()),
'#description' => t("This selection of languages will be connected with the 'access selected languages' permission which you can use to grant a role access to these languages at once.")
);
return system_settings_form($form);
}