first import

This commit is contained in:
Bachir Soussi Chiadmi
2015-04-08 11:40:19 +02:00
commit 1bc61b12ad
8435 changed files with 1582817 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
<?php
/**
* @file
* Ctools access plugin to provide access/visiblity if two user contexts are equal.
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t("User: compare"),
'description' => t('Compare two users (logged-in user and user being viewed, for example)'),
'callback' => 'ctools_compare_users_access_check',
'default' => array(
'equality' => 1,
),
'settings form' => 'ctools_compare_users_settings',
'summary' => 'ctools_compare_users_ctools_access_summary',
'required context' => array(
new ctools_context_required(t('First User'), 'user'),
new ctools_context_required(t("Second User"), 'user')
),
);
/**
* Settings form for the 'by perm' access plugin
*/
function ctools_compare_users_settings($form, &$form_state, $conf) {
$form['settings']['helptext'] = array(
'#type' => 'markup',
'#value' => '<div>' . t('Grant access based on comparison of the two user contexts. For example, to grant access to a user to view their own profile, choose "logged in user" and "user being viewed" and say "grant access if equal". When they\'re the same, access will be granted.') . '</div>',
);
$form['settings']['equality'] = array(
'#type' => 'radios',
'#title' => t('Grant access if user contexts are'),
'#options' => array(1 => t('Equal'), 0 => t('Not equal')),
'#default_value' => $conf['equality'],
);
return $form;
}
/**
* Check for access.
*/
function ctools_compare_users_access_check($conf, $context) {
if (empty($context) || count($context) != 2 || empty($context[0]->data) || empty($context[1]->data)) {
return FALSE;
}
$account1 = $context[0]->data;
$account2 = $context[1]->data;
// xor returns false if the two bools are the same, and true if they are not.
// i.e, if we asked for equality and they are equal, return true.
// If we asked for inequality and they are equal, return false.
return ($account1->uid == $account2->uid) xor empty($conf['equality']);
}
/**
* Describe an instance of this plugin.
*/
function ctools_compare_users_ctools_access_summary($conf, $context) {
$comparison = !empty($conf['equality']) ? "is" : 'is not';
return t('@id1 @comp @id2', array('@comp' => $comparison, '@id1' => $context[0]->identifier, '@id2' => $context[1]->identifier));
}

View File

@@ -0,0 +1,51 @@
<?php
/**
* @file
* Plugin to provide access control/visibility based on existence of a specified context
*/
$plugin = array(
'title' => t("Context exists"),
'description' => t('Control access by whether or not a context exists and contains data.'),
'callback' => 'ctools_context_exists_ctools_access_check',
'settings form' => 'ctools_context_exists_ctools_access_settings',
'summary' => 'ctools_context_exists_ctools_access_summary',
'required context' => new ctools_context_required(t('Context'), 'any', TRUE),
'defaults' => array('exists' => TRUE),
);
/**
* Settings form
*/
function ctools_context_exists_ctools_access_settings($form, &$form_state, $conf) {
$form['settings']['exists'] = array(
'#type' => 'radios',
'#description' => t("Check to see if the context exists (contains data) or does not exist (contains no data). For example, if a context is optional and the path does not contain an argument for that context, it will not exist."),
'#options' => array(TRUE => t('Exists'), FALSE => t("Doesn't exist")),
'#default_value' => $conf['exists'],
);
return $form;
}
/**
* Check for access
*/
function ctools_context_exists_ctools_access_check($conf, $context) {
// xor returns false if the two bools are the same, and true if they are not.
// i.e, if we asked for context_exists and it does, return true.
// If we asked for context does not exist and it does, return false.
return (empty($context->data) xor !empty($conf['exists']));
}
/**
* Provide a summary description based upon the specified context
*/
function ctools_context_exists_ctools_access_summary($conf, $context) {
if (!empty($conf['exists'])) {
return t('@identifier exists', array('@identifier' => $context->identifier));
}
else {
return t('@identifier does not exist', array('@identifier' => $context->identifier));
}
}

View File

@@ -0,0 +1,136 @@
<?php
/**
* @file
* Plugin to provide access control based upon entity bundle.
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t("Entity: bundle"),
'description' => t('Control access by entity bundle.'),
'callback' => 'ctools_entity_bundle_ctools_access_check',
'default' => array('type' => array()),
'settings form' => 'ctools_entity_bundle_ctools_access_settings',
'settings form submit' => 'ctools_entity_bundle_ctools_access_settings_submit',
'summary' => 'ctools_entity_bundle_ctools_access_summary',
'restrictions' => 'ctools_entity_bundle_ctools_access_restrictions',
'get child' => 'ctools_entity_bundle_ctools_access_get_child',
'get children' => 'ctools_entity_bundle_ctools_access_get_children',
);
function ctools_entity_bundle_ctools_access_get_child($plugin, $parent, $child) {
$plugins = ctools_entity_bundle_ctools_access_get_children($plugin, $parent);
return $plugins[$parent . ':' . $child];
}
function ctools_entity_bundle_ctools_access_get_children($plugin, $parent) {
$entities = entity_get_info();
$plugins = array();
foreach ($entities as $entity_type => $entity) {
$plugin['title'] = t('@entity: Bundle', array('@entity' => $entity['label']));
$plugin['keyword'] = $entity_type;
$plugin['description'] = t('Control access by @entity entity bundle.', array('@entity' => $entity_type));
$plugin['name'] = $parent . ':' . $entity_type;
$plugin['required context'] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type);
$plugins[$parent . ':' . $entity_type] = $plugin;
}
return $plugins;
}
/**
* Settings form for the 'by entity_bundle' access plugin
*/
function ctools_entity_bundle_ctools_access_settings($form, &$form_state, $conf) {
$plugin = $form_state['plugin'];
$entity_type = explode(':', $plugin['name']);
$entity_type = $entity_type[1];
$entity = entity_get_info($entity_type);
foreach ($entity['bundles'] as $type => $info) {
$options[$type] = check_plain($info['label']);
}
$form['settings']['type'] = array(
'#title' => t('Entity Bundle'),
'#type' => 'checkboxes',
'#options' => $options,
'#description' => t('Only the checked entity bundles will be valid.'),
'#default_value' => $conf['type'],
);
return $form;
}
/**
* Compress the entity bundles allowed to the minimum.
*/
function ctools_entity_bundle_ctools_access_settings_submit($form, &$form_state) {
$form_state['values']['settings']['type'] = array_filter($form_state['values']['settings']['type']);
}
/**
* Check for access.
*/
function ctools_entity_bundle_ctools_access_check($conf, $context, $plugin) {
list($plugin_name, $entity_type) = explode(':', $plugin['name']);
if (!$entity_type) {
return FALSE;
};
$entity = entity_get_info($entity_type);
// As far as I know there should always be a context at this point, but this
// is safe.
if (empty($context) || empty($context->data) || empty($context->data->{$entity['entity keys']['bundle']})) {
return FALSE;
}
if (array_filter($conf['type']) && empty($conf['type'][$context->data->{$entity['entity keys']['bundle']}])) {
return FALSE;
}
return TRUE;
}
/**
* Inform the UI that we've eliminated a bunch of possibilities for this
* context.
*/
function ctools_entity_bundle_ctools_access_restrictions($conf, &$context) {
if (isset($context->restrictions['type'])) {
$context->restrictions['type'] = array_unique(array_merge($context->restrictions['type'], array_keys(array_filter($conf['type']))));
}
else {
$context->restrictions['type'] = array_keys(array_filter($conf['type']));
}
}
/**
* Provide a summary description based upon the checked entity_bundle.
*/
function ctools_entity_bundle_ctools_access_summary($conf, $context, $plugin) {
if (!isset($conf['type'])) {
$conf['type'] = array();
}
list($plugin_name, $entity_type) = explode(':', $plugin['name']);
if (!$entity_type) {
return t('Error, misconfigured entity_bundle access plugin');
};
$entity = entity_get_info($entity_type);
$names = array();
foreach (array_filter($conf['type']) as $type) {
$names[] = check_plain($entity['bundles'][$type]['label']);
}
if (empty($names)) {
return t('@identifier is any bundle', array('@identifier' => $context->identifier));
}
return format_plural(count($names), '@identifier is bundle "@types"', '@identifier bundle is one of "@types"', array('@types' => implode(', ', $names), '@identifier' => $context->identifier));
}

View File

@@ -0,0 +1,239 @@
<?php
/**
* @file
* Plugin to provide access control based upon entity bundle.
*/
$plugin = array(
'title' => t("(Custom) Entity: Field Value"),
'description' => t('Control access by entity field value.'),
'callback' => 'ctools_entity_field_value_ctools_access_check',
'default' => array('type' => array()),
'settings form' => 'ctools_entity_field_value_ctools_access_settings',
'settings form submit' => 'ctools_entity_field_value_ctools_access_settings_submit',
'summary' => 'ctools_entity_field_value_ctools_access_summary',
'get child' => 'ctools_entity_field_value_ctools_access_get_child',
'get children' => 'ctools_entity_field_value_ctools_access_get_children',
);
function ctools_entity_field_value_ctools_access_get_child($plugin, $parent, $child) {
$plugins = &drupal_static(__FUNCTION__, array());
if (empty($plugins[$parent . ':' . $child])) {
list($entity_type, $bundle_type, $field_name) = explode(':', $child);
$plugins[$parent . ':' . $child] = _ctools_entity_field_value_ctools_access_get_child($plugin, $parent, $entity_type, $bundle_type, $field_name);
}
return $plugins[$parent . ':' . $child];
}
function ctools_entity_field_value_ctools_access_get_children($plugin, $parent) {
$plugins = &drupal_static(__FUNCTION__, array());
if (!empty($plugins)) {
return $plugins;
}
$entities = entity_get_info();
foreach ($entities as $entity_type => $entity) {
foreach ($entity['bundles'] as $bundle_type => $bundle) {
foreach (field_info_instances($entity_type, $bundle_type) as $field_name => $field) {
if (!isset($plugins[$parent . ':' . $entity_type . ':' . $bundle_type . ':' . $field_name])) {
$plugin = _ctools_entity_field_value_ctools_access_get_child($plugin, $parent, $entity_type, $bundle_type, $field_name, $entity, $bundle, $field);
$plugins[$parent . ':' . $entity_type . ':' . $bundle_type . ':' . $field_name] = $plugin;
}
}
}
}
return $plugins;
}
function _ctools_entity_field_value_ctools_access_get_child($plugin, $parent, $entity_type, $bundle_type, $field_name, $entity = NULL, $bundle = NULL, $field = NULL) {
// check that the entity, bundle and field arrays have a value.
// If not, load theme using machine names.
if (empty($entity)) {
$entity = entity_get_info($entity_type);
}
if (empty($bundle)) {
$bundle = $entity['bundles'][$bundle_type];
}
if (empty($field)) {
$field_instances = field_info_instances($entity_type, $bundle_type);
$field = $field_instances[$field_name];
}
$plugin['title'] = t('@entity @type: @field Field', array('@entity' => $entity['label'], '@type' => $bundle_type, '@field' => $field['label']));
$plugin['keyword'] = $entity_type;
$plugin['description'] = t('Control access by @entity entity bundle.', array('@entity' => $entity_type));
$plugin['name'] = $parent . ':' . $entity_type . ':' . $bundle_type . ':' . $field_name;
$plugin['required context'] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type, array(
'type' => $bundle_type,
));
return $plugin;
}
/**
* Settings form for the 'by entity_bundle' access plugin
*/
function ctools_entity_field_value_ctools_access_settings($form, &$form_state, $conf) {
$plugin = $form_state['plugin'];
list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']);
$entity_info = entity_get_info($entity_type);
$instances = field_info_instances($entity_type, $bundle_type);
$instance = $instances[$field_name];
$field = field_info_field_by_id($instance['field_id']);
foreach ($field['columns'] as $column => $attributes) {
$columns[] = _field_sql_storage_columnname($field_name, $column);
}
ctools_include('fields');
$entity = (object)array(
$entity_info['entity keys']['bundle'] => $bundle_type,
);
$langcode = field_valid_language(NULL);
$form['settings'] += (array) ctools_field_invoke_field($instance, 'form', $entity_type, $entity, $form, $form_state, array('default' => TRUE, 'language' => $langcode));
// weight is really not important once this is populated and will only interfere with the form layout.
foreach (element_children($form['settings']) as $element) {
unset($form['settings'][$element]['#weight']);
}
// Need more logic here to handle compound fields.
foreach ($columns as $column) {
if (isset($conf[$column]) && is_array($conf[$column])) {
foreach ($conf[$column] as $delta => $conf_value) {
if (is_numeric($delta) && is_array($conf_value)) {
$form['settings'][$field_name][LANGUAGE_NONE][$delta]['value']['#default_value'] = $conf_value['value'];
}
}
}
else {
$form['settings'][$field_name][LANGUAGE_NONE]['#default_value'] = $conf[$column];
}
}
return $form;
}
/**
* Compress the entity bundles allowed to the minimum.
*/
function ctools_entity_field_value_ctools_access_settings_submit($form, &$form_state) {
$plugin = $form_state['plugin'];
list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']);
$langcode = field_valid_language(NULL);
$langcode = isset($form_state['input']['settings'][$field_name][$langcode]) ? $langcode : LANGUAGE_NONE;
$instances = field_info_instances($entity_type, $bundle_type);
$instance = $instances[$field_name];
$field = field_info_field_by_id($instance['field_id']);
foreach ($field['columns'] as $column => $attributes) {
$columns[] = _field_sql_storage_columnname($field_name, $column);
}
foreach ($columns as $column) {
$form_state['values']['settings'][$column] = $form_state['input']['settings'][$field_name][$langcode];
}
}
/**
* Check for access.
*/
function ctools_entity_field_value_ctools_access_check($conf, $context, $plugin) {
list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']);
if ($field_items = field_get_items($entity_type, $context->data, $field_name)) {
$langcode = field_language($entity_type, $context->data, $field_name);
// Get field storage columns.
$instance = field_info_instance($entity_type, $field_name, $bundle_type);
$field = field_info_field_by_id($instance['field_id']);
$columns = array();
foreach ($field['columns'] as $column => $attributes) {
$columns[$column] = _field_sql_storage_columnname($field_name, $column);
}
foreach ($conf as $potential_field => $values) {
if ($field_name === $potential_field) {
$conf_value_array = _ctools_entity_field_value_ctools_access_get_conf_field_values($values, $langcode);
if (empty($conf_value_array)) {
return FALSE;
}
// Check field value.
foreach ($field_items as $field_value) {
foreach ($field_value as $field_column => $value) {
// Iterate through config values.
foreach ($conf_value_array as $conf_value) {
//
if ($value == $conf_value[$field_column]) {
return TRUE;
}
}
}
}
}
}
}
return FALSE;
}
function _ctools_entity_field_value_ctools_access_get_conf_field_values($values, $langcode = LANGUAGE_NONE) {
if (!is_array($values) || !isset($values[$langcode])) {
return;
}
$conf_values = array();
foreach ($values[$langcode] as $delta => $value) {
$conf_values[$delta] = $value;
}
return $conf_values;
}
/**
* Provide a summary description based upon the checked entity_bundle.
*/
function ctools_entity_field_value_ctools_access_summary($conf, $context, $plugin) {
list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']);
$instances = field_info_instances($entity_type, $bundle_type);
$instance = $instances[$field_name];
$field = field_info_field_by_id($instance['field_id']);
$entity_info = entity_get_info($entity_type);
$entity = (object)array(
$entity_info['entity keys']['bundle'] => $bundle_type,
);
$string = '';
$keys = array();
$values = array();
foreach ($field['columns'] as $column => $attributes) {
$conf_key = _field_sql_storage_columnname($field_name, $column);
if (count($field['columns']) > 1) {
// Add some sort of handling for compound fields
}
else {
if (isset($conf[$conf_key])) {
$entity->{$field_name}[LANGUAGE_NONE][] = array($column => $conf[$conf_key]);
}
}
$string .= " @{$column} equals @{$column}_value";
$keys['@' . $column] = $column;
$values["@{$column}_value"] = $conf[$conf_key];
}
$view_mode = 'full';
$null = NULL;
$options = array('language' => LANGUAGE_NONE);
ctools_include('fields');
$display = field_get_display($instance, $view_mode, $entity);
$display['type'] = 'list_default';
$function = $display['module'] . '_field_formatter_view';
$items = isset($entity->{$field_name}[LANGUAGE_NONE]) ? $entity->{$field_name}[LANGUAGE_NONE] : array();
if (function_exists($function)) {
$elements = $function($entity_type, $entity, $field, $instance, LANGUAGE_NONE, $items, $display);
}
$value_keys = array_keys($values);
foreach ($value_keys as $key => $value) {
$values[$value] = $elements[$key]['#markup'];
}
$values = array_merge($keys, $values);
return t($string, $values);
}

View File

@@ -0,0 +1,46 @@
<?php
/**
* @file
* Plugin to provide access control based on drupal_is_front_page.
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t('Front page'),
'description' => t('Is this the front page.'),
'callback' => 'ctools_front_ctools_access_check',
'default' => array('negate' => 0),
'settings form' => 'ctools_front_ctools_access_settings',
'summary' => 'ctools_front_ctools_access_summary',
);
/**
* Settings form for the 'by parent term' access plugin
*/
function ctools_front_ctools_access_settings($form, &$form_state, $conf) {
// No additional configuration necessary.
return $form;
}
/**
* Check for access.
*/
function ctools_front_ctools_access_check($conf, $context) {
if (drupal_is_front_page()) {
return TRUE;
}
else {
return FALSE;
}
}
/**
* Provide a summary description based upon the checked terms.
*/
function ctools_front_ctools_access_summary($conf, $context) {
return t('The front page');
}

View File

@@ -0,0 +1,89 @@
<?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.
*/
$plugin = array(
'title' => t("Node: accessible"),
'description' => t('Control access with built in Drupal node access test.'),
'callback' => 'ctools_node_access_ctools_access_check',
'default' => array('type' => 'view'),
'settings form' => 'ctools_node_access_ctools_access_settings',
'settings form submit' => 'ctools_node_access_ctools_access_settings_submit',
'summary' => 'ctools_node_access_ctools_access_summary',
'required context' => array(
new ctools_context_required(t('User'), 'user'),
new ctools_context_required(t('Node'), 'node'),
),
);
/**
* Settings form for the 'by node_access' access plugin
*/
function ctools_node_access_ctools_access_settings($form, &$form_state, $conf) {
$form['settings']['type'] = array(
'#title' => t('Operation'),
'#type' => 'radios',
'#options' => array(
'view' => t('View'),
'update' => t('Update'),
'delete' => t('Delete'),
'create' => t('Create nodes of the same type'),
),
'#description' => t('Using built in Drupal node access rules, determine if the user can perform the selected operation on the node.'),
'#default_value' => $conf['type'],
);
return $form;
}
/**
* Check for access.
*/
function ctools_node_access_ctools_access_check($conf, $context) {
// As far as I know there should always be a context at this point, but this
// is safe.
list($user_context, $node_context) = $context;
if (empty($node_context) || empty($node_context->data) || empty($node_context->data->type)) {
return FALSE;
}
if (empty($user_context) || empty($user_context->data)) {
return FALSE;
}
if ($conf['type'] == 'create') {
return node_access('create', $node_context->data->type, $user_context->data);
}
else {
return node_access($conf['type'], $node_context->data, $user_context->data);
}
}
/**
* Provide a summary description based upon the checked node_accesss.
*/
function ctools_node_access_ctools_access_summary($conf, $context) {
list($user_context, $node_context) = $context;
$replacement = array('@user' => $user_context->identifier, '@node' => $node_context->identifier);
switch ($conf['type']) {
case 'view':
return t('@user can view @node.', $replacement);
case 'update':
return t('@user can edit @node.', $replacement);
case 'delete':
return t('@user can delete @node.', $replacement);
case 'create':
return t('@user can create nodes of the same type as @node.', $replacement);
}
}

View File

@@ -0,0 +1,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));
}

View File

@@ -0,0 +1,33 @@
<?php
/**
* @file
* Plugin to provide access control based upon node (un)published status.
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t("Node: (un)published"),
'description' => t('Control access by the nodes published status.'),
'callback' => 'ctools_node_status_ctools_access_check',
'summary' => 'ctools_node_status_ctools_access_summary',
'required context' => new ctools_context_required(t('Node'), 'node'),
);
/**
* Check for access.
*/
function ctools_node_status_ctools_access_check($conf, $context) {
return (!empty($context->data) && $context->data->status);
}
/**
* Provide a summary description based upon the checked node_statuss.
*/
function ctools_node_status_ctools_access_summary($conf, $context) {
return t('Returns true if the nodes status is "published".');
}

View File

@@ -0,0 +1,117 @@
<?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.
*/
$plugin = array(
'title' => t("Node: type"),
'description' => t('Control access by node_type.'),
'callback' => 'ctools_node_type_ctools_access_check',
'default' => array('type' => array()),
'settings form' => 'ctools_node_type_ctools_access_settings',
'settings form submit' => 'ctools_node_type_ctools_access_settings_submit',
'summary' => 'ctools_node_type_ctools_access_summary',
'required context' => new ctools_context_required(t('Node'), 'node'),
'restrictions' => 'ctools_node_type_ctools_access_restrictions',
);
/**
* Settings form for the 'by node_type' access plugin
*/
function ctools_node_type_ctools_access_settings($form, &$form_state, $conf) {
$types = node_type_get_types();
foreach ($types as $type => $info) {
$options[$type] = check_plain($info->name);
}
$form['settings']['type'] = array(
'#title' => t('Node type'),
'#type' => 'checkboxes',
'#options' => $options,
'#description' => t('Only the checked node types will be valid.'),
'#default_value' => $conf['type'],
);
return $form;
}
/**
* Compress the node_types allowed to the minimum.
*/
function ctools_node_type_ctools_access_settings_submit($form, &$form_state) {
$form_state['values']['settings']['type'] = array_filter($form_state['values']['settings']['type']);
}
/**
* Check for access.
*/
function ctools_node_type_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) || empty($context->data->type)) {
return FALSE;
}
if (array_filter($conf['type']) && empty($conf['type'][$context->data->type])) {
return FALSE;
}
return TRUE;
}
/**
* Inform the UI that we've eliminated a bunch of possibilities for this
* context.
*/
function ctools_node_type_ctools_access_restrictions($conf, &$context) {
if (isset($context->restrictions['type'])) {
$context->restrictions['type'] = array_unique(array_merge($context->restrictions['type'], array_keys(array_filter($conf['type']))));
}
else {
$context->restrictions['type'] = array_keys(array_filter($conf['type']));
}
}
/**
* Provide a summary description based upon the checked node_types.
*/
function ctools_node_type_ctools_access_summary($conf, $context) {
if (!isset($conf['type'])) {
$conf['type'] = array();
}
$types = node_type_get_types();
$names = array();
// If a node type doesn't exist, let the user know, but prevent a notice.
$missing_types = array();
foreach (array_filter($conf['type']) as $type) {
if (!empty($types[$type])) {
$names[] = check_plain($types[$type]->name);
}
else {
$missing_types[] = check_plain($type);
}
}
if (empty($names) && empty($missing_types)) {
return t('@identifier is any node type', array('@identifier' => $context->identifier));
}
if (!empty($missing_types)) {
$output = array();
if (!empty($names)) {
$output[] = format_plural(count($names), '@identifier is type "@types"', '@identifier type is one of "@types"', array('@types' => implode(', ', $names), '@identifier' => $context->identifier));
}
$output[] = format_plural(count($missing_types), 'Missing/ deleted type "@types"', 'Missing/ deleted type is one of "@types"', array('@types' => implode(', ', $missing_types)));
return implode(' | ', $output);
}
return format_plural(count($names), '@identifier is type "@types"', '@identifier type is one of "@types"', array('@types' => implode(', ', $names), '@identifier' => $context->identifier));
}

View File

@@ -0,0 +1,88 @@
<?php
/**
* @file
* Plugin to provide access control/visibility based on path.
*/
$plugin = array(
'title' => t('String: URL path'),
'description' => t('Control access by the current path.'),
'callback' => 'ctools_path_visibility_ctools_access_check',
'settings form' => 'ctools_path_visibility_ctools_access_settings',
'summary' => 'ctools_path_visibility_ctools_access_summary',
'required context' => new ctools_context_optional(t('Path'), 'string'),
'default' => array('visibility_setting' => 1, 'paths' => ''),
);
/**
* Settings form
*/
function ctools_path_visibility_ctools_access_settings($form, &$form_state, $conf) {
$form['settings']['note'] = array(
'#value' => '<div class="description">' . t('Note: if no context is chosen, the current page path will be used.') . '</div>',
);
$form['settings']['visibility_setting'] = array(
'#type' => 'radios',
'#options' => array(
1 => t('Allow access on the following pages'),
0 => t('Allow access on all pages except the following pages'),
),
'#default_value' => $conf['visibility_setting'],
);
$form['settings']['paths'] = array(
'#type' => 'textarea',
'#title' => t('Paths'),
'#default_value' => $conf['paths'],
'#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>')),
);
return $form;
}
/**
* Check for access.
*/
function ctools_path_visibility_ctools_access_check($conf, $context) {
if (isset($context->data)) {
$base_path = $context->data;
}
else {
$base_path = $_GET['q'];
}
$path = drupal_get_path_alias($base_path);
$page_match = drupal_match_path($path, $conf['paths']);
// If there's a path alias, we may still be at the un-aliased path
// so check that as well.
if (!isset($context->data) && $path != $base_path) {
$page_match = $page_match || drupal_match_path($base_path, $conf['paths']);
}
// When $conf['visibility_setting'] has a value of 0, the block is displayed
// on all pages except those listed in $block->pages. When set to 1, it
// is displayed only on those pages listed in $block->pages.
$page_match = !($conf['visibility_setting'] xor $page_match);
return $page_match;
}
/**
* Provide a summary description.
*/
function ctools_path_visibility_ctools_access_summary($conf, $context) {
$paths = array();
foreach (explode("\n", $conf['paths']) as $path) {
$paths[] = check_plain($path);
}
$identifier = $context->type == 'any' ? t('Current path') : $context->identifier;
if ($conf['visibility_setting']) {
return format_plural(count($paths), '@identifier is "@paths"', '@identifier type is one of "@paths"', array('@paths' => implode(', ', $paths), '@identifier' => $identifier));
}
else {
return format_plural(count($paths), '@identifier is not "@paths"', '@identifier type is not one of "@paths"', array('@paths' => implode(', ', $paths), '@identifier' => $identifier));
}
}

View File

@@ -0,0 +1,73 @@
<?php
/**
* @file
* Plugin to provide access control based on user permission strings.
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t("User: permission"),
'description' => t('Control access by permission string.'),
'callback' => 'ctools_perm_ctools_access_check',
'default' => array('perm' => 'access content'),
'settings form' => 'ctools_perm_ctools_access_settings',
'summary' => 'ctools_perm_ctools_access_summary',
'required context' => new ctools_context_required(t('User'), 'user'),
);
/**
* Settings form for the 'by perm' access plugin
*/
function ctools_perm_ctools_access_settings($form, &$form_state, $conf) {
$perms = array();
// Get list of permissions
foreach (module_list(FALSE, FALSE, TRUE) as $module) {
// By keeping them keyed by module we can use optgroups with the
// 'select' type.
if ($permissions = module_invoke($module, 'permission')) {
foreach ($permissions as $id => $permission) {
$perms[$module][$id] = $permission['title'];
}
}
}
$form['settings']['perm'] = array(
'#type' => 'select',
'#options' => $perms,
'#title' => t('Permission'),
'#default_value' => $conf['perm'],
'#description' => t('Only users with the selected permission flag will be able to access this.'),
);
return $form;
}
/**
* Check for access.
*/
function ctools_perm_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)) {
return FALSE;
}
return user_access($conf['perm'], $context->data);
}
/**
* Provide a summary description based upon the checked roles.
*/
function ctools_perm_ctools_access_summary($conf, $context) {
if (!isset($conf['perm'])) {
return t('Error, unset permission');
}
$permissions = module_invoke_all('permission');
return t('@identifier has "@perm"', array('@identifier' => $context->identifier, '@perm' => $permissions[$conf['perm']]['title']));
}

View File

@@ -0,0 +1,64 @@
<?php
/**
* @file
* Plugin to provide access control based on evaluated PHP.
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t("PHP Code"),
'description' => t('Control access through arbitrary PHP code.'),
'callback' => 'ctools_php_ctools_access_check',
'default' => array('description' => '', 'php' => ''),
'settings form' => 'ctools_php_ctools_access_settings',
'summary' => 'ctools_php_ctools_access_summary',
'all contexts' => TRUE,
);
/**
* Settings form for the 'by perm' access plugin
*
* @todo Need a way to provide a list of all available contexts to be used by
* the eval-ed PHP.
*/
function ctools_php_ctools_access_settings($form, &$form_state, $conf) {
$perms = array();
$form['settings']['description'] = array(
'#type' => 'textfield',
'#title' => t('Administrative desc'),
'#default_value' => $conf['description'],
'#description' => t('A description for this test for administrative purposes.'),
);
$form['settings']['php'] = array(
'#type' => 'textarea',
'#title' => t('PHP Code'),
'#default_value' => $conf['php'],
'#description' => t('Access will be granted if the following PHP code returns <code>TRUE</code>. Do not include &lt;?php ?&gt;. Note that executing incorrect PHP-code can break your Drupal site. All contexts will be available in the <em>$contexts</em> variable.'),
);
if (!user_access('use PHP for settings')) {
$form['settings']['php']['#disabled'] = TRUE;
$form['settings']['php']['#value'] = $conf['php'];
$form['settings']['php']['#description'] .= ' ' . t('You do not have sufficient permissions to edit PHP code.');
}
return $form;
}
/**
* Check for access.
*/
function ctools_php_ctools_access_check($__conf, $contexts) {
$access = eval($__conf['php']);
return $access;
}
/**
* Provide a summary description based upon the checked roles.
*/
function ctools_php_ctools_access_summary($conf, $contexts) {
return !empty($conf['description']) ? check_plain($conf['description']) : t('No description');
}

View File

@@ -0,0 +1,79 @@
<?php
/**
* @file
* Plugin to provide access control based upon role membership.
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t("User: role"),
'description' => t('Control access by role.'),
'callback' => 'ctools_role_ctools_access_check',
'default' => array('rids' => array()),
'settings form' => 'ctools_role_ctools_access_settings',
'settings form submit' => 'ctools_role_ctools_access_settings_submit',
'summary' => 'ctools_role_ctools_access_summary',
'required context' => new ctools_context_required(t('User'), 'user'),
);
/**
* Settings form for the 'by role' access plugin
*/
function ctools_role_ctools_access_settings($form, &$form_state, $conf) {
$form['settings']['rids'] = array(
'#type' => 'checkboxes',
'#title' => t('Role'),
'#default_value' => $conf['rids'],
'#options' => ctools_get_roles(),
'#description' => t('Only the checked roles will be granted access.'),
);
return $form;
}
/**
* Compress the roles allowed to the minimum.
*/
function ctools_role_ctools_access_settings_submit($form, &$form_state) {
$form_state['values']['settings']['rids'] = array_keys(array_filter($form_state['values']['settings']['rids']));
}
/**
* Check for access.
*/
function ctools_role_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->roles)) {
return FALSE;
}
$roles = array_keys($context->data->roles);
$roles[] = $context->data->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
return (bool) array_intersect($conf['rids'], $roles);
}
/**
* Provide a summary description based upon the checked roles.
*/
function ctools_role_ctools_access_summary($conf, $context) {
if (!isset($conf['rids'])) {
$conf['rids'] = array();
}
$roles = ctools_get_roles();
$names = array();
foreach (array_filter($conf['rids']) as $rid) {
$names[] = check_plain($roles[$rid]);
}
if (empty($names)) {
return t('@identifier can have any role', array('@identifier' => $context->identifier));
}
return format_plural(count($names), '@identifier has role "@roles"', '@identifier has one of "@roles"', array('@roles' => implode(', ', $names), '@identifier' => $context->identifier));
}

View File

@@ -0,0 +1,87 @@
<?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("User: language"),
'description' => t('Control access by the language the user or 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_access_summary',
);
}
/**
* 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'],
);
return $form;
}
/**
* 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_access_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 is any language');
}
return format_plural(count($names), 'Site language is "@languages"', 'Site language is one of "@languages"', array('@languages' => implode(', ', $names)));
}

View File

@@ -0,0 +1,94 @@
<?php
/**
* @file
* Plugin to provide access control/visibility based on specified context string matching user-specified string
*/
$plugin = array(
'title' => t("String: comparison"),
'description' => t('Control access by string match.'),
'callback' => 'ctools_string_equal_ctools_access_check',
'settings form' => 'ctools_string_equal_ctools_access_settings',
'summary' => 'ctools_string_equal_ctools_access_summary',
'required context' => new ctools_context_required(t('String'), 'string'),
'defaults' => array('operator' => '=', 'value' => '', 'case' => FALSE),
);
/**
* Settings form
*/
function ctools_string_equal_ctools_access_settings($form, &$form_state, $conf) {
$form['settings']['operator'] = array(
'#type' => 'radios',
'#title' => t('Operator'),
'#options' => array(
'=' => t('Equal'),
'!=' => t('Not equal'),
'regex' => t('Regular expression'),
'!regex' => t('Not equal to regular expression'),
),
'#default_value' => $conf['operator'],
'#description' => t('If using a regular expression, you should enclose the pattern in slashes like so: <em>/foo/</em>. If you need to compare against slashes you can use another character to enclose the pattern, such as @. See <a href="http://www.php.net/manual/en/reference.pcre.pattern.syntax.php">PHP regex documentation</a> for more.'),
);
$form['settings']['value'] = array(
'#type' => 'textfield',
'#title' => t('String'),
'#default_value' => $conf['value'],
);
$form['settings']['case'] = array(
'#type' => 'checkbox',
'#title' => t('Case sensitive'),
'#default_value' => $conf['case'],
);
return $form;
}
/**
* Check for access
*/
function ctools_string_equal_ctools_access_check($conf, $context) {
if (empty($context) || empty($context->data)) {
$string = '';
}
else {
$string = $context->data;
}
$value = $conf['value'];
if (empty($conf['case'])) {
$string = drupal_strtolower($string);
$value = drupal_strtolower($value);
}
switch ($conf['operator']) {
case '=':
return $string === $value;
case '!=':
return $string !== $value;
case 'regex':
return preg_match($value, $string);
case '!regex':
return !preg_match($value, $string);
}
}
/**
* Provide a summary description based upon the specified context
*/
function ctools_string_equal_ctools_access_summary($conf, $context) {
$values = array('@identifier' => $context->identifier, '@value' => $conf['value']);
switch ($conf['operator']) {
case '=':
return t('@identifier is "@value"', $values);
case '!=':
return t('@identifier is not "@value"', $values);
case 'regex':
return t('@identifier matches "@value"', $values);
case '!regex':
return t('@identifier does not match "@value"', $values);
}
}

View File

@@ -0,0 +1,78 @@
<?php
/**
* @file
* Plugin to provide access control/visibility based on length of
* a string context.
*/
$plugin = array(
'title' => t("String: length"),
'description' => t('Control access by length of string context.'),
'callback' => 'ctools_string_length_ctools_access_check',
'settings form' => 'ctools_string_length_ctools_access_settings',
'summary' => 'ctools_string_length_ctools_access_summary',
'required context' => new ctools_context_required(t('String'), 'string'),
'defaults' => array('operator' => '=', 'length' => 0),
);
/**
* Settings form for the 'by role' access plugin.
*/
function ctools_string_length_ctools_access_settings($form, &$form_state, $conf) {
$form['settings']['operator'] = array(
'#type' => 'radios',
'#title' => t('Operator'),
'#options' => array(
'>' => t('Greater than'),
'>=' => t('Greater than or equal to'),
'=' => t('Equal to'),
'!=' => t('Not equal to'),
'<' => t('Less than'),
'<=' => t('Less than or equal to'),
),
'#default_value' => $conf['operator'],
);
$form['settings']['length'] = array(
'#type' => 'textfield',
'#title' => t('Length of string'),
'#size' => 3,
'#default_value' => $conf['length'],
'#description' => t('Access/visibility will be granted based on string context length.'),
);
return $form;
}
/**
* Check for access.
*/
function ctools_string_length_ctools_access_check($conf, $context) {
if (empty($context) || empty($context->data)) {
$length = 0;
}
else {
$length = drupal_strlen($context->data);
}
switch ($conf['operator']) {
case '<':
return $length < $conf['length'];
case '<=':
return $length <= $conf['length'];
case '==':
return $length == $conf['length'];
case '!=':
return $length != $conf['length'];
case '>':
return $length > $conf['length'];
case '>=':
return $length >= $conf['length'];
}
}
/**
* Provide a summary description based upon the checked roles.
*/
function ctools_string_length_ctools_access_summary($conf, $context) {
return t('@identifier must be @comp @length characters', array('@identifier' => $context->identifier, '@comp' => $conf['operator'], '@length' => $conf['length']));
}

View File

@@ -0,0 +1,129 @@
<?php
/**
* @file
* Plugin to provide access control based upon specific terms.
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t("Taxonomy: term"),
'description' => t('Control access by a specific term.'),
'callback' => 'ctools_term_ctools_access_check',
'default' => array('vids' => array()),
'settings form' => 'ctools_term_ctools_access_settings',
'settings form validation' => 'ctools_term_ctools_access_settings_validate',
'settings form submit' => 'ctools_term_ctools_access_settings_submit',
'summary' => 'ctools_term_ctools_access_summary',
'required context' => new ctools_context_required(t('Term'), array('taxonomy_term', 'terms')),
);
/**
* Settings form for the 'by term' access plugin
*/
function ctools_term_ctools_access_settings($form, &$form_state, $conf) {
// If no configuration was saved before, set some defaults.
if (empty($conf)) {
$conf = array(
'vid' => 0,
);
}
if (!isset($conf['vid'])) {
$conf['vid'] = 0;
}
$form['settings']['vid'] = array(
'#title' => t('Vocabulary'),
'#type' => 'select',
'#options' => array(),
'#description' => t('Select the vocabulary for this form.'),
'#id' => 'ctools-select-vid',
'#default_value' => $conf['vid'],
'#required' => TRUE,
);
ctools_include('dependent');
$options = array();
// A note: Dependency works strangely on these forms as they have never been
// updated to a more modern system so they are not individual forms of their
// own like the content types.
$form['settings']['#tree'] = TRUE;
// Loop over each of the configured vocabularies.
foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
$options[$vid] = $vocabulary->name;
$form['settings'][$vocabulary->vid] = array(
'#title' => t('Terms'),
'#description' => t('Select a term or terms from @vocabulary.', array('@vocabulary' => $vocabulary->name)), //. $description,
'#dependency' => array('ctools-select-vid' => array($vocabulary->vid)),
'#default_value' => !empty($conf[$vid]) ? $conf[$vid] : '',
'#multiple' => TRUE,
);
$terms = array();
foreach (taxonomy_get_tree($vocabulary->vid) as $tid => $term) {
$terms[$term->tid] = str_repeat('-', $term->depth) . ($term->depth ? ' ' : '') . $term->name;
}
$form['settings'][$vocabulary->vid]['#type'] = 'select';
$form['settings'][$vocabulary->vid]['#options'] = $terms;
unset($terms);
}
$form['settings']['vid']['#options'] = $options;
return $form;
}
/**
* Check for access.
*/
function ctools_term_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) || empty($context->data->vid) || empty($context->data->tid)) {
return FALSE;
}
// Get the $vid.
if (!isset($conf['vid'])) {
return FALSE;
}
$vid = $conf['vid'];
// Get the terms.
if (!isset($conf[$vid])) {
return FALSE;
}
$return = FALSE;
$terms = array_filter($conf[$vid]);
// For multi-term if any terms coincide, let's call that good enough:
if (isset($context->tids)) {
return (bool) array_intersect($terms, $context->tids);
}
else {
return in_array($context->data->tid, $terms);
}
}
/**
* Provide a summary description based upon the checked terms.
*/
function ctools_term_ctools_access_summary($conf, $context) {
$vid = $conf['vid'];
$terms = array();
foreach ($conf[$vid] as $tid) {
$term = taxonomy_term_load($tid);
$terms[] = $term->name;
}
return format_plural(count($terms),
'@term can be the term "@terms"',
'@term can be one of these terms: @terms',
array('@terms' => implode(', ', $terms),
'@term' => $context->identifier));
}

View File

@@ -0,0 +1,172 @@
<?php
/**
* @file
* Plugin to provide access control based upon a parent term.
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t("Taxonomy: term has parent(s)"),
'description' => t('Control access if a term belongs to a specific parent term.'),
'callback' => 'ctools_term_has_parent_ctools_access_check',
'default' => array('vid' => array(), 'negate' => 0),
'settings form' => 'ctools_term_has_parent_ctools_access_settings',
'settings form submit' => 'ctools_term_has_parent_ctools_access_settings_submit',
'summary' => 'ctools_term_has_parent_ctools_access_summary',
'required context' => new ctools_context_required(t('Term'), array('taxonomy_term', 'terms')),
);
/**
* Settings form for the 'by parent term' access plugin
*/
function ctools_term_has_parent_ctools_access_settings($form, &$form_state, $conf) {
// If no configuration was saved before, set some defaults.
if (empty($conf)) {
$conf = array(
'vid' => 0,
);
}
if (!isset($conf['vid'])) {
$conf['vid'] = 0;
}
$form['settings']['vid'] = array(
'#title' => t('Vocabulary'),
'#type' => 'select',
'#options' => array(),
'#description' => t('Select the vocabulary for this form.'),
'#id' => 'ctools-select-vid',
'#default_value' => $conf['vid'],
'#required' => TRUE,
);
ctools_include('dependent');
$options = array();
// A note: Dependency works strangely on these forms as they have never been
// updated to a more modern system so they are not individual forms of their
// own like the content types.
$form['settings']['#tree'] = TRUE;
// Loop over each of the configured vocabularies.
foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
$options[$vid] = $vocabulary->name;
$form['settings']['vid_' . $vid] = array(
'#title' => t('Terms'),
'#description' => t('Select a term or terms from @vocabulary.', array('@vocabulary' => $vocabulary->name)),
'#dependency' => array('ctools-select-vid' => array($vocabulary->vid)),
'#default_value' => !empty($conf['vid_' . $vid]) ? $conf['vid_' . $vid] : '',
'#size' => 10,
'#multiple' => TRUE,
//@todo: Remove the following workaround when the following patch is in core. {@see:http://drupal.org/node/1117526}
'#name' => sprintf("settings[%u][]", $vid),
'#attributes' => array('multiple' => 'multiple'),
);
$terms = array();
foreach (taxonomy_get_tree($vocabulary->vid) as $term) {
$terms[$term->tid] = str_repeat('-', $term->depth) . ($term->depth ? ' ' : '') . $term->name;
}
//$form['settings']['vid_' . $vid]['#type'] = 'select';
$form['settings']['vid_' . $vid]['#type'] = 'checkboxes';
$form['settings']['vid_' . $vid]['#options'] = $terms;
unset($terms);
}
$form['settings']['vid']['#options'] = $options;
$form['settings']['include_self'] = array(
'#title' => t('Include these term(s) as candidates?'),
'#description' => t('When this rule is evaluated, should the term(s) you select be included as candidates for access?'),
'#default_value' => !empty($conf['include_self']) ? $conf['include_self'] : FALSE,
'#type' => 'checkbox',
);
return $form;
}
/**
* Filters values to store less.
*/
function ctools_term_has_parent_ctools_access_settings_submit($form, &$form_state) {
foreach ($form_state['values']['settings'] as $key => $value) {
if (strpos($key, 'vid_') === 0) {
$form_state['values']['settings'][$key] = array_filter($form_state['values']['settings'][$key]);
}
}
}
/**
* Check for access.
*/
function ctools_term_has_parent_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) || empty($context->data->vid) || empty($context->data->tid)) {
return FALSE;
}
// Get the $vid.
if (!isset($conf['vid'])) {
return FALSE;
}
$vid = $conf['vid'];
// we'll start looking up the hierarchy from our context term id.
$current_term = $context->data->tid;
$term='';
// scan up the tree.
while (true) {
// select parent as term_parent to avoid PHP5 complications with the parent keyword
//@todo: Find a way to reduce the number of queries required for really deep hierarchies.
$term = db_query("SELECT parent AS term_parent, tid AS tid FROM {taxonomy_term_hierarchy} th WHERE th.tid = :tid", array(':tid'=>$current_term))->fetchObject();
// if no term is found, get out of the loop
if (!$term || empty($term->tid)) {
break;
}
// check the term selected, if the user asked it to.
if (!empty($conf['include_self']) && isset($conf['vid_' . $vid][$term->tid])) {
return TRUE;
}
// did we find the parent TID we were looking for?
if (isset($conf['vid_' . $vid][$term->tid])) {
// YES, we're done!
return TRUE;
}
// Nope, we didn't find it.
// If this is the top of the hierarchy, stop scanning.
if ($term->term_parent==0) {
break;
}
// update the parent, and keep scanning.
$current_term = $term->term_parent;
}
return FALSE;
}
/**
* Provide a summary description based upon the checked terms.
*/
function ctools_term_has_parent_ctools_access_summary($conf, $context) {
$vid = (int)$conf['vid'];
$terms = array();
foreach ($conf['vid_' . $vid] as $tid) {
$term = taxonomy_term_load($tid);
$terms[] = $term->name;
}
return format_plural(count($terms),
'@term can have the parent "@terms"',
'@term can have one of these parents: @terms',
array('@terms' => implode(', ', $terms),
'@term' => $context->identifier));
}

View File

@@ -0,0 +1,86 @@
<?php
/**
* @file
* Plugin to provide access control based upon a parent term.
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t("Taxonomy: parent term"),
'description' => t('Control access by existence of a parent term.'),
'callback' => 'ctools_term_parent_ctools_access_check',
'default' => array('vid' => array(), 'negate' => 0),
'settings form' => 'ctools_term_parent_ctools_access_settings',
'settings form validation' => 'ctools_term_parent_ctools_access_settings_validate',
'settings form submit' => 'ctools_term_parent_ctools_access_settings_submit',
'summary' => 'ctools_term_parent_ctools_access_summary',
'required context' => new ctools_context_required(t('Term'), array('taxonomy_term', 'terms')),
);
/**
* Settings form for the 'by parent term' access plugin
*/
function ctools_term_parent_ctools_access_settings($form, &$form_state, $conf) {
// If no configuration was saved before, set some defaults.
if (empty($conf)) {
$conf = array(
'vid' => 0,
);
}
if (!isset($conf['vid'])) {
$conf['vid'] = 0;
}
$form['settings']['vid'] = array(
'#title' => t('Vocabulary'),
'#type' => 'select',
'#options' => array(),
'#description' => t('Select the vocabulary for this form. If there exists a parent term in that vocabulary, this access check will succeed.'),
'#id' => 'ctools-select-vid',
'#default_value' => $conf['vid'],
'#required' => TRUE,
);
$options = array();
// Loop over each of the configured vocabularies.
foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
$options[$vid] = $vocabulary->name;
}
$form['settings']['vid']['#options'] = $options;
return $form;
}
/**
* Check for access.
*/
function ctools_term_parent_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) || empty($context->data->vid) || empty($context->data->tid)) {
return FALSE;
}
// Get the $vid.
if (!isset($conf['vid'])) {
return FALSE;
}
$vid = $conf['vid'];
$count = db_query('SELECT COUNT(*) FROM {taxonomy_term_hierarchy} th INNER JOIN {taxonomy_term_data} td ON th.parent = td.tid WHERE th.tid = :tid AND td.vid = :vid', array(':tid' => $context->data->tid, ':vid' => $vid))->fetchField();
return $count ? TRUE : FALSE;
}
/**
* Provide a summary description based upon the checked terms.
*/
function ctools_term_parent_ctools_access_summary($conf, $context) {
$vocab = taxonomy_vocabulary_load($conf['vid']);
return t('"@term" has parent in vocabulary "@vocab"', array('@term' => $context->identifier, '@vocab' => $vocab->name));
}

View File

@@ -0,0 +1,87 @@
<?php
/**
* @file
* Plugin to provide access control based upon term vocabulary
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t("Taxonomy: vocabulary"),
'description' => t('Control access by vocabulary.'),
'callback' => 'ctools_term_vocabulary_ctools_access_check',
'default' => array('vids' => array()),
'settings form' => 'ctools_term_vocabulary_ctools_access_settings',
'settings form submit' => 'ctools_term_vocabulary_ctools_access_settings_submit',
'summary' => 'ctools_term_vocabulary_ctools_access_summary',
'required context' => new ctools_context_required(t('Vocabulary'), array('taxonomy_term', 'terms', 'taxonomy_vocabulary')),
);
/**
* Settings form for the 'by term_vocabulary' access plugin
*/
function ctools_term_vocabulary_ctools_access_settings($form, &$form_state, $conf) {
$options = array();
$vocabularies = taxonomy_get_vocabularies();
foreach ($vocabularies as $voc) {
$options[$voc->vid] = check_plain($voc->name);
}
$form['settings']['vids'] = array(
'#type' => 'checkboxes',
'#title' => t('Vocabularies'),
'#options' => $options,
'#description' => t('Only the checked vocabularies will be valid.'),
'#default_value' => $conf['vids'],
);
return $form;
}
/**
* Compress the term_vocabularys allowed to the minimum.
*/
function ctools_term_vocabulary_ctools_access_settings_submit($form, &$form_state) {
$form_state['values']['settings']['vids'] = array_filter($form_state['values']['settings']['vids']);
}
/**
* Check for access.
*/
function ctools_term_vocabulary_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) || empty($context->data->vid)) {
return FALSE;
}
if (array_filter($conf['vids']) && empty($conf['vids'][$context->data->vid])) {
return FALSE;
}
return TRUE;
}
/**
* Provide a summary description based upon the checked term_vocabularys.
*/
function ctools_term_vocabulary_ctools_access_summary($conf, $context) {
if (!isset($conf['type'])) {
$conf['type'] = array();
}
$vocabularies = taxonomy_get_vocabularies();
$names = array();
foreach (array_filter($conf['vids']) as $vid) {
$names[] = check_plain($vocabularies[$vid]->name);
}
if (empty($names)) {
return t('@identifier is any vocabulary', array('@identifier' => $context->identifier));
}
return format_plural(count($names), '@identifier vocabulary is "@vids"', '@identifier vocabulary is one of "@vids"', array('@vids' => implode(', ', $names), '@identifier' => $context->identifier));
}

View File

@@ -0,0 +1,70 @@
<?php
/**
* @file
* Plugin to provide access control based on user themeission strings.
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t("Current theme"),
'description' => t('Control access by checking which theme is in use.'),
'callback' => 'ctools_theme_ctools_access_check',
'default' => array('theme' => variable_get('theme_default', 'garland')),
'settings form' => 'ctools_theme_ctools_access_settings',
'summary' => 'ctools_theme_ctools_access_summary',
);
/**
* Settings form for the 'by theme' access plugin
*/
function ctools_theme_ctools_access_settings($form, &$form_state, $conf) {
$themes = array();
foreach (list_themes() as $key => $theme) {
$themes[$key] = $theme->info['name'];
}
$form['settings']['theme'] = array(
'#type' => 'select',
'#options' => $themes,
'#title' => t('Themes'),
'#default_value' => $conf['theme'],
'#description' => t('This will only be accessed if the current theme is the selected theme.'),
);
return $form;
}
/**
* Check for access.
*/
function ctools_theme_ctools_access_check($conf, $context) {
if (!empty($GLOBALS['theme'])) {
$theme = $GLOBALS['theme'];
}
else if (!empty($GLOBALS['custom_theme'])) {
$theme = $GLOBALS['custom_theme'];
}
else if (!empty($GLOBALS['user']->theme)) {
$theme = $GLOBALS['user']->theme;
}
else {
$theme = variable_get('theme_default', 'garland');
}
return $conf['theme'] == $theme;
}
/**
* Provide a summary description based upon the checked roles.
*/
function ctools_theme_ctools_access_summary($conf, $context) {
if (!isset($conf['theme'])) {
return t('Error, unset theme');
}
$themes = list_themes();
return t('Current theme is "@theme"', array('@theme' => $themes[$conf['theme']]->info['name']));
}