-
Notifications
You must be signed in to change notification settings - Fork 49
/
adminthemeshandler.php
314 lines (259 loc) · 10.1 KB
/
adminthemeshandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
/**
* @package Habari
*
*/
/**
* Habari AdminThemesHandler Class
* Handles theme-related actions in the admin
*
*/
class AdminThemesHandler extends AdminHandler
{
/**
* Handles GET requests for the theme listing
*/
public function get_themes()
{
$all_themes = Themes::get_all_data();
$available_updates = Options::get( 'updates_available', array() );
foreach ( $all_themes as $name => $theme ) {
// only themes with a guid can be checked for updates
if ( isset( $theme['info']->guid ) ) {
if ( isset( $available_updates[ (string)$theme['info']->guid ] ) ) {
// @todo this doesn't use the URL and is therefore worthless
$all_themes[ $name ]['info']->update = $available_updates[ (string)$theme['info']->guid ]['latest_version'];
}
}
}
$this->theme->all_themes = $all_themes;
$this->theme->active_theme = Themes::get_active_data( true );
$this->theme->active_theme_dir = $this->theme->active_theme['path'];
// If the active theme is configurable, allow it to configure
$this->theme->active_theme_name = $this->theme->active_theme['info']->name;
$this->theme->configurable = Plugins::filter( 'theme_config', false, $this->active_theme );
$this->theme->assign( 'configure', Controller::get_var( 'configure' ) );
$activedata = Themes::get_active_data( true );
$areas = array();
if ( isset( $activedata['info']->areas->area ) ) {
foreach ( $activedata['info']->areas->area as $area ) {
$areas[] = (string)$area;
}
}
$this->theme->areas = $areas;
$this->theme->previewed = Themes::get_theme_dir( false );
$this->theme->blocks = Plugins::filter( 'block_list', array() );
$this->theme->block_instances = DB::get_results( 'SELECT b.* FROM {blocks} b ORDER BY b.title ASC', array(), 'Block' );
$blocks_areas_t = DB::get_results( 'SELECT b.*, ba.scope_id, ba.area, ba.display_order FROM {blocks} b INNER JOIN {blocks_areas} ba ON ba.block_id = b.id ORDER BY ba.scope_id ASC, ba.area ASC, ba.display_order ASC', array() );
$blocks_areas = array();
foreach ( $blocks_areas_t as $block ) {
if ( !isset( $blocks_areas[$block->scope_id] ) ) {
$blocks_areas[$block->scope_id] = array();
}
$blocks_areas[$block->scope_id][$block->area][$block->display_order] = $block;
}
$this->theme->blocks_areas = $blocks_areas;
$scopes = DB::get_results( 'SELECT * FROM {scopes} ORDER BY name ASC;' );
$scopes = Plugins::filter( 'get_scopes', $scopes );
$this->theme->scopes = $scopes;
$this->theme->scopeid = 0;
$this->theme->theme_loader = Plugins::filter( 'theme_loader', '', $this->theme );
$this->theme->display( 'themes' );
}
/**
* A POST handler for the admin themes page that simply passes those options through.
*/
public function post_themes()
{
return $this->get_themes();
}
/**
* Activates a theme.
*/
public function get_activate_theme()
{
$theme_name = $this->handler_vars['theme_name'];
$theme_dir = $this->handler_vars['theme_dir'];
if ( isset( $theme_name ) && isset( $theme_dir ) ) {
Themes::activate_theme( $theme_name, $theme_dir );
}
Session::notice( sprintf( _t( "Activated theme '%s'" ), $theme_name ) );
Utils::redirect( URL::get( 'admin', 'page=themes' ) );
}
/**
* Configures a theme to be active for the current user's session.
*/
public function get_preview_theme()
{
$theme_name = $this->handler_vars['theme_name'];
$theme_dir = $this->handler_vars['theme_dir'];
if ( isset( $theme_name ) && isset( $theme_dir ) ) {
if ( Themes::get_theme_dir() == $theme_dir ) {
Themes::cancel_preview();
Session::notice( sprintf( _t( "Ended the preview of the theme '%s'" ), $theme_name ) );
}
else {
Themes::preview_theme( $theme_name, $theme_dir );
Session::notice( sprintf( _t( "Previewing theme '%s'" ), $theme_name ) );
}
}
Utils::redirect( URL::get( 'admin', 'page=themes' ) );
}
/**
* Get the block configuration form to show in a modal iframe on the themes page
*
*/
public function get_configure_block()
{
Utils::check_request_method( array( 'GET', 'POST' ) );
$block = DB::get_row( 'SELECT b.* FROM {blocks} b WHERE id = :id ORDER BY b.title ASC', array( 'id' => $_GET['blockid'] ), 'Block' );
$block_form = $block->get_form();
$first_control = reset ( $block_form->controls );
if ( $first_control ) {
$block_form->insert( $first_control->name, 'fieldset', 'block_admin', _t( 'Block Display Settings' ) );
}
else {
$block_form->append( 'fieldset', 'block_admin', _t( 'Block Display Settings' ) );
}
$block_form->block_admin->append( 'text', '_title', array( 'configure_block_title', $block ), _t( 'Block Title:' ) );
$block_form->_title->value = $block->title;
$block_form->_title->add_validator( 'validate_required' );
$block_form->block_admin->append( 'checkbox', '_show_title', $block, _t( 'Display Block Title:' ) );
$block_form->append( 'submit', 'save', _t( 'Save' ) );
Plugins::register( array( $this, 'action_configure_block_title' ), 'action', 'configure_block_title' );
$this->theme->content = $block_form->get();
$this->display( 'block_configure' );
}
function action_configure_block_title( $value, $name, $storage )
{
$storage[0]->title = $value;
return false;
}
/**
* A POST handler for the block configuration form
*
* @see AdminHandler::get_configure_block
* @return
*/
public function post_configure_block()
{
$this->get_configure_block();
}
/**
* Called from the themes page to create a new block instace
*
* @param mixed $handler_vars
*/
public function ajax_add_block( $handler_vars )
{
Utils::check_request_method( array( 'POST' ) );
$this->setup_admin_theme( '' );
$title = $_POST['title'];
$type = $_POST['type'];
if ( !isset( $_POST['title'] ) ) {
$this->theme->blocks = Plugins::filter( 'block_list', array() );
$this->theme->block_instances = DB::get_results( 'SELECT b.* FROM {blocks} b ORDER BY b.title ASC', array(), 'Block' );
$this->theme->active_theme = Themes::get_active_data( true );
$this->display( 'block_instances' );
}
elseif ( $title == '' ) {
$this->theme->blocks = Plugins::filter( 'block_list', array() );
$this->theme->block_instances = DB::get_results( 'SELECT b.* FROM {blocks} b ORDER BY b.title ASC', array(), 'Block' );
$this->theme->active_theme = Themes::get_active_data( true );
$this->display( 'block_instances' );
$msg = json_encode( _t( 'A new block must first have a name.' ) );
echo '<script type="text/javascript">
alert(' . $msg . ');
</script>';
}
else {
$block = new Block( array( 'title' => $title, 'type' => $type ) );
$block->insert();
$this->theme->blocks = Plugins::filter( 'block_list', array() );
$this->theme->block_instances = DB::get_results( 'SELECT b.* FROM {blocks} b ORDER BY b.title ASC', array(), 'Block' );
$this->theme->active_theme = Themes::get_active_data( true );
$this->display( 'block_instances' );
$msg = json_encode( _t( 'Added new block "%1s" of type "%2s".', array( $title, $type ) ) );
echo '<script type="text/javascript">
human_msg.display_msg(' . $msg . ');
spinner.stop();
</script>';
}
}
/**
* Called from the themes page to delete a block instance
*
* @param mixed $handler_vars
*/
public function ajax_delete_block( $handler_vars )
{
Utils::check_request_method( array( 'POST' ) );
$this->setup_admin_theme( '' );
$block_id = $_POST['block_id'];
$block = DB::get_row( 'SELECT b.* FROM {blocks} b WHERE id = :block_id', array( 'block_id' => $block_id ), 'Block' );
if ( $block->delete() ) {
$msg = json_encode( _t( 'Deleted block "%1s" of type "%2s".', array( $block->title, $block->type ) ) );
}
else {
$msg = json_encode( _t( 'Failed to delete block "%1s" of type "%2s".', array( $block->title, $block->type ) ) );
}
$this->theme->blocks = Plugins::filter( 'block_list', array() );
$this->theme->block_instances = DB::get_results( 'SELECT b.* FROM {blocks} b ORDER BY b.title ASC', array(), 'Block' );
$this->theme->active_theme = Themes::get_active_data( true );
$this->display( 'block_instances' );
echo '<script type="text/javascript">
human_msg.display_msg(' . $msg . ');
spinner.stop();
</script>';
}
/**
* Called from the themes page to save the blocks instances into areas
*
* @param mixed $handler_vars
* @return
*/
public function ajax_save_areas( $handler_vars )
{
Utils::check_request_method( array( 'POST' ) );
$scope = $_POST['scope'];
$msg = '';
if ( isset( $_POST['area_blocks'] ) ) {
$area_blocks = $_POST['area_blocks'];
DB::query( 'DELETE FROM {blocks_areas} WHERE scope_id = :scope_id', array( 'scope_id' => $scope ) );
foreach ( (array)$area_blocks as $area => $blocks ) {
$display_order = 0;
// if there are no blocks for a given area, skip it
if ( empty( $blocks ) ) {
continue;
}
foreach ( $blocks as $block ) {
$display_order++;
DB::query( 'INSERT INTO {blocks_areas} (block_id, area, scope_id, display_order) VALUES (:block_id, :area, :scope_id, :display_order)', array( 'block_id'=>$block, 'area'=>$area, 'scope_id'=>$scope, 'display_order'=>$display_order ) );
}
}
$msg = json_encode( _t( 'Saved block areas settings.' ) );
$msg = '<script type="text/javascript">
human_msg.display_msg(' . $msg . ');
spinner.stop();
</script>';
}
$this->setup_admin_theme( '' );
$blocks_areas_t = DB::get_results( 'SELECT b.*, ba.scope_id, ba.area, ba.display_order FROM {blocks} b INNER JOIN {blocks_areas} ba ON ba.block_id = b.id ORDER BY ba.scope_id ASC, ba.area ASC, ba.display_order ASC', array() );
$blocks_areas = array();
foreach ( $blocks_areas_t as $block ) {
if ( !isset( $blocks_areas[$block->scope_id] ) ) {
$blocks_areas[$block->scope_id] = array();
}
$blocks_areas[$block->scope_id][$block->area][$block->display_order] = $block;
}
$this->theme->blocks_areas = $blocks_areas;
$this->theme->scopeid = $scope;
$scopes = DB::get_results( 'SELECT * FROM {scopes} ORDER BY name ASC;' );
$scopes = Plugins::filter( 'get_scopes', $scopes );
$this->theme->scopes = $scopes;
$this->theme->active_theme = Themes::get_active_data( true );
$this->display( 'block_areas' );
echo $msg;
}
}
?>