You are here

node_revision_delete.module in Node Revision Delete 7

Node Revision Delete Module

File

node_revision_delete.module
View source
<?php

/**
 * @file
 * Node Revision Delete Module
 */

/**
 * Implements hook_help().
 */
function node_revision_delete_help($path, $arg) {
  switch ($path) {
    case 'admin/help#node_revision_delete':
      $output = '';
      $output .= '<h3>' . t('About the Node Revision Delete Module') . '</h3>';
      $output .= '<p>' . t('The Node Revision Delete module allows you to manage the revisions of the Node according to your choice. It helps you to keep the specific number of revisions for the node. This module provides you the flexibility for applying the revision delete for the specific content type and run it on the specific time. You can manage your settings from the <a href="@node_revision_delete">Node Revision Delete Administration Page</a>', array(
        '@node_revision_delete' => url('admin/config/content/node_revision_delete'),
      )) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dd>' . t('Deleting the Node Revisions') . '</dd>';
      $output .= '<dd>' . t('Set the time to execute the Script') . '</dd>';
      $output .= '<dd>' . t('Select the Content type for which the revision delete will work') . '</dd>';
      $output .= '</dl>';
      return $output;
      break;
  }
}

/**
 * Implements hook_cron().
 */
function node_revision_delete_cron() {
  $total_revision_to_keep = variable_get('node_revision_delete_number');
  $node_revision_delete_content_type = explode(',', variable_get('node_revision_delete_content_type'));
  $node_revision_delete_time = variable_get('node_revision_delete_time');
  $node_revision_delete_last_execute = variable_get('node_revision_delete_last_execute');
  $execute_revision_delete = 0;
  $current_time = time();
  $time_difference = $current_time - $node_revision_delete_last_execute;
  switch ($node_revision_delete_time) {
    case 'run_now_only':
      $execute_revision_delete = 1;
      variable_set('node_revision_delete_time', 0);
      variable_set('node_revision_delete_last_execute', 0);
      break;
    case 'everyday':
      if ($time_difference > 86400) {
        variable_set('node_revision_delete_last_execute', $current_time);
        $execute_revision_delete = 1;
      }
      break;
    case 'every_week':
      if ($time_difference > 604800) {
        variable_set('node_revision_delete_last_execute', $current_time);
        $execute_revision_delete = 1;
      }
      break;
    case 'every_10_days':
      if ($time_difference > 864000) {
        variable_set('node_revision_delete_last_execute', $current_time);
        $execute_revision_delete = 1;
      }
      break;
    case 'every_15_days':
      if ($time_difference > 1296000) {
        variable_set('node_revision_delete_last_execute', $current_time);
        $execute_revision_delete = 1;
      }
      break;
    case 'every_month':
      if ($time_difference > 2592000) {
        variable_set('node_revision_delete_last_execute', $current_time);
        $execute_revision_delete = 1;
      }
      break;
    case 'every_3_months':
      if ($time_difference > 7776000) {
        variable_set('node_revision_delete_last_execute', $current_time);
        $execute_revision_delete = 1;
      }
      break;
    case 'every_6_months':
      if ($time_difference > 15552000) {
        variable_set('node_revision_delete_last_execute', $current_time);
        $execute_revision_delete = 1;
      }
      break;
    case 'every_year':
      if ($time_difference > 31536000) {
        variable_set('node_revision_delete_last_execute', $current_time);
        $execute_revision_delete = 1;
      }
      break;
  }
  if ($execute_revision_delete) {
    $query = db_select('node', 'n');
    $query
      ->condition('n.type', array(
      $node_revision_delete_content_type,
    ), 'IN')
      ->fields('n');
    $node_result = $query
      ->execute();
    foreach ($node_result as $node_array) {
      $revision_id_array = array();
      $revision_array = node_revision_list($node_array);
      foreach ($revision_array as $revision_array) {
        $revision_id_array[] = $revision_array->vid;
      }
      $revision_id_array_deleted = array_slice($revision_id_array, $total_revision_to_keep);
      foreach ($revision_id_array_deleted as $revision_id) {
        node_revision_delete($revision_id);
      }
    }
  }
}

/**
 * Implements hook_menu().
 */
function node_revision_delete_menu() {
  $items = array();
  $items['admin/config/content/node_revision_delete'] = array(
    'title' => 'Node Revision Delete Configuration',
    'description' => 'Configuration for Node Revision',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'node_revision_delete_form',
    ),
    'access arguments' => array(
      'access administration pages',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implements hook_form().
 */
function node_revision_delete_form($form, &$form_state) {
  $content_types = node_type_get_types();
  foreach ($content_types as $content_type_name) {
    $content_types_array[$content_type_name->type] = $content_type_name->name;
  }
  for ($i = 1; $i <= 100; $i++) {
    $options[$i] = $i;
  }
  $node_revision_delete_content_type_default = explode(",", variable_get('node_revision_delete_content_type'));
  $form['node_revision_delete_content_type'] = array(
    '#type' => 'checkboxes',
    '#options' => $content_types_array,
    '#title' => t('Select the Content types for deleting revision'),
    '#default_value' => $node_revision_delete_content_type_default,
  );
  $form['node_revision_delete_number'] = array(
    '#type' => 'select',
    '#title' => t('How many revisions do you want to keep ? '),
    '#options' => $options,
    '#default_value' => variable_get('node_revision_delete_number'),
  );
  $option_time = array(
    '0' => 'Please Select',
    'run_now_only' => 'Run Now Only',
    'everyday' => 'Everyday',
    'every_week' => 'Every Week',
    'every_10_days' => 'Every 10 Days',
    'every_15_days' => 'Every 15 Days',
    'every_month' => 'Every Month',
    'every_3_months' => 'Every 3 Months',
    'every_6_months' => 'Every 6 Months',
    'every_year' => 'Every Year',
  );
  $form['node_revision_delete_time'] = array(
    '#type' => 'select',
    '#options' => $option_time,
    '#title' => t('What time do you want to run the script ? '),
    '#default_value' => variable_get('node_revision_delete_time'),
  );
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}

/**
 * FormAPI submission to Save the Setting for deleting the revisions.
 */
function node_revision_delete_form_submit($form, &$form_state) {
  if (isset($form_state['values']['node_revision_delete_number'])) {
    variable_set('node_revision_delete_number', $form_state['values']['node_revision_delete_number']);
  }
  if (isset($form_state['values']['node_revision_delete_content_type'])) {
    $content_types_array = $form_state['values']['node_revision_delete_content_type'];
    foreach ($content_types_array as $key => $value) {
      if ($value) {
        $content_types_list[] = $key;
      }
    }
    if (count($content_types_list)) {
      $node_revision_delete_content_type = implode(",", $content_types_list);
    }
    variable_set('node_revision_delete_content_type', $node_revision_delete_content_type);
  }
  if (isset($form_state['values']['node_revision_delete_time'])) {
    variable_set('node_revision_delete_time', $form_state['values']['node_revision_delete_time']);
  }
  if ($form_state['values']['node_revision_delete_time'] == "run_now_only") {
    node_revision_delete_cron();
  }
  drupal_set_message(t('Setting Saved Successfully.'));
}

Functions

Namesort descending Description
node_revision_delete_cron Implements hook_cron().
node_revision_delete_form Implements hook_form().
node_revision_delete_form_submit FormAPI submission to Save the Setting for deleting the revisions.
node_revision_delete_help Implements hook_help().
node_revision_delete_menu Implements hook_menu().