View source
<?php
function node_recur_node_batch_start($node, $start_dates, $end_dates = array()) {
$operations = array();
if ($start_dates) {
foreach ($start_dates as $key => $start_date) {
$operations[] = array(
'node_recur_node_batch_create_node',
array(
$node,
$start_date,
isset($end_dates[$key]) ? $end_dates[$key] : NULL,
),
);
}
}
$batch = array(
'title' => t('Generating items'),
'operations' => $operations,
'finished' => 'node_recur_node_batch_finished',
'file' => drupal_get_path('module', 'node_recur') . '/node_recur.batch.inc',
);
$_SESSION['batch_recur_nid'] = $node->nid;
batch_set($batch);
}
function node_recur_node_batch_create_node($node, $start_date, $end_date = NULL, $context) {
$context['message'] = t('Date: !date', array(
'!date' => format_date($start_date),
));
$clone = node_load($node->nid, NULL, TRUE);
$clone->is_recurring = TRUE;
$clone->recurring_node = $node;
$field_name = node_recur_get_date_field_name($node->type);
$field_info = field_info_field($field_name);
switch ($field_info['type']) {
case 'date':
$format = DATE_FORMAT_ISO;
break;
case 'datestamp':
$format = DATE_FORMAT_UNIX;
break;
case 'datetime':
$format = DATE_FORMAT_DATETIME;
break;
}
$date = $clone->{$field_name};
$date[LANGUAGE_NONE][0]['value'] = date($format, $start_date);
if ($end_date) {
$date[LANGUAGE_NONE][0]['value2'] = date($format, $end_date);
}
$clone->{$field_name} = $date;
$context['results'][] = $start_date;
if (module_exists('clone')) {
$context['prefix_title'] = FALSE;
clone_action_clone($clone, $context);
}
else {
$clone->clone_from_original_nid = $node->nid;
unset($clone->nid);
unset($clone->created);
unset($clone->changed);
unset($clone->vid);
node_save($clone);
}
}
function node_recur_node_batch_finished($success, $results, $operations) {
if ($success) {
$count = count($results);
drupal_set_message(t('%count %items have been generated.', array(
'%count' => $count,
'%items' => format_plural($count, 'item', 'items'),
)));
}
else {
drupal_set_message(t('An error occurred. Please try again.'), 'error');
}
$path = NULL;
$nid = NULL;
if (isset($_SESSION['batch_recur_nid']) && is_numeric($_SESSION['batch_recur_nid'])) {
$nid = $_SESSION['batch_recur_nid'];
$path = 'node/' . $nid;
}
drupal_alter('node_recur_batch_redirect', $path, $nid);
if ($path) {
drupal_goto($path);
}
}