1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Class Sweaver.
|
6
|
*/
|
7
|
class Sweaver {
|
8
|
|
9
|
private static $instance;
|
10
|
private $style = NULL;
|
11
|
private $theme = NULL;
|
12
|
private $plugins = array();
|
13
|
private $configured = FALSE;
|
14
|
private $plugins_registry = array();
|
15
|
private $plugins_registry_enabled = array();
|
16
|
|
17
|
/**
|
18
|
* Constructor.
|
19
|
* Private constructor to make sure this is never
|
20
|
* instantiated by the constructor.
|
21
|
*/
|
22
|
private function __construct() {
|
23
|
if ($theme_key = sweaver_session(NULL, 'sweaver_theme')) {
|
24
|
$this->set_current_style($theme_key);
|
25
|
}
|
26
|
else {
|
27
|
$this->set_current_style(variable_get('theme_default', 'garland'));
|
28
|
}
|
29
|
|
30
|
if (!$this->is_configured()) {
|
31
|
$this->register_plugins_configuration();
|
32
|
}
|
33
|
}
|
34
|
|
35
|
/**
|
36
|
* GetInstance
|
37
|
* Static method to always return the same object Sweaver.
|
38
|
* There can only be one Sweaver.
|
39
|
*/
|
40
|
final static public function get_instance() {
|
41
|
if (!isset(self::$instance)) {
|
42
|
self::$instance = new Sweaver();
|
43
|
}
|
44
|
return self::$instance;
|
45
|
}
|
46
|
|
47
|
/**
|
48
|
* register_plugins_configuration.
|
49
|
* Registers an array of plugin configuration data to Sweaver.
|
50
|
*/
|
51
|
public function register_plugins_configuration() {
|
52
|
|
53
|
// Load the required plugins of ctools and the sweaver base class.
|
54
|
ctools_include('plugins');
|
55
|
|
56
|
// Check if the configuration was cached or not.
|
57
|
if ($plugins_cache = cache_get('sweaver_plugins')) {
|
58
|
$this->plugins_registry = $plugins_cache->data['sweaver_plugins'];
|
59
|
$this->plugins_registry_enabled = $plugins_cache->data['sweaver_plugins_enabled'];
|
60
|
}
|
61
|
else {
|
62
|
$this->plugins_registry = ctools_get_plugins('sweaver', 'plugins');
|
63
|
|
64
|
// Build enabled plugins.
|
65
|
foreach ($this->plugins_registry as $key => $plugin) {
|
66
|
$this->plugins_registry[$key]['enabled'] = 0;
|
67
|
if (variable_get('sweaver_plugin_status_'. $key, FALSE)) {
|
68
|
$this->plugins_registry[$key]['enabled'] = 1;
|
69
|
$this->plugins_registry_enabled[$key] = $this->plugins_registry[$key];
|
70
|
}
|
71
|
}
|
72
|
|
73
|
// Cache the plugins ourselves too.
|
74
|
$sweaver_plugins['sweaver_plugins'] = $this->plugins_registry;
|
75
|
$sweaver_plugins['sweaver_plugins_enabled'] = $this->plugins_registry_enabled;
|
76
|
cache_set('sweaver_plugins', $sweaver_plugins);
|
77
|
}
|
78
|
|
79
|
$this->configured = TRUE;
|
80
|
|
81
|
}
|
82
|
|
83
|
/**
|
84
|
* Get the sweaver plugins configurations.
|
85
|
*/
|
86
|
public function get_plugins_registry($enabled = TRUE) {
|
87
|
return $enabled ? $this->plugins_registry_enabled : $this->plugins_registry;
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* is_configured.
|
92
|
* Tells us if Sweaver is configured.
|
93
|
*/
|
94
|
public function is_configured() {
|
95
|
return $this->configured;
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* add_plugin.
|
100
|
* Adds a sweaver plugin to the stash.
|
101
|
*/
|
102
|
private function add_plugin(Sweaver_plugin $plugin) {
|
103
|
$this->plugins[] = $plugin;
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* is_plugin_activated
|
108
|
* Check if a plugin is activated
|
109
|
* @return true if activated
|
110
|
*/
|
111
|
public function is_plugin_activated($name) {
|
112
|
if ($this->plugins[$name]) {
|
113
|
return isset($this->plugins_registry_enabled[$name]);
|
114
|
}
|
115
|
return false;
|
116
|
}
|
117
|
|
118
|
/**
|
119
|
* get_plugin.
|
120
|
* Gets a sweaver plugin, and instantiates it if not loaded yet.
|
121
|
*/
|
122
|
public function get_plugin($name, $enabled = TRUE) {
|
123
|
|
124
|
if (!isset($this->plugins_registry[$name])) {
|
125
|
drupal_set_message(t('No configuration found for @name in the plugin registry', array('@name' => $name)));
|
126
|
}
|
127
|
|
128
|
if (!isset($this->plugins[$name])) {
|
129
|
|
130
|
module_load_include('inc', 'sweaver', 'sweaver_plugin');
|
131
|
|
132
|
$check = ($enabled) ? isset($this->plugins_registry_enabled[$name]) : TRUE;
|
133
|
if ($check && $class = ctools_plugin_get_class($this->plugins_registry[$name], 'handler')) {
|
134
|
// Check that class exists until CTools & registry issues are resolved.
|
135
|
if (class_exists($class)) {
|
136
|
$this->plugins[$name] = new $class($this->plugins_registry[$name]);
|
137
|
}
|
138
|
}
|
139
|
else {
|
140
|
$this->plugins[$name] = FALSE;
|
141
|
}
|
142
|
|
143
|
}
|
144
|
|
145
|
return $this->plugins[$name];
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* Sets the current style.
|
150
|
* @param $theme_key
|
151
|
* Optional, if missing the global one will be used.
|
152
|
*/
|
153
|
public function set_current_style($theme_key) {
|
154
|
$this->theme = $theme_key;
|
155
|
}
|
156
|
|
157
|
/**
|
158
|
* Return a style for a theme.
|
159
|
*
|
160
|
* @param $theme
|
161
|
* The machine name of the theme.
|
162
|
* @param $reset
|
163
|
* Whether to reset the current $css variable or not.
|
164
|
* @return $css
|
165
|
* The css definition for this theme.
|
166
|
*/
|
167
|
public function get_current_style($reset = FALSE) {
|
168
|
$run = &drupal_static('run', FALSE);
|
169
|
$css = &drupal_static('css', FALSE);
|
170
|
|
171
|
if (!$run || $reset) {
|
172
|
|
173
|
$run = TRUE;
|
174
|
if (sweaver_session(NULL, 'sweaver_temp')) {
|
175
|
ctools_include('object-cache');
|
176
|
$css = ctools_object_cache_get('sweaver-styling', 'sweaver-styling');
|
177
|
if (!isset($css->style_id)) {
|
178
|
$css = new stdClass();
|
179
|
}
|
180
|
$css->type = 'draft';
|
181
|
}
|
182
|
elseif (sweaver_session(NULL, 'draft_mode')) {
|
183
|
$table = (sweaver_session(NULL, 'loaded_table') == 'live') ? 'sweaver_style' : 'sweaver_style_draft';
|
184
|
$css = db_query("SELECT * FROM {". $table ."} where style_id = :style_id", array(':style_id' => sweaver_session(NULL, 'loaded_style')))->fetchObject();
|
185
|
if (!isset($css->style_id)) {
|
186
|
$css = new stdClass();
|
187
|
}
|
188
|
$css->type = 'draft';
|
189
|
}
|
190
|
else {
|
191
|
$css = db_query("SELECT style_id, theme, style, css, customcss, palette, themesettings, active FROM {sweaver_style} where theme = :theme and active = 1", array(':theme' => $this->theme))->fetchObject();
|
192
|
if (!isset($css->style_id)) {
|
193
|
$css = new stdClass();
|
194
|
}
|
195
|
$css->type = 'live';
|
196
|
}
|
197
|
if (!isset($css->style_id)) {
|
198
|
$css = NULL;
|
199
|
}
|
200
|
}
|
201
|
$this->style = $css;
|
202
|
return $css;
|
203
|
}
|
204
|
|
205
|
/**
|
206
|
* Returns the currently known theme_key.
|
207
|
*/
|
208
|
public function get_theme_key() {
|
209
|
return $this->theme;
|
210
|
}
|
211
|
|
212
|
}
|