materio_translator.module 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. 'access_translations_overview' => array(
  98. 'title' => t('Access translations overview')
  99. ),
  100. 'access_translation_table_fields' => array(
  101. 'title' => t('Access translation Fields table')
  102. ),
  103. 'access_translation_table_content_type' => array(
  104. 'title' => t('Access translation Content types table')
  105. ),
  106. 'access_translation_table_menu' => array(
  107. 'title' => t('Access translation menu table')
  108. ),
  109. 'access_translation_table_taxonomy' => array(
  110. 'title' => t('Access translation taxonomy table')
  111. ),
  112. 'translate_strings' => array(
  113. 'title' => t('Translate strings')
  114. ),
  115. 'delete_strings' => array(
  116. 'title' => t('Delete strings')
  117. ),
  118. 'refresh_strings' => array(
  119. 'title' => t('Refresh strings')
  120. ),
  121. 'import_translations' => array(
  122. 'title' => t('Import translations')
  123. ),
  124. 'export_translations' => array(
  125. 'title' => t('Import translations')
  126. ),
  127. 'update_modules_translations' => array(
  128. 'title' => t('Update modules translations')
  129. ),
  130. );
  131. }
  132. /**
  133. * Implements hook_form_alter().
  134. */
  135. function materio_translator_form_alter(&$form, &$form_state, $form_id) {
  136. // dsm($form_id);
  137. // Add materio_translator things to user/edit /user/add
  138. if ($form_id == 'user_register_form' || $form_id == 'user_profile_form' ) {
  139. // dsm($form_id, 'form_id');
  140. // dsm($form, 'form');
  141. // dsm($form_state, 'form_state');
  142. $form['materio_translator'] = array(
  143. '#type' => 'fieldset',
  144. '#title' => t('Translation access'),
  145. '#tree' => 0,
  146. '#access' => user_access('administer user translation language access'),
  147. );
  148. $form['materio_translator']['materio_translator'] = array(
  149. '#type' => 'checkboxes',
  150. '#options' => array(LANGUAGE_NONE => t('Language neutral')) + locale_language_list('name'),
  151. '#default_value' => materio_translator_load_permissions($form['#user']->uid),
  152. '#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.'),
  153. );
  154. }
  155. // limit fields in string translation
  156. if($form_id == 'i18n_string_locale_translate_edit_form'){
  157. // dsm($form, 'form');
  158. // dsm($form_state, 'form_state');
  159. global $user;
  160. $perms = materio_translator_load_permissions($user->uid);
  161. // dsm($perms);
  162. foreach ($form['translations'] as $langcode => $item) {
  163. // disable field if langcode not in perms
  164. if(!in_array($langcode, $perms) && isset($form['translations'][$langcode])){
  165. $form['translations'][$langcode]['#disabled'] = true;
  166. }
  167. }
  168. }
  169. // limit fields in string translation
  170. if($form_id == 'field_translation_table_form'
  171. || $form_id == "node_translation_table_nodetype_form"
  172. || $form_id == "menu_translation_table_menu_form"
  173. || $form_id == "taxonomy_translation_table_taxonomy_form"){
  174. // dsm($form, 'form');
  175. // dsm($form_state, 'form_state');
  176. global $user;
  177. $perms = materio_translator_load_permissions($user->uid);
  178. // dsm($perms);
  179. foreach ($form['filtered_form']['strings'] as $id => $row) {
  180. foreach ($row as $langcode => $field) {
  181. // disable field if langcode not in perms
  182. if(!in_array($langcode, $perms)){
  183. $form['filtered_form']['strings'][$id][$langcode]['#disabled'] = true;
  184. }
  185. }
  186. }
  187. }
  188. }
  189. /**
  190. * Implements hook_menu().
  191. */
  192. function materio_translator_menu() {
  193. $items['admin/config/regional/language/access'] = array(
  194. 'title' => 'Access',
  195. 'page callback' => 'drupal_get_form',
  196. 'page arguments' => array('materio_translator_admin_settings'),
  197. 'access arguments' => array('administer site configuration'),
  198. 'type' => MENU_LOCAL_TASK,
  199. 'weight' => 10,
  200. );
  201. return $items;
  202. }
  203. /**
  204. * Admin settings form.
  205. */
  206. function materio_translator_admin_settings($form) {
  207. $form['materio_translator_languages'] = array(
  208. '#title' => t('Select the default access languages'),
  209. '#type' => 'select',
  210. '#multiple' => TRUE,
  211. '#options' => array(LANGUAGE_NONE => t('Language neutral')) + locale_language_list('name'),
  212. '#default_value' => variable_get('materio_translator_languages', array()),
  213. '#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.")
  214. );
  215. return system_settings_form($form);
  216. }
  217. // __ ___ ___ ____
  218. // / |/ /__ ____ __ __ / | / / /____ _____
  219. // / /|_/ / _ \/ __ \/ / / / / /| | / / __/ _ \/ ___/
  220. // / / / / __/ / / / /_/ / / ___ |/ / /_/ __/ /
  221. // /_/ /_/\___/_/ /_/\__,_/ /_/ |_/_/\__/\___/_/
  222. /**
  223. * Implements hook_menu_alter().
  224. */
  225. function materio_translator_menu_alter(&$items) {
  226. // 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?
  227. // $items['node/%node/translate']['page callback'] = 'materio_translator_translation_node_overview';
  228. //
  229. foreach ($items as $path => $item) {
  230. // if(strpos($path, 'node/%node/translate') !== false){
  231. if(preg_match('/^node\/%node\/translate$/', $path)){
  232. // dsm($path);
  233. // dsm($item);
  234. // create new page arguments
  235. $pargs = $item['page arguments'];
  236. // add page call back for entity_translation call
  237. // and memorize old page callback (i18n) for entity_translation call args
  238. $pargs[2]['page callback'] = $item['page callback'];
  239. $pargs[2]['file'] = $item['file'];
  240. $pargs[2]['module'] = $item['module'];
  241. $pargs[2]['page arguments'] = $item['page arguments'];
  242. // dsm($pargs, "pargs");
  243. // change page callback for our own function
  244. $items[$path]['page callback'] = "materio_translator_translation_node_overview";
  245. // add our own page raguments
  246. $items[$path]['page arguments'] = $pargs;
  247. }
  248. // translation edit link
  249. if(preg_match('/^node\/%node\/edit\/%entity_translation_language$/', $path)){
  250. // dsm($item, $path);
  251. // create new page arguments
  252. $access_args = $item['access arguments'];
  253. // dsm($access_args, 'access_args avt');
  254. // add page call back for entity_translation call
  255. // and memorize old page callback (i18n) for entity_translation call args
  256. $access_args[3]['access callback'] = $item['access callback'];
  257. $access_args[3]['file'] = 'entity_translation.node.inc';
  258. $access_args[3]['module'] = 'entity_translation';
  259. $access_args[3]['access arguments'] = $item['access arguments'];
  260. // dsm($access_args, "access_args");
  261. // change access callback for our own function
  262. $items[$path]['access callback'] = 'materio_translator_node_edit_access';
  263. // add our own page raguments
  264. $items[$path]['access arguments'] = $access_args;
  265. // dsm($access_args, 'access_args apr');
  266. // dsm($items[$path], $path);
  267. }
  268. // translation add link
  269. // if(preg_match('/^node\/%node\/edit\/add\/%entity_translation_language/', $path)){
  270. // dsm($item, $path);
  271. // }
  272. // translation add link
  273. if(preg_match('/^admin\/config\/regional\/translate$/', $path)){
  274. $items[$path]['access arguments'] = array('access_translations_overview');
  275. }
  276. if(preg_match('/^admin\/config\/regional\/translate\/table$/', $path)){
  277. $items[$path]['access arguments'] = array('access_translation_table_fields');
  278. }
  279. if(preg_match('/^admin\/config\/regional\/translate\/table\/nodetype$/', $path)){
  280. $items[$path]['access arguments'] = array('access_translation_table_content_type');
  281. }
  282. if(preg_match('/^admin\/config\/regional\/translate\/table\/menu$/', $path)){
  283. $items[$path]['access arguments'] = array('access_translation_table_menu');
  284. }
  285. if(preg_match('/^admin\/config\/regional\/translate\/table\/taxonomy$/', $path)){
  286. $items[$path]['access arguments'] = array('access_translation_table_taxonomy');
  287. }
  288. if(preg_match('/^admin\/config\/regional\/translate\/translate$/', $path)){
  289. $items[$path]['access arguments'] = array('translate_strings');
  290. }
  291. if(preg_match('/^admin\/config\/regional\/translate\/import$/', $path)){
  292. $items[$path]['access arguments'] = array('import_translations');
  293. }
  294. if(preg_match('/^admin\/config\/regional\/translate\/i18n_string$/', $path)){
  295. $items[$path]['access arguments'] = array('refresh_strings');
  296. }
  297. if(preg_match('/^admin\/config\/regional\/translate\/update$/', $path)){
  298. $items[$path]['access arguments'] = array('update_modules_translations');
  299. }
  300. if(preg_match('/^admin\/config\/regional\/translate\/export$/', $path)){
  301. $items[$path]['access arguments'] = array('export_translations');
  302. }
  303. if(preg_match('/^admin\/config\/regional\/translate\/delete$/', $path)){
  304. $items[$path]['access arguments'] += array("delete_strings");
  305. }
  306. }
  307. }
  308. function materio_translator_node_edit_access($entity_type, $entity, $langcode, $callback = null){
  309. $args = func_get_args();
  310. // dsm($args, '1 -- materio_translator_node_edit_access args');
  311. // dsm($entity_type, "entity_type");
  312. // dsm($entity, "entity");
  313. // dsm($langcode, 'langcode');
  314. // dsm($callback, "2 -- callback");
  315. if (module_exists($callback['module'])) {
  316. // dsm('module_exists');
  317. if (isset($callback['file'])) {
  318. $path = isset($callback['file path']) ? $callback['file path'] : drupal_get_path('module', $callback['module']);
  319. // dsm($path, 'path');
  320. require_once DRUPAL_ROOT . '/' . $path . '/' . $callback['file'];
  321. }
  322. // dsm($callback['access callback'], 'access callback');
  323. $callback['access arguments'][1] = $entity;
  324. $callback['access arguments'][2] = $langcode;
  325. $callback['access arguments'][5] = $entity;
  326. // dsm($callback['access arguments'], "callback['access arguments']");
  327. $callbackaccess = call_user_func_array($callback['access callback'], $callback['access arguments']);
  328. // dsm($callbackaccess, '3 -- callbackaccess');
  329. if($callbackaccess){
  330. global $user;
  331. $perms = materio_translator_load_permissions($user->uid);
  332. // dsm($perms, '4 -- perms');
  333. // remove link if langcode not in perms
  334. if(in_array($langcode, $perms)){
  335. return true;
  336. }
  337. }
  338. return false;
  339. }
  340. // $callbackaccess = call_user_func_array($callback['access callback'], $callback['access arguments']);
  341. // return $callbackaccess;
  342. return true;
  343. }
  344. // _ __ __ _
  345. // / | / /___ ____/ /__ ____ _ _____ ______ __(_)__ _ __
  346. // / |/ / __ \/ __ / _ \ / __ \ | / / _ \/ ___/ | / / / _ \ | /| / /
  347. // / /| / /_/ / /_/ / __/ / /_/ / |/ / __/ / | |/ / / __/ |/ |/ /
  348. // /_/ |_/\____/\__,_/\___/ \____/|___/\___/_/ |___/_/\___/|__/|__/
  349. /**
  350. * Implements hook_module_implements_alter().
  351. */
  352. function materio_translator_module_implements_alter(&$implementations, $hook) {
  353. switch ($hook) {
  354. case 'menu_alter':
  355. // Move our hook_menu_alter implementation to the end of the list.
  356. $group = $implementations['materio_translator'];
  357. unset($implementations['materio_translator']);
  358. $implementations['materio_translator'] = $group;
  359. break;
  360. }
  361. }
  362. /**
  363. * Most logic comes from translation/i18n_node module.
  364. *
  365. * We removes here only the "add translation" links for languages which are not your selected language.
  366. *
  367. * @see translation_node_overview
  368. * @see i18n_node_translation_overview
  369. *
  370. * @param object $node
  371. *
  372. * @return array.
  373. */
  374. function materio_translator_translation_node_overview($entity_type, $entity, $callback = NULL) {
  375. // dsm('materio_translator_translation_node_overview');
  376. // dsm($entity_type, "entity_type");
  377. // dsm($entity, "entity");
  378. // dsm($callback, "callback");
  379. // first call entity_translation original callback
  380. // we retrieve the original build object of translation overview
  381. if ($callback) {
  382. $callback['page arguments'][1] = $entity;
  383. $build = materio_translator_overview_callback($callback);
  384. // dsm($build, "build");
  385. global $user;
  386. $perms = materio_translator_load_permissions($user->uid);
  387. // dsm($perms, 'perms');
  388. $i = 0;
  389. foreach ($build['entity_translation_overview']['#rows'] as $row) {
  390. // retrieve teh translation link
  391. $link = $row['data'][4];
  392. // dsm($link, "link");
  393. // retrieve the langcode from link
  394. preg_match('/xml:lang="([^"]+)"/', $link, $matches);
  395. $langcode = $matches[1];
  396. // dsm($langcode, 'langcode');
  397. // remove link if langcode not in perms
  398. if(!in_array($langcode, $perms)){
  399. $build['entity_translation_overview']['#rows'][$i]['data'][4] = "";
  400. }
  401. $i++;
  402. }
  403. }
  404. return $build;
  405. }
  406. /**
  407. * Calls the appropriate translation overview callback.
  408. */
  409. function materio_translator_overview_callback($callback) {
  410. if (module_exists($callback['module'])) {
  411. if (isset($callback['file'])) {
  412. $path = isset($callback['file path']) ? $callback['file path'] : drupal_get_path('module', $callback['module']);
  413. require_once DRUPAL_ROOT . '/' . $path . '/' . $callback['file'];
  414. }
  415. return call_user_func_array($callback['page callback'], $callback['page arguments']);
  416. }
  417. }
  418. // _ __ __ __
  419. // / | / /___ ____/ /__ / /___ _____ ____ ___ ______ _____ ____
  420. // / |/ / __ \/ __ / _ \ / / __ `/ __ \/ __ `/ / / / __ `/ __ `/ _ \
  421. // / /| / /_/ / /_/ / __/ / / /_/ / / / / /_/ / /_/ / /_/ / /_/ / __/
  422. // /_/ |_/\____/\__,_/\___/ __/\__,_/_/ /_/\__, /\__,_/\__,_/\__, /\___/
  423. // ________ / /__ _____/ /_(_)___ ___/____/ /____/
  424. // / ___/ _ \/ / _ \/ ___/ __/ / __ \/ __ \
  425. // (__ ) __/ / __/ /__/ /_/ / /_/ / / / /
  426. // /____/\___/_/\___/\___/\__/_/\____/_/ /_/
  427. /**
  428. * Implements hook_menu_local_tasks_alter().
  429. */
  430. function materio_translator_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  431. // dsm($data, 'data');
  432. global $user;
  433. $perms = materio_translator_load_permissions($user->uid);
  434. foreach ($data['tabs'] as $t => $tab) {
  435. foreach ($tab['output'] as $l => $link) {
  436. if($link['#language_tab']){
  437. // dsm($link, $link["#link"]["title"]);
  438. $langcode = $link["#link"]['localized_options']['language']->language;
  439. if(!in_array($langcode, $perms)){
  440. unset($data['tabs'][$t]['output'][$l]);
  441. }
  442. // dsm($data['tabs'][$t]['output'][$l]['#link']);
  443. }
  444. }
  445. }
  446. }
  447. /**
  448. * Implements hook_form_node_form_alter().
  449. */
  450. function materio_translator_form_node_form_alter(&$form) {
  451. $form['#after_build'][] = '_materio_translator_form_node_form_alter';
  452. }
  453. /**
  454. * Unset's languages from language options if user does not have permission to
  455. * use.
  456. *
  457. * @param $form
  458. * @param $form_state
  459. * @return mixed
  460. */
  461. function _materio_translator_form_node_form_alter($form, &$form_state) {
  462. if (isset($form['language']['#options']) && !user_access('bypass node access')) {
  463. $perms = materio_translator_load_permissions();
  464. foreach ($form['language']['#options'] as $key => $value) {
  465. if (empty($perms[$key])) {
  466. unset($form['language']['#options'][$key]);
  467. }
  468. }
  469. }
  470. return $form;
  471. }
  472. function materio_translator_node_tab_access(){
  473. $args = func_get_args();
  474. // dsm($args, '1 -- materio_translator_node_tab_access args');
  475. // dsm($entity_type, "entity_type");
  476. // dsm($entity, "entity");
  477. // dsm($langcode, 'langcode');
  478. // dsm($callback, "2 -- callback");
  479. /*
  480. if (module_exists($callback['module'])) {
  481. // dsm('module_exists');
  482. if (isset($callback['file'])) {
  483. $path = isset($callback['file path']) ? $callback['file path'] : drupal_get_path('module', $callback['module']);
  484. // dsm($path, 'path');
  485. require_once DRUPAL_ROOT . '/' . $path . '/' . $callback['file'];
  486. }
  487. // dsm($callback['access callback'], 'access callback');
  488. $callback['access arguments'][1] = $entity;
  489. $callback['access arguments'][2] = $langcode;
  490. $callback['access arguments'][5] = $entity;
  491. // dsm($callback['access arguments'], "callback['access arguments']");
  492. $callbackaccess = call_user_func_array($callback['access callback'], $callback['access arguments']);
  493. // dsm($callbackaccess, '3 -- callbackaccess');
  494. if($callbackaccess){
  495. global $user;
  496. $perms = materio_translator_load_permissions($user->uid);
  497. // dsm($perms, '4 -- perms');
  498. // remove link if langcode not in perms
  499. if(in_array($langcode, $perms)){
  500. return true;
  501. }
  502. }
  503. return false;
  504. }
  505. // $callbackaccess = call_user_func_array($callback['access callback'], $callback['access arguments']);
  506. // return $callbackaccess;
  507. */
  508. return true;
  509. }