View source
<?php
namespace Drupal\views_bulk_operations\Commands;
use Drush\Commands\DrushCommands;
use Drupal\Core\Session\AccountInterface;
use Drupal\views_bulk_operations\Service\ViewsbulkOperationsViewDataInterface;
use Drupal\views_bulk_operations\Service\ViewsBulkOperationsActionManager;
use Drupal\user\Entity\User;
use Drupal\views\Views;
use Drupal\views_bulk_operations\ViewsBulkOperationsBatch;
class ViewsBulkOperationsCommands extends DrushCommands {
protected $currentUser;
protected $viewData;
protected $actionManager;
public function __construct(AccountInterface $currentUser, ViewsbulkOperationsViewDataInterface $viewData, ViewsBulkOperationsActionManager $actionManager) {
$this->currentUser = $currentUser;
$this->viewData = $viewData;
$this->actionManager = $actionManager;
}
public function vboExecute($view_id, $action_id, array $options = [
'display-id' => 'default',
'args' => '',
'exposed' => '',
'batch-size' => 100,
'configuration' => '',
'user-id' => 1,
]) {
if (empty($view_id) || empty($action_id)) {
throw new \Exception($this
->t('You must specify the view ID and the action ID parameters.'));
}
$this
->timer($options['verbose']);
if ($options['args']) {
$options['args'] = explode('/', $options['args']);
}
else {
$options['args'] = [];
}
foreach ([
'configuration',
'exposed',
] as $name) {
if (!empty($options[$name]) && !is_array($options[$name])) {
parse_str($options[$name], $options[$name]);
}
else {
$options[$name] = [];
}
}
$vbo_data = [
'list' => [],
'view_id' => $view_id,
'display_id' => $options['display-id'],
'action_id' => $action_id,
'preconfiguration' => $options['configuration'],
'batch' => TRUE,
'arguments' => $options['args'],
'exposed_input' => $options['exposed'],
'batch_size' => $options['batch-size'],
'relationship_id' => 'none',
];
$account = User::load($options['user-id']);
$this->currentUser
->setAccount($account);
if (!($view = Views::getView($vbo_data['view_id']))) {
throw new \Exception($this
->t('Incorrect view ID provided.'));
}
if (!$view
->setDisplay($vbo_data['display_id'])) {
throw new \Exception($this
->t('Incorrect view display ID provided.'));
}
if (!empty($vbo_data['arguments'])) {
$view
->setArguments($vbo_data['arguments']);
}
if (!empty($vbo_data['exposed_input'])) {
$view
->setExposedInput($vbo_data['exposed_input']);
}
$view->get_total_rows = TRUE;
$view
->execute();
$vbo_data['relationship_id'] = 'none';
foreach ($view->field as $field) {
if ($field->options['id'] === 'views_bulk_operations_bulk_form') {
$vbo_data['relationship_id'] = $field->options['relationship'];
}
}
$this->viewData
->init($view, $view
->getDisplay(), $vbo_data['relationship_id']);
$vbo_data['total_results'] = $this->viewData
->getTotalResults();
$action_definition = $this->actionManager
->getDefinition($action_id);
$vbo_data['action_label'] = (string) $action_definition['label'];
$this
->timer($options['verbose'], 'init');
$context = [];
do {
$context['finished'] = 1;
$context['message'] = '';
ViewsBulkOperationsBatch::getList($vbo_data, $context);
if (!empty($context['message'])) {
$this->logger
->info($context['message']);
}
} while ($context['finished'] < 1);
$vbo_data = $context['results'];
$this
->timer($options['verbose'], 'list');
$context = [];
do {
$context['finished'] = 1;
$context['message'] = '';
ViewsBulkOperationsBatch::operation($vbo_data, $context);
if (!empty($context['message'])) {
$this->logger
->info($context['message']);
}
} while ($context['finished'] < 1);
$operations = array_count_values($context['results']['operations']);
$details = [];
foreach ($operations as $op => $count) {
$details[] = $op . ' (' . $count . ')';
}
if ($options['verbose']) {
$this
->timer($options['verbose'], 'execute');
$this->logger
->info($this
->t('Initialization time: @time ms.', [
'@time' => $this
->timer($options['verbose'], 'init'),
]));
$this->logger
->info($this
->t('Entity list generation time: @time ms.', [
'@time' => $this
->timer($options['verbose'], 'list'),
]));
$this->logger
->info($this
->t('Execution time: @time ms.', [
'@time' => $this
->timer($options['verbose'], 'execute'),
]));
}
return $this
->t('Action processing results: @results.', [
'@results' => implode(', ', $details),
]);
}
protected function timer($debug = TRUE, $id = NULL) {
if (!$debug) {
return;
}
static $timers = [];
if (!isset($id)) {
$timers['start'] = microtime(TRUE);
}
else {
if (isset($timers[$id])) {
end($timers);
do {
if (key($timers) === $id) {
return round((current($timers) - prev($timers)) * 1000, 3);
}
else {
$result = prev($timers);
}
} while ($result);
}
else {
$timers[$id] = microtime(TRUE);
}
}
}
protected function t($message, array $arguments = []) {
return dt($message, $arguments);
}
}