comparison sites/all/modules/ctools/plugins/access/node_language.inc @ 0:ff03f76ab3fe

initial version
author danieleb <danielebarchiesi@me.com>
date Wed, 21 Aug 2013 18:51:11 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ff03f76ab3fe
1 <?php
2
3 /**
4 * @file
5 * Plugin to provide access control based upon node type.
6 */
7
8 /**
9 * Plugins are described by creating a $plugin array which will be used
10 * by the system that includes this file.
11 */
12 if (module_exists('locale')) {
13 $plugin = array(
14 'title' => t("Node: language"),
15 'description' => t('Control access by node language.'),
16 'callback' => 'ctools_node_language_ctools_access_check',
17 'default' => array('language' => array()),
18 'settings form' => 'ctools_node_language_ctools_access_settings',
19 'settings form submit' => 'ctools_node_language_ctools_access_settings_submit',
20 'summary' => 'ctools_node_language_ctools_access_summary',
21 'required context' => new ctools_context_required(t('Node'), 'node'),
22 );
23 }
24
25 /**
26 * Settings form for the 'by node_language' access plugin
27 */
28 function ctools_node_language_ctools_access_settings($form, &$form_state, $conf) {
29 $options = array(
30 'current' => t('Current site language'),
31 'default' => t('Default site language'),
32 'no_language' => t('No language'),
33 );
34 $options = array_merge($options, locale_language_list());
35 $form['settings']['language'] = array(
36 '#title' => t('Language'),
37 '#type' => 'checkboxes',
38 '#options' => $options,
39 '#description' => t('Pass only if the node is in one of the selected languages.'),
40 '#default_value' => $conf['language'],
41 );
42 return $form;
43 }
44
45 /**
46 * Check for access.
47 */
48 function ctools_node_language_ctools_access_check($conf, $context) {
49 // As far as I know there should always be a context at this point, but this
50 // is safe.
51 if (empty($context) || empty($context->data) || !isset($context->data->language)) {
52 return FALSE;
53 }
54
55 global $language;
56
57 // Specialcase: if 'no language' is checked, return TRUE if the language field is
58 // empty.
59 if (!empty($conf['language']['no_language'])) {
60 if (empty($context->data->language)) {
61 return TRUE;
62 }
63 }
64
65 // Specialcase: if 'current' is checked, return TRUE if the current site language
66 // matches the node language.
67 if (!empty($conf['language']['current'])) {
68 if ($context->data->language == $language->language) {
69 return TRUE;
70 }
71 }
72
73 // Specialcase: If 'default' is checked, return TRUE if the default site language
74 // matches the node language.
75 if (!empty($conf['language']['default'])) {
76 if ($context->data->language == language_default('language')) {
77 return TRUE;
78 }
79 }
80
81 if (array_filter($conf['language']) && empty($conf['language'][$context->data->language])) {
82 return FALSE;
83 }
84
85 return TRUE;
86 }
87
88 /**
89 * Provide a summary description based upon the checked node_languages.
90 */
91 function ctools_node_language_ctools_access_summary($conf, $context) {
92 $languages = array(
93 'current' => t('Current site language'),
94 'default' => t('Default site language'),
95 'no_language' => t('No language'),
96 );
97 $languages = array_merge($languages, locale_language_list());
98
99 if (!isset($conf['language'])) {
100 $conf['language'] = array();
101 }
102
103 $names = array();
104 foreach (array_filter($conf['language']) as $language) {
105 $names[] = $languages[$language];
106 }
107
108 if (empty($names)) {
109 return t('@identifier is in any language', array('@identifier' => $context->identifier));
110 }
111
112 return format_plural(count($names), '@identifier language is "@languages"', '@identifier language is one of "@languages"', array('@languages' => implode(', ', $names), '@identifier' => $context->identifier));
113 }
114