Projet

Général

Profil

Paste
Télécharger (19,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / sweaver / plugins / sweaver_plugin_themesettings / sweaver_plugin_themesettings.inc @ 651307cd

1
<?php
2

    
3
/**
4
 * @file
5
 * Theme settings plugin.
6
 */
7
class sweaver_plugin_themesettings extends sweaver_plugin {
8

    
9
  /**
10
   * Init function.
11
   */
12
  public function sweaver_init() {
13
    // Override theme settings in $conf variable when in Draft.
14
    if (sweaver_session(NULL, 'draft_mode') || sweaver_session(NULL, 'sweaver_temp')) {
15
      $sweaver = Sweaver::get_instance();
16
      $sweaver_style = $sweaver->get_current_style();
17
      if (isset($sweaver_style->themesettings) && !empty($sweaver_style->themesettings)) {
18
        global $conf;
19
        $theme_settings_name = str_replace('/', '_', 'theme_'. $sweaver->get_theme_key() .'_settings');
20
        $conf[$theme_settings_name] = unserialize($sweaver_style->themesettings);
21
      }
22
    }
23
  }
24

    
25
  /**
26
   * Frontend form.
27
   */
28
  public function sweaver_form() {
29
    $form = array();
30
    $form['#popups'] = array();
31

    
32
    // Get theme settings form.
33
    $themesettings = array();
34
    $sweaver = Sweaver::get_instance();
35
    $sweaver_style = $sweaver->get_current_style();
36
    if (isset($sweaver_style->themesettings) && !empty($sweaver_style->themesettings)) {
37
      $themesettings = unserialize($sweaver_style->themesettings);
38
    }
39
    $form = $this->sweaver_get_theme_settings_form($sweaver->get_theme_key(), $themesettings);
40

    
41
    // Add extra property for theme settings which need to be saved also.
42
    // Form alters on the theme settings form must be put those keys in this array.
43
    $form['#sweaver_other_themesettings'] = array();
44

    
45
    // Let other modules alter the form.
46
    $form['#method'] = 'post';
47
    $form_state['method'] = 'post';
48
    drupal_prepare_form('system_theme_settings', $form, $form_state);
49
    // We need to unset the #type, otherwise a nested form is created, which breaks in IE7.
50
    unset($form['#type']);
51
    
52
    // Convert all fieldsets to buttons. The content of those fieldsets
53
    // will be moved into the Sweaver popup.
54
    $i = 1;
55
    $weight = -100;
56
    foreach (element_children($form) as $key) {
57
      if (isset($form[$key]['#type']) && $form[$key]['#type'] == 'fieldset') {
58

    
59
        // Skip fieldsets which the user doesn't have access to.
60
        if (isset($form[$key]['#access']) && $form[$key]['#access'] === FALSE) {
61
          continue;
62
        }
63

    
64
        $title = $form[$key]['#title'];
65

    
66
        $form['item_'. $i] = array(
67
          '#markup' => '<div class="popup-link"><a href="#" id="theme-settings-link-'. $i .'">'. check_plain($title) .'</a></div>',
68
          '#weight' => $weight++,
69
        );
70

    
71
        if (isset($form[$key]['#prefix'])) {
72
          $form[$key]['#prefix'] .= '<div style="display:none" id="theme-settings-data-'. $i .'">';
73
        }
74
        else {
75
          $form[$key]['#prefix'] = '<div style="display:none"id="theme-settings-data-'. $i .'">';
76
        }
77

    
78
        if (isset($form[$key]['#suffix'])) {
79
          $form[$key]['#suffix'] = '</div>'. $form[$key]['#suffix'];
80
        }
81
        else {
82
          $form[$key]['#suffix'] = '</div>';
83
        }
84

    
85
        unset($form[$key]['#collapsible']);
86
        unset($form[$key]['#collapsed']);
87
        unset($form[$key]['#attributes']);
88

    
89
        // Add to popups.
90
        $form['#popups'][] = $key;
91

    
92
        $i++;
93
      }
94
    }
95

    
96
    // Deny access to the buttons and add our own.
97
    $form['buttons']['#access'] = FALSE;
98
    $form['theme_settings_apply'] = array(
99
      '#type' => 'submit',
100
      '#value' => t('Apply theme settings'),
101
      '#prefix' => '<div class="clearfix"></div>',
102
    );
103

    
104
    return $form;
105
  }
106

    
107
  /**
108
   * Frontend form submit.
109
   */
110
  public function sweaver_form_submit($form, &$form_state) {
111

    
112
    $values = $form_state['values'];
113
    $theme_key = $values['var'];
114
    $clicked_button = $form_state['clicked_button']['#value'];
115

    
116
    // Save style.
117
    if ($clicked_button == t('Save and continue') || $clicked_button == t('Save and publish') || $clicked_button == t('Publish style') || $clicked_button == t('Apply theme settings')) {
118

    
119
      // Style id is not always set.
120
      $style_id = isset($form_state['style_id']) ? $form_state['style_id'] : 'temp';
121

    
122
      // General theme settings.
123
      $theme_values = array();
124
      $theme_settings_form = $form['sweaver_plugin_themesettings']['form']['theme_settings'];
125
      $theme_form_keys = element_children($theme_settings_form);
126

    
127
      // Logo and favicon.
128
      if (isset($form['sweaver_plugin_themesettings']['form']['logo'])) {
129
        $theme_form_keys = array_merge($theme_form_keys, array('default_logo', 'logo_path', 'logo_upload'));
130
      }
131
      if (isset($form['sweaver_plugin_themesettings']['form']['favicon'])) {
132
        $theme_form_keys = array_merge($theme_form_keys, array('default_favicon', 'favicon_path', 'favicon_upload'));
133
      }
134

    
135
      // Theme specific settings.
136
      if (isset($form['sweaver_plugin_themesettings']['form']['theme_specific'])) {
137
        $theme_specific_keys = array();
138
        $fapi_types = array('select', 'checkbox', 'checkboxes', 'textfield', 'textarea', 'value', 'hidden', 'radio', 'weight', 'radios');
139
        $this->sweaver_get_theme_specific_keys($form['sweaver_plugin_themesettings']['form']['theme_specific'], $theme_specific_keys, $fapi_types);
140
        $theme_form_keys = array_merge($theme_form_keys, $theme_specific_keys);
141
      }
142

    
143
      // Other variables which are set by other form alters.
144
      // All keys must be put in a $form['#sweaver_other_themesettings'];
145
      $theme_form_keys = array_merge($theme_form_keys, $form['sweaver_plugin_themesettings']['form']['#sweaver_other_themesettings']);
146

    
147
      // Only save the necessary theme values.
148
      foreach ($values as $fkey => $fvalue) {
149
        if (in_array($fkey, $theme_form_keys)) {
150
          $theme_values[$fkey] = $fvalue;
151
        }
152
      }
153

    
154
      // Rename logo / favicon when needed.
155
      foreach (array('logo_path', 'favicon_path') as $file) {
156

    
157
        // Create the default variable for this file.
158
        $default = 'default_' . str_replace('_path', '', $file);
159
        $theme_name = $form['#current_theme'];
160

    
161
        // Rename when just uploaded.
162
        if (isset($form['sweaver_plugin_themesettings']['form']['#'. $file])) {
163
          $type = str_replace('_path', '', $file);
164
          $source_path = $form['sweaver_plugin_themesettings']['form']['#' . $file];
165
          $dest_draft = 'public://sweaver/' . $type . '_' . $theme_name . '_' . $style_id . '_draft.' . $form['sweaver_plugin_themesettings']['form']['#' . $file . '_extension'];
166
          file_unmanaged_move($source_path, $dest_draft, FILE_EXISTS_REPLACE);
167
          $theme_values[$file] = $dest_draft;
168
          $theme_values[$default] = 0;
169
        }
170

    
171
        // Move temporary if needed.
172
        if ($style_id != 'temp' && !$theme_values[$default] && !empty($theme_values[$file]) && strpos($theme_values[$file], 'temp_draft') !== FALSE) {
173
          $type = str_replace('_path', '', $file);
174
          $parts = pathinfo($theme_values[$file]);
175
          $temp_path = $theme_values[$file];
176
          $dest_draft = 'public://sweaver/' . $type . '_'. $theme_name . '_' . $style_id . '_draft.' . $parts['extension'];
177
          file_unmanaged_move($temp_path, $dest_draft, FILE_EXISTS_REPLACE);
178
          $theme_values[$file] = $dest_draft;
179
        }
180

    
181
        // Copy to live if save_live is found. Can either be directly after the upload
182
        // or when setting a style live after the uploads were done already.
183
        if (isset($form_state['publish']) && $form_state['publish'] && !$theme_values[$default] && !empty($theme_values[$file])) {
184
          $source_path = $theme_values[$file];
185
          $dest_live = str_replace('_draft', '_live', $theme_values[$file]);
186
          file_unmanaged_copy($source_path, $dest_live, FILE_EXISTS_REPLACE);
187
          $theme_values[$file] = $dest_live;
188
        }
189
      }
190

    
191
      // Save when style_id is found. Always save the draft version. If the
192
      // save_live key is found also update the live table.
193
      if ($style_id != 'temp') {
194
        $theme_values_serialized = serialize($theme_values);
195
        db_query("UPDATE {sweaver_style_draft} set themesettings = :themesettings WHERE style_id = :style_id", array(':themesettings' => $theme_values_serialized, ':style_id' => $style_id));
196
        if ($form_state['publish']) {
197
          variable_set($theme_key, $theme_values);
198
          db_query("UPDATE {sweaver_style} set themesettings = :themesettings WHERE style_id = :style_id", array(':themesettings' => $theme_values_serialized, ':style_id' => $style_id));
199
        }
200

    
201
        // Fire other submit functions on the theme settings form.
202
        $submit_handlers = isset($form['sweaver_plugin_themesettings']['form']['#submit']) ? $form['sweaver_plugin_themesettings']['form']['#submit'] : array();
203
        foreach ($submit_handlers as $key => $function) {
204
          if (function_exists($function)) {
205
            $function($form['sweaver_plugin_themesettings']['form'], $form_state);
206
          }
207
        }
208
      }
209
      else {
210
        // Save to CTools object cache.
211
        $sweaver = Sweaver::get_instance();
212
        ctools_include('object-cache');
213
        $style = $sweaver->get_current_style();
214
        if (!isset($style->style_id)) {
215
          $style = new stdClass;
216
          $style->style_id = 0;
217
          $style->style = t('Temporary');
218
          $style->css = '';
219
          $style->customcss = '';
220
          $style->palette = '';
221
        }
222
        $style->themesettings = serialize($theme_values);
223
        ctools_object_cache_set('sweaver-styling', 'sweaver-styling', $style);
224
        sweaver_session(t('The theme settings have been applied.'));
225
        sweaver_session(TRUE, 'sweaver_temp');
226
      }
227
    }
228

    
229
    // Delete style.
230
    if (isset($form_state['style_to_delete'])) {
231
      $style = $form_state['style_to_delete'];
232

    
233
      // Reset theme settings when active.
234
      if ($form_state['style_active']) {
235
        variable_del($theme_key);
236
      }
237
    }
238
  }
239

    
240
  /**
241
   * Frontend css and js.
242
   */
243
  public function sweaver_form_css_js(&$inline_settings) {
244
    drupal_add_js('misc/collapse.js');
245
  }
246

    
247
  /**
248
   * Get theme specific keys.
249
   */
250
  public function sweaver_get_theme_specific_keys($theme_specific_form, &$theme_specific_keys, $fapi_types) {
251
    $children = element_children($theme_specific_form);
252
    foreach ($children as $element) {
253
      if (in_array($theme_specific_form[$element]['#type'], $fapi_types)) {
254
        $theme_specific_keys[] = $element;
255
      }
256
      else {
257
        $this->sweaver_get_theme_specific_keys($theme_specific_form[$element], $theme_specific_keys, $fapi_types);
258
      }
259
    }
260
  }
261

    
262
  /**
263
   * Get theme settings form.
264
   * We basically copy the form from system.admin.inc. This makes it easer to
265
   * copy the uploads of the favicon & logo to revision versions of the style
266
   * and getting the right settings for the theme.
267
   *
268
   * People altering form and having also uploads wishing to work with sweaver must
269
   * handle the revisioning themeselves.
270
   *
271
   * @param $key
272
   *   The name of the theme.
273
   * @param $revision_theme_settings
274
   *   Revisions theme settings.
275
   */
276
  public function sweaver_get_theme_settings_form($key, $revision_theme_settings) {
277
    $has_theme = FALSE;
278
    $form_state = array();
279

    
280
    $features = array();
281
    // Default settings are defined in theme_get_setting() in includes/theme.inc
282
    if ($key) {
283
      $var = 'theme_' . $key . '_settings';
284
      $all_themes = sweaver_get_all_themes();
285
      $theme_info = $all_themes[$key];
286
      $features = $theme_info->info['features'];
287
    }
288
    else {
289
      $var = 'theme_settings';
290
    }
291

    
292
    $form['var'] = array('#type' => 'hidden', '#value' => $var);
293

    
294
    // Check for a new uploaded logo. Put it in the form
295
    // so we can give it the right filename in the submit function.
296
    if ($file = file_save_upload('logo_upload', array('file_validate_is_image' => array()))) {
297
      $parts = pathinfo($file->uri);
298
      $filename = 'public://sweaver/temp_logo.' . $parts['extension'];
299
      $form['#logo_path'] = $filename;
300
      $form['#logo_path_extension'] = $parts['extension'];
301

    
302
      // The image was saved using file_save_upload() and was added to the
303
      // files table as a temporary file. We'll make a copy and let the garbage
304
      // collector delete the original upload.
305
      if (file_unmanaged_copy($file->uri, $filename, FILE_EXISTS_REPLACE)) {
306
        $_POST['default_logo'] = 0;
307
        $_POST['logo_path'] = $file->uri;
308
        $_POST['toggle_logo'] = 1;
309
      }
310
    }
311

    
312
    // Check for a new uploaded favicon. Put it in the form
313
    // so we can give it the right filename in the submit function.
314
    if ($file = file_save_upload('favicon_upload')) {
315
      $parts = pathinfo($file->uri);
316
      $filename = 'public://sweaver/temp_favicon.' . $parts['extension'];
317
      $form['#favicon_path'] = $filename;
318
      $form['#favicon_path_extension'] = $parts['extension'];
319

    
320
      // The image was saved using file_save_upload() and was added to the
321
      // files table as a temporary file. We'll make a copy and let the garbage
322
      // collector delete the original upload.
323
      if (file_unmanaged_copy($file->uri, $filename)) {
324
        $_POST['default_favicon'] = 0;
325
        $_POST['favicon_path'] = $file->uri;
326
        $_POST['toggle_favicon'] = 1;
327
      }
328
    }
329

    
330
    // Toggle settings
331
    $toggles = array(
332
      'logo'                 => t('Logo'),
333
      'name'                 => t('Site name'),
334
      'slogan'               => t('Site slogan'),
335
      'mission'              => t('Mission statement'),
336
      'node_user_picture'    => t('User pictures in posts'),
337
      'comment_user_picture' => t('User pictures in comments'),
338
      'search'               => t('Search box'),
339
      'favicon'              => t('Shortcut icon'),
340
      'primary_links'        => t('Primary links'),
341
      'secondary_links'      => t('Secondary links'),
342
    );
343

    
344
    // Some features are not always available
345
    $disabled = array();
346
    if (!variable_get('user_pictures', 0)) {
347
      $disabled['toggle_node_user_picture'] = TRUE;
348
      $disabled['toggle_comment_user_picture'] = TRUE;
349
    }
350
    if (!module_exists('search')) {
351
      $disabled['toggle_search'] = TRUE;
352
    }
353

    
354
    $form['theme_settings'] = array(
355
      '#type' => 'fieldset',
356
      '#title' => t('Toggle display'),
357
      '#description' => t('Enable or disable the display of certain page elements.'),
358
    );
359
    foreach ($toggles as $name => $title) {
360
      if ((!$key) || in_array($name, $features)) {
361
        $form['theme_settings']['toggle_' . $name] = array('#type' => 'checkbox', '#title' => check_plain($title), '#default_value' => $this->sweaver_has_theme_setting('toggle_' . $name, $key, $revision_theme_settings));        // Disable checkboxes for features not supported in the current configuration.
362
        if (isset($disabled['toggle_'. $name])) {
363
          $form['theme_settings']['toggle_'. $name]['#disabled'] = TRUE;
364
        }
365
      }
366
    }
367

    
368
    // System wide only settings.
369
    if (!element_children($form['theme_settings'])) {
370
      // If there is no element in the theme settings fieldset then do not show
371
      // it -- but keep it in the form if another module wants to alter.
372
      $form['theme_settings']['#access'] = FALSE;
373
    }
374

    
375
    // Logo settings
376
    if ((!$key) || in_array('logo', $features)) {
377
      $form['logo'] = array(
378
        '#type' => 'fieldset',
379
        '#title' => t('Logo image settings'),
380
        '#description' => t('If toggled on, the following logo will be displayed.'),
381
        '#attributes' => array('class' => array('theme-settings-bottom')),
382
      );
383
      $form['logo']['default_logo'] = array(
384
        '#type' => 'checkbox',
385
        '#title' => t('Use the default logo'),
386
        '#default_value' => $this->sweaver_has_theme_setting('default_logo', $key, $revision_theme_settings),
387
        '#tree' => FALSE,
388
        '#description' => t('Check here if you want the theme to use the logo supplied with it.')
389
      );
390
      $form['logo']['logo_path'] = array(
391
        '#type' => 'textfield',
392
        '#title' => t('Path to custom logo'),
393
        '#default_value' => $this->sweaver_has_theme_setting('logo_path', $key, $revision_theme_settings),
394
        '#description' => t('The path to the file you would like to use as your logo file instead of the default logo.'));
395

    
396
      $form['logo']['logo_upload'] = array(
397
        '#type' => 'file',
398
        '#title' => t('Upload logo image'),
399
        '#maxlength' => 40,
400
        '#description' => t("If you don't have direct file access to the server, use this field to upload your logo.")
401
      );
402
    }
403

    
404
    if ((!$key) || in_array('favicon', $features)) {
405
      $form['favicon'] = array(
406
        '#type' => 'fieldset',
407
        '#title' => t('Shortcut icon settings'),
408
        '#description' => t("Your shortcut icon, or 'favicon', is displayed in the address bar and bookmarks of most browsers.")
409
      );
410
      $form['favicon']['default_favicon'] = array(
411
        '#type' => 'checkbox',
412
        '#title' => t('Use the default shortcut icon.'),
413
        '#default_value' => $this->sweaver_has_theme_setting('default_favicon', $key, $revision_theme_settings),
414
        '#description' => t('Check here if you want the theme to use the default shortcut icon.')
415
      );
416
      $form['favicon']['favicon_path'] = array(
417
        '#type' => 'textfield',
418
        '#title' => t('Path to custom icon'),
419
        '#default_value' => $this->sweaver_has_theme_setting('favicon_path', $key, $revision_theme_settings),
420
        '#description' => t('The path to the image file you would like to use as your custom shortcut icon.')
421
      );
422

    
423
      $form['favicon']['favicon_upload'] = array(
424
        '#type' => 'file',
425
        '#title' => t('Upload icon image'),
426
        '#description' => t("If you don't have direct file access to the server, use this field to upload your shortcut icon.")
427
      );
428
    }
429

    
430
    if ($key) {
431
      // Call engine-specific settings.
432
      $function = $theme_info->prefix . '_engine_settings';
433
      if (function_exists($function)) {
434
        $form['engine_specific'] = array(
435
          '#type' => 'fieldset',
436
          '#title' => t('Theme-engine-specific settings'),
437
          '#description' => t('These settings only exist for the themes based on the %engine theme engine.', array('%engine' => $themes[$key]->prefix)),
438
        );
439
        $function($form, $form_state);
440
      }
441

    
442
      // Create a list which includes the current theme and all its base themes.
443
      if (isset($themes[$key]->base_themes)) {
444
        $theme_keys = array_keys($themes[$key]->base_themes);
445
        $theme_keys[] = $key;
446
      }
447
      else {
448
        $theme_keys = array($key);
449
      }
450

    
451
      // Save the name of the current theme (if any), so that we can temporarily
452
      // override the current theme and allow theme_get_setting() to work
453
      // without having to pass the theme name to it.
454
      $default_theme = !empty($GLOBALS['theme_key']) ? $GLOBALS['theme_key'] : NULL;
455
      $GLOBALS['theme_key'] = $key;
456

    
457
      // Process the theme and all its base themes.
458
      foreach ($theme_keys as $theme) {
459
        // Include the theme-settings.php file.
460
        $filename = DRUPAL_ROOT . '/' . str_replace("/$theme.info", '', $theme_info->filename) . '/theme-settings.php';
461
        if (file_exists($filename)) {
462
          require_once $filename;
463
        }
464

    
465
        // Call theme-specific settings.
466
        $function = $theme . '_form_system_theme_settings_alter';
467
        if (function_exists($function)) {
468
          $function($form, $form_state);
469
        }
470
      }
471

    
472
      // Restore the original current theme.
473
      if (!is_null($default_theme)) {
474
        $GLOBALS['theme_key'] = $default_theme;
475
      }
476
      else {
477
        unset($GLOBALS['theme_key']);
478
      }
479
    }
480

    
481
    return $form;
482
  }
483

    
484
  /**
485
   * Helper function.
486
   *
487
   * @param $revision_theme_settings
488
   * @param $theme_setting
489
   */
490
  function sweaver_has_theme_setting($theme_setting, $theme, $revision_theme_settings) {
491
    if (isset($revision_theme_settings[$theme_setting])) {
492
      return $revision_theme_settings[$theme_setting];
493
    }
494
    else {
495
      return theme_get_setting($theme_setting, $theme);
496
    }
497
  }
498
}