Projet

Général

Profil

Paste
Télécharger (11,9 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / panels / plugins / styles / stylizer.inc @ 08475715

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of the 'stylizer' panel style.
6
 */
7

    
8
if (module_exists('stylizer')) {
9
  // Plugin definition
10
  $plugin = array(
11
    'title' => t('Custom style'),
12
    'weight' => -10,
13
    'description' => t('Allows choice of a stylizer style'),
14

    
15
    'render pane' => 'panels_stylizer_stylizer_style_render_pane',
16
    'pane settings form' => 'panels_stylizer_stylizer_style_settings_form',
17

    
18
    'render region' => 'panels_stylizer_stylizer_style_render_region',
19
    'settings form' => 'panels_stylizer_stylizer_style_settings_form',
20

    
21
    // We offer substyles so provide callbacks to do so.
22
    'get child' => 'panels_stylizer_get_substyle',
23
    'get children' => 'panels_stylizer_get_substyles',
24

    
25
    // Set up an AJAX callback for the style
26
    'ajax' => array(
27
      'custom' => 'panels_stylizer_pane_add_style',
28
    ),
29
  //  'settings validate' => 'panels_stylizer_stylizer_style_settings_validate',
30
  );
31
}
32

    
33
/**
34
 * Merge the main stylizer plugin with a style to create a sub plugin.
35
 *
36
 * This is used for both panels_stylizer_get_substyle and
37
 * panels_stylizer_get_substyles.
38
 */
39
function panels_stylizer_merge_plugin($plugin, $style) {
40
  $plugin['name'] = 'stylizer:' . $style->name;
41
  $plugin['title'] = check_plain($style->admin_title);
42
  $plugin['description'] = check_plain($style->admin_description);
43
  $plugin['style'] = $style;
44
  $plugin['weight'] = 0;
45

    
46
  ctools_include('stylizer');
47
  $base = ctools_get_style_base($style->settings['style_base']);
48
  if ($base['type'] == 'pane') {
49
    unset($plugin['render region']);
50
  }
51
  else {
52
    unset($plugin['render pane']);
53
  }
54

    
55
  unset($plugin['settings form']);
56
  unset($plugin['pane settings form']);
57
  return $plugin;
58
}
59

    
60
/**
61
 * Callback to provide a single stored stylizer style.
62
 */
63
function panels_stylizer_get_substyle($plugin, $style_name, $substyle_name) {
64
  // Do not worry about caching; Panels is handling that for us.
65
  ctools_include('export');
66
  $item = ctools_export_crud_load('stylizer', $substyle_name);
67
  if ($item) {
68
    return panels_stylizer_merge_plugin($plugin, $item);
69
  }
70
}
71

    
72
/**
73
 * Callback to provide all stored stylizer styles.
74
 */
75
function panels_stylizer_get_substyles($plugin, $style_name) {
76
  $styles[$style_name] = $plugin;
77
  ctools_include('export');
78
  ctools_include('stylizer');
79
  $items = ctools_export_crud_load_all('stylizer');
80
  foreach ($items as $name => $item) {
81
    $base = ctools_get_style_base($item->settings['style_base']);
82
    if ($base && $base['module'] == 'panels') {
83
      $styles['stylizer:' . $name] = panels_stylizer_merge_plugin($plugin, $item);
84
    }
85
  }
86

    
87
  return $styles;
88
}
89

    
90
/**
91
 * Get style settings for a stylizer style.
92
 *
93
 * Because style settings can come from a couple of different places,
94
 * depending on if it's a custom style in the panel or a custom style
95
 * in the database, we have a tricky way of looking for this info.
96
 */
97
function _panels_stylizer_get_style($plugin, $style_settings) {
98
  if (!empty($plugin['style'])) {
99
    return $plugin['style']->settings;
100
  }
101

    
102
  if (empty($style_settings)) {
103
    return array();
104
  }
105

    
106
  if ($style_settings['style'] == '$') {
107
    return $style_settings['settings'];
108
  }
109

    
110
  ctools_include('export');
111
  $style = ctools_export_crud_load('stylizer', $style_settings['style']);
112
  if ($style) {
113
    return $style->settings;
114
  }
115
}
116

    
117
/**
118
 * Region render theme.
119
 */
120
function theme_panels_stylizer_stylizer_style_render_region($vars) {
121
  $display = $vars['display'];
122
  $panes = $vars['panes'];
123
  $style_settings = $vars['settings'];
124
  $region_id = $vars['region_id'];
125
  $plugin = $vars['style'];
126

    
127
  $output = '';
128

    
129
  foreach ($panes as $pane_id => $pane_output) {
130
    $output .= $pane_output;
131
  }
132

    
133
  $settings = _panels_stylizer_get_style($plugin, $style_settings);
134

    
135
  if (!empty($settings)) {
136
    ctools_include('stylizer');
137
    $plugin = ctools_get_style_base($settings['style_base']);
138
    ctools_stylizer_add_css($plugin, $settings);
139

    
140
    return theme($plugin['theme'], array('settings' => $settings, 'class' => ctools_stylizer_get_css_class($plugin, $settings), 'content' => $output));
141
  }
142
  else {
143
    // if the style is gone, just display the output.
144
    return $output;
145
  }
146
}
147

    
148
/**
149
 * Pane render theme
150
 */
151
function theme_panels_stylizer_stylizer_style_render_pane($vars) {
152
  $content = $vars['content'];
153
  $pane = $vars['pane'];
154
  $display = $vars['display'];
155
  $plugin = $vars['style'];
156

    
157
  $settings = _panels_stylizer_get_style($plugin, $vars['settings']);
158

    
159
  if ($settings) {
160
    ctools_include('stylizer');
161
    $plugin = ctools_get_style_base($settings['style_base']);
162

    
163
    if (empty($content->css_class)) {
164
      $content->css_class = ctools_stylizer_get_css_class($plugin, $settings);
165
    }
166
    else {
167
      $content->css_class .= ' ' . ctools_stylizer_get_css_class($plugin, $settings);
168
    }
169

    
170
    ctools_stylizer_add_css($plugin, $settings);
171

    
172
    if (isset($plugin['theme'])) {
173
      return theme($plugin['theme'], array('settings' => $settings, 'content' => $content, 'pane' => $pane, 'display' => $display));
174
    }
175
  }
176

    
177
  // if the style is gone or has no theme of its own, just display the output.
178
  return theme('panels_pane', array('content' => $content, 'pane' => $pane, 'display' => $display));
179
}
180

    
181
/**
182
 * Settings form callback.
183
 */
184
function panels_stylizer_stylizer_style_settings_form($style_settings, $display, $pid, $type, $form_state) {
185
  // Just redirect this to the custom style settings ajax.
186
  panels_stylizer_pane_add_style($form_state['renderer'], array(), $style_settings, $type, $pid);
187
  print ajax_render($form_state['renderer']->commands);
188
  ajax_footer();
189
  exit;
190
}
191

    
192

    
193
/**
194
 * Allow on-the-fly creation of styles in panes.
195
 */
196
function panels_stylizer_pane_add_style(&$renderer, $plugin, &$conf, $type, $pid, $step = NULL) {
197
  if (!user_access("administer panels $type styles")) {
198
    return;
199
  }
200

    
201
  // Reset the $_POST['ajax_html_ids'] values to preserve
202
  // proper IDs on form elements when auto-submit is used.
203
  $_POST['ajax_html_ids'] = array();
204

    
205
  ctools_include('stylizer');
206
  $js = FALSE;
207

    
208
  $path = $renderer->get_url('style', 'custom', $type, $pid, '%step');
209

    
210
  $info = array(
211
    'module' => 'panels',
212
    'type' => $type,
213
    'path' => $path,
214
    'modal' => t('Create custom style'),
215
    'owner form' => 'panels_stylizer_edit_pane_style_form',
216
    'owner form validate' => 'panels_stylizer_edit_pane_style_form_validate',
217
    'owner form submit' => 'panels_stylizer_edit_pane_style_form_submit',
218
    'owner settings' => array('preconfigured' => FALSE, 'name' => '', 'admin_title' => '', 'admin_description' => ''),
219
    'cache' => &$renderer->cache,
220
    'conf' => &$conf,
221
    'pid' => $pid,
222
  );
223

    
224
  if (!empty($conf['settings'])) {
225
    $info['settings'] = $conf['settings'];
226
  }
227

    
228
  $output = ctools_stylizer_edit_style($info, TRUE, $step);
229
  if (!empty($info['complete'])) {
230
    if (!empty($info['owner settings']['preconfigured'])) {
231
      ctools_include('export');
232
      $style = ctools_export_crud_new('stylizer');
233
      $style->name = $info['settings']['name'];
234
      $style->admin_title = $info['owner settings']['admin_title'];
235
      $style->admin_description = $info['owner settings']['admin_description'];
236
      $style->settings = $info['settings'];
237
      ctools_export_crud_save('stylizer', $style);
238
      $conf['style'] = $info['settings']['name'];
239
      if (isset($conf['settings'])) {
240
        unset($conf['settings']);
241
      }
242
    }
243
    else {
244
      $conf['style'] = '$';
245
      $conf['settings'] = $info['settings'];
246
    }
247

    
248
    // Be sure to unset the temporary if the style was just changed.
249
    if (isset($renderer->cache->style)) {
250
      unset($renderer->cache->style);
251
    }
252
    // $conf was a reference so it should just modify.
253
    panels_edit_cache_set($renderer->cache);
254

    
255
    $renderer->commands[] = ctools_modal_command_dismiss();
256

    
257
    if ($type == 'pane') {
258
      $renderer->command_update_pane($pid);
259
    }
260
    else if ($type == 'region') {
261
      $renderer->command_update_region_links($pid);
262
    }
263
    else {
264
      $renderer->command_update_display_links();
265
    }
266
  }
267
  else {
268
    $renderer->commands = $output;
269
  }
270
}
271

    
272

    
273
/**
274
 * The form for determining if a pane should create a local style or a
275
 * preconfigured style.
276
 */
277
function panels_stylizer_edit_pane_style_form(&$form, &$form_state) {
278
  if (!user_access('administer panels pane styles') || !module_exists('stylizer')) {
279
    return;
280
  }
281
  ctools_include('dependent');
282

    
283
  $settings = $form_state['owner info']['owner settings'];
284
  $form['panels']['admin_title'] = array(
285
    '#type' => 'textfield',
286
    '#title' => t('Administrative title'),
287
    '#description' => t('The name of this style. This will appear in the administrative interface to easily identify it.'),
288
    '#default_value' => $settings['admin_title'],
289
    '#process' => array('ctools_dependent_process'),
290
    '#dependency' => array('edit-preconfigured' => array(1)),
291
  );
292

    
293
  $form['panels']['name'] = array(
294
    '#type' => 'textfield',
295
    '#title' => t('Machine name'),
296
    '#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!'),
297
    '#default_value' => $settings['name'],
298
    '#process' => array('ctools_dependent_process'),
299
    '#dependency' => array('edit-preconfigured' => array(1)),
300
  );
301

    
302
  $form['panels']['admin_description'] = array(
303
    '#type' => 'textarea',
304
    '#title' => t('Administrative description'),
305
    '#description' => t('A description of what this style is, does or is for, for administrative use.'),
306
    '#default_value' => $settings['admin_description'],
307
    '#process' => array('ctools_dependent_process'),
308
    '#dependency' => array('edit-preconfigured' => array(1)),
309
  );
310

    
311
  // Add the checkbox, set the weight early
312
  $form['panels']['preconfigured'] = array(
313
    '#type' => 'checkbox',
314
    '#title' => t('Make this style available to other regions or panes'),
315
    '#default_value' => $settings['name'],
316
    '#weight' => -1,
317
  );
318

    
319
}
320

    
321
/**
322
 * Validate to see if we need to check the preconfigured values.
323
 */
324
function panels_stylizer_edit_pane_style_form_validate(&$form, &$form_state) {
325
  if (!user_access('administer panels pane styles')) {
326
    return;
327
  }
328

    
329
  // Only validate if preconfigured is checked.
330
  if ($form_state['values']['preconfigured'] && !empty($form_state['clicked_button']['#wizard type'])) {
331
    if (empty($form_state['values']['admin_title'])) {
332
      form_error($form['panels']['admin_title'], t('You must choose an administrative title.'));
333
    }
334

    
335
    // If this is new, make sure the name is unique:
336
    if ($form_state['op'] == 'add') {
337
      if (empty($form_state['values']['name'])) {
338
        form_error($form['panels']['name'], t('You must choose a machine name.'));
339
      }
340

    
341
      ctools_include('export');
342
      $test = ctools_export_crud_load('stylizer', $form_state['values']['name']);
343
      if ($test) {
344
        form_error($form['panels']['name'], t('That name is used by another style: @page', array('@page' => $test->admin_title)));
345
      }
346

    
347
      // Ensure name fits the rules:
348
      if (preg_match('/[^a-zA-Z0-9_]/', $form_state['values']['name'])) {
349
        form_error($form['panels']['name'], t('Name must be alphanumeric or underscores only.'));
350
      }
351
    }
352
  }
353
}
354

    
355
/**
356
 * Store the preconfigured values.
357
 */
358
function panels_stylizer_edit_pane_style_form_submit(&$form, &$form_state) {
359
  if (!user_access('administer panels pane styles')) {
360
    return;
361
  }
362

    
363
  // Only validate if preconfigured is checked.
364
  if ($form_state['values']['preconfigured'] && !empty($form_state['clicked_button']['#wizard type'])) {
365
    $form_state['owner info']['owner settings']['admin_title'] = $form_state['values']['admin_title'];
366
    $form_state['owner info']['owner settings']['admin_description'] = $form_state['values']['admin_description'];
367

    
368
    // Clean up preview files before we set the name
369
    ctools_stylizer_cleanup_style($form_state['plugin'], $form_state['settings']);
370

    
371
    $form_state['settings']['name'] = $form_state['values']['name'];
372
    $form_state['name'] = $form_state['values']['name'];
373
    $form_state['owner info']['owner settings']['preconfigured'] = $form_state['values']['preconfigured'];
374
  }
375
}