View source
<?php
function queue_ui_page($form, &$form_state) {
if ($form_state['rebuild']) {
$form_state['input'] = array();
}
if (empty($form_state['storage'])) {
$form_state['storage'] = array(
'step' => 'queue_ui_overview_form',
);
}
$function = $form_state['storage']['step'];
$form = $function($form, $form_state);
return $form;
}
function queue_ui_page_submit($form, &$form_state) {
$values = $form_state['values'];
$queue_classes = _queue_ui_array_keys_contain($values, 'queues');
$queues = array();
foreach ($queue_classes as $class_name) {
foreach ($values[$class_name] as $queue) {
$queues[] = $queue;
}
}
$queues = array_filter($queues);
if (empty($queues)) {
return;
}
if (isset($values['step_submit'])) {
$function = $values['step_submit'];
$function($form, $form_state, $queues);
}
return;
}
function queue_ui_overview_form() {
$queues = $options = array();
$header = array(
'name' => array(
'data' => t('Name'),
),
'title' => array(
'data' => t('Title'),
),
'items' => array(
'data' => t('Number of items'),
),
'class' => array(
'data' => t('Class'),
),
'inspect' => array(
'data' => t('Inspect'),
),
);
$defined_queues = queue_ui_defined_queues();
$queues = queue_ui_queues();
foreach ($queues as $class => $classed_queues) {
$options = array();
$class_info = QueueUI::get('QueueUI' . $class);
foreach ($classed_queues as $name => $queue) {
$title = '';
$operations = '';
$inspect = FALSE;
if (isset($defined_queues[$name])) {
$title = $defined_queues[$name]['title'];
}
if (isset($defined_queues[$name]['batch'])) {
$operations = 'batch';
}
if (is_object($class_info) && $class_info->inspect) {
$inspect = TRUE;
}
$options[$name] = array(
'name' => array(
'data' => $name,
),
'title' => array(
'data' => $title,
),
'items' => array(
'data' => $queue['items'],
),
'class' => array(
'data' => $class,
),
);
if ($inspect) {
$options[$name]['inspect'] = array(
'data' => l(t('Inspect'), QUEUE_UI_BASE . "/inspect/{$name}"),
);
}
else {
$options[$name]['inspect'] = '';
}
}
$form[$class] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#title' => $class,
);
$form[$class]['queues_' . $class] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#empty' => t('No queues exist for @class.', array(
'@class' => $class,
)),
);
}
$form['batch'] = array(
'#type' => 'submit',
'#value' => t('Batch process'),
);
$form['release'] = array(
'#type' => 'submit',
'#value' => t('Remove leases'),
);
$form['cron'] = array(
'#type' => 'submit',
'#value' => t('Cron process'),
);
$form['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete queues'),
);
$form['step_submit'] = array(
'#type' => 'value',
'#value' => 'queue_ui_overview_submit',
);
return $form;
}
function queue_ui_overview_submit($form, &$form_state, $queues) {
$values = $form_state['values'];
switch ($values['op']) {
case $values['cron']:
$defined_queues = queue_ui_defined_queues();
$intersect = array_intersect(array_keys($defined_queues), $queues);
foreach ($intersect as $name) {
if (isset($defined_queues[$name]['cron'])) {
variable_set('queue_ui_cron_' . $name, TRUE);
}
}
break;
case $values['batch']:
$defined_queues = queue_ui_defined_queues();
$cron_queues = module_invoke_all('cron_queue_info');
drupal_alter('cron_queue_info', $cron_queues);
foreach ($queues as $name) {
if (isset($defined_queues[$name])) {
$batch = $defined_queues[$name]['batch'];
$operations = array();
$queue = DrupalQueue::get($name);
foreach ($batch['operations'] as $operation) {
$operations[] = array(
$operation[0],
array_merge(array(
$queue,
), $operation[1]),
);
}
$batch['operations'] = $operations;
batch_set($batch);
continue;
}
if (isset($cron_queues[$name])) {
$batch = array(
'operations' => array(
array(
'queue_ui_cron_queue_batch_process',
array(
$name,
$cron_queues[$name],
),
),
),
);
batch_set($batch);
continue;
}
}
break;
case $values['delete']:
$form_state['rebuild'] = TRUE;
$form_state['storage']['queues'] = $queues;
$form_state['storage']['step'] = 'queue_ui_confirm_delete';
break;
case $values['release']:
foreach ($queues as $name) {
$num_updated = db_update('queue')
->fields(array(
'expire' => 0,
))
->condition('name', $name, '=')
->execute();
drupal_set_message(t('!count lease reset in queue !name', array(
'!count' => $num_updated,
'!name' => $name,
)));
}
break;
}
return;
}
function queue_ui_confirm_delete($form, &$form_state) {
$form['queues'] = array(
'#type' => 'value',
'#value' => $form_state['storage']['queues'],
);
$description = t('All items in each queue will be deleted, regardless of if leases exist. This operation cannot be undone.');
$form['step_submit'] = array(
'#type' => 'value',
'#value' => 'queue_ui_delete_submit',
);
return confirm_form($form, format_plural(count($form_state['storage']['queues']), 'Are you sure you want to delete the queue?', 'Are you sure you want to delete @count queues?'), 'admin/config/system/queue-ui', $description, t('Delete'), t('Cancel'));
}
function queue_ui_delete_submit($form, &$form_state, $queues) {
$values = $form_state['values'];
$defined_queues = queue_ui_defined_queues();
foreach ($queues as $name) {
$queue = DrupalQueue::get($name);
if (isset($defined_queues[$name]['delete'])) {
$function = $defined_queues[$name]['delete'];
$function($queue);
}
$queue
->deleteQueue();
}
drupal_set_message(format_plural(count($values['queues']), 'Queue deleted', '@count queues deleted'));
return;
}
function _queue_ui_array_keys_contain($input, $search_value, $strict = FALSE) {
$tmpkeys = array();
$keys = array_keys($input);
foreach ($keys as $k) {
if ($strict && strpos($k, $search_value) !== FALSE) {
$tmpkeys[] = $k;
}
elseif (!$strict && stripos($k, $search_value) !== FALSE) {
$tmpkeys[] = $k;
}
}
return $tmpkeys;
}