Projet

Général

Profil

Paste
Télécharger (4,06 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Theme classes plugin.
6
 */
7
define('SWEAVER_PLUGIN_THEMECLASSES_DEFAULT', 'skinr');
8

    
9
class sweaver_plugin_themeclasses extends sweaver_plugin {
10

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

    
16
    $items = array();
17

    
18
    // Theme classes groups administration.
19
    $items['admin/config/user-interface/sweaver/themeclasses'] = $base + array(
20
      'title' => 'Theme styles',
21
      'page arguments' => array($page_arguments),
22
      'type' => MENU_LOCAL_TASK,
23
      'weight' => $weight++,
24
    );
25

    
26
    return $items;
27
  }
28

    
29
  /**
30
   * Frontend form.
31
   */
32
  public function sweaver_form() {
33
    $form = array();
34

    
35
    $class_groups = trim(variable_get('sweaver_themeclasses_groups', SWEAVER_PLUGIN_THEMECLASSES_DEFAULT));
36
    if (empty($class_groups)) {
37
      return $form;
38
    }
39

    
40
    $theme_key = Sweaver::get_instance()->get_theme_key();
41
    $theme_info = sweaver_get_theme_info($theme_key);
42
    if (empty($theme_info)) {
43
      return $form;
44
    }
45

    
46
    /*$cache = variable_get('sweaver_plugin_themeclasses_'. $theme_key, array());
47
    if (!empty($cache)) {
48
      $styles = $cache['styles'];
49
    }
50
    else {*/
51
      $styles = array();
52
      $groups = explode("\n", $class_groups);
53
      foreach ($groups as $group) {
54
        $group = trim($group);
55
        if (isset($theme_info[$group])) {
56
          $this->sweaver_get_styles($theme_info[$group], $styles);
57
        }
58
      }
59

    
60
      // Cache the stuff.
61
      //variable_set('sweaver_plugin_themeclasses_'. $theme_key, array('checked' => TRUE, 'styles' => $styles));
62
    //}
63

    
64
    if (!empty($styles)) {
65
      $content = t('<h2>Styles</h2>!styles', array('!styles' => implode(' ', $styles)));
66
    }
67
    else {
68
      $content = t('<p>No styles found.</p>');
69
    }
70

    
71
    $form['sweaver_plugin_themeclasses'] = array(
72
      '#markup' => $content,
73
    );
74

    
75
    return $form;
76
  }
77

    
78

    
79
  /**
80
   * Frontend css and js.
81
   */
82
  public function sweaver_form_css_js(&$inline_settings) {
83
    drupal_add_js(drupal_get_path('module', 'sweaver') .'/plugins/sweaver_plugin_themeclasses/sweaver_plugin_themeclasses.js');
84
  }
85

    
86
  /**
87
   * Menu callback.
88
   */
89
  public function sweaver_menu_callback() {
90
    $form = array();
91

    
92
    $form['sweaver_themeclasses_groups'] = array(
93
      '#type' => 'textarea',
94
      '#title' => t('Theme groups'),
95
      '#description' => t('Enter a group which is available in your theme(s) info file. This plugin will try to fetch all classes from it and make them available in a separate tab. Enter one group per line. This is inspired by the skinr way of theming, so we need to find a key called \'class\' to get a list of styles. If you have added new classes and the editor does not pick them up yet, click on submit button to reset the cache.'),
96
      '#default_value' => variable_get('sweaver_themeclasses_groups', SWEAVER_PLUGIN_THEMECLASSES_DEFAULT),
97
      '#wysiwyg' => FALSE,
98
    );
99

    
100
    $form['sweaver_themeclasses_submit'] = array(
101
      '#type' => 'submit',
102
      '#value' => t('Save configuration'),
103
    );
104

    
105
    return $form;
106
  }
107

    
108
  /**
109
   * Menu callback submit.
110
   */
111
  public function sweaver_menu_callback_submit($form, &$form_state) {
112
    db_query("DELETE FROM {variable} WHERE name LIKE 'sweaver_plugin_themeclasses_%%'")->execute();
113
    variable_set('sweaver_themeclasses_groups', $form_state['values']['sweaver_themeclasses_groups']);
114
    drupal_set_message(t('The configuration options have been saved.'));
115
  }
116

    
117
  /**
118
   * Get styles from the group. We look for a key called 'class'.
119
   */
120
  public function sweaver_get_styles($theme_info, &$styles) {
121
    foreach ($theme_info as $key => $value) {
122
      if (is_array($value) && isset($value['class'])) {
123
        $label = isset($value['label']) ? $value['label'] : $value['class'];
124
        $styles[] = '<div class="sweaver-switch-to-style " id="spt-'. $value['class'] .'"><a href="javascript:Drupal.Sweaver.ThemeClasses(\'spt-'. $value['class'] .'\', \'spt-'. strip_tags($value['label']) .'\')">'. strip_tags($value['label']) .'</a></div>';
125
      }
126
      if (is_array($value)) {
127
        $this->sweaver_get_styles($theme_info[$key], $styles);
128
      }
129
    }
130
  }
131
}