site_language.inc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide access control based upon node type.
  5. */
  6. /**
  7. * Plugins are described by creating a $plugin array which will be used
  8. * by the system that includes this file.
  9. */
  10. if (module_exists('locale')) {
  11. $plugin = array(
  12. 'title' => t("User: language"),
  13. 'description' => t('Control access by the language the user or site currently uses.'),
  14. 'callback' => 'ctools_site_language_ctools_access_check',
  15. 'default' => array('language' => array()),
  16. 'settings form' => 'ctools_site_language_ctools_access_settings',
  17. 'settings form submit' => 'ctools_site_language_ctools_access_settings_submit',
  18. 'summary' => 'ctools_site_language_ctools_access_summary',
  19. );
  20. }
  21. /**
  22. * Settings form for the 'by site_language' access plugin
  23. */
  24. function ctools_site_language_ctools_access_settings($form, &$form_state, $conf) {
  25. $options = array(
  26. 'default' => t('Default site language'),
  27. );
  28. $options = array_merge($options, locale_language_list());
  29. $form['settings']['language'] = array(
  30. '#title' => t('Language'),
  31. '#type' => 'checkboxes',
  32. '#options' => $options,
  33. '#description' => t('Pass only if the current site language is one of the selected languages.'),
  34. '#default_value' => $conf['language'],
  35. );
  36. return $form;
  37. }
  38. /**
  39. * Check for access.
  40. */
  41. function ctools_site_language_ctools_access_check($conf, $context) {
  42. global $language;
  43. // Specialcase: If 'default' is checked, return TRUE if the default site language
  44. // matches the node language.
  45. if (!empty($conf['language']['default'])) {
  46. if ($language->language == language_default('language')) {
  47. return TRUE;
  48. }
  49. }
  50. if (array_filter($conf['language']) && empty($conf['language'][$language->language])) {
  51. return FALSE;
  52. }
  53. return TRUE;
  54. }
  55. /**
  56. * Provide a summary description based upon the checked site_languages.
  57. */
  58. function ctools_site_language_ctools_access_summary($conf, $context) {
  59. $languages = array(
  60. 'default' => t('Default site language'),
  61. );
  62. $languages = array_merge($languages, locale_language_list());
  63. if (!isset($conf['language'])) {
  64. $conf['language'] = array();
  65. }
  66. $names = array();
  67. foreach (array_filter($conf['language']) as $language) {
  68. $names[] = $languages[$language];
  69. }
  70. if (empty($names)) {
  71. return t('Site language is any language');
  72. }
  73. return format_plural(count($names), 'Site language is "@languages"', 'Site language is one of "@languages"', array('@languages' => implode(', ', $names)));
  74. }