context_condition.inc in Context 6
File
plugins/context_condition.inc
View source
<?php
class context_condition {
var $plugin;
var $title;
var $description;
var $values = array();
function __clone() {
foreach ($this as $key => $val) {
if (is_object($val) || is_array($val)) {
$this->{$key} = unserialize(serialize($val));
}
}
}
function __construct($plugin, $info) {
$this->plugin = $plugin;
$this->title = isset($info['title']) ? $info['title'] : $plugin;
$this->description = isset($info['description']) ? $info['description'] : '';
}
function condition_values() {
return array();
}
function condition_form($context) {
return array(
'#title' => $this->title,
'#description' => $this->description,
'#options' => $this
->condition_values(),
'#type' => 'checkboxes',
'#default_value' => $this
->fetch_from_context($context, 'values'),
);
}
function condition_form_submit($values) {
ksort($values);
return drupal_map_assoc(array_keys(array_filter($values)));
}
function options_form($context) {
return array();
}
function options_form_submit($values) {
return $values;
}
function settings_form() {
return array();
}
function editor_form($context) {
$form = array();
if (!empty($this->values)) {
$options = $this
->condition_values();
foreach ($this->values as $value => $contexts) {
$label = "{$this->title}: ";
$label .= isset($options[$value]) ? trim($options[$value], ' -') : $value;
$form[$value] = array(
'#type' => 'checkbox',
'#title' => check_plain($label),
'#default_value' => empty($context->name) ? TRUE : in_array($context->name, $contexts, TRUE),
);
}
}
return $form;
}
function editor_form_submit(&$context, $values) {
$existing = $this
->fetch_from_context($context, 'values');
$values += !empty($existing) ? $existing : array();
ksort($values);
return drupal_map_assoc(array_keys(array_filter($values)));
}
function condition_met($context, $value = NULL) {
if (isset($value)) {
$this->values[$value] = isset($this->values[$value]) ? $this->values[$value] : array();
$this->values[$value][] = $context->name;
}
context_condition_met($context, $this->plugin);
}
function condition_used() {
$map = context_condition_map();
return !empty($map[$this->plugin]);
}
function get_contexts($value = NULL) {
$map = context_condition_map();
$map = isset($map[$this->plugin]) ? $map[$this->plugin] : array();
$contexts = array();
if (isset($value) && (is_string($value) || is_numeric($value))) {
if (isset($map[$value]) && is_array($map[$value])) {
foreach ($map[$value] as $name) {
if (!isset($contexts[$name])) {
$context = context_load($name);
$contexts[$context->name] = $context;
}
}
}
}
else {
foreach ($map as $submap) {
foreach ($submap as $name) {
if (!isset($contexts[$name])) {
$context = context_load($name);
$contexts[$context->name] = $context;
}
}
}
}
return $contexts;
}
function fetch_from_context($context, $key = NULL) {
if (isset($key)) {
return isset($context->conditions[$this->plugin][$key]) ? $context->conditions[$this->plugin][$key] : array();
}
return isset($context->conditions[$this->plugin]) ? $context->conditions[$this->plugin] : array();
}
}