View source
<?php
require_once dirname(__FILE__) . '/node_revision_generate.helpers.inc';
require_once dirname(__FILE__) . '/../../node_revision_delete.helpers.inc';
function node_revision_generate_help($path, $arg) {
switch ($path) {
case 'admin/help#node_revision_generate':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Node Revision Generate module allow you to generate node revisions. This module is for test purpose.') . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Generating revisions') . '</dt>';
$output .= '<dd>' . t('To generate revisions visit the <a href="@generate-page">Node Revision Generate</a> page and select the content type for which you are going to generate revisions, then select the number of revision to generate for each node and the age between each revision and click on the Generate revisions button. The first revision for each node will be generated starting from the created date of the last revision and the last one will not have a date in the future.', array(
'@generate-page' => url('admin/config/content/node_revision_delete/generate_revisions'),
)) . '</dd>';
$output .= '</dl>';
return $output;
}
}
function node_revision_generate_permission() {
return array(
'generate revisions' => array(
'title' => t('Generate revisions'),
'description' => t('Allows generate revisions for content types.'),
),
);
}
function node_revision_generate_menu() {
$items = array();
$items['admin/config/content/node_revision_delete/generate_revisions'] = array(
'title' => 'Node Revision Generate',
'description' => 'Form to generate revisions.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'node_revision_generate_form',
),
'access callback' => 'user_access',
'access arguments' => array(
'generate revisions',
),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function node_revision_generate_node_presave($node) {
if (isset($node->node_revision_generate) && $node->node_revision_generate) {
$node->timestamp = $node->next_revision;
}
}
function node_revision_generate_form(array $form, array &$form_state) {
$form = array();
$form['bundles'] = array(
'#type' => 'checkboxes',
'#title' => t('Content types'),
'#options' => node_type_get_names(),
'#required' => TRUE,
);
$form['revisions_number'] = array(
'#type' => 'textfield',
'#title' => t('Revisions number'),
'#default_value' => 1,
'#description' => t('The number of revisions that will be created for each node of the selected content types.'),
'#required' => TRUE,
);
$form['age'] = array(
'#type' => 'fieldset',
'#title' => t('Revisions age'),
'#description' => t('The age between each generated revision.'),
'#required' => TRUE,
);
$form['age']['number'] = array(
'#type' => 'textfield',
'#default_value' => 1,
'#required' => TRUE,
);
$time_options = array(
'86400' => t('Day'),
'604800' => t('Week'),
'2592000' => t('Month'),
);
$form['age']['time'] = array(
'#type' => 'select',
'#options' => $time_options,
);
$form['description'] = array(
'#type' => 'item',
'#markup' => t('The first revision will be generated starting from the created date of the last node revision and the last one will not have a date in the future. So, depending on this maybe we will not generate the number of revisions you expect.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Generate revisions'),
);
$form['#prefix'] = _node_revision_delete_get_donation_text();
return $form;
}
function node_revision_generate_form_submit(array $form, array &$form_state) {
$bundles = array_filter($form_state['values']['bundles'], function ($bundles_selected) {
return $bundles_selected !== 0;
});
$revisions_number = $form_state['values']['revisions_number'];
$interval_number = $form_state['values']['number'];
$interval_time = $form_state['values']['time'];
$revisions_age = $interval_number * $interval_time;
node_revision_generate_create_revisions($bundles, $revisions_number, $revisions_age);
}
function node_revision_generate_create_revisions(array $bundles, $revisions_number, $revisions_age) {
$nodes_for_revisions = _node_revision_generate_get_available_nodes_for_revisions($bundles, $revisions_age);
if ($nodes_for_revisions) {
$operations = array();
foreach ($nodes_for_revisions as $node) {
$revision_timestamp = $node->timestamp;
$i = 0;
$revision_timestamp += $revisions_age;
while ($i < $revisions_number && $revision_timestamp <= REQUEST_TIME) {
$operations[] = array(
'node_revision_generate_create_operation',
array(
$node->nid,
$revision_timestamp,
),
);
$revision_timestamp += $revisions_age;
$i++;
}
}
$batch = array(
'title' => t('Generate revisions'),
'operations' => $operations,
'init_message' => t('Starting the creation of revisions.'),
'file' => drupal_get_path('module', 'node_revision_generate') . '/node_revision_generate.batch.inc',
'progress_message' => t('Processed @current out of @total (@percentage%). Estimated time: @estimate.'),
'finished' => 'node_revision_generate_create_finished',
'error_message' => t('The revision creation process has encountered an error.'),
);
batch_set($batch);
}
else {
drupal_set_message(t('There are not more available nodes to generate revisions of the selected content types and specified options.'), 'warning', FALSE);
}
}