i18n_access.module 9.8 KB

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