View source
<?php
namespace HookUpdateDeployTools;
class Contexts {
public static function checkDisabled($contexts = array()) {
$contexts = (array) $contexts;
$enabled_contexts = array();
$t = get_t();
$enabled = $t('enabled');
$not_enabled = $t('disabled');
$not_present = $t('not present');
$report = array();
self::canSwitch();
$existing_contexts = context_load(NULL, TRUE);
foreach ($contexts as $context) {
if (!empty($existing_contexts[$context])) {
if (!empty($existing_contexts[$context]->disabled)) {
$report[$context] = $not_enabled;
}
else {
$report[$context] = $enabled;
}
}
else {
$report[$context] = "{$not_enabled} - {$not_present}";
}
}
if (in_array($enabled, $report)) {
$message = 'The contexts that were supposed to be disabled by this update, were not. Please investigate the problem and re-run this update. Report: !report';
$variables = array(
'!report' => $report,
);
throw new HudtException($message, $variables, WATCHDOG_ERROR, TRUE);
}
else {
$message = "The requested contexts are disabled. Report: !report";
$variables = array(
'!report' => $report,
);
return Message::make($message, $variables, WATCHDOG_INFO);
}
}
public static function checkEnabled($contexts = array()) {
$contexts = (array) $contexts;
$t = get_t();
$enabled = $t('enabled');
$not_enabled = $t('not-enabled');
$report = array();
self::canSwitch();
$enabled_contexts = context_enabled_contexts(TRUE);
foreach ($contexts as $context) {
if (!empty($enabled_contexts[$context])) {
$report[$context] = $enabled;
}
else {
$report[$context] = $not_enabled;
}
}
if (in_array($not_enabled, $report)) {
$message .= 'Some of the contexts that were supposed to be enabled, are not showing as enabled. Please investigate the problem and re-run this update. Report: !report';
$variables = array(
'!report' => $report,
);
throw new HudtException($message, $variables, WATCHDOG_ERROR, TRUE);
}
else {
$message .= "The requested contexts were enabled successfully. Report: !report";
$variables = array(
'!report' => $report,
);
return Message::make($message, $variables, WATCHDOG_INFO);
}
}
public static function enable($contexts = array()) {
try {
$contexts = (array) $contexts;
$t = get_t();
$enabled = $t('enabled');
$failed = $t('failed');
$report = array();
self::canSwitch();
self::checkContextsExist($contexts, TRUE);
foreach ($contexts as $context) {
$context_object = context_load($context);
ctools_export_crud_enable('context', $context_object);
}
$success = self::checkEnabled($contexts);
return $success;
} catch (\Exception $e) {
$vars['!error'] = method_exists($e, 'logMessage') ? $e
->logMessage() : $e
->getMessage();
if (!method_exists($e, 'logMessage')) {
$message = 'contexts::enable failed because: !error';
Message::make($message, $vars, WATCHDOG_ERROR);
}
throw new HudtException('Update aborted! !error', $vars, WATCHDOG_ERROR, FALSE);
}
}
public static function disable($contexts = array(), $disable_dependents = TRUE) {
try {
$contexts = (array) $contexts;
self::canSwitch();
self::checkContextsExist($contexts, FALSE);
foreach ($contexts as $context) {
$context_object = context_load($context);
ctools_export_crud_disable('context', $context_object);
}
$success = self::checkDisabled($contexts);
return $success;
} catch (\Exception $e) {
$vars['!error'] = method_exists($e, 'logMessage') ? $e
->logMessage() : $e
->getMessage();
if (!method_exists($e, 'logMessage')) {
$message = 'contexts::disable failed because: !error';
Message::make($message, $vars, WATCHDOG_ERROR);
}
throw new HudtException('Update aborted! !error', $vars, WATCHDOG_ERROR, FALSE);
}
}
private static function canSwitch() {
Check::canUse('context');
Check::canCall('context_load');
Check::canCall('context_enabled_contexts');
Check::canUse('ctools');
ctools_include('export');
Check::canCall('ctools_export_crud_disable');
Check::canCall('ctools_export_crud_enable');
}
public static function checkContextsExist($contexts, $strict = TRUE) {
$existing_contexts = context_load(NULL, TRUE);
$report = array();
$return = TRUE;
$t = get_t();
$exists = $t('exists');
$missing = $t('missing');
foreach ($contexts as $context) {
$report[$context] = !empty($existing_contexts[$context]) ? $exists : $missing;
}
$variables = array(
'!report' => $report,
);
if (in_array($missing, $report)) {
if ($strict) {
$message = "One or more contexts seem to be missing. Report: !report";
throw new HudtException($message, $variables, WATCHDOG_ERROR, TRUE);
}
else {
$message = "One or more of the contexts is missing. Report: !report";
Message::make($message, $variables, WATCHDOG_NOTICE);
$return = FALSE;
}
}
return $return;
}
}