by douggreen: D6 menu and cache system compliance.
[project/panels.git] / panels_simple_cache / panels_simple_cache.module
CommitLineData
db205271
EM
1<?php
2// $Id$
3
4
5/**
6 * @file panels_simple_cache.module
7 *
8 * Provides a simple time-based caching option for panel panes.
9 */
10
11/**
12 * Implementation of hook_menu().
13 */
53dd59af
SB
14function panels_simple_cache_menu() {
15 $items['admin/panels/simple-cache'] = array(
16 'title' => 'Simple cache',
17 'access' => 'panels_simple_cache_access',
18 'type' => MENU_NORMAL_ITEM,
19 'callback' => 'panels_simple_cache_admin',
20 'description' => 'Information about Panels simple cache.',
21 );
22 return $items;
23}
24
25/**
26 * Access callback for the simple_cache administration menu.
27 */
28function panels_simple_cache_access() {
29 return user_access('access administration pages') && user_access('use panels caching features');
db205271 30}
db205271
EM
31
32/**
33 * Page callback for the very short admin page.
34 */
35function panels_simple_cache_admin() {
36 $output = '<p>';
37 $output .= t('Panels simple cache does not have a normal administrative UI, such as panels pages or mini panels. With this module, you are given the option to add caching features to any panel display in panel pages, mini panels, panel nodes or any other displays provided by other modules or plugins. These options are available as an icon on each pane of the edit content pane.');
38 $output .= '</p><p>';
39 $output .= t('This module provides only very simple, time-based caching; it is not at all suitable if your content will change at all per user (and this can mean administrative additions that are just visible to you) as all users will see the same content; it is provided mostly as a reference implementation for other, smarter caching modules.');
40 $output .= '</p>';
41 return $output;
42}
43
44/**
45 * Implementation of hook_panels_cache()
46 */
47function panels_simple_cache_panels_cache() {
48 $cache['simple'] = array(
49 'title' => t("Simple cache"),
50 'description' => t('Simple caching is a time-based cache. This is a hard limit, and once cached it will remain that way until the time limit expires.'),
51 'cache get' => 'panels_simple_cache_get_cache',
52 'cache set' => 'panels_simple_cache_set_cache',
53 'cache clear' => 'panels_simple_cache_clear_cache',
54 'settings form' => 'panels_simple_cache_settings_form',
55 'settings form submit' => 'panels_simple_cache_settings_form_submit',
56 );
57 return $cache;
58}
59
60/**
61 * Get cached content.
62 */
63function panels_simple_cache_get_cache($conf, $display, $args, $contexts, $pane = NULL) {
64 $cid = panels_simple_cache_get_id($conf, $display, $args, $contexts, $pane);
65 $cache = cache_get($cid, 'cache');
66 if (!$cache) {
67 return FALSE;
68 }
69
70 if ((time() - $cache->created) > $conf['lifetime']) {
71 return FALSE;
72 }
73
74 return unserialize($cache->data);
75}
76
77/**
78 * Set cached content.
79 */
80function panels_simple_cache_set_cache($conf, $content, $display, $args, $contexts, $pane = NULL) {
81 $cid = panels_simple_cache_get_id($conf, $display, $args, $contexts, $pane);
53dd59af 82 cache_set($cid, $content);
db205271
EM
83}
84
85/**
86 * Clear cached content.
87 *
88 * Cache clears are always for an entire display, regardless of arguments.
89 */
90function panels_simple_cache_clear_cache($display) {
91 $cid = 'panels_simple_cache';
92
93 // This is used in case this is an in-code display, which means did will be something like 'new-1'.
94 if (isset($display->owner) && isset($display->owner->id)) {
95 $cid .= ':' . $display->owner->id;
96 }
97 $cid .= ':' . $display->did;
98
99 cache_clear_all($cid, 'cache', TRUE);
100}
101
102/**
103 * Figure out an id for our cache based upon input and settings.
104 */
105function panels_simple_cache_get_id($conf, $display, $args, $contexts, $pane) {
106 $id = 'panels_simple_cache';
107
108 // This is used in case this is an in-code display, which means did will be something like 'new-1'.
109 if (isset($display->owner) && isset($display->owner->id)) {
110 $id .= ':' . $display->owner->id;
111 }
112 $id .= ':' . $display->did;
113
114 if ($pane) {
115 $id .= ':' . $pane->pid;
116 }
117
118 if (user_access('view pane admin links')) {
119 $id .= ':admin';
120 }
121
122 switch ($conf['granularity']) {
123 case 'args':
124 foreach ($args as $arg) {
125 $id .= ':' . $arg;
126 }
127 break;
128
129 case 'context':
130 if (!is_array($contexts)) {
131 $contexts = array($contexts);
132 }
133 foreach ($contexts as $context) {
134 if (isset($context->argument)) {
135 $id .= ':' . $context->argument;
136 }
137 }
138 }
139 return $id;
140}
141
142function panels_simple_cache_settings_form($conf, $display, $pid) {
143 $options = drupal_map_assoc(array(15, 30, 60, 120, 180, 240, 300, 600, 900, 1200, 1800, 3600, 7200, 14400, 28800, 43200, 86400, 172800, 259200, 345600, 604800), 'format_interval');
144 $form['lifetime'] = array(
145 '#title' => t('Lifetime'),
146 '#type' => 'select',
147 '#options' => $options,
148 '#default_value' => $conf['lifetime'],
149 );
150
151 $form['granularity'] = array(
152 '#title' => t('Granularity'),
153 '#type' => t('select'),
154 '#options' => array(
155 'args' => t('Arguments'),
156 'context' => t('Context'),
157 'none' => t('None'),
158 ),
159 '#description' => t('If "arguments" are selected, this content will be cached per individual argument to the entire display; if "contexts" are selected, this content will be cached per unique context in the pane or display; if "neither" there will be only one cache for this pane.'),
160 '#default_value' => $conf['granularity'],
161 );
162
163 return $form;
164}
165