demo_reset.module 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. // $Id: demo_reset.module,v 1.2 2010/09/25 17:06:05 sun Exp $
  3. /**
  4. * @file
  5. * Demonstration site reset module.
  6. *
  7. * This module is supposed to be used on public Drupal demonstration sites only.
  8. * Use at your own risk.
  9. */
  10. /**
  11. * Implements hook_form_FORMID_alter().
  12. */
  13. function demo_reset_form_demo_admin_settings_alter(&$form, &$form_state) {
  14. $intervals = array(
  15. // 0, 5, 10, 15, 30 minutes
  16. 0, 300, 600, 900, 1800,
  17. // 1-6, 9, 12 hours
  18. 3600, 3600 * 2, 3600 * 3, 3600 * 4, 3600 * 5, 3600 * 6, 3600 * 9, 3600 * 12,
  19. // 1-3 days
  20. 86400, 86400 * 2, 86400 * 3,
  21. // 1, 2, 3 weeks
  22. 604800, 604800 * 2, 604800 * 3,
  23. // 1, 3 months
  24. 86400 * 30, 86400 * 90,
  25. );
  26. $intervals = drupal_map_assoc($intervals, 'format_interval');
  27. $form['demo_reset_interval'] = array(
  28. '#type' => 'select',
  29. '#title' => t('Automatic reset interval'),
  30. '#required' => FALSE,
  31. '#default_value' => variable_get('demo_reset_interval', 0),
  32. '#options' => $intervals,
  33. '#empty_value' => 0,
  34. '#description' => t('Requires a <a href="@snapshots-url">default snapshot</a> and <a href="@cron-url">cron</a> to run in the configured interval.', array(
  35. '@snapshots-url' => url('admin/structure/demo'),
  36. '@cron-url' => url('admin/config/system/cron'),
  37. )),
  38. );
  39. }
  40. /**
  41. * Implements hook_form_FORMID_alter().
  42. */
  43. function demo_reset_form_demo_manage_form_alter(&$form, &$form_state) {
  44. $form['status']['demo_reset_default'] = array(
  45. '#type' => 'item',
  46. '#title' => t('Default snapshot'),
  47. '#markup' => check_plain(variable_get('demo_dump_cron', t('- None -'))),
  48. );
  49. $demo_dump_cron = variable_get('demo_dump_cron', 'demo_site');
  50. foreach ($form['dump'] as $name => $option) {
  51. if ($name == $demo_dump_cron) {
  52. $form['dump'][$name]['#value'] = $name;
  53. break;
  54. }
  55. }
  56. $form['actions']['cron'] = array(
  57. '#type' => 'submit',
  58. '#value' => t('Use for cron runs'),
  59. '#submit' => array('demo_reset_demo_manage_form_submit'),
  60. );
  61. }
  62. /**
  63. * Form submit handler for demo_manage_form().
  64. */
  65. function demo_reset_demo_manage_form_submit($form, &$form_state) {
  66. demo_reset_set_default($form_state['values']['filename']);
  67. }
  68. /**
  69. * Sets a specific snapshot as default for cron runs or the site reset block.
  70. *
  71. * @param $filename
  72. * The filename of the snapshot.
  73. */
  74. function demo_reset_set_default($filename) {
  75. variable_set('demo_dump_cron', $filename);
  76. drupal_set_message(t('Snapshot %title will be used for cron runs.', array('%title' => $filename)));
  77. }
  78. function demo_reset_form_demo_dump_form_alter(&$form, &$form_state) {
  79. $form['dump']['default'] = array(
  80. '#title' => t('Use as default snapshot for cron runs'),
  81. '#type' => 'checkbox',
  82. );
  83. }
  84. /**
  85. * Implements hook_demo_dump().
  86. */
  87. function demo_reset_demo_dump($options, $info, $fileconfig) {
  88. // Set as new default snapshot.
  89. if (!empty($options['default'])) {
  90. demo_reset_set_default($info['filename']);
  91. }
  92. }
  93. /**
  94. * Implements hook_demo_reset().
  95. */
  96. function demo_reset_demo_reset($filename, $info, $fileconfig) {
  97. // Reset default dump to load on cron. Normally, this should be the same as
  98. // the original value, but whenever we reset the site to a different snapshot,
  99. // it's very unlikely that we want to reset to the previous snapshot.
  100. variable_set('demo_dump_cron', $info['filename']);
  101. }
  102. /**
  103. * Implements hook_block_list().
  104. */
  105. function demo_reset_block_info() {
  106. $blocks['reset'] = array(
  107. 'info' => t('Demonstration site reset'),
  108. 'status' => 1,
  109. 'region' => 'sidebar_second',
  110. 'cache' => DRUPAL_NO_CACHE,
  111. );
  112. return $blocks;
  113. }
  114. /**
  115. * Implements hook_block_view().
  116. */
  117. function demo_reset_block_view($delta = '') {
  118. if (!variable_get('demo_dump_cron', '')) {
  119. return array();
  120. }
  121. $block = array(
  122. 'subject' => t('Reset demo'),
  123. 'content' => drupal_get_form('demo_reset_block_form'),
  124. );
  125. return $block;
  126. }
  127. /**
  128. * Form builder to reset site to configured snapshot.
  129. *
  130. * No access permission check or any condition here. By design.
  131. */
  132. function demo_reset_block_form($form, &$form_state) {
  133. $form['redirect'] = array(
  134. '#type' => 'value',
  135. '#value' => $_GET['q'],
  136. );
  137. $filename = variable_get('demo_dump_cron', '');
  138. $form['filename'] = array(
  139. '#type' => 'value',
  140. '#value' => $filename,
  141. );
  142. $form['snapshot'] = array(
  143. '#markup' => t('Active snapshot: @snapshot', array('@snapshot' => $filename)),
  144. );
  145. $form['submit'] = array(
  146. '#type' => 'submit',
  147. '#value' => t('Reset now'),
  148. );
  149. return $form;
  150. }
  151. /**
  152. * Form submit handler for demo_reset_block_form().
  153. */
  154. function demo_reset_block_form_submit($form, &$form_state) {
  155. demo_reset($form_state['values']['filename']);
  156. $form_state['redirect'] = $form_state['values']['redirect'];
  157. }
  158. /**
  159. * Implementation of hook_cron().
  160. */
  161. function demo_reset_cron() {
  162. if ($interval = variable_get('demo_reset_interval', 0)) {
  163. // See if it's time for a reset.
  164. if ((REQUEST_TIME - $interval) >= variable_get('demo_reset_last', 0)) {
  165. demo_reset(variable_get('demo_dump_cron', 'demo_site'), FALSE);
  166. }
  167. }
  168. }