From e24eec56e7ba6e3059f912073064a68a198b0097 Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Tue, 13 Dec 2016 12:52:01 +0100 Subject: [PATCH] materio translator : Node overview DONE --- .../materiobasemod/materio_translator.info | 2 +- .../materiobasemod/materio_translator.module | 322 +++++++++++++++--- 2 files changed, 283 insertions(+), 41 deletions(-) diff --git a/sites/all/modules/gui/materiobasemod/materio_translator.info b/sites/all/modules/gui/materiobasemod/materio_translator.info index 5e74f1b2..972f780b 100644 --- a/sites/all/modules/gui/materiobasemod/materio_translator.info +++ b/sites/all/modules/gui/materiobasemod/materio_translator.info @@ -1,5 +1,5 @@ name = Materio Tranlator -description = "Materio translator dedicated module" +description = "Materio translator dedicated module, based on i18n_access" ; Core version (required) core = 7.x diff --git a/sites/all/modules/gui/materiobasemod/materio_translator.module b/sites/all/modules/gui/materiobasemod/materio_translator.module index 61df636e..bd374b28 100644 --- a/sites/all/modules/gui/materiobasemod/materio_translator.module +++ b/sites/all/modules/gui/materiobasemod/materio_translator.module @@ -1,61 +1,303 @@ 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(); +} + /** - * Implements hook_init(). + * Load the language permissions for a given user */ -// function materio_translator_init() { -// drupal_add_js(drupal_get_path('module', 'materio_translator').'/js/materio_translator.js'); -// } +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() { -// $perms = array( -// 'add showroom localisation' => array( -// 'title' => t('add showroom localisation'), -// 'description' => t('add showroom localisation'), -// ), -// ); -// -// return $perms; -// } +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 selected language. View/create access needs a different access level.', array('!url' => url('admin/config/regional/language/access'))), + 'restrict access' => TRUE, + ), + ); +} /** * Implements hook_form_alter(). */ -function DISABLED_materio_translator_form_alter(&$form, &$form_state, $form_id) { - // act only on node edit form - if(isset($form['#node_edit_form'])){ - // dsm($form_id, 'form_id'); - // dsm($form, 'form'); - // dsm($form_state, 'form_state'); +function materio_translator_form_alter(&$form, &$form_state, $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 users'), + ); + $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.'), + ); + } } + + +/** + * 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; +} + /** - * Implements hook_field_access(). + * Admin settings form. */ -function DISABLED_materio_translator_field_access($op, $field, $entity_type, $entity, $account) { - // dsm($op,'op'); - // dsm($entity_type,'entity_type'); - // if($op == "edit" && $entity_type == 'node'){ - // if($field['translatable'] == 0){ - // if($field['field_permissions']['type'] == FIELD_PERMISSIONS_CUSTOM){ - // dsm($field,$field['field_name']); - // // dsm($entity,'entity'); - // // dsm($account,'account'); - // - // // check if field permissions are handled by field_permissions module - // - // // return FALSE; - // } - // } - // } - // - // return TRUE; +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["node/%node/translate"]['page callback'] = "materio_translator_translation_node_overview"; + // add our own page raguments + $items["node/%node/translate"]['page arguments'] = $pargs; + /* + original $item : + Array( + [title] => Translate + [page callback] => entity_translation_overview + [page arguments] => Array + ( + [0] => node + [1] => 1 + [2] => Array + ( + [page callback] => i18n_node_translation_overview + [file] => i18n_node.pages.inc + [module] => i18n_node + ) + + [3] => 1 + ) + + [type] => 132 + [weight] => 1 + [context] => 3 + [access callback] => entity_translation_node_tab_access + [access arguments] => Array + ( + [0] => 1 + [1] => _translation_tab_access + [2] => 1 + ) + + [file] => entity_translation.admin.inc + [module] => entity_translation + ) + */ + } + } +} + + +/** + * 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($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']); + } }