#940002: Custom style modal was broken.
[project/panels.git] / plugins / styles / stylizer.inc
CommitLineData
946024d2
SB
1<?php
2// $Id$
3
4/**
5 * @file
6 * Definition of the 'stylizer' panel style.
7 */
8
2f354acb
SB
9if (module_exists('stylizer')) {
10 // Plugin definition
11 $plugin = array(
12 'title' => t('Custom style'),
13 'weight' => -10,
14 'description' => t('Allows choice of a stylizer style'),
15
16 'render pane' => 'panels_stylizer_stylizer_style_render_pane',
17 'pane settings form' => 'panels_stylizer_stylizer_style_settings_form',
18
19 'render region' => 'panels_stylizer_stylizer_style_render_region',
20 'settings form' => 'panels_stylizer_stylizer_style_settings_form',
21
22 // We offer substyles so provide callbacks to do so.
23 'get child' => 'panels_stylizer_get_substyle',
24 'get children' => 'panels_stylizer_get_substyles',
25
26 // Set up an AJAX callback for the style
27 'ajax' => array(
28 'custom' => 'panels_stylizer_pane_add_style',
29 ),
30 // 'settings validate' => 'panels_stylizer_stylizer_style_settings_validate',
31 );
32}
946024d2
SB
33
34/**
35 * Merge the main stylizer plugin with a style to create a sub plugin.
36 *
37 * This is used for both panels_stylizer_get_substyle and
38 * panels_stylizer_get_substyles.
39 */
40function panels_stylizer_merge_plugin($plugin, $style) {
41 $plugin['name'] = 'stylizer:' . $style->name;
42 $plugin['title'] = check_plain($style->admin_title);
43 $plugin['description'] = check_plain($style->admin_description);
44 $plugin['style'] = $style;
45 $plugin['weight'] = 0;
46
47 ctools_include('stylizer');
48 $base = ctools_get_style_base($style->settings['style_base']);
49 if ($base['type'] == 'pane') {
50 unset($plugin['render region']);
51 }
52 else {
53 unset($plugin['render pane']);
54 }
55
56 unset($plugin['settings form']);
57 unset($plugin['pane settings form']);
58 return $plugin;
59}
60
61/**
62 * Callback to provide a single stored stylizer style.
63 */
64function panels_stylizer_get_substyle($plugin, $style_name, $substyle_name) {
65 // Do not worry about caching; Panels is handling that for us.
66 ctools_include('export');
67 $item = ctools_export_crud_load('stylizer', $substyle_name);
68 if ($item) {
69 return panels_stylizer_merge_plugin($plugin, $item);
70 }
71}
72
73/**
74 * Callback to provide all stored stylizer styles.
75 */
76function panels_stylizer_get_substyles($plugin, $style_name) {
77 $styles[$style_name] = $plugin;
78 ctools_include('export');
79 ctools_include('stylizer');
80 $items = ctools_export_crud_load_all('stylizer');
81 foreach ($items as $name => $item) {
82 $base = ctools_get_style_base($item->settings['style_base']);
83 if ($base && $base['module'] == 'panels') {
84 $styles['stylizer:' . $name] = panels_stylizer_merge_plugin($plugin, $item);
85 }
86 }
87
88 return $styles;
89}
90
91function _panels_stylizer_get_style($plugin, $style_settings) {
92 if (!empty($plugin['style'])) {
93 return $plugin['style']->settings;
94 }
95 else if ($style_settings['style'] == '$') {
96 return $style_settings['settings'];
97 }
98 else {
99 ctools_include('export');
100 $style = ctools_export_crud_load('stylizer', $style_settings['style']);
101 if ($style) {
102 return $style->settings;
103 }
104 }
105}
106
107/**
108 * Region render theme.
109 */
2f354acb
SB
110function theme_panels_stylizer_stylizer_style_render_region($vars) {
111 $display = $vars['display'];
112 $panes = $vars['panes'];
113 $style_settings = $vars['settings'];
114 $region_id = $vars['region_id'];
115 $plugin = $vars['style'];
116
946024d2
SB
117 $output = '';
118
119 foreach ($panes as $pane_id => $pane_output) {
120 $output .= $pane_output;
121 }
122
123 $settings = _panels_stylizer_get_style($plugin, $style_settings);
124
125 if (!empty($settings)) {
126 ctools_include('stylizer');
127 $plugin = ctools_get_style_base($settings['style_base']);
128 ctools_stylizer_add_css($plugin, $settings);
129
2f354acb 130 return theme($plugin['theme'], array('settings' => $settings, 'class' => ctools_stylizer_get_css_class($plugin, $settings), 'content' => $output));
946024d2
SB
131 }
132 else {
133 // if the style is gone, just display the output.
134 return $output;
135 }
136}
137
138/**
139 * Pane render theme
140 */
2f354acb
SB
141function theme_panels_stylizer_stylizer_style_render_pane($vars) {
142 $content = $vars['content'];
143 $pane = $vars['pane'];
144 $display = $vars['display'];
145 $plugin = $vars['style'];
146
147 $settings = _panels_stylizer_get_style($plugin, $vars['settings']);
946024d2
SB
148
149 if ($settings) {
150 ctools_include('stylizer');
151 $plugin = ctools_get_style_base($settings['style_base']);
152
153 if (empty($content->css_class)) {
154 $content->css_class = ctools_stylizer_get_css_class($plugin, $settings);
155 }
156 else {
157 $content->css_class .= ' ' . ctools_stylizer_get_css_class($plugin, $settings);
158 }
159
160 ctools_stylizer_add_css($plugin, $settings);
161
162 if (isset($plugin['theme'])) {
2f354acb 163 return theme($plugin['theme'], array('settings' => $settings, 'content' => $content, 'pane' => $pane, 'display' => $display));
946024d2
SB
164 }
165 }
166
167 // if the style is gone or has no theme of its own, just display the output.
2f354acb 168 return theme('panels_pane', array('content' => $content, 'pane' => $pane, 'display' => $display));
946024d2
SB
169}
170
171/**
172 * Settings form callback.
173 */
174function panels_stylizer_stylizer_style_settings_form($style_settings, $display, $pid, $type, $form_state) {
175 // Just redirect this to the custom style settings ajax.
176 panels_stylizer_pane_add_style($form_state['renderer'], array(), $style_settings, $type, $pid);
c7642ed5
EM
177 print ajax_render($form_state['renderer']->commands);
178 ajax_footer();
179 exit;
946024d2
SB
180}
181
182
183/**
184 * Allow on-the-fly creation of styles in panes.
185 */
186function panels_stylizer_pane_add_style(&$renderer, $plugin, &$conf, $type, $pid, $step = NULL) {
187 if (!user_access('administer panels styles')) {
188 return;
189 }
190
191 ctools_include('stylizer');
192 $js = FALSE;
193
194 $path = $renderer->get_url('style', 'custom', $type, $pid, '%step');
195
196 $info = array(
197 'module' => 'panels',
198 'type' => $type,
199 'path' => $path,
200 'modal' => t('Create custom style'),
201 'owner form' => 'panels_stylizer_edit_pane_style_form',
202 'owner form validate' => 'panels_stylizer_edit_pane_style_form_validate',
203 'owner form submit' => 'panels_stylizer_edit_pane_style_form_submit',
204 'owner settings' => array('preconfigured' => FALSE, 'name' => '', 'admin_title' => '', 'admin_description' => ''),
205 'cache' => &$renderer->cache,
206 'conf' => &$conf,
207 'pid' => $pid,
208 );
209
210 if (!empty($conf['settings'])) {
211 $info['settings'] = $conf['settings'];
212 }
213
214 $output = ctools_stylizer_edit_style($info, TRUE, $step);
215 if (!empty($info['complete'])) {
216 if (!empty($info['owner settings']['preconfigured'])) {
217 ctools_include('export');
218 $style = ctools_export_crud_new('stylizer');
219 $style->name = $info['settings']['name'];
220 $style->admin_title = $info['owner settings']['admin_title'];
221 $style->admin_description = $info['owner settings']['admin_description'];
222 $style->settings = $info['settings'];
223 ctools_export_crud_save('stylizer', $style);
224 $conf['style'] = $info['settings']['name'];
225 if (isset($conf['settings'])) {
226 unset($conf['settings']);
227 }
228 }
229 else {
230 $conf['style'] = '$';
231 $conf['settings'] = $info['settings'];
232 }
233
234 // Be sure to unset the temporary if the style was just changed.
235 if (isset($renderer->cache->style)) {
236 unset($renderer->cache->style);
237 }
238 // $conf was a reference so it should just modify.
239 panels_edit_cache_set($renderer->cache);
240
241 $renderer->commands[] = ctools_modal_command_dismiss();
242
243 if ($type == 'pane') {
244 $renderer->command_update_pane($pid);
245 }
246 else if ($type == 'region') {
247 $renderer->command_update_region_links($pid);
248 }
249 else {
250 $renderer->command_update_display_links();
251 }
252 }
253 else {
254 $renderer->commands = $output;
255 }
256}
257
258
259/**
260 * The form for determining if a pane should create a local style or a
261 * preconfigured style.
262 */
263function panels_stylizer_edit_pane_style_form(&$form, &$form_state) {
264 if (!user_access('administer panels styles') || !module_exists('stylizer')) {
265 return;
266 }
267 ctools_include('dependent');
268
269 $settings = $form_state['owner info']['owner settings'];
270 $form['panels']['admin_title'] = array(
271 '#type' => 'textfield',
272 '#title' => t('Administrative title'),
273 '#description' => t('The name of this style. This will appear in the administrative interface to easily identify it.'),
274 '#default_value' => $settings['admin_title'],
275 '#process' => array('ctools_dependent_process'),
276 '#dependency' => array('edit-preconfigured' => array(1)),
277 );
278
279 $form['panels']['name'] = array(
280 '#type' => 'textfield',
281 '#title' => t('Machine name'),
282 '#description' => t('The machine readable name of this page. It must be unique, and it must contain only alphanumeric characters and underscores. Once created, you will not be able to change this value!'),
283 '#default_value' => $settings['name'],
284 '#process' => array('ctools_dependent_process'),
285 '#dependency' => array('edit-preconfigured' => array(1)),
286 );
287
288 $form['panels']['admin_description'] = array(
289 '#type' => 'textarea',
290 '#title' => t('Administrative description'),
291 '#description' => t('A description of what this style is, does or is for, for administrative use.'),
292 '#default_value' => $settings['admin_description'],
293 '#process' => array('ctools_dependent_process'),
294 '#dependency' => array('edit-preconfigured' => array(1)),
295 );
296
297 // Add the checkbox, set the weight early
298 $form['panels']['preconfigured'] = array(
299 '#type' => 'checkbox',
300 '#title' => t('Make this style available to other regions or panes'),
301 '#default_value' => $settings['name'],
302 '#weight' => -1,
303 );
304
305}
306
307/**
308 * Validate to see if we need to check the preconfigured values.
309 */
310function panels_stylizer_edit_pane_style_form_validate(&$form, &$form_state) {
311 if (!user_access('administer panels styles')) {
312 return;
313 }
314
315 // Only validate if preconfigured is checked.
316 if ($form_state['values']['preconfigured'] && !empty($form_state['clicked_button']['#wizard type'])) {
317 if (empty($form_state['values']['admin_title'])) {
318 form_error($form['panels']['admin_title'], t('You must choose an administrative title.'));
319 }
320
321 // If this is new, make sure the name is unique:
322 if ($form_state['op'] == 'add') {
323 if (empty($form_state['values']['name'])) {
324 form_error($form['panels']['name'], t('You must choose a machine name.'));
325 }
326
327 ctools_include('export');
328 $test = ctools_export_crud_load('stylizer', $form_state['values']['name']);
329 if ($test) {
330 form_error($form['panels']['name'], t('That name is used by another style: @page', array('@page' => $test->admin_title)));
331 }
332
333 // Ensure name fits the rules:
334 if (preg_match('/[^a-zA-Z0-9_]/', $form_state['values']['name'])) {
335 form_error($form['panels']['name'], t('Name must be alphanumeric or underscores only.'));
336 }
337 }
338 }
339}
340
341/**
342 * Store the preconfigured values.
343 */
344function panels_stylizer_edit_pane_style_form_submit(&$form, &$form_state) {
345 if (!user_access('administer panels styles')) {
346 return;
347 }
348
349 // Only validate if preconfigured is checked.
350 if ($form_state['values']['preconfigured'] && !empty($form_state['clicked_button']['#wizard type'])) {
351 $form_state['owner info']['owner settings']['admin_title'] = $form_state['values']['admin_title'];
352 $form_state['owner info']['owner settings']['admin_description'] = $form_state['values']['admin_description'];
353
354 // Clean up preview files before we set the name
355 ctools_stylizer_cleanup_style($form_state['plugin'], $form_state['settings']);
356
357 $form_state['settings']['name'] = $form_state['values']['name'];
358 $form_state['name'] = $form_state['values']['name'];
359 $form_state['owner info']['owner settings']['preconfigured'] = $form_state['values']['preconfigured'];
360 }
361}