Bachir Soussi Chiadmi 0d940c99db updated features
2016-12-20 01:15:04 +01:00

583 lines
20 KiB
Plaintext

<?php
// __ __ __ __ _
// / / / /_______ __________ ________ / /_/ /_(_)___ ____ ______
// / / / / ___/ _ \/ ___/ ___/ / ___/ _ \/ __/ __/ / __ \/ __ `/ ___/
// / /_/ (__ ) __/ / (__ ) (__ ) __/ /_/ /_/ / / / / /_/ (__ )
// \____/____/\___/_/ /____/ /____/\___/\__/\__/_/_/ /_/\__, /____/
// /____/
/**
* Implements hook_user_insert().
*/
function materio_translator_user_insert(&$edit, &$account, $category = NULL) {
materio_translator_user_update($edit, $account, $category);
}
/**
* Implements hook_user_update().
*/
function materio_translator_user_update(&$edit, &$account, $category = NULL) {
if ($category == 'account') {
// see user_admin_perm_submit()
if (isset($edit['materio_translator'])) {
db_delete('materio_translator')
->condition('uid', $account->uid)
->execute();
$edit['materio_translator'] = array_filter($edit['materio_translator']);
if (count($edit['materio_translator'])) {
db_insert('materio_translator')
->fields(array(
'uid' => $account->uid,
'perm' => implode(', ', array_keys($edit['materio_translator'])),
))->execute();
}
unset($edit['materio_translator']);
}
}
}
/**
* Implements hook_user_delete().
*/
function materio_translator_user_delete($account) {
db_delete('materio_translator')
->condition('uid', $account->uid)
->execute();
}
// __ __ __
// / / / /__ / /___ ___ __________
// / /_/ / _ \/ / __ \/ _ \/ ___/ ___/
// / __ / __/ / /_/ / __/ / (__ )
// /_/ /_/\___/_/ .___/\___/_/ /____/
// /_/
/**
* Load the language permissions for a given user
*/
function materio_translator_load_permissions($uid = NULL) {
$perms = &drupal_static(__FUNCTION__);
// 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 {materio_translator} 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('materio_translator_languages', array())));
}
return $perms[$uid];
}
// ________ __ __
// / ____/ /___ / /_ ____ _/ / ____ ___ _________ ___ _____
// / / __/ / __ \/ __ \/ __ `/ / / __ \/ _ \/ ___/ __ `__ \/ ___/
// / /_/ / / /_/ / /_/ / /_/ / / / /_/ / __/ / / / / / / (__ )
// \____/_/\____/_.___/\__,_/_/ / .___/\___/_/ /_/ /_/ /_/____/
// /_/
/**
* Implements hook_permission().
*/
function materio_translator_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,
),
'administer user translation language access' => array(
'title' => t('Administer user translation language access'),
'description' => t('administer user translation language access'),
'restrict access' => TRUE,
),
'access_translations_overview' => array(
'title' => t('Access translations overview')
),
'access_translation_table_fields' => array(
'title' => t('Access translation Fields table')
),
'access_translation_table_content_type' => array(
'title' => t('Access translation Content types table')
),
'access_translation_table_menu' => array(
'title' => t('Access translation menu table')
),
'access_translation_table_taxonomy' => array(
'title' => t('Access translation taxonomy table')
),
'translate_strings' => array(
'title' => t('Translate strings')
),
'delete_strings' => array(
'title' => t('Delete strings')
),
'refresh_strings' => array(
'title' => t('Refresh strings')
),
'import_translations' => array(
'title' => t('Import translations')
),
'export_translations' => array(
'title' => t('Import translations')
),
'update_modules_translations' => array(
'title' => t('Update modules translations')
),
);
}
/**
* Implements hook_form_alter().
*/
function materio_translator_form_alter(&$form, &$form_state, $form_id) {
// dsm($form_id);
// Add materio_translator things to user/edit /user/add
if ($form_id == 'user_register_form' || $form_id == 'user_profile_form' ) {
// dsm($form_id, 'form_id');
// dsm($form, 'form');
// dsm($form_state, 'form_state');
$form['materio_translator'] = array(
'#type' => 'fieldset',
'#title' => t('Translation access'),
'#tree' => 0,
'#access' => user_access('administer user translation language access'),
);
$form['materio_translator']['materio_translator'] = array(
'#type' => 'checkboxes',
'#options' => array(LANGUAGE_NONE => t('Language neutral')) + locale_language_list('name'),
'#default_value' => materio_translator_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.'),
);
}
// limit fields in string translation
if($form_id == 'i18n_string_locale_translate_edit_form'){
// dsm($form, 'form');
// dsm($form_state, 'form_state');
global $user;
$perms = materio_translator_load_permissions($user->uid);
// dsm($perms);
foreach ($form['translations'] as $langcode => $item) {
// disable field if langcode not in perms
if(!in_array($langcode, $perms) && isset($form['translations'][$langcode])){
$form['translations'][$langcode]['#disabled'] = true;
}
}
}
// limit fields in string translation
if($form_id == 'field_translation_table_form'
|| $form_id == "node_translation_table_nodetype_form"
|| $form_id == "menu_translation_table_menu_form"
|| $form_id == "taxonomy_translation_table_taxonomy_form"){
// dsm($form, 'form');
// dsm($form_state, 'form_state');
global $user;
$perms = materio_translator_load_permissions($user->uid);
// dsm($perms);
foreach ($form['filtered_form']['strings'] as $id => $row) {
foreach ($row as $langcode => $field) {
// disable field if langcode not in perms
if(!in_array($langcode, $perms)){
$form['filtered_form']['strings'][$id][$langcode]['#disabled'] = true;
}
}
}
}
}
/**
* Implements hook_menu().
*/
function materio_translator_menu() {
$items['admin/config/regional/language/access'] = array(
'title' => 'Access',
'page callback' => 'drupal_get_form',
'page arguments' => array('materio_translator_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
);
return $items;
}
/**
* Admin settings form.
*/
function materio_translator_admin_settings($form) {
$form['materio_translator_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('materio_translator_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);
}
// __ ___ ___ ____
// / |/ /__ ____ __ __ / | / / /____ _____
// / /|_/ / _ \/ __ \/ / / / / /| | / / __/ _ \/ ___/
// / / / / __/ / / / /_/ / / ___ |/ / /_/ __/ /
// /_/ /_/\___/_/ /_/\__,_/ /_/ |_/_/\__/\___/_/
/**
* Implements hook_menu_alter().
*/
function materio_translator_menu_alter(&$items) {
// due to hook_module_implementation_alter calling entity translation last, we can't change the callback here, i've done it in entity_translation.node.inc - consider calling it here?
// $items['node/%node/translate']['page callback'] = 'materio_translator_translation_node_overview';
//
foreach ($items as $path => $item) {
// if(strpos($path, 'node/%node/translate') !== false){
if(preg_match('/^node\/%node\/translate$/', $path)){
// dsm($path);
// dsm($item);
// create new page arguments
$pargs = $item['page arguments'];
// add page call back for entity_translation call
// and memorize old page callback (i18n) for entity_translation call args
$pargs[2]['page callback'] = $item['page callback'];
$pargs[2]['file'] = $item['file'];
$pargs[2]['module'] = $item['module'];
$pargs[2]['page arguments'] = $item['page arguments'];
// dsm($pargs, "pargs");
// change page callback for our own function
$items[$path]['page callback'] = "materio_translator_translation_node_overview";
// add our own page raguments
$items[$path]['page arguments'] = $pargs;
}
// translation edit link
if(preg_match('/^node\/%node\/edit\/%entity_translation_language$/', $path)){
// dsm($item, $path);
// create new page arguments
$access_args = $item['access arguments'];
// dsm($access_args, 'access_args avt');
// add page call back for entity_translation call
// and memorize old page callback (i18n) for entity_translation call args
$access_args[3]['access callback'] = $item['access callback'];
$access_args[3]['file'] = 'entity_translation.node.inc';
$access_args[3]['module'] = 'entity_translation';
$access_args[3]['access arguments'] = $item['access arguments'];
// dsm($access_args, "access_args");
// change access callback for our own function
$items[$path]['access callback'] = 'materio_translator_node_edit_access';
// add our own page raguments
$items[$path]['access arguments'] = $access_args;
// dsm($access_args, 'access_args apr');
// dsm($items[$path], $path);
}
// translation add link
// if(preg_match('/^node\/%node\/edit\/add\/%entity_translation_language/', $path)){
// dsm($item, $path);
// }
// translation add link
if(preg_match('/^admin\/config\/regional\/translate$/', $path)){
$items[$path]['access arguments'] = array('access_translations_overview');
}
if(preg_match('/^admin\/config\/regional\/translate\/table$/', $path)){
$items[$path]['access arguments'] = array('access_translation_table_fields');
}
if(preg_match('/^admin\/config\/regional\/translate\/table\/nodetype$/', $path)){
$items[$path]['access arguments'] = array('access_translation_table_content_type');
}
if(preg_match('/^admin\/config\/regional\/translate\/table\/menu$/', $path)){
$items[$path]['access arguments'] = array('access_translation_table_menu');
}
if(preg_match('/^admin\/config\/regional\/translate\/table\/taxonomy$/', $path)){
$items[$path]['access arguments'] = array('access_translation_table_taxonomy');
}
if(preg_match('/^admin\/config\/regional\/translate\/translate$/', $path)){
$items[$path]['access arguments'] = array('translate_strings');
}
if(preg_match('/^admin\/config\/regional\/translate\/import$/', $path)){
$items[$path]['access arguments'] = array('import_translations');
}
if(preg_match('/^admin\/config\/regional\/translate\/i18n_string$/', $path)){
$items[$path]['access arguments'] = array('refresh_strings');
}
if(preg_match('/^admin\/config\/regional\/translate\/update$/', $path)){
$items[$path]['access arguments'] = array('update_modules_translations');
}
if(preg_match('/^admin\/config\/regional\/translate\/export$/', $path)){
$items[$path]['access arguments'] = array('export_translations');
}
if(preg_match('/^admin\/config\/regional\/translate\/delete$/', $path)){
$items[$path]['access arguments'] += array("delete_strings");
}
}
}
function materio_translator_node_edit_access($entity_type, $entity, $langcode, $callback = null){
$args = func_get_args();
// dsm($args, '1 -- materio_translator_node_edit_access args');
// dsm($entity_type, "entity_type");
// dsm($entity, "entity");
// dsm($langcode, 'langcode');
// dsm($callback, "2 -- callback");
if (module_exists($callback['module'])) {
// dsm('module_exists');
if (isset($callback['file'])) {
$path = isset($callback['file path']) ? $callback['file path'] : drupal_get_path('module', $callback['module']);
// dsm($path, 'path');
require_once DRUPAL_ROOT . '/' . $path . '/' . $callback['file'];
}
// dsm($callback['access callback'], 'access callback');
$callback['access arguments'][1] = $entity;
$callback['access arguments'][2] = $langcode;
$callback['access arguments'][5] = $entity;
// dsm($callback['access arguments'], "callback['access arguments']");
$callbackaccess = call_user_func_array($callback['access callback'], $callback['access arguments']);
// dsm($callbackaccess, '3 -- callbackaccess');
if($callbackaccess){
global $user;
$perms = materio_translator_load_permissions($user->uid);
// dsm($perms, '4 -- perms');
// remove link if langcode not in perms
if(in_array($langcode, $perms)){
return true;
}
}
return false;
}
// $callbackaccess = call_user_func_array($callback['access callback'], $callback['access arguments']);
// return $callbackaccess;
return true;
}
// _ __ __ _
// / | / /___ ____/ /__ ____ _ _____ ______ __(_)__ _ __
// / |/ / __ \/ __ / _ \ / __ \ | / / _ \/ ___/ | / / / _ \ | /| / /
// / /| / /_/ / /_/ / __/ / /_/ / |/ / __/ / | |/ / / __/ |/ |/ /
// /_/ |_/\____/\__,_/\___/ \____/|___/\___/_/ |___/_/\___/|__/|__/
/**
* Implements hook_module_implements_alter().
*/
function materio_translator_module_implements_alter(&$implementations, $hook) {
switch ($hook) {
case 'menu_alter':
// Move our hook_menu_alter implementation to the end of the list.
$group = $implementations['materio_translator'];
unset($implementations['materio_translator']);
$implementations['materio_translator'] = $group;
break;
}
}
/**
* Most logic comes from translation/i18n_node module.
*
* We removes here only the "add translation" links for languages which are not your selected language.
*
* @see translation_node_overview
* @see i18n_node_translation_overview
*
* @param object $node
*
* @return array.
*/
function materio_translator_translation_node_overview($entity_type, $entity, $callback = NULL) {
// dsm('materio_translator_translation_node_overview');
// dsm($entity_type, "entity_type");
// dsm($entity, "entity");
// dsm($callback, "callback");
// first call entity_translation original callback
// we retrieve the original build object of translation overview
if ($callback) {
$callback['page arguments'][1] = $entity;
$build = materio_translator_overview_callback($callback);
// dsm($build, "build");
global $user;
$perms = materio_translator_load_permissions($user->uid);
// dsm($perms, 'perms');
$i = 0;
foreach ($build['entity_translation_overview']['#rows'] as $row) {
// retrieve teh translation link
$link = $row['data'][4];
// dsm($link, "link");
// retrieve the langcode from link
preg_match('/xml:lang="([^"]+)"/', $link, $matches);
$langcode = $matches[1];
// dsm($langcode, 'langcode');
// remove link if langcode not in perms
if(!in_array($langcode, $perms)){
$build['entity_translation_overview']['#rows'][$i]['data'][4] = "";
}
$i++;
}
}
return $build;
}
/**
* Calls the appropriate translation overview callback.
*/
function materio_translator_overview_callback($callback) {
if (module_exists($callback['module'])) {
if (isset($callback['file'])) {
$path = isset($callback['file path']) ? $callback['file path'] : drupal_get_path('module', $callback['module']);
require_once DRUPAL_ROOT . '/' . $path . '/' . $callback['file'];
}
return call_user_func_array($callback['page callback'], $callback['page arguments']);
}
}
// _ __ __ __
// / | / /___ ____/ /__ / /___ _____ ____ ___ ______ _____ ____
// / |/ / __ \/ __ / _ \ / / __ `/ __ \/ __ `/ / / / __ `/ __ `/ _ \
// / /| / /_/ / /_/ / __/ / / /_/ / / / / /_/ / /_/ / /_/ / /_/ / __/
// /_/ |_/\____/\__,_/\___/ __/\__,_/_/ /_/\__, /\__,_/\__,_/\__, /\___/
// ________ / /__ _____/ /_(_)___ ___/____/ /____/
// / ___/ _ \/ / _ \/ ___/ __/ / __ \/ __ \
// (__ ) __/ / __/ /__/ /_/ / /_/ / / / /
// /____/\___/_/\___/\___/\__/_/\____/_/ /_/
/**
* Implements hook_menu_local_tasks_alter().
*/
function materio_translator_menu_local_tasks_alter(&$data, $router_item, $root_path) {
// dsm($data, 'data');
global $user;
$perms = materio_translator_load_permissions($user->uid);
foreach ($data['tabs'] as $t => $tab) {
foreach ($tab['output'] as $l => $link) {
if($link['#language_tab']){
// dsm($link, $link["#link"]["title"]);
$langcode = $link["#link"]['localized_options']['language']->language;
if(!in_array($langcode, $perms)){
unset($data['tabs'][$t]['output'][$l]);
}
// dsm($data['tabs'][$t]['output'][$l]['#link']);
}
}
}
}
/**
* Implements hook_form_node_form_alter().
*/
function materio_translator_form_node_form_alter(&$form) {
$form['#after_build'][] = '_materio_translator_form_node_form_alter';
}
/**
* Unset's languages from language options if user does not have permission to
* use.
*
* @param $form
* @param $form_state
* @return mixed
*/
function _materio_translator_form_node_form_alter($form, &$form_state) {
if (isset($form['language']['#options']) && !user_access('bypass node access')) {
$perms = materio_translator_load_permissions();
foreach ($form['language']['#options'] as $key => $value) {
if (empty($perms[$key])) {
unset($form['language']['#options'][$key]);
}
}
}
return $form;
}
function materio_translator_node_tab_access(){
$args = func_get_args();
// dsm($args, '1 -- materio_translator_node_tab_access args');
// dsm($entity_type, "entity_type");
// dsm($entity, "entity");
// dsm($langcode, 'langcode');
// dsm($callback, "2 -- callback");
/*
if (module_exists($callback['module'])) {
// dsm('module_exists');
if (isset($callback['file'])) {
$path = isset($callback['file path']) ? $callback['file path'] : drupal_get_path('module', $callback['module']);
// dsm($path, 'path');
require_once DRUPAL_ROOT . '/' . $path . '/' . $callback['file'];
}
// dsm($callback['access callback'], 'access callback');
$callback['access arguments'][1] = $entity;
$callback['access arguments'][2] = $langcode;
$callback['access arguments'][5] = $entity;
// dsm($callback['access arguments'], "callback['access arguments']");
$callbackaccess = call_user_func_array($callback['access callback'], $callback['access arguments']);
// dsm($callbackaccess, '3 -- callbackaccess');
if($callbackaccess){
global $user;
$perms = materio_translator_load_permissions($user->uid);
// dsm($perms, '4 -- perms');
// remove link if langcode not in perms
if(in_array($langcode, $perms)){
return true;
}
}
return false;
}
// $callbackaccess = call_user_func_array($callback['access callback'], $callback['access arguments']);
// return $callbackaccess;
*/
return true;
}