Projet

Général

Profil

Paste
Télécharger (3,79 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Themeswitch plugin.
6
 */
7
class sweaver_plugin_themeswitch extends sweaver_plugin {
8

    
9
  /**
10
   * Menu registry.
11
   */
12
  public function sweaver_menu(&$weight, $page_arguments, $base) {
13

    
14
    $items = array();
15

    
16
    // Theme classes groups administration.
17
    $base['access arguments'] = array('use editor');
18
    $items['sweaver_themeswitch'] = $base + array(
19
      'title' => 'Theme switch',
20
      'page arguments' => array($page_arguments),
21
      'type' => MENU_CALLBACK,
22
      'weight' => $weight++,
23
    );
24

    
25
    return $items;
26
  }
27

    
28
  /**
29
   * Frontend form.
30
   */
31
  public function sweaver_form() {
32
    $form = array();
33
    $content = '';
34
    $no_image = drupal_get_path('module', 'sweaver') .'/plugins/sweaver_plugin_themeswitch/no_screenshot.png';
35

    
36
    $theme_key = Sweaver::get_instance()->get_theme_key();
37
    $number_of_themes = 0;
38
    $themes = sweaver_get_all_themes();
39
    foreach ($themes as $theme) {
40

    
41
      if (isset($theme->info['screenshot']) && file_exists($theme->info['screenshot'])) {
42
        $image_file = $theme->info['screenshot'];
43
      }
44
      else {
45
        $image_file = $no_image;
46
      }
47

    
48
      if ($theme_key != $theme->name) {
49
        $switch_description = t('Switch to @theme', array('@theme' => $theme->info['name']));
50
        $image = sweaver_theme_image($image_file, $switch_description, $switch_description, array('width' => '150', 'height' => '90'), FALSE);
51
        $image = l($image, 'sweaver_themeswitch/'. $theme->name, array('alias' => TRUE, 'html' => TRUE, 'query' => drupal_get_destination()));
52
        $content .= '<div class="selected-image">'. $image;
53
      }
54
      else {
55
        $switch_description = t('This theme is currently selected');
56
        $image = sweaver_theme_image($image_file, $switch_description, $switch_description, array('width' => '150', 'height' => '90'), FALSE);
57
        $content .= '<div class="selected-image selected-image-default">'. $image;
58
      }
59
      $number_of_themes++;
60

    
61
      $content .= '<br />'. check_plain($theme->info['name']);
62

    
63
      $content .= '</div>';
64
    }
65

    
66
    $form['markup'] = array(
67
      '#markup' => '<div id="themeswitch-pane"><div id="themeswitch-content">'. $content .'</div><div class="scroll-bar-wrap"><div class="scroll-bar"></div></div></div>',
68
    );
69

    
70
    return $form;
71
  }
72

    
73
  /**
74
   * Frontend form submit handler.
75
   */
76
  public function sweaver_form_submit($form, &$form_state) {
77
    if ($form_state['clicked_button']['#value'] == t('Publish style') && isset($form_state['style_id'])) {
78
      $theme_key = $form['#current_theme'];
79
      variable_set('theme_default', $theme_key);
80
    }
81
  }
82

    
83
  /**
84
   * Frontend css and js.
85
   */
86
  public function sweaver_form_css_js(&$inline_settings) {
87
    drupal_add_js(drupal_get_path('module', 'sweaver') .'/plugins/sweaver_plugin_themeswitch/sweaver_plugin_themeswitch.js', 'module');
88
  }
89

    
90
  /**
91
   * Frontend themeswitch.
92
   */
93
  public function sweaver_menu_callback() {
94
    $theme = arg(1);
95
    $theme_default = variable_get('theme_default', 'garland');
96

    
97
    $all_themes = sweaver_get_all_themes();
98
    if (!empty($theme) && isset($all_themes[$theme]) && $all_themes[$theme]->status == 1) {
99

    
100
      if ($theme != $theme_default) {
101
        sweaver_session(NULL, 'sweaver_theme', TRUE);
102
        sweaver_session($theme, 'sweaver_theme');
103
      }
104
      else {
105
        // Reset session variable because it's the default.
106
        sweaver_session(NULL, 'sweaver_theme', TRUE);
107
      }
108

    
109
      // Let other modules act on the themeswitch.
110
      $arguments = array(
111
        'switched_theme' => $theme,
112
        'default_theme' => $theme_default,
113
      );
114
      module_invoke_all('sweaver_action', 'theme_switch', $arguments);
115

    
116
      sweaver_session(t('You have switched to @switched_theme.', array('@switched_theme' => $all_themes[$theme]->info['name'])));
117
    }
118

    
119
    // Go back to previous page.
120
    drupal_goto();
121
  }
122
}