View source
<?php
class context_reaction_block extends context_reaction {
function options_form($context) {
$this
->get_blocks(NULL, NULL, $this
->rebuild_needed());
$this
->rebuild_needed(FALSE);
$theme_key = variable_get('theme_default', 'garland');
$weight_delta = $this
->max_block_weight();
$form = array(
'#tree' => TRUE,
'#theme' => 'context_block_form',
'max_block_weight' => array(
'#value' => $weight_delta,
'#type' => 'value',
),
'state' => array(
'#type' => 'hidden',
'#attributes' => array(
'class' => 'context-blockform-state',
),
),
);
$modules = module_list();
$form['selector'] = array(
'#type' => 'item',
'#tree' => TRUE,
'#prefix' => '<div class="context-blockform-selector">',
'#suffix' => '</div>',
);
foreach ($this
->get_blocks() as $block) {
if (!isset($form['selector'][$block->module])) {
$form['selector'][$block->module] = array(
'#type' => 'checkboxes',
'#title' => $modules[$block->module],
'#options' => array(),
);
}
$form['selector'][$block->module]['#options'][$block->bid] = check_plain($block->info);
}
ksort($form['selector']);
$form['blocks'] = array(
'#tree' => TRUE,
'#theme' => 'context_block_regions_form',
);
foreach (system_region_list($theme_key) as $region => $label) {
$form['blocks'][$region] = array(
'#type' => 'item',
'#title' => $label,
'#tree' => TRUE,
);
foreach ($this
->get_blocks($region, $context) as $block) {
if (!empty($block->context)) {
$form['blocks'][$region][$block->bid] = array(
'#value' => check_plain($block->info),
'#weight' => $block->weight,
'#type' => 'markup',
'#tree' => TRUE,
'weight' => array(
'#type' => 'weight',
'#delta' => $weight_delta,
'#default_value' => 0,
),
);
}
}
}
return $form;
}
function options_form_submit($values) {
$blocks = array();
$block_info = $this
->get_blocks();
if (!empty($values['state'])) {
$edited = $this
->json_decode($values['state']);
}
else {
$edited = array();
}
foreach ($edited as $region => $bids) {
foreach ($bids as $position => $bid) {
if (isset($block_info[$bid])) {
$blocks[$bid] = array(
'module' => $block_info[$bid]->module,
'delta' => $block_info[$bid]->delta,
'region' => $region,
'weight' => $position,
);
}
}
}
return array(
'blocks' => $blocks,
);
}
function editor_form($context) {
$form = array();
if (module_exists('jquery_ui')) {
jquery_ui_add(array(
'ui.draggable',
'ui.droppable',
'ui.sortable',
));
drupal_add_js(drupal_get_path('module', 'context_ui') . '/json2.js');
drupal_add_js(drupal_get_path('module', 'context') . '/plugins/context_reaction_block.js');
drupal_add_css(drupal_get_path('module', 'context') . '/plugins/context_reaction_block.css');
static $once;
if (!isset($once)) {
$settings = array(
'path' => url($_GET['q']),
'params' => (object) array_diff_key($_GET, array(
'q' => '',
)),
'scriptPlaceholder' => theme('context_block_script_placeholder', ''),
);
drupal_add_js(array(
'contextBlockEditor' => $settings,
), 'setting');
$once = TRUE;
}
$form['state'] = array(
'#type' => 'hidden',
'#attributes' => array(
'class' => 'context-block-editor-state',
),
);
$form['browser'] = array(
'#type' => 'markup',
'#value' => theme('context_block_browser', $this
->get_blocks(NULL, NULL, $this
->rebuild_needed()), $context),
);
$this
->rebuild_needed(FALSE);
}
return $form;
}
function editor_form_submit(&$context, $values) {
$edited = !empty($values['state']) ? (array) $this
->json_decode($values['state']) : array();
$options = array();
$affected_regions = array_keys($edited);
if (!empty($context->reactions['block']['blocks'])) {
$options = $context->reactions['block'];
foreach ($options['blocks'] as $key => $block) {
if (in_array($block['region'], $affected_regions)) {
unset($options['blocks'][$key]);
}
}
}
foreach ($edited as $region => $blocks) {
foreach ($blocks as $weight => $block) {
if ($block->context === $context->name) {
$split = explode('-', $block->bid);
$options['blocks'][$block->bid] = array(
'module' => array_shift($split),
'delta' => implode('-', $split),
'region' => $region,
'weight' => $weight,
);
}
}
}
return $options;
}
function settings_form() {
$form = array();
$form['context_reaction_block_disable_core'] = array(
'#title' => t('Core block system'),
'#type' => 'select',
'#options' => array(
FALSE => t('Enabled'),
TRUE => t('Disabled'),
),
'#default_value' => variable_get('context_reaction_block_disable_core', FALSE),
'#description' => t('If enabled Context will include blocks enabled through the core block system when rendering regions. Disable to improve performance and hide the core block administration UI.'),
);
$form['context_reaction_block_all_regions'] = array(
'#title' => t('Show all regions'),
'#type' => 'checkbox',
'#default_value' => variable_get('context_reaction_block_all_regions', FALSE),
'#description' => t('Show all regions including those that are empty. Enable if you are administering your site using the inline editor.'),
);
return $form;
}
function execute($region) {
if (isset($_GET['context_block']) && !ob_get_contents()) {
return $this
->render_ajax($_GET['context_block']);
}
if ($this
->is_enabled_region($region)) {
return theme('context_block_editable_region', $region, $this
->block_list($region), $this
->is_editable());
}
return '';
}
protected function is_editable($reset = FALSE) {
static $editable;
if (!isset($editable) || $reset) {
global $user;
if (module_exists('jquery_ui') && $user->uid) {
$editable = TRUE;
jquery_ui_add(array(
'ui.draggable',
'ui.droppable',
'ui.sortable',
));
drupal_add_js(drupal_get_path('module', 'context') . '/plugins/context_reaction_block.js');
drupal_add_css(drupal_get_path('module', 'context') . '/plugins/context_reaction_block.css');
}
else {
$editable = FALSE;
}
}
return $editable;
}
protected function is_enabled_region($region) {
global $theme;
$regions = array_keys(system_region_list($theme));
return in_array($region, $regions, TRUE);
}
function block_list($region) {
static $build_queue;
static $build_contexts;
static $context_blocks = array();
static $empty_blocks = array();
$contexts = context_active_contexts();
if (!isset($build_queue) || $build_contexts != array_keys($contexts)) {
$info = $this
->get_blocks();
$build_queue = array();
$build_contexts = array_keys($contexts);
foreach ($contexts as $context) {
$options = $this
->fetch_from_context($context);
if (!empty($options['blocks'])) {
foreach ($options['blocks'] as $block) {
$block = (object) $block;
$block->context = $context->name;
$block->bid = "{$block->module}-{$block->delta}";
$block->cache = isset($info[$block->bid]->cache) ? $info[$block->bid]->cache : BLOCK_NO_CACHE;
$build_queue[$block->region][] = $block;
}
}
}
}
if (!isset($context_blocks[$region])) {
$context_blocks[$region] = array();
$empty_blocks[$region] = array();
if (!empty($build_queue[$region])) {
foreach ($build_queue[$region] as $block) {
$block = $this
->build_block($block);
if (!empty($block->content)) {
$context_blocks[$region][] = $block;
}
else {
$empty_blocks[$region][] = $block;
}
}
}
}
$blocks = !variable_get('context_reaction_block_disable_core', FALSE) ? block_list($region) : array();
$blocks = array_merge($blocks, $context_blocks[$region]);
$all_regions = variable_get('context_reaction_block_all_regions', FALSE);
if ($this
->is_editable() && ($all_regions || !empty($blocks))) {
$blocks = array_merge($blocks, $empty_blocks[$region]);
}
uasort($blocks, array(
'context_reaction_block',
'block_sort',
));
return $blocks;
}
protected function build_block($block, $reset = FALSE) {
static $cacheable;
if (!isset($cacheable) || $reset) {
$cacheable = !count(module_implements('node_grants')) && $_SERVER['REQUEST_METHOD'] == 'GET';
}
if (!isset($block->content)) {
$block->content = '';
if ($cacheable && ($cid = _block_get_cache_id($block))) {
if ($cache = cache_get($cid, 'cache_block')) {
$array = $cache->data;
}
else {
$array = module_invoke($block->module, 'block', 'view', $block->delta);
cache_set($cid, $array, 'cache_block', CACHE_TEMPORARY);
}
}
else {
$array = module_invoke($block->module, 'block', 'view', $block->delta);
}
if (isset($array) && is_array($array)) {
foreach ($array as $k => $v) {
$block->{$k} = $v;
}
}
}
if (!empty($block->content)) {
if (!variable_get('context_reaction_block_disable_core', FALSE)) {
global $user, $theme_key;
$block->title = db_select('block')
->fields('block', array(
'title',
))
->condition('module', $block->module)
->condition('delta', $block->delta)
->condition('theme', $theme_key)
->execute()
->fetchField();
}
if (!empty($block->title)) {
$block->subject = $block->title == '<none>' ? '' : check_plain($block->title);
}
if (!isset($block->subject)) {
$block->subject = '';
}
}
return $block;
}
protected function max_block_weight() {
$blocks = $this
->get_blocks();
$block_count = 0;
foreach ($blocks as $region => $block_list) {
$block_count += count($block_list);
}
return round(($block_count + 2) / 2);
}
function rebuild_needed($set = NULL) {
if (isset($set) && $set != variable_get('context_block_rebuild_needed', FALSE)) {
variable_set('context_block_rebuild_needed', $set);
}
return (bool) variable_get('context_block_rebuild_needed', FALSE);
}
function get_blocks($region = NULL, $context = NULL, $reset = FALSE) {
static $block_info;
if (!isset($block_info) || $reset) {
$block_info = array();
if (!$reset) {
$block_info = context_cache_get('block_info');
}
if (empty($block_info)) {
$block_info = array();
foreach (module_implements('block_info') as $module) {
$module_blocks = module_invoke($module, 'block_info');
if (!empty($module_blocks)) {
foreach ($module_blocks as $delta => $block) {
$block = (object) $block;
$block->module = $module;
$block->delta = $delta;
$block->bid = "{$block->module}-{$block->delta}";
$block_info[$block->bid] = $block;
}
}
}
context_cache_set('block_info', $block_info);
}
drupal_alter('context_block_info', $block_info);
$theme_key = variable_get('theme_default', 'garland');
$result = db_select('block')
->fields('block', array(
'module',
'weight',
'delta',
'region',
))
->condition('theme', $theme_key)
->condition('status', 1)
->execute();
foreach ($result as $row) {
if (isset($block_info["{$row->module}-{$row->delta}"])) {
$block_info["{$row->module}-{$row->delta}"]->weight = $row->weight;
$block_info["{$row->module}-{$row->delta}"]->region = $row->region;
}
}
}
$blocks = array();
if (!isset($region)) {
$blocks = $block_info;
}
else {
foreach ($block_info as $bid => $block) {
if (isset($block->region) && $block->region == $region) {
$blocks[$bid] = $block;
}
}
}
if (is_object($context) && ($options = $this
->fetch_from_context($context))) {
if (!empty($options['blocks'])) {
foreach ($options['blocks'] as $block) {
if (isset($block_info["{$block['module']}-{$block['delta']}"]) && (!isset($region) || !empty($region) && $block['region'] == $region)) {
$context_block = $block_info["{$block['module']}-{$block['delta']}"];
$context_block->weight = $block['weight'];
$context_block->region = $block['region'];
$context_block->context = $context->name;
$blocks[$context_block->bid] = $context_block;
}
}
}
uasort($blocks, array(
'context_reaction_block',
'block_sort',
));
}
return $blocks;
}
static function block_sort($a, $b) {
return $a->weight - $b->weight;
}
protected function json_decode($json, $assoc = FALSE) {
if (function_exists('json_decode')) {
return json_decode($json, $assoc);
}
return context_reaction_block::_json_decode($json);
}
static function _json_decode($json) {
$comment = false;
$out = '$x = ';
for ($i = 0; $i < strlen($json); $i++) {
if (!$comment) {
switch ($json[$i]) {
case '{':
$out .= ' (object) array(';
break;
case '}':
$out .= ')';
break;
case '[':
$out .= ' array(';
break;
case ']':
$out .= ')';
break;
case ':':
$out .= '=>';
break;
default:
$out .= $json[$i];
break;
}
}
else {
$out .= $json[$i];
}
if ($json[$i] == '"') {
$comment = !$comment;
}
}
eval($out . ';');
return $x;
}
function render_ajax($param) {
$headers = drupal_set_header();
foreach (explode("\n", $headers) as $header) {
if ($header == "HTTP/1.1 404 Not Found" || $header == "HTTP/1.1 403 Forbidden") {
return;
}
}
drupal_set_header('Content-Type: text/javascript; charset=utf-8');
if (strpos($param, ',') !== FALSE) {
list($bid, $context) = explode(',', $param);
list($module, $delta) = explode('-', $bid, 2);
$info = $this
->get_blocks();
if (isset($info[$bid])) {
$block = $info[$bid];
$block->context = $context;
$block->region = '';
$block = $this
->build_block($block);
if (empty($block->content)) {
$block->content = "<div class='context-block-empty'>" . t('This block appears empty when displayed on this page.') . "</div>";
}
$css = array();
if ($block->module == 'panels_mini') {
$panel = panels_mini_load($block->delta);
$layout = panels_get_layout($panel->display->layout);
if (!empty($layout['css'])) {
if (file_exists(path_to_theme() . '/' . $layout['css'])) {
$css[] = path_to_theme() . '/' . $layout['css'];
}
else {
$css[] = $layout['path'] . '/' . $layout['css'];
}
}
}
echo drupal_to_js(array(
'status' => 1,
'block' => theme('context_block_editable_block', $block),
'css' => $css,
));
exit;
}
}
echo drupal_to_js(array(
'status' => 0,
));
exit;
}
}