materio_translator.module 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. // __ __ __ __ _
  3. // / / / /_______ __________ ________ / /_/ /_(_)___ ____ ______
  4. // / / / / ___/ _ \/ ___/ ___/ / ___/ _ \/ __/ __/ / __ \/ __ `/ ___/
  5. // / /_/ (__ ) __/ / (__ ) (__ ) __/ /_/ /_/ / / / / /_/ (__ )
  6. // \____/____/\___/_/ /____/ /____/\___/\__/\__/_/_/ /_/\__, /____/
  7. // /____/
  8. /**
  9. * Implements hook_user_insert().
  10. */
  11. function materio_translator_user_insert(&$edit, &$account, $category = NULL) {
  12. materio_translator_user_update($edit, $account, $category);
  13. }
  14. /**
  15. * Implements hook_user_update().
  16. */
  17. function materio_translator_user_update(&$edit, &$account, $category = NULL) {
  18. if ($category == 'account') {
  19. // see user_admin_perm_submit()
  20. if (isset($edit['materio_translator'])) {
  21. db_delete('materio_translator')
  22. ->condition('uid', $account->uid)
  23. ->execute();
  24. $edit['materio_translator'] = array_filter($edit['materio_translator']);
  25. if (count($edit['materio_translator'])) {
  26. db_insert('materio_translator')
  27. ->fields(array(
  28. 'uid' => $account->uid,
  29. 'perm' => implode(', ', array_keys($edit['materio_translator'])),
  30. ))->execute();
  31. }
  32. unset($edit['materio_translator']);
  33. }
  34. }
  35. }
  36. /**
  37. * Implements hook_user_delete().
  38. */
  39. function materio_translator_user_delete($account) {
  40. db_delete('materio_translator')
  41. ->condition('uid', $account->uid)
  42. ->execute();
  43. }
  44. /**
  45. * Load the language permissions for a given user
  46. */
  47. function materio_translator_load_permissions($uid = NULL) {
  48. $perms = &drupal_static(__FUNCTION__);
  49. // use the global user id if none is passed
  50. if (!isset($uid)) {
  51. $uid = $GLOBALS['user']->uid;
  52. $account = NULL;
  53. }else {
  54. $account = user_load($uid);
  55. }
  56. if (!isset($perms[$uid])) {
  57. $perm_string = db_query('SELECT perm FROM {materio_translator} WHERE uid = :uid', array(':uid' => $uid))->fetchField();
  58. if ($perm_string) {
  59. $perms[$uid] = drupal_map_assoc(explode(', ', $perm_string));
  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('materio_translator_languages', array())));
  67. }
  68. return $perms[$uid];
  69. }
  70. /**
  71. * Implements hook_permission().
  72. */
  73. function materio_translator_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_alter().
  84. */
  85. function materio_translator_form_alter(&$form, &$form_state, $form_id) {
  86. // Add materio_translator things to user/edit /user/add
  87. if ($form_id == 'user_register_form' || $form_id == 'user_profile_form' ) {
  88. // dsm($form_id, 'form_id');
  89. // dsm($form, 'form');
  90. // dsm($form_state, 'form_state');
  91. $form['materio_translator'] = array(
  92. '#type' => 'fieldset',
  93. '#title' => t('Translation access'),
  94. '#tree' => 0,
  95. '#access' => user_access('administer users'),
  96. );
  97. $form['materio_translator']['materio_translator'] = array(
  98. '#type' => 'checkboxes',
  99. '#options' => array(LANGUAGE_NONE => t('Language neutral')) + locale_language_list('name'),
  100. '#default_value' => materio_translator_load_permissions($form['#user']->uid),
  101. '#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.'),
  102. );
  103. }
  104. }
  105. /**
  106. * Implements hook_menu().
  107. */
  108. function materio_translator_menu() {
  109. $items['admin/config/regional/language/access'] = array(
  110. 'title' => 'Access',
  111. 'page callback' => 'drupal_get_form',
  112. 'page arguments' => array('materio_translator_admin_settings'),
  113. 'access arguments' => array('administer site configuration'),
  114. 'type' => MENU_LOCAL_TASK,
  115. 'weight' => 10,
  116. );
  117. return $items;
  118. }
  119. /**
  120. * Admin settings form.
  121. */
  122. function materio_translator_admin_settings($form) {
  123. $form['materio_translator_languages'] = array(
  124. '#title' => t('Select the default access languages'),
  125. '#type' => 'select',
  126. '#multiple' => TRUE,
  127. '#options' => array(LANGUAGE_NONE => t('Language neutral')) + locale_language_list('name'),
  128. '#default_value' => variable_get('materio_translator_languages', array()),
  129. '#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.")
  130. );
  131. return system_settings_form($form);
  132. }
  133. // _ __ __ _
  134. // / | / /___ ____/ /__ ____ _ _____ ______ __(_)__ _ __
  135. // / |/ / __ \/ __ / _ \ / __ \ | / / _ \/ ___/ | / / / _ \ | /| / /
  136. // / /| / /_/ / /_/ / __/ / /_/ / |/ / __/ / | |/ / / __/ |/ |/ /
  137. // /_/ |_/\____/\__,_/\___/ \____/|___/\___/_/ |___/_/\___/|__/|__/
  138. /**
  139. * Implements hook_menu_alter().
  140. */
  141. function materio_translator_menu_alter(&$items) {
  142. // 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?
  143. // $items['node/%node/translate']['page callback'] = 'materio_translator_translation_node_overview';
  144. //
  145. foreach ($items as $path => $item) {
  146. // if(strpos($path, 'node/%node/translate') !== false){
  147. if(preg_match('/^node\/%node\/translate$/', $path)){
  148. // dsm($path);
  149. // dsm($item);
  150. // create new page arguments
  151. $pargs = $item['page arguments'];
  152. // add page call back for entity_translation call
  153. // and memorize old page callback (i18n) for entity_translation call args
  154. $pargs[2]['page callback'] = $item['page callback'];
  155. $pargs[2]['file'] = $item['file'];
  156. $pargs[2]['module'] = $item['module'];
  157. $pargs[2]['page arguments'] = $item['page arguments'];
  158. // dsm($pargs, "pargs");
  159. // change page callback for our own function
  160. $items["node/%node/translate"]['page callback'] = "materio_translator_translation_node_overview";
  161. // add our own page raguments
  162. $items["node/%node/translate"]['page arguments'] = $pargs;
  163. /*
  164. original $item :
  165. Array(
  166. [title] =&gt; Translate
  167. [page callback] =&gt; entity_translation_overview
  168. [page arguments] =&gt; Array
  169. (
  170. [0] =&gt; node
  171. [1] =&gt; 1
  172. [2] =&gt; Array
  173. (
  174. [page callback] =&gt; i18n_node_translation_overview
  175. [file] =&gt; i18n_node.pages.inc
  176. [module] =&gt; i18n_node
  177. )
  178. [3] =&gt; 1
  179. )
  180. [type] =&gt; 132
  181. [weight] =&gt; 1
  182. [context] =&gt; 3
  183. [access callback] =&gt; entity_translation_node_tab_access
  184. [access arguments] =&gt; Array
  185. (
  186. [0] =&gt; 1
  187. [1] =&gt; _translation_tab_access
  188. [2] =&gt; 1
  189. )
  190. [file] =&gt; entity_translation.admin.inc
  191. [module] =&gt; entity_translation
  192. )
  193. */
  194. }
  195. }
  196. }
  197. /**
  198. * Implements hook_module_implements_alter().
  199. */
  200. function materio_translator_module_implements_alter(&$implementations, $hook) {
  201. switch ($hook) {
  202. case 'menu_alter':
  203. // Move our hook_menu_alter implementation to the end of the list.
  204. $group = $implementations['materio_translator'];
  205. unset($implementations['materio_translator']);
  206. $implementations['materio_translator'] = $group;
  207. break;
  208. }
  209. }
  210. /**
  211. * Most logic comes from translation/i18n_node module.
  212. *
  213. * We removes here only the "add translation" links for languages which are not your selected language.
  214. *
  215. * @see translation_node_overview
  216. * @see i18n_node_translation_overview
  217. *
  218. * @param object $node
  219. *
  220. * @return array.
  221. */
  222. function materio_translator_translation_node_overview($entity_type, $entity, $callback = NULL) {
  223. // dsm($entity_type, "entity_type");
  224. // dsm($entity, "entity");
  225. // dsm($callback, "callback");
  226. // first call entity_translation original callback
  227. // we retrieve the original build object of translation overview
  228. if ($callback) {
  229. $callback['page arguments'][1] = $entity;
  230. $build = materio_translator_overview_callback($callback);
  231. // dsm($build, "build");
  232. global $user;
  233. $perms = materio_translator_load_permissions($user->uid);
  234. // dsm($perms, 'perms');
  235. $i = 0;
  236. foreach ($build['entity_translation_overview']['#rows'] as $row) {
  237. // retrieve teh translation link
  238. $link = $row['data'][4];
  239. // dsm($link, "link");
  240. // retrieve the langcode from link
  241. preg_match('/xml:lang="([^"]+)"/', $link, $matches);
  242. $langcode = $matches[1];
  243. // dsm($langcode, 'langcode');
  244. // remove link if langcode not in perms
  245. if(!in_array($langcode, $perms)){
  246. $build['entity_translation_overview']['#rows'][$i]['data'][4] = "";
  247. }
  248. $i++;
  249. }
  250. }
  251. return $build;
  252. }
  253. /**
  254. * Calls the appropriate translation overview callback.
  255. */
  256. function materio_translator_overview_callback($callback) {
  257. if (module_exists($callback['module'])) {
  258. if (isset($callback['file'])) {
  259. $path = isset($callback['file path']) ? $callback['file path'] : drupal_get_path('module', $callback['module']);
  260. require_once DRUPAL_ROOT . '/' . $path . '/' . $callback['file'];
  261. }
  262. return call_user_func_array($callback['page callback'], $callback['page arguments']);
  263. }
  264. }