diff --git a/includes/context-access-admin.inc b/includes/context-access-admin.inc index 257c4c92584fee5613bf2d1d2b777e97bd28d5a5..b58d480c24c7f3a1fc9e3e5cf78b58f2a5e479a8 100644 --- a/includes/context-access-admin.inc +++ b/includes/context-access-admin.inc @@ -452,7 +452,9 @@ function ctools_access_ajax_edit_item_submit(&$form, &$form_state) { } $form_state['test']['settings'] = $form_state['values']['settings']; - $form_state['test']['context'] = $form_state['values']['context']; + if (isset($form_state['values']['context'])) { + $form_state['test']['context'] = $form_state['values']['context']; + } } /** diff --git a/includes/context.inc b/includes/context.inc index c6678b7b3adc8fb53709ca4945e34f5228e8dd14..9be416db6ac2a301de2bab896373de41397445d8 100644 --- a/includes/context.inc +++ b/includes/context.inc @@ -933,7 +933,9 @@ function ctools_access_summary($plugin, $contexts, $test) { $description = ''; if ($function = ctools_plugin_get_function($plugin, 'summary')) { - $description = $function($test['settings'], ctools_context_select($contexts, $plugin['required context'], $test['context'])); + $required_context = isset($plugin['required context']) ? $plugin['required context'] : array(); + $context = isset($test['context']) ? $test['context'] : array(); + $description = $function($test['settings'], ctools_context_select($contexts, $required_context, $context)); } return $description; } @@ -969,7 +971,9 @@ function ctools_access($settings, $contexts = array()) { $pass = FALSE; $plugin = ctools_get_access_plugin($test['name']); if ($plugin && $function = ctools_plugin_get_function($plugin, 'callback')) { - $pass = $function($test['settings'], ctools_context_select($contexts, $plugin['required context'], $test['context'])); + $required_context = isset($plugin['required context']) ? $plugin['required context'] : array(); + $context = isset($test['context']) ? $test['context'] : array(); + $pass = $function($test['settings'], ctools_context_select($contexts, $required_context, $context)); } if ($pass && $settings['logic'] == 'or') { diff --git a/plugins/access/node_language.inc b/plugins/access/node_language.inc index 7ce2254e5f882dc78c1d3c63e0d9407507c28c57..b787db07f2da4cb9147656d0397840bfbb17a592 100644 --- a/plugins/access/node_language.inc +++ b/plugins/access/node_language.inc @@ -33,10 +33,11 @@ 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('Access type'), + '#title' => t('Language'), '#type' => 'checkboxes', '#options' => $options, '#description' => t('Pass only if the node is in one of the selected languages.'), @@ -56,6 +57,14 @@ function ctools_node_language_ctools_access_check($conf, $context) { 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'])) { @@ -86,6 +95,7 @@ function ctools_node_language_ctools_acesss_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()); diff --git a/plugins/access/site_language.inc b/plugins/access/site_language.inc new file mode 100644 index 0000000000000000000000000000000000000000..5f0b0d2d6fedeadb399347179298d01b18ac2fed --- /dev/null +++ b/plugins/access/site_language.inc @@ -0,0 +1,90 @@ +<?php +// $Id$ + +/** + * @file + * Plugin to provide access control based upon node type. + */ + +/** + * Implementation of specially named hook_ctools_arguments(). + */ +function ctools_site_language_ctools_access() { + if (module_exists('locale')) { + $args['site_language'] = array( + 'title' => t("Access by site language"), + 'description' => t('Control access by the language the site currently uses.'), + 'callback' => 'ctools_site_language_ctools_access_check', + 'default' => array('language' => array()), + 'settings form' => 'ctools_site_language_ctools_access_settings', + 'settings form submit' => 'ctools_site_language_ctools_access_settings_submit', + 'summary' => 'ctools_site_language_ctools_acesss_summary', +// 'required context' => new ctools_context_required(t('Node'), 'node'), + ); + } + + return $args; +} + +/** + * Settings form for the 'by site_language' access plugin + */ +function ctools_site_language_ctools_access_settings(&$form, &$form_state, $conf) { + $options = array( + 'default' => t('Default site 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 current site language is one of the selected languages.'), + '#default_value' => $conf['language'], + ); +} + +/** + * Check for access. + */ +function ctools_site_language_ctools_access_check($conf, $context) { + global $language; + + // Specialcase: If 'default' is checked, return TRUE if the default site language + // matches the node language. + if (!empty($conf['language']['default'])) { + if ($language->language == language_default('language')) { + return TRUE; + } + } + + if (array_filter($conf['language']) && empty($conf['language'][$language->language])) { + return FALSE; + } + + return TRUE; +} + +/** + * Provide a summary description based upon the checked site_languages. + */ +function ctools_site_language_ctools_acesss_summary($conf, $context) { + $languages = array( + 'default' => t('Default site 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('Site language can be in any language'); + } + + return format_plural(count($names), 'Site language can be languages "@languages"', 'Site language can be language "@languages"', array('@languages' => implode(', ', $names))); +} \ No newline at end of file