You are here

demo_reset.module in Demonstration site (Sandbox / Snapshot) 6

Demonstration site reset module.

This module is supposed to be used on public Drupal demonstration sites only. Use at your own risk.

File

demo_reset.module
View source
<?php

/**
 * @file
 * Demonstration site reset module.
 *
 * This module is supposed to be used on public Drupal demonstration sites only.
 * Use at your own risk.
 */
function demo_reset_form_demo_admin_settings_alter(&$form, &$form_state) {
  $form['status'][] = array(
    '#value' => t('<p><strong>Default snapshot:</strong> @snapshot</p>', array(
      '@snapshot' => variable_get('demo_dump_cron', t('None')),
    )),
  );
  $intervals = array(
    // 0, 5, 10, 15, 30 minutes
    0,
    300,
    600,
    900,
    1800,
    // 1-6, 9, 12 hours
    3600,
    3600 * 2,
    3600 * 3,
    3600 * 4,
    3600 * 5,
    3600 * 6,
    3600 * 9,
    3600 * 12,
    // 1-3 days
    86400,
    86400 * 2,
    86400 * 3,
    // 1, 2, 3 weeks
    604800,
    604800 * 2,
    604800 * 3,
    // 1, 3 months
    86400 * 30,
    86400 * 90,
  );
  $intervals = drupal_map_assoc($intervals, 'format_interval');
  $intervals[0] = t('Disabled');
  $form['dump']['demo_reset_interval'] = array(
    '#type' => 'select',
    '#title' => t('Automatically reset site every'),
    '#default_value' => variable_get('demo_reset_interval', 0),
    '#options' => $intervals,
    '#description' => t('Select how often this demonstration site is automatically reset. Ensure that you have chosen a snapshot for cron runs in <a href="!manage">Manage snapshots</a> first. <strong>Note:</strong> This requires cron to run at least within this interval.', array(
      '!manage' => url('admin/build/demo/manage'),
    )),
  );
}
function demo_reset_form_demo_manage_form_alter(&$form, &$form_state) {
  $demo_dump_cron = variable_get('demo_dump_cron', 'demo_site');
  foreach ($form['dump'] as $name => $option) {
    if ($name == $demo_dump_cron) {
      $form['dump'][$name]['#value'] = $name;
      break;
    }
  }
  $form['cron'] = array(
    '#type' => 'submit',
    '#value' => t('Use for cron runs'),
    '#submit' => array(
      'demo_reset_demo_manage_form_submit',
    ),
  );
}

/**
 * Form submit handler for demo_manage_form().
 */
function demo_reset_demo_manage_form_submit($form, &$form_state) {
  demo_reset_set_default($form_state['values']['filename']);
}

/**
 * Sets a specific snapshot as default for cron runs or the site reset block.
 *
 * @param $filename
 *   The filename of the snapshot.
 */
function demo_reset_set_default($filename) {
  variable_set('demo_dump_cron', $filename);
  drupal_set_message(t('Snapshot %title will be used for upcoming cron runs.', array(
    '%title' => $filename,
  )));
}
function demo_reset_form_demo_dump_form_alter(&$form, &$form_state) {
  $form['dump']['default'] = array(
    '#title' => t('Set as new default snapshot'),
    '#type' => 'checkbox',
  );
}

/**
 * Implements hook_demo_dump().
 */
function demo_reset_demo_dump($options, $info, $fileconfig) {

  // Set as new default snapshot.
  if (!empty($options['default'])) {
    demo_reset_set_default($info['filename']);
  }
}

/**
 * Implements hook_demo_reset().
 */
function demo_reset_demo_reset($filename, $info, $fileconfig) {

  // Reset default dump to load on cron. Normally, this should be the same as
  // the original value, but whenever we reset the site to a different snapshot,
  // it's very unlikely that we want to reset to the previous snapshot.
  variable_set('demo_dump_cron', $info['filename']);
}

/**
 * Implements hook_block().
 *
 * Lazy backport.
 */
function demo_reset_block($op = 'list', $delta = '', $edit = array()) {
  if ($op == 'list') {
    return demo_reset_block_info();
  }
  elseif ($op == 'view') {
    return demo_reset_block_view('reset');
  }
}

/**
 * Implements hook_block_list().
 */
function demo_reset_block_info() {
  $blocks['reset'] = array(
    'info' => t('Demonstration site reset'),
    'cache' => BLOCK_NO_CACHE,
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function demo_reset_block_view($delta = '') {
  $block = array(
    'subject' => t('Reset demo'),
    'content' => drupal_get_form('demo_reset_block_form'),
  );
  return $block;
}

/**
 * Form builder to reset site to configured snapshot.
 *
 * No access permission check or any condition here. By design.
 */
function demo_reset_block_form(&$form_state) {
  $form['redirect'] = array(
    '#type' => 'value',
    '#value' => $_GET['q'],
  );
  $filename = variable_get('demo_dump_cron', 'demo_site');
  $form['filename'] = array(
    '#type' => 'value',
    '#value' => $filename,
  );
  $form['snapshot'] = array(
    '#value' => t('Active snapshot: @snapshot', array(
      '@snapshot' => $filename,
    )),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Reset now'),
  );
  return $form;
}

/**
 * Form submit handler for demo_reset_block_form().
 */
function demo_reset_block_form_submit($form, &$form_state) {
  demo_reset($form_state['values']['filename']);
  $form_state['redirect'] = $form_state['values']['redirect'];
}

/**
 * Implementation of hook_cron().
 */
function demo_reset_cron() {
  if ($interval = variable_get('demo_reset_interval', 0)) {

    // See if it's time for a reset.
    if (time() - $interval >= variable_get('demo_reset_last', 0)) {
      demo_reset(variable_get('demo_dump_cron', 'demo_site'), FALSE);
    }
  }
}

Functions

Namesort descending Description
demo_reset_block Implements hook_block().
demo_reset_block_form Form builder to reset site to configured snapshot.
demo_reset_block_form_submit Form submit handler for demo_reset_block_form().
demo_reset_block_info Implements hook_block_list().
demo_reset_block_view Implements hook_block_view().
demo_reset_cron Implementation of hook_cron().
demo_reset_demo_dump Implements hook_demo_dump().
demo_reset_demo_manage_form_submit Form submit handler for demo_manage_form().
demo_reset_demo_reset Implements hook_demo_reset().
demo_reset_form_demo_admin_settings_alter @file Demonstration site reset module.
demo_reset_form_demo_dump_form_alter
demo_reset_form_demo_manage_form_alter
demo_reset_set_default Sets a specific snapshot as default for cron runs or the site reset block.