View source
<?php
define('I18N_ACCESS_LANGUAGE_NEUTRAL', 'NEUTRAL');
function i18n_access_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'form' && $category == 'account' || $op == 'register') {
$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($account),
'#description' => t('Select the languages that this user should have permission to create and edit content for.'),
);
return $form;
}
elseif (($op == 'insert' || $op == 'submit') && $category == 'account') {
if (isset($edit['i18n_access'])) {
db_query('DELETE FROM {i18n_access} WHERE uid = %d', $account->uid);
$edit['i18n_access'] = array_filter($edit['i18n_access']);
if (count($edit['i18n_access'])) {
db_query("INSERT INTO {i18n_access} (uid, perm) VALUES (%d, '%s')", $account->uid, implode(', ', array_keys($edit['i18n_access'])));
}
unset($edit['i18n_access']);
}
}
}
function i18n_access_load_permissions($account = NULL) {
static $perms = array();
if (!isset($account)) {
$account = $GLOBALS['user'];
}
if (!isset($perms[$account->uid])) {
$perm_string = db_result(db_query('SELECT perm FROM {i18n_access} WHERE uid = %d', $account->uid));
if ($perm_string) {
$perms[$account->uid] = drupal_map_assoc(explode(', ', $perm_string));
}
else {
$perms[$account->uid] = array();
}
}
if (user_access('access selected languages', $account)) {
$perms[$account->uid] = array_merge($perms[$account->uid], drupal_map_assoc(variable_get('i18n_access_languages', array())));
}
return $perms[$account->uid];
}
function i18n_access_perm() {
return array(
'access selected languages',
);
}
function i18n_access_form_alter(&$form, $form_state, $form_id) {
if (isset($form['#id']) && $form['#id'] == 'node-form' && isset($form['language']['#options'])) {
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 (empty($perms[$perm_key])) {
unset($form['language']['#options'][$key]);
}
}
}
}
if ($form['#id'] == 'locale-translate-seek-form' && !user_access('administer nodes') && user_access('translate interface')) {
$perms = i18n_access_load_permissions();
foreach ($form['search']['language']['#options'] as $key => $value) {
$perm_key = $key == '' ? I18N_ACCESS_LANGUAGE_NEUTRAL : $key;
if (!isset($perms[$perm_key])) {
unset($form['search']['language']['#options'][$key]);
}
}
}
if ($form['#id'] == 'locale-translate-edit-form' && !user_access('administer nodes') && user_access('translate interface')) {
$perms = i18n_access_load_permissions();
foreach ($form['translations'] as $language => $translation) {
if (!isset($perms[$language]) && $language != '#tree') {
unset($form['translations'][$language]);
}
}
}
}
function _i18n_access_node_access($op, $node = NULL, $account = NULL) {
global $user;
if (empty($account)) {
$account = $user;
}
if (function_exists('_revisioning_node_revision_access')) {
$access = _revisioning_view_edit_access_callback($op, $node, $account);
}
else {
$access = node_access($op, $node, $account);
}
if (!$access) {
return FALSE;
}
if ($op == 'view') {
return TRUE;
}
if (user_access('administer nodes', $account)) {
return TRUE;
}
$perms = i18n_access_load_permissions($account);
$langcode = $node->language ? $node->language : I18N_ACCESS_LANGUAGE_NEUTRAL;
return isset($perms[$langcode]) ? (bool) $perms[$langcode] : FALSE;
}
function i18n_access_menu_alter(&$callbacks) {
$callbacks['node/%node/edit']['access callback'] = '_i18n_access_node_access';
$callbacks['node/%node/translate']['page callback'] = 'i18n_access_translation_node_overview';
}
function i18n_access_translation_node_overview($node) {
if ($node->tnid) {
$tnid = $node->tnid;
$translations = translation_node_get_translations($node->tnid);
}
else {
$tnid = $node->nid;
$translations = array(
$node->language => $node,
);
}
$header = array(
t('Language'),
t('Title'),
t('Status'),
t('Operations'),
);
foreach (language_list() as $language) {
$options = array();
$language_name = $language->name;
if (isset($translations[$language->language])) {
$translation_node = node_load($translations[$language->language]->nid);
$title = l($translation_node->title, 'node/' . $translation_node->nid);
if (_i18n_access_node_access('update', $translation_node)) {
$options[] = l(t('edit'), "node/{$translation_node->nid}/edit");
}
$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 {
$title = t('n/a');
$translation_node = drupal_clone($node);
$translation_node->language = $language->language;
if (_i18n_access_node_access('create', $translation_node)) {
$options[] = l(t('add translation'), 'node/add/' . str_replace('_', '-', $node->type), array(
'query' => "translation={$node->nid}&language={$language->language}",
));
}
$status = t('Not translated');
}
$rows[] = array(
$language_name,
$title,
$status,
implode(" | ", $options),
);
}
drupal_set_title(t('Translations of %title', array(
'%title' => $node->title,
)));
$output = theme('table', $header, $rows);
if (user_access('administer translations')) {
$output .= drupal_get_form('i18n_node_select_translation', $node, $translations);
}
return $output;
}
function i18n_access_menu() {
$items = array();
$items['admin/settings/language/access'] = array(
'title' => t('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;
}
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);
}