View source
<?php
namespace HookUpdateDeployTools;
class Blocks {
private static function diff($block_original, $block_new) {
$properties_changed = array_keys(array_diff((array) $block_original, (array) $block_new));
$messages = array();
foreach ($properties_changed as $property) {
$vars = array(
'@original' => $block_original->{$property},
'@new' => $block_new->{$property},
);
$messages[$property] = t('Changed from @original to @new', $vars);
}
$messages = empty($messages) ? 'nothing' : $messages;
return $messages;
}
public static function disable($module, $block_delta, $theme = NULL) {
try {
$theme = !empty($theme) ? $theme : variable_get('theme_default', NULL);
Check::notEmpty('$theme', $theme);
Check::notEmpty('$module', $module);
Check::notEmpty('$block_delta', $block_delta);
$vars = array(
'@module' => $module,
'@block_delta' => $block_delta,
'@theme' => $theme,
);
$block_original = self::load($module, $block_delta, $theme);
if ($block_original->region === (string) \BLOCK_REGION_NONE) {
$message = 'The block @module:@block_delta for theme:@theme was already disabled. No change.';
}
else {
$fields = array(
'region' => \BLOCK_REGION_NONE,
);
self::updateInstancePropertiesSilent($module, $block_delta, $theme, $fields);
$block_new = self::load($module, $block_delta, $theme);
if ($block_new->region === (string) \BLOCK_REGION_NONE) {
$message = 'The block @module:@block_delta for theme:@theme was disabled.';
}
else {
throw new HudtException('The block @module:@block_delta for theme:@theme was NOT disabled.', $vars, WATCHDOG_ERROR, TRUE);
}
}
} catch (\Exception $e) {
$vars['!error'] = method_exists($e, 'logMessage') ? $e
->logMessage() : $e
->getMessage();
if (!method_exists($e, 'logMessage')) {
$message = 'Blocks::disable @module:@block_delta for theme:@theme failed because: !error';
Message::make($message, $vars, WATCHDOG_ERROR);
}
throw new HudtException('Caught Exception: Update aborted! !error', $vars, WATCHDOG_ERROR, FALSE);
}
$return_msg = Message::make($message, $vars, WATCHDOG_INFO, 2);
return $return_msg;
}
public static function enable($module, $block_delta, $region, $theme = NULL) {
try {
$theme = !empty($theme) ? $theme : variable_get('theme_default', NULL);
Check::notEmpty('$theme', $theme);
Check::notEmpty('$module', $module);
Check::notEmpty('$block_delta', $block_delta);
Check::notEmpty('$region', $region);
$vars = array(
'@module' => $module,
'@block_delta' => $block_delta,
'@theme' => $theme,
'@region' => $region,
);
if ($region == -1) {
$message = 'Trying to set block @module:@block_delta to region:-1 would disable the block. If that is the intention, use Blocks:disable instead.';
throw new HudtException($message, $vars, WATCHDOG_ERROR, TRUE);
}
$block_original = self::load($module, $block_delta, $theme);
if ($block_original->region === (string) $region && $block_original->status === '1') {
$message = 'The block @module:@block_delta for theme:@theme was already enabled in region:@region. No change.';
}
else {
$fields = array(
'region' => $region,
);
self::updateInstancePropertiesSilent($module, $block_delta, $theme, $fields);
$block_new = self::load($module, $block_delta, $theme);
if ($block_new->region === (string) $region && $block_new->status === '1') {
$message = 'The block @module:@block_delta for theme:@theme was enabled in region:@region.';
}
else {
throw new HudtException('The block @module:@block_delta for theme:@theme was NOT enabled in region:@region.', $vars, WATCHDOG_ERROR, TRUE);
}
}
} catch (\Exception $e) {
$vars['!error'] = method_exists($e, 'logMessage') ? $e
->logMessage() : $e
->getMessage();
if (!method_exists($e, 'logMessage')) {
$message = 'Blocks::enable @module:@block_delta for theme:@theme region:@region failed because: !error';
Message::make($message, $vars, WATCHDOG_ERROR);
}
throw new HudtException('Caught Exception: Update aborted! !error', $vars, WATCHDOG_ERROR, FALSE);
}
$return_msg = Message::make($message, $vars, WATCHDOG_INFO, 2);
return $return_msg;
}
private static function load($module, $block_delta, $theme = NULL) {
$theme = !empty($theme) ? $theme : variable_get('theme_default', NULL);
Check::notEmpty('$theme', $theme);
Check::notEmpty('$module', $module);
Check::notEmpty('$delta', $block_delta);
$params = array(
':module' => $module,
':delta' => $block_delta,
':theme' => $theme,
);
$block = db_query('SELECT * FROM {block} WHERE module = :module AND delta = :delta AND theme = :theme', $params)
->fetchObject();
if (!empty($block)) {
return $block;
}
else {
$vars = array(
'@module' => $module,
'@delta' => $block_delta,
'@theme' => $theme,
);
$message = 'Trying to load block @module:@delta from theme:@theme found no block.';
throw new HudtException($message, $vars, WATCHDOG_ERROR, TRUE);
}
}
public static function updateInstanceProperties($module, $block_delta, $theme, $block_properties) {
$block_original = self::load($module, $block_delta, $theme);
self::updateInstancePropertiesSilent($module, $block_delta, $theme, $block_properties);
$block_new = self::load($module, $block_delta, $theme);
$diff = self::diff($block_original, $block_new);
$vars = array(
'@module' => $module,
'@delta' => $block_delta,
'@theme' => $theme,
'!diff' => $diff,
);
$message = 'The block @module:@delta in theme:@theme updated !diff';
return Message::make($message, $vars, WATCHDOG_INFO, 1);
}
private static function updateInstancePropertiesSilent($module, $block_delta, $theme, $block_properties) {
$theme = !empty($theme) ? $theme : variable_get('theme_default', NULL);
Check::notEmpty('$theme', $theme);
Check::notEmpty('$module', $module);
Check::notEmpty('$delta', $block_delta);
if (!empty($block_properties['region'])) {
$block_properties['status'] = (int) ($block_properties['region'] != \BLOCK_REGION_NONE);
}
$keys = array(
'theme' => $theme,
'delta' => $block_delta,
'module' => $module,
);
$query = db_merge('block')
->key($keys)
->fields($block_properties)
->execute();
}
}