1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file panels_mini.module |
---|
5 | * |
---|
6 | * This module provides mini panels which are basically panels that can be |
---|
7 | * used within blocks or other panels. |
---|
8 | */ |
---|
9 | |
---|
10 | /** |
---|
11 | * Implementation of hook_perm(). |
---|
12 | */ |
---|
13 | function panels_mini_perm() { |
---|
14 | return array('create mini panels', 'administer mini panels'); |
---|
15 | } |
---|
16 | |
---|
17 | /** |
---|
18 | * Implementation of hook_menu(). |
---|
19 | */ |
---|
20 | function panels_mini_menu() { |
---|
21 | // Safety: go away if CTools is not at an appropriate version. |
---|
22 | if (!defined('PANELS_REQUIRED_CTOOLS_API') || !module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) { |
---|
23 | return array(); |
---|
24 | } |
---|
25 | require_once drupal_get_path('module', 'panels_mini') . '/panels_mini.admin.inc'; |
---|
26 | return _panels_mini_menu(); |
---|
27 | } |
---|
28 | |
---|
29 | // --------------------------------------------------------------------------- |
---|
30 | // Allow the rest of the system access to mini panels |
---|
31 | |
---|
32 | /** |
---|
33 | * Implementation of hook_block(). |
---|
34 | * |
---|
35 | * Expose qualifying mini panels to Drupal's block system. |
---|
36 | */ |
---|
37 | function panels_mini_block($op = 'list', $delta = 0, $edit = array()) { |
---|
38 | // Safety: go away if CTools is not at an appropriate version. |
---|
39 | if (!defined('PANELS_REQUIRED_CTOOLS_API') || !module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) { |
---|
40 | return array(); |
---|
41 | } |
---|
42 | |
---|
43 | if ($op == 'list') { |
---|
44 | $blocks = array(); |
---|
45 | |
---|
46 | $minis = panels_mini_load_all(); |
---|
47 | foreach ($minis as $panel_mini) { |
---|
48 | if (empty($mini->disabled) && empty($mini->requiredcontext)) { |
---|
49 | $blocks[$panel_mini->name] = array( |
---|
50 | 'info' => t('Mini panel: "@title"', array('@title' => $panel_mini->admin_title)), |
---|
51 | 'cache' => BLOCK_NO_CACHE, |
---|
52 | ); |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | return $blocks; |
---|
57 | } |
---|
58 | elseif ($op == 'view') { |
---|
59 | // static recursion protection. |
---|
60 | static $viewing = array(); |
---|
61 | if (!empty($viewing[$delta])) { |
---|
62 | return; |
---|
63 | } |
---|
64 | $viewing[$delta] = TRUE; |
---|
65 | |
---|
66 | $panel_mini = panels_mini_load($delta); |
---|
67 | if (empty($panel_mini)) { |
---|
68 | // Bail out early if the specified mini panel doesn't exist. |
---|
69 | return; |
---|
70 | } |
---|
71 | |
---|
72 | ctools_include('context'); |
---|
73 | $panel_mini->context = $panel_mini->display->context = ctools_context_load_contexts($panel_mini); |
---|
74 | $panel_mini->display->css_id = panels_mini_get_id($panel_mini->name); |
---|
75 | |
---|
76 | $block = array(); |
---|
77 | |
---|
78 | $block['content'] = panels_render_display($panel_mini->display); |
---|
79 | $block['subject'] = $panel_mini->display->get_title(); |
---|
80 | |
---|
81 | unset($viewing[$delta]); |
---|
82 | return $block; |
---|
83 | } |
---|
84 | elseif ($op = 'configure') { |
---|
85 | return array( |
---|
86 | 'admin-shortcut' => array( |
---|
87 | '#value' => l(t('Manage this mini-panel'), "admin/build/mini-panels/list/$delta/edit") |
---|
88 | ), |
---|
89 | ); |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * Statically store all used IDs to ensure all mini panels get a unique id. |
---|
95 | */ |
---|
96 | function panels_mini_get_id($name) { |
---|
97 | static $id_cache = array(); |
---|
98 | |
---|
99 | $id = 'mini-panel-' . $name; |
---|
100 | if (!empty($id_cache[$name])) { |
---|
101 | $id .= "-" . $id_cache[$name]++; |
---|
102 | } |
---|
103 | else { |
---|
104 | $id_cache[$name] = 1; |
---|
105 | } |
---|
106 | |
---|
107 | return $id; |
---|
108 | } |
---|
109 | |
---|
110 | // --------------------------------------------------------------------------- |
---|
111 | // Database functions. |
---|
112 | |
---|
113 | /** |
---|
114 | * Create a new page with defaults appropriately set from schema. |
---|
115 | */ |
---|
116 | function panels_mini_new($set_defaults = TRUE) { |
---|
117 | ctools_include('export'); |
---|
118 | return ctools_export_new_object('panels_mini', $set_defaults); |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * Load a single mini panel. |
---|
123 | */ |
---|
124 | function panels_mini_load($name) { |
---|
125 | $cache = &ctools_static('panels_mini_load_all', array()); |
---|
126 | |
---|
127 | // We use array_key_exists because failed loads will be NULL and |
---|
128 | // isset() will try to load it again. |
---|
129 | if (!array_key_exists($name, $cache)) { |
---|
130 | ctools_include('export'); |
---|
131 | $result = ctools_export_load_object('panels_mini', 'names', array($name)); |
---|
132 | if (isset($result[$name])) { |
---|
133 | if (empty($result[$name]->display)) { |
---|
134 | $result[$name]->display = panels_load_display($result[$name]->did); |
---|
135 | if (!empty($result[$name]->title) && empty($result[$name]->display->title)) { |
---|
136 | $result[$name]->display->title = $result[$name]->title; |
---|
137 | } |
---|
138 | } |
---|
139 | $cache[$name] = $result[$name]; |
---|
140 | if (!empty($result[$name]->title) && empty($result[$name]->admin_title)) { |
---|
141 | $cache[$name]->admin_title = $result[$name]->title; |
---|
142 | } |
---|
143 | } |
---|
144 | else { |
---|
145 | $cache[$name] = NULL; |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | if (isset($cache[$name])) { |
---|
150 | return $cache[$name]; |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | /** |
---|
155 | * Load all mini panels. |
---|
156 | */ |
---|
157 | function panels_mini_load_all($reset = FALSE) { |
---|
158 | $cache = &ctools_static('panels_mini_load_all', array()); |
---|
159 | static $all_loaded = FALSE; |
---|
160 | |
---|
161 | // We check our own private static because individual minis could have |
---|
162 | // been loaded prior to load all and we need to know that. |
---|
163 | if (!$all_loaded || $reset) { |
---|
164 | $all_loaded = TRUE; |
---|
165 | if ($reset) { |
---|
166 | $cache = array(); |
---|
167 | } |
---|
168 | |
---|
169 | ctools_include('export'); |
---|
170 | $minis = ctools_export_load_object('panels_mini'); |
---|
171 | $dids = array(); |
---|
172 | foreach ($minis as $mini) { |
---|
173 | if (empty($cache[$mini->name])) { |
---|
174 | if (!empty($mini->did)) { |
---|
175 | $dids[$mini->did] = $mini->name; |
---|
176 | } |
---|
177 | else { |
---|
178 | // Translate old style titles into new titles. |
---|
179 | if (!empty($mini->title) && empty($mini->display->title)) { |
---|
180 | $mini->display->title = $mini->title; |
---|
181 | } |
---|
182 | } |
---|
183 | // Translate old style titles into new titles. |
---|
184 | if (isset($mini->title) && empty($mini->admin_title)) { |
---|
185 | $mini->admin_title = $mini->title; |
---|
186 | } |
---|
187 | $cache[$mini->name] = $mini; |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | $displays = panels_load_displays(array_keys($dids)); |
---|
192 | foreach ($displays as $did => $display) { |
---|
193 | if (!empty($cache[$dids[$did]]->title) && empty($display->title)) { |
---|
194 | $display->title = $cache[$dids[$did]]->title; |
---|
195 | } |
---|
196 | $cache[$dids[$did]]->display = $display; |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | return $cache; |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * Write a mini panel to the database. |
---|
205 | */ |
---|
206 | function panels_mini_save(&$mini) { |
---|
207 | if (!empty($mini->display)) { |
---|
208 | $display = panels_save_display($mini->display); |
---|
209 | $mini->did = $display->did; |
---|
210 | } |
---|
211 | |
---|
212 | $update = (isset($mini->pid) && $mini->pid != 'new') ? array('pid') : array(); |
---|
213 | drupal_write_record('panels_mini', $mini, $update); |
---|
214 | |
---|
215 | return $mini; |
---|
216 | } |
---|
217 | |
---|
218 | /** |
---|
219 | * Remove a mini panel. |
---|
220 | */ |
---|
221 | function panels_mini_delete($mini) { |
---|
222 | db_query("DELETE FROM {panels_mini} WHERE name = '%s'", $mini->name); |
---|
223 | if ($mini->type != t('Overridden')) { |
---|
224 | db_query("DELETE FROM {blocks} WHERE module = 'panels_mini' AND delta = '%s'", $mini->name); |
---|
225 | } |
---|
226 | return panels_delete_display($mini->did); |
---|
227 | } |
---|
228 | |
---|
229 | /** |
---|
230 | * Export a mini panel. |
---|
231 | */ |
---|
232 | function panels_mini_export($mini, $indent = '') { |
---|
233 | ctools_include('export'); |
---|
234 | $output = ctools_export_object('panels_mini', $mini, $indent); |
---|
235 | // Export the primary display |
---|
236 | $display = !empty($mini->display) ? $mini->display : panels_load_display($mini->did); |
---|
237 | $output .= panels_export_display($display, $indent); |
---|
238 | $output .= $indent . '$mini->display = $display' . ";\n"; |
---|
239 | return $output; |
---|
240 | } |
---|
241 | |
---|
242 | /** |
---|
243 | * Remove the block version of mini panels from being available content types. |
---|
244 | */ |
---|
245 | function panels_mini_ctools_block_info($module, $delta, &$info) { |
---|
246 | $info = NULL; |
---|
247 | } |
---|
248 | |
---|
249 | /** |
---|
250 | * Implementation of hook_ctools_plugin_directory() to let the system know |
---|
251 | * we implement task and task_handler plugins. |
---|
252 | */ |
---|
253 | function panels_mini_ctools_plugin_directory($module, $plugin) { |
---|
254 | if ($module == 'ctools' && ($plugin == 'content_types' || $plugin == 'export_ui')) { |
---|
255 | return 'plugins/' . $plugin; |
---|
256 | } |
---|
257 | } |
---|
258 | |
---|
259 | /** |
---|
260 | * Get the display cache for the panels_mini plugin. |
---|
261 | */ |
---|
262 | function _panels_mini_panels_cache_get($key) { |
---|
263 | ctools_include('export-ui'); |
---|
264 | $plugin = ctools_get_export_ui('panels_mini'); |
---|
265 | $handler = ctools_export_ui_get_handler($plugin); |
---|
266 | if (!$handler) { |
---|
267 | return; |
---|
268 | } |
---|
269 | |
---|
270 | $item = $handler->edit_cache_get($key); |
---|
271 | if (!$item) { |
---|
272 | $item = ctools_export_crud_load($handler->plugin['schema'], $key); |
---|
273 | } |
---|
274 | |
---|
275 | return array($handler, $item); |
---|
276 | } |
---|
277 | |
---|
278 | /** |
---|
279 | * Get display edit cache for the panels mini export UI |
---|
280 | * |
---|
281 | * The key is the second half of the key in this form: |
---|
282 | * panels_mini:TASK_NAME:HANDLER_NAME; |
---|
283 | */ |
---|
284 | function panels_mini_panels_cache_get($key) { |
---|
285 | ctools_include('common', 'panels'); |
---|
286 | list($handler, $item) = _panels_mini_panels_cache_get($key); |
---|
287 | if (isset($item->mini_panels_display_cache)) { |
---|
288 | return $item->mini_panels_display_cache; |
---|
289 | } |
---|
290 | |
---|
291 | $cache = new stdClass(); |
---|
292 | $cache->display = $item->display; |
---|
293 | $cache->display->context = ctools_context_load_contexts($item); |
---|
294 | $cache->display->cache_key = 'panels_mini:' . $key; |
---|
295 | $cache->content_types = panels_common_get_allowed_types('panels_mini', $cache->display->context); |
---|
296 | $cache->display_title = TRUE; |
---|
297 | |
---|
298 | // @TODO support locking |
---|
299 | $cache->locked = FALSE; |
---|
300 | |
---|
301 | return $cache; |
---|
302 | } |
---|
303 | |
---|
304 | /** |
---|
305 | * Store a display edit in progress in the page cache. |
---|
306 | */ |
---|
307 | function panels_mini_panels_cache_set($key, $cache) { |
---|
308 | list($handler, $item) = _panels_mini_panels_cache_get($key); |
---|
309 | $item->mini_panels_display_cache = $cache; |
---|
310 | $handler->edit_cache_set_key($item, $key); |
---|
311 | } |
---|
312 | |
---|
313 | /** |
---|
314 | * Save all changes made to a display using the panels mini UI cache. |
---|
315 | */ |
---|
316 | function panels_mini_panels_cache_clear($key, $cache) { |
---|
317 | list($handler, $item) = _panels_mini_panels_cache_get($key); |
---|
318 | $handler->edit_cache_clear($item); |
---|
319 | } |
---|
320 | |
---|
321 | /** |
---|
322 | * Save all changes made to a display using the panels mini UI cache. |
---|
323 | */ |
---|
324 | function panels_mini_panels_cache_save($key, $cache) { |
---|
325 | list($handler, $item) = _panels_mini_panels_cache_get($key); |
---|
326 | $item->display = $cache->display; |
---|
327 | panels_mini_save($item); |
---|
328 | |
---|
329 | $handler->edit_cache_clear($item); |
---|
330 | } |
---|
331 | |
---|
332 | /** |
---|
333 | * Break the lock on a panels mini page. |
---|
334 | */ |
---|
335 | function panels_mini_panels_cache_break_lock($key, $cache) { |
---|
336 | } |
---|
337 | |
---|
338 | /** |
---|
339 | * Alter default mini panels to ensure they have new fields and avoid warnings. |
---|
340 | */ |
---|
341 | function panels_mini_default_panels_mini_alter(&$minis) { |
---|
342 | foreach ($minis as $name => $mini) { |
---|
343 | if (!isset($mini->admin_description)) { |
---|
344 | $minis[$name]->admin_description = ''; |
---|
345 | } |
---|
346 | } |
---|
347 | } |
---|
348 | |
---|
349 | /** |
---|
350 | * Implementation of hook_panels_dashboard_blocks(). |
---|
351 | * |
---|
352 | * Adds mini panels information to the Panels dashboard. |
---|
353 | */ |
---|
354 | function panels_mini_panels_dashboard_blocks(&$vars) { |
---|
355 | $vars['links']['panels_mini'] = array( |
---|
356 | 'title' => l(t('Mini panel'), 'admin/build/mini-panels/add'), |
---|
357 | 'description' => t('Mini panels are small content areas exposed as blocks, for when you need to have complex block layouts or layouts within layouts.'), |
---|
358 | 'weight' => -1, |
---|
359 | ); |
---|
360 | |
---|
361 | // Load all mini panels and their displays. |
---|
362 | $panel_minis = panels_mini_load_all(); |
---|
363 | $count = 0; |
---|
364 | $rows = array(); |
---|
365 | |
---|
366 | foreach ($panel_minis as $panel_mini) { |
---|
367 | $rows[] = array( |
---|
368 | check_plain($panel_mini->admin_title), |
---|
369 | array( |
---|
370 | 'data' => l(t('Edit'), "admin/build/mini-panels/list/$panel_mini->name/edit"), |
---|
371 | 'class' => 'links', |
---|
372 | ), |
---|
373 | ); |
---|
374 | |
---|
375 | // Only show 10. |
---|
376 | if (++$count >= 10) { |
---|
377 | break; |
---|
378 | } |
---|
379 | } |
---|
380 | |
---|
381 | if ($rows) { |
---|
382 | $content = theme('table', array(), $rows, array('class' => 'panels-manage')); |
---|
383 | } |
---|
384 | else { |
---|
385 | $content = '<p>' . t('There are no mini panels.') . '</p>'; |
---|
386 | } |
---|
387 | |
---|
388 | $vars['blocks']['panels_mini'] = array( |
---|
389 | 'weight' => -100, |
---|
390 | 'title' => t('Manage mini panels'), |
---|
391 | 'link' => l(t('Go to list'), 'admin/build/mini-panels'), |
---|
392 | 'content' => $content, |
---|
393 | 'class' => 'dashboard-mini-panels', |
---|
394 | 'section' => 'left', |
---|
395 | ); |
---|
396 | |
---|
397 | } |
---|