1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Provides a simple time-based caching option for panel panes.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
// Plugin definition
|
9 |
|
|
$plugin = array(
|
10 |
|
|
'title' => t("Simple cache"),
|
11 |
|
|
'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.'),
|
12 |
|
|
'cache get' => 'panels_simple_cache_get_cache',
|
13 |
|
|
'cache set' => 'panels_simple_cache_set_cache',
|
14 |
|
|
'cache clear' => 'panels_simple_cache_clear_cache',
|
15 |
|
|
'settings form' => 'panels_simple_cache_settings_form',
|
16 |
|
|
'settings form submit' => 'panels_simple_cache_settings_form_submit',
|
17 |
|
|
'defaults' => array(
|
18 |
|
|
'lifetime' => 15,
|
19 |
|
|
'granularity' => 'none',
|
20 |
|
|
),
|
21 |
|
|
);
|
22 |
|
|
|
23 |
|
|
/**
|
24 |
|
|
* Get cached content.
|
25 |
|
|
*/
|
26 |
|
|
function panels_simple_cache_get_cache($conf, $display, $args, $contexts, $pane = NULL) {
|
27 |
|
|
$cid = panels_simple_cache_get_id($conf, $display, $args, $contexts, $pane);
|
28 |
5a7e6170
|
Florent Torregrosa
|
$cache = cache_get($cid, 'cache_panels');
|
29 |
85ad3d82
|
Assos Assos
|
if (!$cache) {
|
30 |
|
|
return FALSE;
|
31 |
|
|
}
|
32 |
|
|
|
33 |
|
|
if ((time() - $cache->created) > $conf['lifetime']) {
|
34 |
|
|
return FALSE;
|
35 |
|
|
}
|
36 |
|
|
|
37 |
|
|
return $cache->data;
|
38 |
|
|
}
|
39 |
|
|
|
40 |
|
|
/**
|
41 |
|
|
* Set cached content.
|
42 |
|
|
*/
|
43 |
|
|
function panels_simple_cache_set_cache($conf, $content, $display, $args, $contexts, $pane = NULL) {
|
44 |
|
|
$cid = panels_simple_cache_get_id($conf, $display, $args, $contexts, $pane);
|
45 |
5a7e6170
|
Florent Torregrosa
|
cache_set($cid, $content, 'cache_panels');
|
46 |
85ad3d82
|
Assos Assos
|
}
|
47 |
|
|
|
48 |
|
|
/**
|
49 |
|
|
* Clear cached content.
|
50 |
|
|
*
|
51 |
|
|
* Cache clears are always for an entire display, regardless of arguments.
|
52 |
|
|
*/
|
53 |
|
|
function panels_simple_cache_clear_cache($display) {
|
54 |
|
|
$cid = 'panels_simple_cache';
|
55 |
|
|
|
56 |
5a7e6170
|
Florent Torregrosa
|
// If the panel is stored in the database it'll have a numeric did value.
|
57 |
|
|
if (is_numeric($display->did)) {
|
58 |
|
|
$cid .= ':' . $display->did;
|
59 |
|
|
}
|
60 |
|
|
// Exported panels won't have a numeric did but may have a usable cache_key.
|
61 |
|
|
elseif (!empty($display->cache_key)) {
|
62 |
|
|
$cid .= ':' . str_replace('panel_context:', '', $display->cache_key);
|
63 |
|
|
}
|
64 |
|
|
// Alternatively use the css_id.
|
65 |
|
|
elseif (!empty($display->css_id)) {
|
66 |
|
|
$cid .= ':' . $display->css_id;
|
67 |
|
|
}
|
68 |
|
|
// Failover to just appending the did, which may be the completely unusable
|
69 |
|
|
// string 'new'.
|
70 |
|
|
else {
|
71 |
|
|
$cid .= ':' . $display->did;
|
72 |
85ad3d82
|
Assos Assos
|
}
|
73 |
|
|
|
74 |
5a7e6170
|
Florent Torregrosa
|
cache_clear_all($cid, 'cache_panels', TRUE);
|
75 |
85ad3d82
|
Assos Assos
|
}
|
76 |
|
|
|
77 |
|
|
/**
|
78 |
|
|
* Figure out an id for our cache based upon input and settings.
|
79 |
|
|
*/
|
80 |
|
|
function panels_simple_cache_get_id($conf, $display, $args, $contexts, $pane) {
|
81 |
|
|
$id = 'panels_simple_cache';
|
82 |
|
|
|
83 |
|
|
// If the panel is stored in the database it'll have a numeric did value.
|
84 |
|
|
if (is_numeric($display->did)) {
|
85 |
|
|
$id .= ':' . $display->did;
|
86 |
|
|
}
|
87 |
|
|
// Exported panels won't have a numeric did but may have a usable cache_key.
|
88 |
|
|
elseif (!empty($display->cache_key)) {
|
89 |
|
|
$id .= ':' . str_replace('panel_context:', '', $display->cache_key);
|
90 |
|
|
}
|
91 |
|
|
// Alternatively use the css_id.
|
92 |
|
|
elseif (!empty($display->css_id)) {
|
93 |
|
|
$id .= ':' . $display->css_id;
|
94 |
|
|
}
|
95 |
|
|
// Failover to just appending the did, which may be the completely unusable
|
96 |
|
|
// string 'new'.
|
97 |
|
|
else {
|
98 |
|
|
$id .= ':' . $display->did;
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
if ($pane) {
|
102 |
|
|
$id .= ':' . $pane->pid;
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
if (user_access('view pane admin links')) {
|
106 |
|
|
$id .= ':admin';
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
switch ($conf['granularity']) {
|
110 |
|
|
case 'args':
|
111 |
|
|
foreach ($args as $arg) {
|
112 |
|
|
$id .= ':' . $arg;
|
113 |
|
|
}
|
114 |
|
|
break;
|
115 |
|
|
|
116 |
|
|
case 'context':
|
117 |
|
|
if (!is_array($contexts)) {
|
118 |
|
|
$contexts = array($contexts);
|
119 |
|
|
}
|
120 |
|
|
foreach ($contexts as $context) {
|
121 |
|
|
if (isset($context->argument)) {
|
122 |
|
|
$id .= ':' . $context->argument;
|
123 |
|
|
}
|
124 |
|
|
}
|
125 |
|
|
}
|
126 |
|
|
if (module_exists('locale')) {
|
127 |
|
|
global $language;
|
128 |
|
|
$id .= ':' . $language->language;
|
129 |
|
|
}
|
130 |
|
|
|
131 |
|
|
if(!empty($pane->configuration['use_pager']) && !empty($_GET['page'])) {
|
132 |
|
|
$id .= ':p' . check_plain($_GET['page']);
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
return $id;
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
function panels_simple_cache_settings_form($conf, $display, $pid) {
|
139 |
|
|
$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');
|
140 |
|
|
$form['lifetime'] = array(
|
141 |
|
|
'#title' => t('Lifetime'),
|
142 |
|
|
'#type' => 'select',
|
143 |
|
|
'#options' => $options,
|
144 |
|
|
'#default_value' => $conf['lifetime'],
|
145 |
|
|
);
|
146 |
|
|
|
147 |
|
|
$form['granularity'] = array(
|
148 |
|
|
'#title' => t('Granularity'),
|
149 |
|
|
'#type' => 'select',
|
150 |
|
|
'#options' => array(
|
151 |
|
|
'args' => t('Arguments'),
|
152 |
|
|
'context' => t('Context'),
|
153 |
|
|
'none' => t('None'),
|
154 |
|
|
),
|
155 |
|
|
'#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.'),
|
156 |
|
|
'#default_value' => $conf['granularity'],
|
157 |
|
|
);
|
158 |
|
|
|
159 |
|
|
return $form;
|
160 |
|
|
}
|