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'];
$queues = array_filter($values['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'),
),
);
$defined_queues = queue_ui_defined_queues();
$queues = queue_ui_queues();
foreach ($queues as $name => $queue) {
$title = '';
$operations = '';
if (isset($defined_queues[$name])) {
$title = $defined_queues[$name]['title'];
}
if (isset($defined_queues[$name]['batch'])) {
$operations = 'batch';
}
$options[$name] = array(
'name' => array(
'data' => $name,
),
'title' => array(
'data' => $title,
),
'items' => array(
'data' => $queue['items'],
),
);
}
$form['queues'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#empty' => t('No queues exist.'),
);
$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();
$intersect = array_intersect(array_keys($defined_queues), $queues);
foreach ($intersect as $name) {
if (isset($defined_queues[$name]['batch'])) {
$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);
}
}
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;
}