summaryrefslogtreecommitdiff
path: root/sites/all/modules/ctools/plugins/access/node_language.inc
blob: 0fdcfc66a533ef9d2318e01b6e5b6b507f60f1e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php

/**
 * @file
 * Plugin to provide access control based upon node type.
 */

/**
 * Plugins are described by creating a $plugin array which will be used
 * by the system that includes this file.
 */
if (module_exists('locale')) {
  $plugin = array(
    'title' => t("Node: language"),
    'description' => t('Control access by node language.'),
    'callback' => 'ctools_node_language_ctools_access_check',
    'default' => array('language' => array()),
    'settings form' => 'ctools_node_language_ctools_access_settings',
    'settings form submit' => 'ctools_node_language_ctools_access_settings_submit',
    'summary' => 'ctools_node_language_ctools_access_summary',
    'required context' => new ctools_context_required(t('Node'), 'node'),
  );
}

/**
 * Settings form for the 'by node_language' access plugin
 */
function ctools_node_language_ctools_access_settings($form, &$form_state, $conf) {
  $options = array(
    'current' => t('Current site language'),
    'default' => t('Default site language'),
    'no_language' => t('No language'),
  );
  $options = array_merge($options, locale_language_list());
  $form['settings']['language'] = array(
    '#title' => t('Language'),
    '#type' => 'checkboxes',
    '#options' => $options,
    '#description' => t('Pass only if the node is in one of the selected languages.'),
    '#default_value' => $conf['language'],
  );
  return $form;
}

/**
 * Check for access.
 */
function ctools_node_language_ctools_access_check($conf, $context) {
  // As far as I know there should always be a context at this point, but this
  // is safe.
  if (empty($context) || empty($context->data) || !isset($context->data->language)) {
    return FALSE;
  }

  global $language;

  // Specialcase: if 'no language' is checked, return TRUE if the language field is
  // empty.
  if (!empty($conf['language']['no_language'])) {
    if (empty($context->data->language)) {
      return TRUE;
    }
  }

  // Specialcase: if 'current' is checked, return TRUE if the current site language
  // matches the node language.
  if (!empty($conf['language']['current'])) {
    if ($context->data->language == $language->language) {
      return TRUE;
    }
  }

  // Specialcase: If 'default' is checked, return TRUE if the default site language
  // matches the node language.
  if (!empty($conf['language']['default'])) {
    if ($context->data->language == language_default('language')) {
      return TRUE;
    }
  }

  if (array_filter($conf['language']) && empty($conf['language'][$context->data->language])) {
    return FALSE;
  }

  return TRUE;
}

/**
 * Provide a summary description based upon the checked node_languages.
 */
function ctools_node_language_ctools_access_summary($conf, $context) {
  $languages = array(
    'current' => t('Current site language'),
    'default' => t('Default site language'),
    'no_language' => t('No language'),
  );
  $languages = array_merge($languages, locale_language_list());

  if (!isset($conf['language'])) {
    $conf['language'] = array();
  }

  $names = array();
  foreach (array_filter($conf['language']) as $language) {
    $names[] = $languages[$language];
  }

  if (empty($names)) {
    return t('@identifier is in any language', array('@identifier' => $context->identifier));
  }

  return format_plural(count($names), '@identifier language is "@languages"', '@identifier language is one of "@languages"', array('@languages' => implode(', ', $names), '@identifier' => $context->identifier));
}