123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- <?php
- function i18n_access_user_insert(&$edit, &$account, $category = NULL) {
- i18n_access_user_update($edit, $account, $category);
- }
- function i18n_access_user_update(&$edit, &$account, $category = NULL) {
- if ($category == 'account') {
-
- 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']);
- }
- }
- }
- function i18n_access_user_delete($account) {
- db_delete('i18n_access')
- ->condition('uid', $account->uid)
- ->execute();
- }
- function i18n_access_load_permissions($uid = NULL) {
- $perms = &drupal_static(__FUNCTION__);
-
- 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();
- }
- }
-
- 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];
- }
- function i18n_access_permission() {
- return array(
- 'access selected languages' => array(
- 'title' => t('Access selected languages'),
- 'description' => t('This permission gives this role edit/delete access to all content which are in the <a href="!url" target="_blank">selected language</a>. View/create access needs a different access level.', array('!url' => url('admin/config/regional/language/access'))),
- 'restrict access' => TRUE,
- ),
- );
- }
- function i18n_access_form_node_form_alter(&$form) {
- $form['#after_build'][] = '_i18n_access_form_node_form_alter';
- }
- function _i18n_access_form_node_form_alter($form, &$form_state) {
- if (isset($form['language']['#options']) && !user_access('bypass node access')) {
- $perms = i18n_access_load_permissions();
- foreach ($form['language']['#options'] as $key => $value) {
- if (empty($perms[$key])) {
- unset($form['language']['#options'][$key]);
- }
- }
- }
- return $form;
- }
- function i18n_access_form_alter(&$form, &$form_state, $form_id) {
-
- if ($form_id == 'i18n_node_select_translation' && !user_access('bypass node access')) {
- $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]);
- }
- }
- }
-
- 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(LANGUAGE_NONE => t('Language neutral')) + locale_language_list('name'),
- '#default_value' => i18n_access_load_permissions($form['#user']->uid),
- '#description' => t('The user get edit, delete access to all content which are in this enabled languages. Create, view access needs a different access level.'),
- );
- }
- }
- function i18n_access_node_access($node, $op, $account = NULL, $langcode = NULL) {
-
- if (is_object($node)) {
-
- $permissions = i18n_access_load_permissions($user);
- if (user_access('site administrator', $account)) {
- return TRUE;
- }
-
- elseif ($langcode == NULL) {
- global $language;
- $langcode = $language->language;
- switch ($op) {
- case 'view':
- return NODE_ACCESS_ALLOW;
- break;
- case 'update':
- if (empty($permissions[$langcode])) {
- return NODE_ACCESS_DENY;
- }
- else {
- return NODE_ACCESS_ALLOW;
- }
- break;
- case 'create':
- if (empty($permissions[$langcode])) {
- return NODE_ACCESS_DENY;
- }
- else {
- return NODE_ACCESS_ALLOW;
- }
- break;
- }
- }
-
- else {
- switch ($op) {
- case 'view':
- return TRUE;
- break;
- case 'update':
- if (empty($permissions[$langcode])) {
- return FALSE;
- }
- else {
- return TRUE;
- }
- break;
- case 'create':
- if (empty($permissions[$langcode])) {
- return FALSE;
- }
- else {
- return TRUE;
- }
- break;
- }
- }
- }
- }
- function i18n_access_node_menu_alter(&$items) {
-
- $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';
-
- include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'i18n_node') . '/i18n_node.pages.inc';
-
-
- $available_translations = $node->translations->data;
-
- foreach ($available_translations as $key => $value) {
- $translations[$key] = $node;
- }
- $header = array(t('Language'), t('Title'), t('Status'), t('Operations'));
- $rows = array();
- global $user;
- $perms = i18n_access_load_permissions($user->uid);
-
-
- foreach (i18n_node_language_list($node) as $langcode => $language_name) {
- if ($langcode == LANGUAGE_NONE) {
-
- continue;
- }
- $options = array();
- if (isset($translations[$langcode])) {
-
-
- $translation_node = node_load($translations[$langcode]->nid);
- $path = 'node/' . $translation_node->nid;
-
- if (isset($translation_node->title_field) && isset($translation_node->title_field[$langcode])) {
- $title = i18n_node_translation_link($translation_node->title_field[$langcode][0]['value'], $path, $langcode);
- }
- else {
- $title = i18n_node_translation_link($translation_node->title, $path, $langcode);
- }
- if (i18n_access_node_access($translation_node, 'update', $user, $langcode)) {
- $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 ? ' - ' . t('outdated') . '' : '';
- if ($translation_node->nid == $tnid) {
- $language_name = t('@language_name (source)', array('@language_name' => $language_name));
- }
- }
- else {
-
- $title = t('n/a');
- if (node_access('create', $node->type) && (!empty($perms[$langcode]) || user_access('bypass node access'))) {
- $text = t('add translation');
- $path = 'node/add/' . str_replace('_', '-', $node->type);
- $query = array('query' => array('translation' => $node->nid, 'target' => $langcode));
- $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;
- }
- function i18n_access_menu() {
- $items['admin/config/regional/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;
- }
- function i18n_access_menu_alter(&$items, $backup) {
- if (isset($backup['node'])) {
- $item = $backup['node'];
-
- $callback['page callback'] = $item['page callback'];
- $callback['file'] = $item['file'];
- $callback['module'] = $item['module'];
- $access_arguments = array_merge(array(1, $item['access callback']), $item['access arguments']);
- }
- else {
- $access_arguments = array(1);
- }
-
- $items['node/%node/translate']['page callback'] = 'i18n_access_translation_node_overview';
-
- $items['node/%node/translate']['page arguments'] = array(1);
-
- $items['node/%node/translate']['access arguments'] = $access_arguments;
-
- $items['node/%node/translate']['file'] = 'i18n_access.module';
-
- $items['node/%node/translate']['module'] = 'i18n_access';
- }
- function i18n_access_module_implements_alter(&$implementations, $hook) {
- switch ($hook) {
- case 'menu_alter':
-
- $group = $implementations['i18n_access'];
- unset($implementations['i18n_access']);
- $implementations['i18n_access'] = $group;
- break;
- }
- }
- function i18n_access_admin_settings($form) {
- $form['i18n_access_languages'] = array(
- '#title' => t('Select the default access languages'),
- '#type' => 'select',
- '#multiple' => TRUE,
- '#options' => array(LANGUAGE_NONE => 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);
- }
|