You are here

i18n_access.module in Translation Access 6

Same filename and directory in other branches
  1. 7 i18n_access.module

file_description

File

i18n_access.module
View source
<?php

/**
 * @file
 * file_description
 */
define('I18N_ACCESS_LANGUAGE_NEUTRAL', 'NEUTRAL');

/**
 * Implementation of hook_user().
 */
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') {

    // see user_admin_perm_submit()
    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']);
    }
  }
}

/**
 * Load the language permissions for a given user
 */
function i18n_access_load_permissions($account = NULL) {
  static $perms = array();

  // use the global user id if none is passed
  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();
    }
  }

  // adding the default languages if permission has been granted
  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];
}

/**
 * Implementation of hook_perm()
 */
function i18n_access_perm() {
  return array(
    'access selected languages',
  );
}

/**
 * Implementation of hook_form_alter().
 */
function i18n_access_form_alter(&$form, $form_state, $form_id) {

  // Remove inaccessible languages from the select box
  if (isset($form['#id']) && $form['#id'] == 'node-form' && isset($form['language']['#options'])) {

    // don't do it form 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 (empty($perms[$perm_key])) {
          unset($form['language']['#options'][$key]);
        }
      }
    }
  }

  //Configuring locale search form to limit it to domain language
  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]);
      }
    }
  }

  //Configuring translation edit form to limit it to allowed language
  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]);
      }
    }
  }
}

/**
 * Wrapper around node_access() with additional checks for language permissions.
 *
 * @see node_access()
 */
function _i18n_access_node_access($op, $node = NULL, $account = NULL) {
  global $user;

  // If no user object is supplied, the access check is for the current user.
  if (empty($account)) {
    $account = $user;
  }

  // if revisioning is enabled, check revision access
  if (function_exists('_revisioning_node_revision_access')) {
    $access = _revisioning_view_edit_access_callback($op, $node, $account);
  }
  else {
    $access = node_access($op, $node, $account);
  }

  // Bypass completely if access returns false.
  if (!$access) {
    return FALSE;
  }

  // This module doesn't deal with view permissions
  if ($op == 'view') {
    return TRUE;
  }

  // make sure that administrators always have access
  if (user_access('administer nodes', $account)) {
    return TRUE;
  }
  $perms = i18n_access_load_permissions($account);

  // 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] : FALSE;
}

/**
 * Implementation of hook_menu_alter().
 */
function i18n_access_menu_alter(&$callbacks) {

  // Use _i18n_access_node_access() instead of node_access().
  $callbacks['node/%node/edit']['access callback'] = '_i18n_access_node_access';

  // Replace the translation overview page since we can't hook it.
  $callbacks['node/%node/translate']['page callback'] = 'i18n_access_translation_node_overview';
}

/**
 * Replacement for translation_node_overview().
 */
function i18n_access_translation_node_overview($node) {
  if ($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,
    );
  }
  $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])) {

      // 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[$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 {

      // No such translation in the set yet: help user to create it.
      $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;
}

/**
 * Implementation of hook_menu()
 */
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;
}

/**
 * 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);
}

Functions

Namesort descending Description
i18n_access_admin_settings Admin settings form
i18n_access_form_alter Implementation of hook_form_alter().
i18n_access_load_permissions Load the language permissions for a given user
i18n_access_menu Implementation of hook_menu()
i18n_access_menu_alter Implementation of hook_menu_alter().
i18n_access_perm Implementation of hook_perm()
i18n_access_translation_node_overview Replacement for translation_node_overview().
i18n_access_user Implementation of hook_user().
_i18n_access_node_access Wrapper around node_access() with additional checks for language permissions.

Constants

Namesort descending Description
I18N_ACCESS_LANGUAGE_NEUTRAL @file file_description