demo.module 4.1 KB
Newer Older
sun's avatar
sun committed
1 2 3
<?php
// $Id$

smk-ka's avatar
smk-ka committed
4 5
/**
 * @file
6
 * Demonstration Site module
smk-ka's avatar
smk-ka committed
7
 */
sun's avatar
sun committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21

/**
 * Implementation of hook_perm().
 */
function demo_perm() {
  return array('administer demo settings');
}

/**
 * Implementation of hook_menu().
 */
function demo_menu($may_cache) {
  $items = array();
  if ($may_cache) {
22
    $admin_access = user_access('administer demo settings');
sun's avatar
sun committed
23
    $items[] = array(
24
      'path' => 'admin/build/demo',
sun's avatar
sun committed
25 26 27 28
      'title' => t('Demonstration site'),
      'description' => t('Administer reset interval, create new dumps and manually reset this site.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('demo_admin_settings'),
29
      'access' => $admin_access,
sun's avatar
sun committed
30 31
    );
    $items[] = array(
32
      'path' => 'admin/build/demo/maintenance',
33
      'title' => t('Status'),
sun's avatar
sun committed
34 35 36
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => 0,
    );
37
    $items[] = array(
38
      'path' => 'admin/build/demo/manage',
39 40 41
      'title' => t('Manage snapshots'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('demo_manage'),
42
      'access' => $admin_access,
43 44 45
      'type' => MENU_LOCAL_TASK,
      'weight' => 1,
    );
sun's avatar
sun committed
46
    $items[] = array(
47
      'path' => 'admin/build/demo/dump',
sun's avatar
sun committed
48 49 50
      'title' => t('Create snapshot'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('demo_dump'),
51
      'access' => $admin_access,
sun's avatar
sun committed
52
      'type' => MENU_LOCAL_TASK,
53
      'weight' => 2,
sun's avatar
sun committed
54 55
    );
    $items[] = array(
56
      'path' => 'admin/build/demo/reset',
sun's avatar
sun committed
57 58 59
      'title' => t('Reset site'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('demo_reset_confirm'),
60
      'access' => $admin_access,
sun's avatar
sun committed
61
      'type' => MENU_LOCAL_TASK,
62
      'weight' => 3,
sun's avatar
sun committed
63
    );
64 65 66 67 68 69 70
    $items[] = array(
      'path' => 'demo/autocomplete',
      'title' => t('Demo Site autocomplete'),
      'callback' => 'demo_autocomplete',
      'access' => $admin_access,
      'type' => MENU_CALLBACK,
    );
sun's avatar
sun committed
71 72 73 74
  }
  return $items;
}

75 76 77
/**
 * Implementation of hook_block().
 */
78 79 80
function demo_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
81
      $blocks[0] = array('info' => t('Demo site reset'), 'enabled' => 1, 'region' => 'right');
82 83 84 85 86 87 88 89 90 91 92 93
      return $blocks;
    
    case 'view':
      $block = array(
        'subject' => t('Reset demo'),
        'content' => drupal_get_form('demo_reset_now'),
      );
      return $block;
  }
}

function demo_reset_now() {
94
  $form = array();
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  $form['redirect'] = array(
    '#type' => 'value',
    '#value' => $_GET['q'],
  );
  $form['filename'] = array(
    '#type' => 'value',
    '#value' => variable_get('demo_dump_cron', 'demo_site'),
  );
  $form['snapshot'] = array(
    '#value' => t('Active snapshot: !snapshot', array('!snapshot' => variable_get('demo_dump_cron', 'demo_site'))),
  );
  $form['reset-demo'] = array(
    '#type' => 'submit',
    '#value' => t('Reset now'),
  );
  return $form;
}

113 114
function demo_reset_now_submit($form_id, $values) {
  require_once drupal_get_path('module', 'demo') .'/demo.admin.inc';
115
  return demo_reset_confirm_submit($form_id, $values);
116 117
}

118 119 120 121 122 123 124 125 126 127
/**
 * Implementation of hook_cron().
 */
function demo_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)) {
      require_once drupal_get_path('module', 'demo') .'/demo.admin.inc';
      demo_reset(variable_get('demo_dump_cron', 'demo_site'), FALSE);
      variable_set('demo_reset_last', time());
128 129 130 131
    }
  }
}

132 133 134
function demo_admin_settings() {
  require_once drupal_get_path('module', 'demo') .'/demo.admin.inc';
  return _demo_admin_settings();
135 136
}

137 138 139
function demo_manage() {
  require_once drupal_get_path('module', 'demo') .'/demo.admin.inc';
  return _demo_manage();
sun's avatar
sun committed
140 141
}

142 143 144
function demo_dump() {
  require_once drupal_get_path('module', 'demo') .'/demo.admin.inc';
  return _demo_dump();
145 146
}

147 148 149
function demo_reset_confirm() {
  require_once drupal_get_path('module', 'demo') .'/demo.admin.inc';
  return _demo_reset_confirm();
sun's avatar
sun committed
150 151
}

152
function demo_autocomplete($string = '') {
153 154
  require_once drupal_get_path('module', 'demo') .'/demo.admin.inc';
  return _demo_autocomplete($string);
155
}
156