View source
<?php
namespace HookUpdateDeployTools;
class Views {
public static function enable($views_names = array()) {
$message = self::viewSwitch($views_names, 'enable');
return $message;
}
public static function disable($views_names = array()) {
$message = self::viewSwitch($views_names, 'disable');
return $message;
}
public static function delete($views_names = array()) {
$message = '';
$views_names = (array) $views_names;
if (count($views_names)) {
foreach ($views_names as $view_name) {
if ($view = views_get_view($view_name)) {
views_delete_view($view);
if (views_get_view($view_name, TRUE)) {
throw new HudtException('View @viewname still exists after being deleted. Possibly in code?', array(
'@viewname' => $view_name,
), WATCHDOG_ERROR, TRUE);
}
else {
$message .= Message::make('View @viewname successfully deleted', array(
'@viewname' => $view_name,
), WATCHDOG_INFO);
}
}
else {
$message .= Message::make('View @viewname already deleted or could not be found.', array(
'@viewname' => $view_name,
), WATCHDOG_INFO);
}
}
}
return $message;
}
private static function viewSwitch($views_names, $operation) {
$t = get_t();
$views_names = (array) $views_names;
$op_friendly = $operation === 'disable' ? $t('Disabled') : $t('Enabled');
$enable = $operation === 'disable' ? FALSE : TRUE;
$summary_description = $t('Views @op', array(
'@op' => $op_friendly,
));
$completed = array();
$total_requested = count($views_names);
try {
self::canSwitch();
foreach ($views_names as $view_name) {
$variables = array(
'@view' => $view_name,
'@op' => $op_friendly,
);
$view = views_get_view($view_name, TRUE);
if (empty($view)) {
$message = 'The View:@view does not exist so it could not be @op.';
throw new HudtException($message, $variables, WATCHDOG_ERROR, TRUE);
}
if (property_exists($view, 'disabled') && $view->disabled == $enable) {
ctools_export_crud_set_status('views_view', $view, !$enable);
$view = views_get_view($view_name, TRUE);
if (property_exists($view, 'disabled') && $view->disabled == $enable) {
$message = 'The View:@view status does not match the requested state of "@op".';
$completed[$view_name] = $t($message, $variables);
throw new HudtException($message, $variables, WATCHDOG_ERROR, TRUE);
}
else {
$completed[$view_name] = $op_friendly;
}
}
else {
$completed[$view_name] = $t("Skipped. Already @op.", array(
'@op' => $op_friendly,
));
}
}
} catch (\Exception $e) {
$vars = array(
'!error' => method_exists($e, 'logMessage') ? $e
->logMessage() : $e
->getMessage(),
);
if (!method_exists($e, 'logMessage')) {
$message = 'View enable/disable denied because: !error';
Message::make($message, $vars, WATCHDOG_ERROR);
}
$done = HudtInternal::getSummary($completed, $total_requested, $summary_description);
Message::make($done, array(), FALSE, 1);
throw new HudtException('Caught Exception: Update aborted! !error', $vars, WATCHDOG_ERROR, FALSE);
}
$done = HudtInternal::getSummary($completed, $total_requested, $summary_description);
$done = Message::make($done, array(), WATCHDOG_INFO, 1);
return $done;
}
private static function canSwitch() {
Check::canUse('views');
Check::canCall('views_get_view');
Check::canUse('ctools');
ctools_include('export');
Check::canCall('ctools_export_crud_set_status');
}
}