<?php
function batch_example_menu() {
$items = array();
$items['batch_example'] = array(
'title' => 'Batch example',
'page callback' => 'drupal_get_form',
'page arguments' => array('batch_example_simple_form'),
'access callback' => TRUE,
);
$items['batch_example/example_1'] = array(
'title' => 'Simple form',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 0,
);
$items['batch_example/example_2'] = array(
'title' => 'Multistep form',
'page callback' => 'drupal_get_form',
'page arguments' => array('batch_example_multistep_form'),
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK,
'weight' => 1,
);
$items['batch_example/example_3'] = array(
'title' => 'No form',
'page callback' => 'batch_example_page',
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
return $items;
}
function batch_example_simple_form() {
$form['batch'] = array(
'#type' => 'select',
'#title' => 'Choose batch',
'#options' => array(
'batch_1' => 'batch 1 - load a node 100 times',
'batch_2' => 'batch 2 - load all nodes, 20 times')
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Go',
);
return $form;
}
function batch_example_simple_form_submit($form, &$form_state) {
$values =& $form_state['values'];
$function = 'batch_example_'. $values['batch'];
$batch = $function();
batch_set($batch);
$form_state['redirect'] = 'batch_example/example_2';
}
function batch_example_multistep_form($form_state = NULL) {
$step = isset($form_state['storage']['step']) ? $form_state['storage']['step'] : 1;
$form['step_display'] = array(
'#type' => 'item',
'#value' => 'step '. $step,
);
if ($step < 3) {
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Go',
);
}
return $form;
}
function batch_example_multistep_form_submit($form, &$form_state) {
$step = isset($form_state['storage']['step']) ? $form_state['storage']['step'] : 1;
switch ($step) {
case 1:
drupal_set_message('step 1 submitted');
batch_set(batch_example_batch_1());
break;
case 2:
drupal_set_message('step 2 submitted');
batch_set(batch_example_batch_2());
$form_state['redirect'] = 'batch_example/example_3';
break;
}
$form_state['storage']['step'] = $step + 1;
}
function batch_example_page() {
batch_set(batch_example_batch_1());
batch_process('batch_example/example_1');
}
function batch_example_batch_1() {
$nid = db_result(db_query_range("SELECT nid FROM {node} ORDER BY nid ASC", 0, 1));
$operations = array();
for ($i = 0; $i<100; $i++) {
$operations[] = array('batch_example_op_1', array($nid));
}
$batch = array(
'operations' => $operations,
'finished' => 'batch_example_finished',
);
return $batch;
}
function batch_example_op_1($nid, &$context) {
$node = node_load($nid, NULL, TRUE);
$context['results'][] = $node->nid .' : '. $node->title;
$context['message'] = 'Loading '. $node->title;
}
function batch_example_batch_2() {
$operations = array();
for ($i = 0; $i<20; $i++) {
$operations[] = array('batch_example_op_2', array());
}
$batch = array(
'operations' => $operations,
'finished' => 'batch_example_finished',
'title' => t('Processing batch 2'),
'init_message' => t('Batch 2 is starting.'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('Batch 1 has encountered an error.'),
);
return $batch;
}
function batch_example_op_2(&$context) {
if (!isset($context['sandbox']['progress'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['current_node'] = 0;
$context['sandbox']['max'] = db_result(db_query('SELECT COUNT(DISTINCT nid) FROM {node}'));
}
$limit = 5;
$result = db_query_range("SELECT nid FROM {node} WHERE nid > %d ORDER BY nid ASC", $context['sandbox']['current_node'], 0, $limit);
while ($row = db_fetch_array($result)) {
$node = node_load($row['nid'], NULL, TRUE);
$context['results'][] = $node->nid .' : '. $node->title;
$context['sandbox']['progress']++;
$context['sandbox']['current_node'] = $node->nid;
$context['message'] = $node->title;
}
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
function batch_example_finished($success, $results, $operations) {
if ($success) {
$message = count($results) .' processed.';
}
else {
$error_operation = reset($operations);
$message = 'An error occurred while processing '. $error_operation[0] .' with arguments :'. print_r($error_operation[0], TRUE);
}
drupal_set_message($message);
}