i18n_access.module 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. /**
  3. * @file
  4. * file_description
  5. */
  6. define('I18N_ACCESS_LANGUAGE_NEUTRAL', 'NEUTRAL');
  7. /**
  8. * Implements hook_user_insert().
  9. */
  10. function i18n_access_user_insert(&$edit, &$account, $category = NULL) {
  11. if ($category == 'account') {
  12. // see user_admin_perm_submit()
  13. if (isset($edit['i18n_access'])) {
  14. db_delete('i18n_access')
  15. ->condition('uid', $account->uid)
  16. ->execute();
  17. $edit['i18n_access'] = array_filter($edit['i18n_access']);
  18. if (count($edit['i18n_access'])) {
  19. db_insert('i18n_access')
  20. ->fields(array(
  21. 'uid' => $account->uid,
  22. 'perm' => implode(', ', array_keys($edit['i18n_access'])),
  23. ))->execute();
  24. }
  25. unset($edit['i18n_access']);
  26. }
  27. }
  28. }
  29. /**
  30. * Implements hook_user_update().
  31. */
  32. function i18n_access_user_update(&$edit, &$account, $category = NULL) {
  33. if ($category == 'account') {
  34. // see user_admin_perm_submit()
  35. if (isset($edit['i18n_access'])) {
  36. db_delete('i18n_access')
  37. ->condition('uid', $account->uid)
  38. ->execute();
  39. $edit['i18n_access'] = array_filter($edit['i18n_access']);
  40. if (count($edit['i18n_access'])) {
  41. db_insert('i18n_access')
  42. ->fields(array(
  43. 'uid' => $account->uid,
  44. 'perm' => implode(', ', array_keys($edit['i18n_access'])),
  45. ))->execute();
  46. }
  47. unset($edit['i18n_access']);
  48. }
  49. }
  50. }
  51. /**
  52. * Load the language permissions for a given user
  53. */
  54. function i18n_access_load_permissions($uid = NULL) {
  55. static $perms = array();
  56. // use the global user id if none is passed
  57. if (!isset($uid)) {
  58. $uid = $GLOBALS['user']->uid;
  59. $account = NULL;
  60. }
  61. else {
  62. $account = user_load($uid);
  63. }
  64. if (!isset($perms[$uid])) {
  65. $perm_string = db_query('SELECT perm FROM {i18n_access} WHERE uid = :uid', array(':uid' => $uid))->fetchField();
  66. if ($perm_string) {
  67. $perms[$uid] = drupal_map_assoc(explode(', ', $perm_string));
  68. }
  69. else {
  70. $perms[$uid] = array();
  71. }
  72. }
  73. // adding the default languages if permission has been granted
  74. if (user_access('access selected languages', $account)) {
  75. $perms[$uid] = array_merge($perms[$uid], drupal_map_assoc(variable_get('i18n_access_languages', array())));
  76. }
  77. return $perms[$uid];
  78. }
  79. /**
  80. * Implements hook_permission().
  81. */
  82. function i18n_access_permission() {
  83. return array(
  84. 'access selected languages' => array(
  85. 'title' => t('Access selected languages'),
  86. 'description' => t('access selected languages.'),
  87. ),
  88. );
  89. }
  90. /**
  91. * Implements hook_form_node_form_alter().
  92. */
  93. function i18n_access_form_node_form_alter(&$form, &$form_state, $form_id) {
  94. if (isset($form['language']['#options'])) {
  95. // Remove inaccessible languages from the select box
  96. // don't do it for admininstrators
  97. if (!user_access('administer nodes')) {
  98. $perms = i18n_access_load_permissions();
  99. foreach ($form['language']['#options'] as $key => $value) {
  100. $perm_key = ($key == '') ? I18N_ACCESS_LANGUAGE_NEUTRAL : $key;
  101. if ($key!='en' && empty($perms[$perm_key])) {
  102. unset($form['language']['#options']["$key"]);
  103. }
  104. }
  105. }
  106. unset($form['#after_build']['0']);
  107. }
  108. }
  109. /**
  110. * Implements hook_form_alter().
  111. */
  112. function i18n_access_form_alter(&$form, &$form_state, $form_id) {
  113. //Configuring translation edit form to limit it to allowed language
  114. if ($form_id == 'i18n_node_select_translation' && !user_access('administer nodes')) {
  115. $perms = i18n_access_load_permissions();
  116. foreach ($form['translations']['nid'] as $language => $translation) {
  117. if (!isset($perms[$language]) && $language != '#tree') {
  118. unset($form['translations']['nid'][$language]);
  119. }
  120. }
  121. foreach ($form['translations']['language'] as $language => $translation) {
  122. if (!isset($perms[$language]) && $language != '#tree') {
  123. unset($form['translations']['language'][$language]);
  124. }
  125. }
  126. foreach ($form['translations']['node'] as $language => $translation) {
  127. if (!isset($perms[$language]) && $language != '#tree') {
  128. unset($form['translations']['node'][$language]);
  129. }
  130. }
  131. }
  132. // Add i18n_access things to user/edit /user/add
  133. if ($form_id == 'user_register_form' || $form_id == 'user_profile_form' ) {
  134. $form['i18n_access'] = array(
  135. '#type' => 'fieldset',
  136. '#title' => t('Translation access'),
  137. '#tree' => 0,
  138. '#access' => user_access('administer users'),
  139. );
  140. $form['i18n_access']['i18n_access'] = array(
  141. '#type' => 'checkboxes',
  142. '#options' => array(I18N_ACCESS_LANGUAGE_NEUTRAL => t('Language neutral')) + locale_language_list('name'),
  143. '#default_value' => i18n_access_load_permissions($form['#user']->uid),
  144. '#description' => t('Select the languages that this user should have permission to create and edit content for.'),
  145. );
  146. }
  147. }
  148. /**
  149. * Wrapper around node_access() with additional checks for language permissions.
  150. *
  151. * @see node_access()
  152. */
  153. function i18n_access_node_access($node, $op, $account = NULL) {
  154. if (is_object($node)) {
  155. global $user;
  156. // If no user object is supplied, the access check is for the current user.
  157. if (empty($account)) {
  158. $account = $user;
  159. }
  160. // Bypass completely if node_access returns false.
  161. //TODO $access = node_access($node, $op, $account);
  162. /* TODO if (!$access) {
  163. return FALSE;
  164. } */
  165. // This module doesn't deal with view permissions
  166. if ($op == 'view') {
  167. return NODE_ACCESS_IGNORE;
  168. }
  169. // make sure that administrators always have access
  170. if (user_access('administer nodes', $account)) {
  171. return TRUE;
  172. }
  173. $perms = i18n_access_load_permissions($account->uid);
  174. // Make sure to use the language neutral constant if node language is empty
  175. $langcode = $node->language ? $node->language : I18N_ACCESS_LANGUAGE_NEUTRAL;
  176. //return isset($perms[$langcode]) ? (bool) $perms[$langcode] : NODE_ACCESS_DENY;
  177. return isset($perms[$langcode]) ? NODE_ACCESS_ALLOW : NODE_ACCESS_DENY;
  178. }
  179. }
  180. /**
  181. * Implements hook_menu_alter().
  182. */
  183. function i18n_access_menu_alter(&$items) {
  184. // Replace the translation overview page since we can't hook it.
  185. $items['node/%node/translate']['page callback'] = 'i18n_access_translation_node_overview';
  186. }
  187. function i18n_access_translation_node_overview($node) {
  188. include_once DRUPAL_ROOT . '/includes/language.inc';
  189. if (!empty($node->tnid)) {
  190. // Already part of a set, grab that set.
  191. $tnid = $node->tnid;
  192. $translations = translation_node_get_translations($node->tnid);
  193. }
  194. else {
  195. // We have no translation source nid, this could be a new set, emulate that.
  196. $tnid = $node->nid;
  197. $translations = array($node->language => $node);
  198. }
  199. $type = variable_get('translation_language_type', LANGUAGE_TYPE_INTERFACE);
  200. $header = array(t('Language'), t('Title'), t('Status'), t('Operations'));
  201. //added from i18n/i18n_node/i18n_node.pages.inc function
  202. global $user;
  203. $account = $user;
  204. $perms = i18n_access_load_permissions($account->uid);
  205. //end
  206. // Modes have different allowed languages
  207. foreach (i18n_node_language_list($node) as $langcode => $language_name) {
  208. if ($langcode == LANGUAGE_NONE) {
  209. // Never show language neutral on the overview.
  210. continue;
  211. }
  212. $options = array();
  213. if (isset($translations[$langcode])) {
  214. // Existing translation in the translation set: display status.
  215. // We load the full node to check whether the user can edit it.
  216. $translation_node = node_load($translations[$langcode]->nid);
  217. $path = 'node/' . $translation_node->nid;
  218. $title = i18n_node_translation_link($translation_node->title, $path, $langcode);
  219. if (node_access('update', $translation_node)) {
  220. $text = t('edit');
  221. $path = 'node/' . $translation_node->nid . '/edit';
  222. $options[] = i18n_node_translation_link($text, $path, $langcode);
  223. }
  224. $status = $translation_node->status ? t('Published') : t('Not published');
  225. $status .= $translation_node->translate ? ' - <span class="marker">' . t('outdated') . '</span>' : '';
  226. if ($translation_node->nid == $tnid) {
  227. $language_name = t('<strong>@language_name</strong> (source)', array('@language_name' => $language_name));
  228. }
  229. }
  230. else {
  231. // No such translation in the set yet: help user to create it.
  232. $title = t('n/a');
  233. if (node_access('create', $node)) {
  234. $text = t('add translation');
  235. $path = 'node/add/' . str_replace('_', '-', $node->type);
  236. $query = array('query' => array('translation' => $node->nid, 'target' => $langcode));
  237. //condition added from i18n/i18n_node/i18n_node.pages.inc
  238. if (in_array($langcode, $perms)) {
  239. $options[] = i18n_node_translation_link($text, $path, $langcode, $query);
  240. }
  241. }
  242. $status = t('Not translated');
  243. }
  244. $rows[] = array($language_name, $title, $status, implode(" | ", $options));
  245. }
  246. drupal_set_title(t('Translations of %title', array('%title' => $node->title)), PASS_THROUGH);
  247. $build['translation_node_overview'] = array(
  248. '#theme' => 'table',
  249. '#header' => $header,
  250. '#rows' => $rows,
  251. );
  252. if (user_access('administer content translations')) {
  253. $build['translation_node_select'] = drupal_get_form('i18n_node_select_translation', $node, $translations);
  254. }
  255. return $build;
  256. }
  257. /**
  258. * Implements hook_menu().
  259. */
  260. function i18n_access_menu() {
  261. $items = array();
  262. $items['admin/settings/language/access'] = array(
  263. 'title' => 'Access',
  264. 'page callback' => 'drupal_get_form',
  265. 'page arguments' => array('i18n_access_admin_settings'),
  266. 'access arguments' => array('administer site configuration'),
  267. 'type' => MENU_LOCAL_TASK,
  268. 'weight' => 10,
  269. );
  270. return $items;
  271. }
  272. /**
  273. * Admin settings form
  274. */
  275. function i18n_access_admin_settings() {
  276. $form['i18n_access_languages'] = array(
  277. '#title' => t('Select the default access languages'),
  278. '#type' => 'select',
  279. '#multiple' => 'true',
  280. '#options' => array(I18N_ACCESS_LANGUAGE_NEUTRAL => t('Language neutral')) + locale_language_list('name'),
  281. '#default_value' => variable_get('i18n_access_languages', array()),
  282. '#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.")
  283. );
  284. return system_settings_form($form);
  285. }