demo.module 4.05 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 22

/**
 * 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) {
23
    $admin_access = user_access('administer demo settings');
sun's avatar
sun committed
24 25 26 27 28 29
    $items[] = array(
      'path' => 'admin/settings/demo',
      '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'),
30
      'access' => $admin_access,
sun's avatar
sun committed
31 32 33
    );
    $items[] = array(
      'path' => 'admin/settings/demo/maintenance',
34
      'title' => t('Status'),
sun's avatar
sun committed
35 36 37
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => 0,
    );
38 39 40 41 42
    $items[] = array(
      'path' => 'admin/settings/demo/manage',
      'title' => t('Manage snapshots'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('demo_manage'),
43
      'access' => $admin_access,
44 45 46
      'type' => MENU_LOCAL_TASK,
      'weight' => 1,
    );
sun's avatar
sun committed
47 48 49 50 51
    $items[] = array(
      'path' => 'admin/settings/demo/dump',
      'title' => t('Create snapshot'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('demo_dump'),
52
      'access' => $admin_access,
sun's avatar
sun committed
53
      'type' => MENU_LOCAL_TASK,
54
      'weight' => 2,
sun's avatar
sun committed
55 56 57 58 59 60
    );
    $items[] = array(
      'path' => 'admin/settings/demo/reset',
      'title' => t('Reset site'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('demo_reset_confirm'),
61
      'access' => $admin_access,
sun's avatar
sun committed
62
      'type' => MENU_LOCAL_TASK,
63
      'weight' => 3,
sun's avatar
sun committed
64
    );
65 66 67 68 69 70 71
    $items[] = array(
      'path' => 'demo/autocomplete',
      'title' => t('Demo Site autocomplete'),
      'callback' => 'demo_autocomplete',
      'access' => $admin_access,
      'type' => MENU_CALLBACK,
    );
sun's avatar
sun committed
72 73 74 75 76
  }
  
  return $items;
}

77 78 79
function demo_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
80
      $blocks[0] = array('info' => t('Demo site reset'), 'enabled' => 1, 'region' => 'right');
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
      return $blocks;
    
    case 'view':
      $block = array(
        'subject' => t('Reset demo'),
        'content' => drupal_get_form('demo_reset_now'),
      );
      return $block;
  }
}

function demo_reset_now() {
  $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;
}

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

116 117 118 119 120 121 122 123 124 125
/**
 * 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());
126 127 128 129
    }
  }
}

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

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

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

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

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