materio_translator.module 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. // / / / /__ / /___ ___ __________
  46. // / /_/ / _ \/ / __ \/ _ \/ ___/ ___/
  47. // / __ / __/ / /_/ / __/ / (__ )
  48. // /_/ /_/\___/_/ .___/\___/_/ /____/
  49. // /_/
  50. /**
  51. * Load the language permissions for a given user
  52. */
  53. function materio_translator_load_permissions($uid = NULL) {
  54. $perms = &drupal_static(__FUNCTION__);
  55. // use the global user id if none is passed
  56. if (!isset($uid)) {
  57. $uid = $GLOBALS['user']->uid;
  58. $account = NULL;
  59. }else {
  60. $account = user_load($uid);
  61. }
  62. if (!isset($perms[$uid])) {
  63. $perm_string = db_query('SELECT perm FROM {materio_translator} WHERE uid = :uid', array(':uid' => $uid))->fetchField();
  64. if ($perm_string) {
  65. $perms[$uid] = drupal_map_assoc(explode(', ', $perm_string));
  66. }else {
  67. $perms[$uid] = array();
  68. }
  69. }
  70. // adding the default languages if permission has been granted
  71. if (user_access('access selected languages', $account)) {
  72. $perms[$uid] = array_merge($perms[$uid], drupal_map_assoc(variable_get('materio_translator_languages', array())));
  73. }
  74. return $perms[$uid];
  75. }
  76. // ________ __ __
  77. // / ____/ /___ / /_ ____ _/ / ____ ___ _________ ___ _____
  78. // / / __/ / __ \/ __ \/ __ `/ / / __ \/ _ \/ ___/ __ `__ \/ ___/
  79. // / /_/ / / /_/ / /_/ / /_/ / / / /_/ / __/ / / / / / / (__ )
  80. // \____/_/\____/_.___/\__,_/_/ / .___/\___/_/ /_/ /_/ /_/____/
  81. // /_/
  82. /**
  83. * Implements hook_permission().
  84. */
  85. function materio_translator_permission() {
  86. return array(
  87. 'access selected languages' => array(
  88. 'title' => t('Access selected languages'),
  89. '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'))),
  90. 'restrict access' => TRUE,
  91. ),
  92. 'administer user translation language access' => array(
  93. 'title' => t('Administer user translation language access'),
  94. 'description' => t('administer user translation language access'),
  95. 'restrict access' => TRUE,
  96. ),
  97. );
  98. }
  99. /**
  100. * Implements hook_form_alter().
  101. */
  102. function materio_translator_form_alter(&$form, &$form_state, $form_id) {
  103. // Add materio_translator things to user/edit /user/add
  104. if ($form_id == 'user_register_form' || $form_id == 'user_profile_form' ) {
  105. // dsm($form_id, 'form_id');
  106. // dsm($form, 'form');
  107. // dsm($form_state, 'form_state');
  108. $form['materio_translator'] = array(
  109. '#type' => 'fieldset',
  110. '#title' => t('Translation access'),
  111. '#tree' => 0,
  112. '#access' => user_access('administer user translation language access'),
  113. );
  114. $form['materio_translator']['materio_translator'] = array(
  115. '#type' => 'checkboxes',
  116. '#options' => array(LANGUAGE_NONE => t('Language neutral')) + locale_language_list('name'),
  117. '#default_value' => materio_translator_load_permissions($form['#user']->uid),
  118. '#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.'),
  119. );
  120. }
  121. }
  122. /**
  123. * Implements hook_menu().
  124. */
  125. function materio_translator_menu() {
  126. $items['admin/config/regional/language/access'] = array(
  127. 'title' => 'Access',
  128. 'page callback' => 'drupal_get_form',
  129. 'page arguments' => array('materio_translator_admin_settings'),
  130. 'access arguments' => array('administer site configuration'),
  131. 'type' => MENU_LOCAL_TASK,
  132. 'weight' => 10,
  133. );
  134. return $items;
  135. }
  136. /**
  137. * Admin settings form.
  138. */
  139. function materio_translator_admin_settings($form) {
  140. $form['materio_translator_languages'] = array(
  141. '#title' => t('Select the default access languages'),
  142. '#type' => 'select',
  143. '#multiple' => TRUE,
  144. '#options' => array(LANGUAGE_NONE => t('Language neutral')) + locale_language_list('name'),
  145. '#default_value' => variable_get('materio_translator_languages', array()),
  146. '#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.")
  147. );
  148. return system_settings_form($form);
  149. }
  150. // __ ___ ___ ____
  151. // / |/ /__ ____ __ __ / | / / /____ _____
  152. // / /|_/ / _ \/ __ \/ / / / / /| | / / __/ _ \/ ___/
  153. // / / / / __/ / / / /_/ / / ___ |/ / /_/ __/ /
  154. // /_/ /_/\___/_/ /_/\__,_/ /_/ |_/_/\__/\___/_/
  155. /**
  156. * Implements hook_menu_alter().
  157. */
  158. function materio_translator_menu_alter(&$items) {
  159. // 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?
  160. // $items['node/%node/translate']['page callback'] = 'materio_translator_translation_node_overview';
  161. //
  162. foreach ($items as $path => $item) {
  163. // if(strpos($path, 'node/%node/translate') !== false){
  164. if(preg_match('/^node\/%node\/translate$/', $path)){
  165. // dsm($path);
  166. // dsm($item);
  167. // create new page arguments
  168. $pargs = $item['page arguments'];
  169. // add page call back for entity_translation call
  170. // and memorize old page callback (i18n) for entity_translation call args
  171. $pargs[2]['page callback'] = $item['page callback'];
  172. $pargs[2]['file'] = $item['file'];
  173. $pargs[2]['module'] = $item['module'];
  174. $pargs[2]['page arguments'] = $item['page arguments'];
  175. // dsm($pargs, "pargs");
  176. // change page callback for our own function
  177. $items[$path]['page callback'] = "materio_translator_translation_node_overview";
  178. // add our own page raguments
  179. $items[$path]['page arguments'] = $pargs;
  180. }
  181. // translation edit link
  182. if(preg_match('/^node\/%node\/edit\/%entity_translation_language$/', $path)){
  183. // dsm($item, $path);
  184. // create new page arguments
  185. $access_args = $item['access arguments'];
  186. // dsm($access_args, 'access_args avt');
  187. // add page call back for entity_translation call
  188. // and memorize old page callback (i18n) for entity_translation call args
  189. $access_args[3]['access callback'] = $item['access callback'];
  190. $access_args[3]['file'] = 'entity_translation.node.inc';
  191. $access_args[3]['module'] = 'entity_translation';
  192. $access_args[3]['access arguments'] = $item['access arguments'];
  193. // dsm($access_args, "access_args");
  194. // change access callback for our own function
  195. $items[$path]['access callback'] = 'materio_translator_node_edit_access';
  196. // add our own page raguments
  197. $items[$path]['access arguments'] = $access_args;
  198. // dsm($access_args, 'access_args apr');
  199. // dsm($items[$path], $path);
  200. }
  201. // translation add link
  202. if(preg_match('/^node\/%node\/edit\/add\/%entity_translation_language/', $path)){
  203. // dsm($item, $path);
  204. }
  205. }
  206. }
  207. function materio_translator_node_edit_access($entity_type, $entity, $langcode, $callback = null){
  208. $args = func_get_args();
  209. // dsm($args, '1 -- materio_translator_node_edit_access args');
  210. // dsm($entity_type, "entity_type");
  211. // dsm($entity, "entity");
  212. // dsm($langcode, 'langcode');
  213. // dsm($callback, "2 -- callback");
  214. if (module_exists($callback['module'])) {
  215. // dsm('module_exists');
  216. if (isset($callback['file'])) {
  217. $path = isset($callback['file path']) ? $callback['file path'] : drupal_get_path('module', $callback['module']);
  218. // dsm($path, 'path');
  219. require_once DRUPAL_ROOT . '/' . $path . '/' . $callback['file'];
  220. }
  221. // dsm($callback['access callback'], 'access callback');
  222. $callback['access arguments'][1] = $entity;
  223. $callback['access arguments'][2] = $langcode;
  224. $callback['access arguments'][5] = $entity;
  225. // dsm($callback['access arguments'], "callback['access arguments']");
  226. $callbackaccess = call_user_func_array($callback['access callback'], $callback['access arguments']);
  227. // dsm($callbackaccess, '3 -- callbackaccess');
  228. if($callbackaccess){
  229. global $user;
  230. $perms = materio_translator_load_permissions($user->uid);
  231. // dsm($perms, '4 -- perms');
  232. // remove link if langcode not in perms
  233. if(in_array($langcode, $perms)){
  234. return true;
  235. }
  236. }
  237. return false;
  238. }
  239. // $callbackaccess = call_user_func_array($callback['access callback'], $callback['access arguments']);
  240. // return $callbackaccess;
  241. return true;
  242. }
  243. // _ __ __ _
  244. // / | / /___ ____/ /__ ____ _ _____ ______ __(_)__ _ __
  245. // / |/ / __ \/ __ / _ \ / __ \ | / / _ \/ ___/ | / / / _ \ | /| / /
  246. // / /| / /_/ / /_/ / __/ / /_/ / |/ / __/ / | |/ / / __/ |/ |/ /
  247. // /_/ |_/\____/\__,_/\___/ \____/|___/\___/_/ |___/_/\___/|__/|__/
  248. /**
  249. * Implements hook_module_implements_alter().
  250. */
  251. function materio_translator_module_implements_alter(&$implementations, $hook) {
  252. switch ($hook) {
  253. case 'menu_alter':
  254. // Move our hook_menu_alter implementation to the end of the list.
  255. $group = $implementations['materio_translator'];
  256. unset($implementations['materio_translator']);
  257. $implementations['materio_translator'] = $group;
  258. break;
  259. }
  260. }
  261. /**
  262. * Most logic comes from translation/i18n_node module.
  263. *
  264. * We removes here only the "add translation" links for languages which are not your selected language.
  265. *
  266. * @see translation_node_overview
  267. * @see i18n_node_translation_overview
  268. *
  269. * @param object $node
  270. *
  271. * @return array.
  272. */
  273. function materio_translator_translation_node_overview($entity_type, $entity, $callback = NULL) {
  274. // dsm('materio_translator_translation_node_overview');
  275. // dsm($entity_type, "entity_type");
  276. // dsm($entity, "entity");
  277. // dsm($callback, "callback");
  278. // first call entity_translation original callback
  279. // we retrieve the original build object of translation overview
  280. if ($callback) {
  281. $callback['page arguments'][1] = $entity;
  282. $build = materio_translator_overview_callback($callback);
  283. // dsm($build, "build");
  284. global $user;
  285. $perms = materio_translator_load_permissions($user->uid);
  286. // dsm($perms, 'perms');
  287. $i = 0;
  288. foreach ($build['entity_translation_overview']['#rows'] as $row) {
  289. // retrieve teh translation link
  290. $link = $row['data'][4];
  291. // dsm($link, "link");
  292. // retrieve the langcode from link
  293. preg_match('/xml:lang="([^"]+)"/', $link, $matches);
  294. $langcode = $matches[1];
  295. // dsm($langcode, 'langcode');
  296. // remove link if langcode not in perms
  297. if(!in_array($langcode, $perms)){
  298. $build['entity_translation_overview']['#rows'][$i]['data'][4] = "";
  299. }
  300. $i++;
  301. }
  302. }
  303. return $build;
  304. }
  305. /**
  306. * Calls the appropriate translation overview callback.
  307. */
  308. function materio_translator_overview_callback($callback) {
  309. if (module_exists($callback['module'])) {
  310. if (isset($callback['file'])) {
  311. $path = isset($callback['file path']) ? $callback['file path'] : drupal_get_path('module', $callback['module']);
  312. require_once DRUPAL_ROOT . '/' . $path . '/' . $callback['file'];
  313. }
  314. return call_user_func_array($callback['page callback'], $callback['page arguments']);
  315. }
  316. }
  317. // _ __ __ __
  318. // / | / /___ ____/ /__ / /___ _____ ____ ___ ______ _____ ____
  319. // / |/ / __ \/ __ / _ \ / / __ `/ __ \/ __ `/ / / / __ `/ __ `/ _ \
  320. // / /| / /_/ / /_/ / __/ / / /_/ / / / / /_/ / /_/ / /_/ / /_/ / __/
  321. // /_/ |_/\____/\__,_/\___/ __/\__,_/_/ /_/\__, /\__,_/\__,_/\__, /\___/
  322. // ________ / /__ _____/ /_(_)___ ___/____/ /____/
  323. // / ___/ _ \/ / _ \/ ___/ __/ / __ \/ __ \
  324. // (__ ) __/ / __/ /__/ /_/ / /_/ / / / /
  325. // /____/\___/_/\___/\___/\__/_/\____/_/ /_/
  326. /**
  327. * Implements hook_menu_local_tasks_alter().
  328. */
  329. function materio_translator_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  330. // dsm($data, 'data');
  331. global $user;
  332. $perms = materio_translator_load_permissions($user->uid);
  333. foreach ($data['tabs'] as $t => $tab) {
  334. foreach ($tab['output'] as $l => $link) {
  335. if($link['#language_tab']){
  336. // dsm($link, $link["#link"]["title"]);
  337. $langcode = $link["#link"]['localized_options']['language']->language;
  338. if(!in_array($langcode, $perms)){
  339. unset($data['tabs'][$t]['output'][$l]);
  340. }
  341. // dsm($data['tabs'][$t]['output'][$l]['#link']);
  342. }
  343. }
  344. }
  345. }
  346. /**
  347. * Implements hook_form_node_form_alter().
  348. */
  349. function materio_translator_form_node_form_alter(&$form) {
  350. $form['#after_build'][] = '_materio_translator_form_node_form_alter';
  351. }
  352. /**
  353. * Unset's languages from language options if user does not have permission to
  354. * use.
  355. *
  356. * @param $form
  357. * @param $form_state
  358. * @return mixed
  359. */
  360. function _materio_translator_form_node_form_alter($form, &$form_state) {
  361. if (isset($form['language']['#options']) && !user_access('bypass node access')) {
  362. $perms = materio_translator_load_permissions();
  363. foreach ($form['language']['#options'] as $key => $value) {
  364. if (empty($perms[$key])) {
  365. unset($form['language']['#options'][$key]);
  366. }
  367. }
  368. }
  369. return $form;
  370. }
  371. function materio_translator_node_tab_access(){
  372. $args = func_get_args();
  373. dsm($args, '1 -- materio_translator_node_tab_access args');
  374. // dsm($entity_type, "entity_type");
  375. // dsm($entity, "entity");
  376. // dsm($langcode, 'langcode');
  377. // dsm($callback, "2 -- callback");
  378. /*
  379. if (module_exists($callback['module'])) {
  380. // dsm('module_exists');
  381. if (isset($callback['file'])) {
  382. $path = isset($callback['file path']) ? $callback['file path'] : drupal_get_path('module', $callback['module']);
  383. // dsm($path, 'path');
  384. require_once DRUPAL_ROOT . '/' . $path . '/' . $callback['file'];
  385. }
  386. // dsm($callback['access callback'], 'access callback');
  387. $callback['access arguments'][1] = $entity;
  388. $callback['access arguments'][2] = $langcode;
  389. $callback['access arguments'][5] = $entity;
  390. // dsm($callback['access arguments'], "callback['access arguments']");
  391. $callbackaccess = call_user_func_array($callback['access callback'], $callback['access arguments']);
  392. // dsm($callbackaccess, '3 -- callbackaccess');
  393. if($callbackaccess){
  394. global $user;
  395. $perms = materio_translator_load_permissions($user->uid);
  396. // dsm($perms, '4 -- perms');
  397. // remove link if langcode not in perms
  398. if(in_array($langcode, $perms)){
  399. return true;
  400. }
  401. }
  402. return false;
  403. }
  404. // $callbackaccess = call_user_func_array($callback['access callback'], $callback['access arguments']);
  405. // return $callbackaccess;
  406. */
  407. return true;
  408. }