~quam-plures-core/quam-plures/qp5_colls-blogs_chaps-cats

« back to all changes in this revision

Viewing changes to qp_inc/widgets/widgets.ctrl.php

http://forums.quamplures.net/viewtopic.php?p=9237#p9237

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
<?php
2
2
/**
3
 
 * This file implements the UI controller for managing widgets inside of a blog.
4
 
 *
5
 
 * Quam Plures - {@link http://quamplures.net/}
6
 
 * Released under GNU GPL License - {@link http://quamplures.net/license.html}
7
 
 * @copyright (c) 2009 - 2011 by the Quam Plures developers - {@link http://quamplures.net/}
8
 
 * @copyright (c)2003-2009 by Francois PLANQUE - {@link http://fplanque.net/}
9
 
 *
10
 
 * {@internal Open Source relicensing agreement:
11
 
 * }}
12
 
 *
13
 
 * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
14
 
 * @author fplanque: Francois PLANQUE.
15
 
 *
 
3
 * $ctrl_mappings: 'widgets' || Blog settings -> {{blog}} -> Widgets
 
4
 *
 
5
 * This is a (partial?) list of authors who have contributed to this file:
 
6
 * @author {@link http://wonderwinds.com/ Ed Bennett}
 
7
 * @author {@link http://fplanque.net/ Francois PLANQUE}
 
8
 * @copyright (c) 2009 by {@link http://quamplures.net/ the Quam Plures project}
 
9
 * @copyright (c) 2003 by Francois PLANQUE - {@link http://fplanque.net/}
 
10
 * @license {@link http://www.gnu.org/licenses/gpl.txt GNU General Public License v3}
16
11
 * @package widgets
17
12
 */
18
 
if( !defined('QP_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
 
13
if(!defined('QP_MAIN_INIT')) die('fail');
19
14
 
20
 
/**
21
 
 * @var AdminUI
22
 
 */
23
15
global $AdminUI;
24
 
/**
25
 
 * @var Plugins
26
 
 */
27
16
global $Plugins;
28
17
 
29
 
load_class( 'widgets/model/_widget.class.php' );
30
 
 
31
18
param( 'action', 'string', 'list' );
32
19
param( 'display_mode', 'string', 'normal' );
33
20
$display_mode = ( in_array( $display_mode, array( 'js', 'normal' ) ) ? $display_mode : 'normal' );
34
21
if( $display_mode == 'js' )
35
 
{       // Javascript in debug mode conflicts/fails.
 
22
{
 
23
        // Javascript in debug mode conflicts/fails.
36
24
        // fp> TODO: either fix the debug javascript or have an easy way to disable JS in the debug output.
37
25
        $debug = 0;
38
26
}
39
 
// This should probably be handled with teh existing $mode var
 
27
// This should probably be handled with the existing $mode var
40
28
 
41
 
/*
42
 
 * Init the objects we want to work on.
43
 
 */
 
29
// Init the objects we want to work on
44
30
switch( $action )
45
31
{
46
 
        case 'nil':
47
 
        case 'list':
48
 
                // Do nothing
49
 
                break;
 
32
        case 'nil':
 
33
        case 'list':
 
34
        // Do nothing
 
35
        break;
50
36
 
51
37
        case 'create':
52
 
                param( 'type', 'string', true );
53
 
                param( 'code', 'string', true );
 
38
        param( 'type', 'string', true );
 
39
        param( 'code', 'string', true );
54
40
        case 'new':
55
 
                param( 'container', 'string', true, true );     // memorize
56
 
                break;
 
41
        param( 'container', 'string', true, true ); // memorize
 
42
        break;
57
43
 
58
 
        case 're-order' : // js request
59
 
                param( 'container_list', 'string', true );
60
 
                $containers_list = explode( ',', $container_list );
61
 
                $containers = array();
62
 
                foreach( $containers_list as $a_container )
63
 
                {       // add each container and grab its widgets:
64
 
                        if( $container_name = trim( str_replace( array( 'container_', '_' ), array( '', ' ' ), $a_container ), ',' ) )
65
 
                        {
66
 
                                $containers[ $container_name ] = explode( ',', param( trim( $a_container, ',' ), 'string', true ) );
67
 
                        }
 
44
        case 're-order': // js request
 
45
        param( 'container_list', 'string', true );
 
46
        $containers_list = explode( ',', $container_list );
 
47
        $containers = array();
 
48
        foreach( $containers_list as $a_container )
 
49
        {
 
50
                // add each container and grab its widgets
 
51
                if( $container_name = trim( str_replace( array( 'container_', '_' ), array( '', ' ' ), $a_container ), ',' ) )
 
52
                {
 
53
                        $containers[$container_name] = explode( ',', param( trim( $a_container, ',' ), 'string', true ) );
68
54
                }
69
 
                break;
 
55
        }
 
56
        break;
70
57
 
71
58
        case 'edit':
72
59
        case 'update':
74
61
        case 'move_up':
75
62
        case 'move_down':
76
63
        case 'toggle':
77
 
                param( 'wi_ID', 'integer', true );
78
 
                $WidgetCache = & get_Cache( 'WidgetCache' );
79
 
                $edited_ComponentWidget = & $WidgetCache->get_by_ID( $wi_ID );
80
 
                // Take blog from here!
81
 
                set_working_blog( $edited_ComponentWidget->coll_ID );
82
 
                $BlogCache = & get_Cache( 'BlogCache' );
83
 
                $Blog = & $BlogCache->get_by_ID( $blog );
84
 
 
85
 
                break;
 
64
        param( 'wi_ID', 'integer', true );
 
65
        $WidgetCache = & get_Cache( 'WidgetCache' );
 
66
        $edited_ComponentWidget = & $WidgetCache->get_by_ID( $wi_ID );
 
67
        // Take blog from here!
 
68
        set_working_blog( $edited_ComponentWidget->coll_ID );
 
69
        $BlogCache = & get_Cache( 'BlogCache' );
 
70
        $Blog = & $BlogCache->get_by_ID( $blog );
 
71
        break;
86
72
 
87
73
        default:
88
 
                debug_die( 'Init objects: unhandled action' );
 
74
        debug_die( 'Init objects: unhandled action' );
89
75
}
90
76
 
91
77
if( ! valid_blog_requested() )
95
81
 
96
82
switch( $display_mode )
97
83
{
98
 
        case 'js' : // js response needed
99
 
// fp> when does this happen -- should be documented
100
 
                if( !$current_User->check_perm( 'blog_properties', 'edit', false, $blog ) )
101
 
                {       // user doesn't have permissions
102
 
                        $Messages->add( T_('You do not have permission to perform this action' ) );
103
 
// fp>does this only happen when we try to edit settings. The hardcoded 'closeWidgetSettings' response looks bad.
104
 
                        send_javascript_message( array( 'closeWidgetSettings' => array() ) );
105
 
                }
106
 
                break;
 
84
        // js response needed
 
85
        case 'js':
 
86
        if( !$current_User->check_perm( 'blog_properties', 'edit', false, $blog ) )
 
87
        {
 
88
                // user doesn't have permissions
 
89
                $Messages->add( T_('You do not have permission to perform this action' ) );
 
90
                // fp> does this only happen when we try to edit settings? The hardcoded 'closeWidgetSettings' response looks bad.
 
91
                send_javascript_message( array( 'closeWidgetSettings' => array() ) );
 
92
        }
 
93
        break;
107
94
 
 
95
        // take usual approach
108
96
        case 'normal':
109
 
        default : // take usual approach
110
 
                $current_User->check_perm( 'blog_properties', 'edit', true, $blog );
 
97
        default:
 
98
        $current_User->check_perm( 'blog_properties', 'edit', true, $blog );
111
99
}
112
100
 
113
 
// Get Template used by current Blog:
 
101
// get template used by current blog
114
102
$TemplateCache = & get_Cache( 'TemplateCache' );
115
103
$Template = & $TemplateCache->get_by_ID( $Blog->template_ID );
116
 
// Make sure containers are loaded for that template:
 
104
// make sure containers are loaded for that template
117
105
$container_list = $Template->get_containers();
118
106
 
119
 
 
120
 
/**
121
 
 * Perform action:
122
 
 */
 
107
// perform action
123
108
switch( $action )
124
109
{
125
 
        case 'nil':
126
 
        case 'new':
127
 
        case 'edit':
128
 
                // Do nothing
129
 
                break;
 
110
        case 'nil':
 
111
        case 'new':
 
112
        case 'edit':
 
113
        // do nothing
 
114
        break;
130
115
 
131
116
        case 'create':
132
 
                // Add a Widget to container:
133
 
                if( !in_array( $container, $container_list ) )
134
 
                {
135
 
                        $Messages->add( T_('WARNING: you are adding to a container that does not seem to be part of the current template.'), 'error' );
136
 
                }
137
 
 
138
 
                switch( $type )
139
 
                {
140
 
                        case 'widget':
141
 
                                // Check the requested core widget is valid:
142
 
                                $WidgetCache = & get_Cache( 'WidgetCache' );
143
 
                                if( $WidgetCache->load_widget( $code ) )
144
 
                                {
145
 
                                        $objtype = $code.'_Widget';
146
 
                                        $edited_ComponentWidget = new $objtype();
147
 
                                }
148
 
                                else
149
 
                                {
150
 
                                        debug_die( 'Requested widget not found' );
151
 
                                }
152
 
                                break;
153
 
 
154
 
                        case 'plugin':
155
 
                                if( ! $Plugin = & $Plugins->get_by_code( $code ) )
156
 
                                {
157
 
                                        debug_die( 'Requested plugin not found' );
158
 
                                }
159
 
                                if( ! $Plugins->has_event( $Plugin->ID, 'TemplateTag' ) )
160
 
                                {
161
 
                                        debug_die( 'Requested plugin does not support TemplateTag' );
162
 
                                }
163
 
                                $edited_ComponentWidget = new ComponentWidget( NULL, 'plugin', $code, array() );
164
 
                                break;
165
 
 
166
 
                        default:
167
 
                                debug_die( 'Unhandled widget type' );
168
 
                }
169
 
 
170
 
                $edited_ComponentWidget->set( 'coll_ID', $Blog->ID );
171
 
                $edited_ComponentWidget->set( 'sco_name', $container );
172
 
                $edited_ComponentWidget->set( 'enabled', 1 );
173
 
 
174
 
                // INSERT INTO DB:
175
 
                $edited_ComponentWidget->dbinsert();
176
 
 
177
 
                $Messages->add( sprintf( T_('Widget &laquo;%s&raquo; has been added to container &laquo;%s&raquo;.'),
178
 
                                        $edited_ComponentWidget->get_name(), T_($container)     ), 'success' );
179
 
 
180
 
                switch( $display_mode )
181
 
                {
182
 
                        case 'js' :
183
 
                                send_javascript_message( array(
184
 
                                        'addNewWidgetCallback' => array(
185
 
                                                $edited_ComponentWidget->ID,
186
 
                                                $container,
187
 
                                                $edited_ComponentWidget->get( 'order' ),
188
 
                                                $edited_ComponentWidget->get_name(),
189
 
                                        ),
190
 
                                        // Open widget settings:
191
 
                                        'editWidget' => array(
192
 
                                                'wi_ID_'.$edited_ComponentWidget->ID,
193
 
                                        ),
194
 
                                ) );
195
 
                                break;
196
 
 
197
 
                        case 'normal' :
198
 
                        default : // take usual action
199
 
                                header_redirect( '?ctrl=widgets&action=edit&wi_ID='.$edited_ComponentWidget->ID );
200
 
                                break;
201
 
                }
202
 
                break;
203
 
 
204
 
 
205
 
        case 'update':
206
 
                // Update Settings
207
 
                $edited_ComponentWidget->load_from_Request();
208
 
 
209
 
                if(     ! param_errors_detected() )
210
 
                {       // Update settings:
211
 
                        $edited_ComponentWidget->dbupdate();
212
 
                        $Messages->add( T_('Widget settings have been updated'), 'success' );
213
 
                        switch( $display_mode )
214
 
                        {
215
 
                                case 'js' : // js reply
216
 
                                        $edited_ComponentWidget->init_display( array() );
217
 
                                        send_javascript_message(array( 'widgetSettingsCallback' => array( $edited_ComponentWidget->ID, $edited_ComponentWidget->get_desc_for_list() ), 'closeWidgetSettings' => array() ), true );
218
 
                                        break;
219
 
                        }
220
 
                        $action = 'list';
221
 
                }
222
 
                elseif( $display_mode == 'js' )
223
 
                { // send errors back as js
224
 
                        send_javascript_message( array(), true );
225
 
                }
226
 
                break;
227
 
 
228
 
 
229
 
        case 'move_up':
230
 
                // Move the widget up:
231
 
 
232
 
                $order = $edited_ComponentWidget->order;
233
 
                $DB->begin();
234
 
 
235
 
                // Get the previous element
236
 
                $row = $DB->get_row( 'SELECT *
237
 
                                                                                                                FROM T_widget
238
 
                                                                                                         WHERE wi_coll_ID = '.$Blog->ID.'
239
 
                                                                                                                 AND wi_sco_name = '.$DB->quote($edited_ComponentWidget->sco_name).'
240
 
                                                                                                                 AND wi_order < '.$order.'
241
 
                                                                                                         ORDER BY wi_order DESC
242
 
                                                                                                         LIMIT 0,1' );
243
 
                if( !empty( $row) )
244
 
                {
245
 
                        $prev_ComponentWidget = new ComponentWidget( $row );
246
 
                        $prev_order = $prev_ComponentWidget->order;
247
 
 
248
 
                        $edited_ComponentWidget->set( 'order', 0 );     // Temporary
249
 
                        $edited_ComponentWidget->dbupdate();
250
 
 
251
 
                        $prev_ComponentWidget->set( 'order', $order );
252
 
                        $prev_ComponentWidget->dbupdate();
253
 
 
254
 
                        $edited_ComponentWidget->set( 'order', $prev_order );
255
 
                        $edited_ComponentWidget->dbupdate();
256
 
 
257
 
                }
258
 
                $DB->commit();
259
 
                break;
260
 
 
261
 
        case 'move_down':
262
 
                // Move the widget down:
263
 
 
264
 
                $order = $edited_ComponentWidget->order;
265
 
                $DB->begin();
266
 
 
267
 
                // Get the next element
268
 
                $row = $DB->get_row( 'SELECT *
269
 
                                                                                                                FROM T_widget
270
 
                                                                                                         WHERE wi_coll_ID = '.$Blog->ID.'
271
 
                                                                                                                 AND wi_sco_name = '.$DB->quote($edited_ComponentWidget->sco_name).'
272
 
                                                                                                                 AND wi_order > '.$order.'
273
 
                                                                                                         ORDER BY wi_order ASC
274
 
                                                                                                         LIMIT 0,1' );
275
 
                if( !empty( $row ) )
276
 
                {
277
 
                        $next_ComponentWidget = new ComponentWidget( $row );
278
 
                        $next_order = $next_ComponentWidget->order;
279
 
 
280
 
                        $edited_ComponentWidget->set( 'order', 0 );     // Temporary
281
 
                        $edited_ComponentWidget->dbupdate();
282
 
 
283
 
                        $next_ComponentWidget->set( 'order', $order );
284
 
                        $next_ComponentWidget->dbupdate();
285
 
 
286
 
                        $edited_ComponentWidget->set( 'order', $next_order );
287
 
                        $edited_ComponentWidget->dbupdate();
288
 
 
289
 
                }
290
 
                $DB->commit();
291
 
                break;
292
 
 
293
 
        case 'toggle':
294
 
                // Enable or disable the widget:
295
 
                $enabled = $edited_ComponentWidget->get( 'enabled' );
296
 
                $edited_ComponentWidget->set( 'enabled', (int)! $enabled );
297
 
                $edited_ComponentWidget->dbupdate();
298
 
 
299
 
                if ( $enabled )
300
 
                {
301
 
                        $msg = T_( 'Widget has been disabled.' );
 
117
        // add a widget to a container
 
118
        if( !in_array( $container, $container_list ) )
 
119
        {
 
120
                $Messages->add( T_('WARNING: you are adding to a container that does not seem to be part of the current template.'), 'error' );
 
121
        }
 
122
 
 
123
        switch( $type )
 
124
        {
 
125
                case 'widget':
 
126
                // Check the requested core widget is valid
 
127
                $WidgetCache = & get_Cache( 'WidgetCache' );
 
128
                if( $WidgetCache->load_widget( $code ) )
 
129
                {
 
130
                        $objtype = $code.'_Widget';
 
131
                        $edited_ComponentWidget = new $objtype();
302
132
                }
303
133
                else
304
134
                {
305
 
                        $msg = T_( 'Widget has been enabled.' );
306
 
                }
307
 
                $Messages->add( $msg, 'success' );
308
 
 
309
 
                if ( $display_mode == 'js' )
310
 
                {
311
 
                        // EXITS:
312
 
                        send_javascript_message( array( 'doToggle' => array( $edited_ComponentWidget->ID, (int)! $enabled ) ) );
313
 
                }
314
 
                break;
315
 
 
 
135
                        debug_die( 'Requested widget not found' );
 
136
                }
 
137
                break;
 
138
 
 
139
                case 'plugin':
 
140
                if( ! $Plugin = & $Plugins->get_by_code( $code ) )
 
141
                {
 
142
                        debug_die( 'Requested plugin not found' );
 
143
                }
 
144
                if( ! $Plugins->has_event( $Plugin->ID, 'TemplateTag' ) )
 
145
                {
 
146
                        debug_die( 'Requested plugin does not support TemplateTag' );
 
147
                }
 
148
                $edited_ComponentWidget = new ComponentWidget( NULL, 'plugin', $code, array() );
 
149
                break;
 
150
 
 
151
                default:
 
152
                debug_die( 'Unhandled widget type' );
 
153
        }
 
154
 
 
155
        $edited_ComponentWidget->set( 'coll_ID', $Blog->ID );
 
156
        $edited_ComponentWidget->set( 'sco_name', $container );
 
157
        $edited_ComponentWidget->set( 'enabled', 1 );
 
158
 
 
159
        // insert into database
 
160
        $edited_ComponentWidget->dbinsert();
 
161
 
 
162
        $Messages->add( sprintf( T_('Widget &laquo;%s&raquo; has been added to container &laquo;%s&raquo;.'),
 
163
                $edited_ComponentWidget->get_name(), T_($container) ), 'success' );
 
164
 
 
165
        switch( $display_mode )
 
166
        {
 
167
                case 'js':
 
168
                send_javascript_message(
 
169
                        array(
 
170
                                'addNewWidgetCallback' => array(
 
171
                                        $edited_ComponentWidget->ID,
 
172
                                        $container,
 
173
                                        $edited_ComponentWidget->get( 'order' ),
 
174
                                        $edited_ComponentWidget->get_name(),
 
175
                                ),
 
176
                                // open widget settings
 
177
                                'editWidget' => array(
 
178
                                        'wi_ID_'.$edited_ComponentWidget->ID,
 
179
                                ),
 
180
                        )
 
181
                );
 
182
                break;
 
183
 
 
184
                // take usual action
 
185
                case 'normal':
 
186
                default:
 
187
                header_redirect( '?ctrl=widgets&action=edit&wi_ID='.$edited_ComponentWidget->ID );
 
188
                break;
 
189
        }
 
190
        break;
 
191
 
 
192
        // Update Settings
 
193
        case 'update':
 
194
        $edited_ComponentWidget->load_from_Request();
 
195
 
 
196
        if( ! param_errors_detected() )
 
197
        {
 
198
                // update settings
 
199
                $edited_ComponentWidget->dbupdate();
 
200
                $Messages->add( T_('Widget settings have been updated'), 'success' );
 
201
                switch( $display_mode )
 
202
                {
 
203
                        // js reply
 
204
                        case 'js':
 
205
                        $edited_ComponentWidget->init_display( array() );
 
206
                        send_javascript_message( array( 'widgetSettingsCallback' => array( $edited_ComponentWidget->ID, $edited_ComponentWidget->get_desc_for_list() ), 'closeWidgetSettings' => array() ), true );
 
207
                        break;
 
208
                }
 
209
                $action = 'list';
 
210
        }
 
211
        elseif( $display_mode == 'js' )
 
212
        {
 
213
                // send errors back as js
 
214
                send_javascript_message( array(), true );
 
215
        }
 
216
        break;
 
217
 
 
218
        // Move the widget up
 
219
        case 'move_up':
 
220
        $order = $edited_ComponentWidget->order;
 
221
        $DB->begin();
 
222
        // Get the previous element
 
223
        $row = $DB->get_row( 'SELECT * FROM T_widget WHERE wi_coll_ID = '.$Blog->ID.'
 
224
                AND wi_sco_name = '.$DB->quote( $edited_ComponentWidget->sco_name ).'
 
225
                AND wi_order < '.$order.' ORDER BY wi_order DESC LIMIT 0, 1' );
 
226
        if( ! empty( $row ) )
 
227
        {
 
228
                $prev_ComponentWidget = new ComponentWidget( $row );
 
229
                $prev_order = $prev_ComponentWidget->order;
 
230
                $edited_ComponentWidget->set( 'order', 0 ); // Temporary
 
231
                $edited_ComponentWidget->dbupdate();
 
232
                $prev_ComponentWidget->set( 'order', $order );
 
233
                $prev_ComponentWidget->dbupdate();
 
234
                $edited_ComponentWidget->set( 'order', $prev_order );
 
235
                $edited_ComponentWidget->dbupdate();
 
236
        }
 
237
        $DB->commit();
 
238
        break;
 
239
 
 
240
        // Move the widget down
 
241
        case 'move_down':
 
242
        $order = $edited_ComponentWidget->order;
 
243
        $DB->begin();
 
244
        // Get the next element
 
245
        $row = $DB->get_row( 'SELECT * FROM T_widget WHERE wi_coll_ID = '.$Blog->ID.'
 
246
                AND wi_sco_name = '.$DB->quote( $edited_ComponentWidget->sco_name ).'
 
247
                AND wi_order > '.$order.' ORDER BY wi_order ASC LIMIT 0, 1' );
 
248
        if( ! empty( $row ) )
 
249
        {
 
250
                $next_ComponentWidget = new ComponentWidget( $row );
 
251
                $next_order = $next_ComponentWidget->order;
 
252
                $edited_ComponentWidget->set( 'order', 0 ); // Temporary
 
253
                $edited_ComponentWidget->dbupdate();
 
254
                $next_ComponentWidget->set( 'order', $order );
 
255
                $next_ComponentWidget->dbupdate();
 
256
                $edited_ComponentWidget->set( 'order', $next_order );
 
257
                $edited_ComponentWidget->dbupdate();
 
258
        }
 
259
        $DB->commit();
 
260
        break;
 
261
 
 
262
        // Enable or disable the widget
 
263
        case 'toggle':
 
264
        $enabled = $edited_ComponentWidget->get( 'enabled' );
 
265
        $edited_ComponentWidget->set( 'enabled', (int)! $enabled );
 
266
        $edited_ComponentWidget->dbupdate();
 
267
 
 
268
        if( $enabled )
 
269
        {
 
270
                $msg = T_('Widget has been disabled.');
 
271
        }
 
272
        else
 
273
        {
 
274
                $msg = T_('Widget has been enabled.');
 
275
        }
 
276
        $Messages->add( $msg, 'success' );
 
277
 
 
278
        if( $display_mode == 'js' )
 
279
        {
 
280
                // EXITS
 
281
                send_javascript_message( array( 'doToggle' => array( $edited_ComponentWidget->ID, (int)! $enabled ) ) );
 
282
        }
 
283
        break;
 
284
 
 
285
        // Remove a widget from container
316
286
        case 'delete':
317
 
                // Remove a widget from container:
318
 
                $msg = sprintf( T_('Widget &laquo;%s&raquo; removed.'), $edited_ComponentWidget->get_name() );
319
 
                $edited_widget_ID = $edited_ComponentWidget->ID;
320
 
                $edited_ComponentWidget->dbdelete( true );
321
 
                unset( $edited_ComponentWidget );
322
 
                forget_param( 'wi_ID' );
323
 
                $Messages->add( $msg, 'success' );
324
 
 
325
 
                switch( $display_mode )
 
287
        $msg = sprintf( T_('Widget &laquo;%s&raquo; removed.'), $edited_ComponentWidget->get_name() );
 
288
        $edited_widget_ID = $edited_ComponentWidget->ID;
 
289
        $edited_ComponentWidget->dbdelete( true );
 
290
        unset( $edited_ComponentWidget );
 
291
        forget_param( 'wi_ID' );
 
292
        $Messages->add( $msg, 'success' );
 
293
 
 
294
        switch( $display_mode )
 
295
        {
 
296
                // js call, return success message
 
297
                case 'js':
 
298
                send_javascript_message( array( 'doDelete' => $edited_widget_ID ) );
 
299
                break;
 
300
 
 
301
                // take usual action
 
302
                case 'normal':
 
303
                default:
 
304
                // PREVENT RELOAD & Switch to list mode
 
305
                header_redirect( '?ctrl=widgets&blog='.$blog );
 
306
                break;
 
307
        }
 
308
        break;
 
309
 
 
310
        case 'list':
 
311
        break;
 
312
 
 
313
        // js request
 
314
        case 're-order':
 
315
        $DB->begin();
 
316
 
 
317
        // Reset the current orders and make container names temp to avoid duplicate entry errors
 
318
        $DB->query( 'UPDATE T_widget SET wi_order = wi_order * -1,
 
319
                wi_sco_name = CONCAT( \'temp_\', wi_sco_name ) WHERE wi_coll_ID = '.$Blog->ID );
 
320
 
 
321
        foreach( $containers as $container => $widgets )
 
322
        {
 
323
                // loop through each container and set new order
 
324
                $order = 0;
 
325
                foreach( $widgets as $widget )
326
326
                {
327
 
                        case 'js' :     // js call : return success message
328
 
                                send_javascript_message( array( 'doDelete' => $edited_widget_ID ) );
329
 
                                break;
330
 
 
331
 
                        case 'normal' :
332
 
                        default : // take usual action
333
 
                                // PREVENT RELOAD & Switch to list mode:
334
 
                                header_redirect( '?ctrl=widgets&blog='.$blog );
335
 
                                break;
336
 
                }
337
 
                break;
338
 
 
339
 
        case 'list':
340
 
                break;
341
 
 
342
 
        case 're-order' : // js request
343
 
                $DB->begin();
344
 
 
345
 
                // Reset the current orders and make container names temp to avoid duplicate entry errors
346
 
                $DB->query( 'UPDATE T_widget
347
 
                                                                                SET wi_order = wi_order * -1,
348
 
                                                                                                wi_sco_name = CONCAT( \'temp_\', wi_sco_name )
349
 
                                                                        WHERE wi_coll_ID = '.$Blog->ID );
350
 
 
351
 
                foreach( $containers as $container => $widgets )
352
 
                {       // loop through each container and set new order
353
 
                        $order = 0; // reset counter for this container
354
 
                        foreach( $widgets as $widget )
355
 
                        {       // loop through each widget
356
 
                                if( $widget = preg_replace( '~[^0-9]~', '', $widget ) )
357
 
                                { // valid widget id
358
 
                                        $order++;
359
 
                                        $DB->query( 'UPDATE T_widget
360
 
                                                                                                        SET wi_order = '.$order.',
361
 
                                                                                                                        wi_sco_name = '.$DB->quote( $container ).'
362
 
                                                                                                WHERE wi_ID = '.$widget.'
363
 
                                                                                                  AND wi_coll_ID = '.$Blog->ID );       // Doh! Don't trust the client request!!
364
 
                                }
 
327
                        // loop through each widget
 
328
                        if( $widget = preg_replace( '~[^0-9]~', '', $widget ) )
 
329
                        {
 
330
                                // valid widget id
 
331
                                $order++;
 
332
                                $DB->query( 'UPDATE T_widget SET wi_order = '.$order.', wi_sco_name = '.$DB->quote( $container ).'
 
333
                                        WHERE wi_ID = '.$widget.' AND wi_coll_ID = '.$Blog->ID ); // Don't trust the client request!!
365
334
                        }
366
335
                }
367
 
 
368
 
                // Cleanup deleted widgets and empty temp containers
369
 
                $DB->query( 'DELETE FROM T_widget
370
 
                                                                        WHERE wi_order < 1
371
 
                                                                                AND wi_coll_ID = '.$Blog->ID ); // Doh! Don't touch other blogs!
372
 
 
373
 
                $DB->commit();
374
 
 
375
 
                $Messages->add( T_( 'Widgets updated' ), 'success' );
376
 
                send_javascript_message( array( 'sendWidgetOrderCallback' => array( 'blog='.$Blog->ID ) ) ); // exits() automatically
377
 
                break;
 
336
        }
 
337
 
 
338
        // Cleanup deleted widgets and empty temp containers
 
339
        $DB->query( 'DELETE FROM T_widget WHERE wi_order < 1 AND wi_coll_ID = '.$Blog->ID );
 
340
        $DB->commit();
 
341
        $Messages->add( T_('Widgets updated'), 'success' );
 
342
        // exits() automatically
 
343
        send_javascript_message( array( 'sendWidgetOrderCallback' => array( 'blog='.$Blog->ID ) ) );
 
344
        break;
378
345
 
379
346
        default:
380
 
                debug_die( 'Action: unhandled action' );
 
347
        debug_die( 'Action: unhandled action' );
381
348
}
382
349
 
383
350
if( $display_mode == 'normal' )
384
 
{       // this is a normal (not a JS) request
 
351
{
 
352
        // this is a normal (not a JS) request
385
353
        // fp> This probably shouldn't be handled like this but with $mode
386
 
        /**
387
 
         * Display page header, menus & messages:
388
 
         */
 
354
        // Display page header, menus & messages
389
355
        $AdminUI->set_coll_list_params( 'blog_properties', 'edit', array( 'ctrl' => 'widgets' ),
390
 
                                T_('List'), '?ctrl=collections&amp;blog=0' );
391
 
 
 
356
                T_('List'), '?ctrl=collections&amp;blog=0' );
392
357
        $AdminUI->set_path( 'blogs', 'widgets' );
393
358
 
394
359
        // load the js and css required to make the magic work
395
360
        add_js_headline( '
396
 
        /**
397
 
         * @internal T_ array of translation strings required by the UI
398
 
         */
 
361
        // array of translation strings required by the UI
399
362
        var T_arr = new Array();
400
363
        T_arr["Changes pending"] = \''.TS_( 'Changes pending' ).'\';
401
364
        T_arr["Saving changes"] = \''.TS_( 'Saving changes' ).'\';
403
366
        T_arr["Update cancelled"] = \''.TS_( 'Update cancelled' ).'\';
404
367
        T_arr["Update Paused"] = \''.TS_( 'Update Paused' ).'\';
405
368
 
406
 
        /**
407
 
         * Image tags for the JavaScript widget UI.
408
 
         *
409
 
         * @internal Tblue> We get the whole img tags here (easier).
410
 
         */
411
 
        var enabled_icon_tag = \''.get_icon( 'enabled', 'imgtag', array( 'title' => T_( 'The widget is enabled.' ) ) ).'\';
412
 
        var disabled_icon_tag = \''.get_icon( 'disabled', 'imgtag', array( 'title' => T_( 'The widget is disabled.' ) ) ).'\';
413
 
        var activate_icon_tag = \''.get_icon( 'activate', 'imgtag', array( 'title' => T_( 'Enable this widget!' ) ) ).'\';
414
 
        var deactivate_icon_tag = \''.get_icon( 'deactivate', 'imgtag', array( 'title' => T_( 'Disable this widget!' ) ) ).'\';
415
 
 
 
369
        // Image tags for the JavaScript widget UI.
 
370
        // Note Tblue> We get the whole img tags here (easier).
 
371
        var enabled_icon_tag = \''.get_icon( 'enabled', 'imgtag', array( 'title' => T_('The widget is enabled.') ) ).'\';
 
372
        var disabled_icon_tag = \''.get_icon( 'disabled', 'imgtag', array( 'title' => T_('The widget is disabled.') ) ).'\';
 
373
        var activate_icon_tag = \''.get_icon( 'activate', 'imgtag', array( 'title' => T_('Enable this widget!') ) ).'\';
 
374
        var deactivate_icon_tag = \''.get_icon( 'deactivate', 'imgtag', array( 'title' => T_('Disable this widget!') ) ).'\';
416
375
        var b2evo_dispatcher_url = "'.$admin_url.'";' );
417
376
        require_js( '#jqueryUI#' ); // auto requires jQuery
418
377
        require_js( 'communication.js' ); // auto requires jQuery
419
378
        require_js( 'blog_widgets.js' );
420
379
        require_css( 'blog_widgets.css' );
421
380
 
422
 
 
423
 
        // Display <html><head>...</head> section! (Note: should be done early if actions do not redirect)
 
381
        // Display <html><head>...</head> section (should be done early if actions do not redirect)
424
382
        $AdminUI->disp_html_head();
425
 
 
426
 
        // Display title, menu, messages, etc. (Note: messages MUST be displayed AFTER the actions)
 
383
        // Display title, menu, messages, etc... (messages MUST be displayed AFTER the actions)
427
384
        $AdminUI->disp_body_top();
428
385
}
429
386
 
430
 
/**
431
 
 * Display payload:
432
 
 */
 
387
// Display payload
433
388
switch( $action )
434
389
{
 
390
        // Do nothing
435
391
        case 'nil':
436
 
                // Do nothing
437
 
                break;
438
 
 
 
392
        break;
439
393
 
440
394
        case 'new':
441
 
                // Begin payload block:
442
 
                $AdminUI->disp_payload_begin();
443
 
 
444
 
                // Display VIEW:
445
 
                $AdminUI->disp_view( 'widgets/views/_widget_list_available.view.php' );
446
 
 
447
 
                // End payload block:
448
 
                $AdminUI->disp_payload_end();
449
 
                break;
450
 
 
 
395
        // Begin payload block
 
396
        $AdminUI->disp_payload_begin();
 
397
 
 
398
        // Display VIEW
 
399
        $AdminUI->disp_view( 'widgets/views/_widget_list_available.view.php' );
 
400
 
 
401
        // End payload block
 
402
        $AdminUI->disp_payload_end();
 
403
        break;
451
404
 
452
405
        case 'edit':
453
 
        case 'update':  // on error
454
 
                switch( $display_mode )
455
 
                {
456
 
                        case 'js' : // js request
457
 
                                ob_start();
458
 
                                // Display VIEW:
459
 
                                $AdminUI->disp_view( 'widgets/views/_widget.form.php' );
460
 
                                $output = ob_get_clean();
461
 
                                send_javascript_message( array( 'widgetSettings' => $output ) );
462
 
                                break;
463
 
 
464
 
                        case 'normal' :
465
 
                        default : // take usual action
466
 
                                // Begin payload block:
467
 
                                $AdminUI->disp_payload_begin();
468
 
 
469
 
                                // Display VIEW:
470
 
                                $AdminUI->disp_view( 'widgets/views/_widget.form.php' );
471
 
 
472
 
                                // End payload block:
473
 
                                $AdminUI->disp_payload_end();
474
 
                                break;
475
 
                }
476
 
                break;
477
 
 
 
406
        case 'update': // on error
 
407
        switch( $display_mode )
 
408
        {
 
409
                // js request
 
410
                case 'js':
 
411
                ob_start();
 
412
                // Display VIEW
 
413
                $AdminUI->disp_view( 'widgets/views/_widget.form.php' );
 
414
                $output = ob_get_clean();
 
415
                send_javascript_message( array( 'widgetSettings' => $output ) );
 
416
                break;
 
417
 
 
418
                // take usual action
 
419
                case 'normal':
 
420
                default:
 
421
                // Begin payload block
 
422
                $AdminUI->disp_payload_begin();
 
423
 
 
424
                // Display VIEW
 
425
                $AdminUI->disp_view( 'widgets/views/_widget.form.php' );
 
426
 
 
427
                // End payload block
 
428
                $AdminUI->disp_payload_end();
 
429
                break;
 
430
        }
 
431
        break;
478
432
 
479
433
        case 'list':
480
434
        default:
481
 
                // Begin payload block:
482
 
                $AdminUI->disp_payload_begin();
483
 
 
484
 
                // Display VIEW:
485
 
 
486
 
                // this will be enabled if js available:
487
 
                echo '<div class="available_widgets">'."\n";
488
 
                echo '<div class="available_widgets_toolbar"><a href="#" class="rollover floatright" style="padding: 1px 0;">'.get_icon('close').'</a>'.T_( 'Select widget to add:' ).'</div>'."\n";
489
 
                echo '<div id="available_widgets_inner">'."\n";
490
 
                $AdminUI->disp_view( 'widgets/views/_widget_list_available.view.php' );
491
 
                echo '</div></div>'."\n";
492
 
                echo '
493
 
                <script type="text/javascript">
494
 
                        <!--
495
 
                        var blog = '.$Blog->ID.';
496
 
                        // -->
497
 
                </script>
498
 
                ';
499
 
 
500
 
                // Display VIEW:
501
 
                $AdminUI->disp_view( 'widgets/views/_widget_list.view.php' );
502
 
 
503
 
                // End payload block:
504
 
                $AdminUI->disp_payload_end();
505
 
                break;
 
435
        // Begin payload block
 
436
        $AdminUI->disp_payload_begin();
 
437
 
 
438
        // Display VIEW
 
439
        // this will be enabled if js available
 
440
        echo '<div class="available_widgets">'."\n";
 
441
        echo '<div class="available_widgets_toolbar"><a href="#" class="rollover floatright" style="padding: 1px 0;">'.get_icon('close').'</a>'.T_('Select widget to add:').'</div>'."\n";
 
442
        echo '<div id="available_widgets_inner">'."\n";
 
443
        $AdminUI->disp_view( 'widgets/views/_widget_list_available.view.php' );
 
444
        echo '</div></div>'."\n";
 
445
        echo '
 
446
        <script type="text/javascript">
 
447
                <!--
 
448
                var blog = '.$Blog->ID.';
 
449
                // -->
 
450
        </script>
 
451
        ';
 
452
 
 
453
        // Display VIEW
 
454
        $AdminUI->disp_view( 'widgets/views/_widget_list.view.php' );
 
455
 
 
456
        // End payload block
 
457
        $AdminUI->disp_payload_end();
 
458
        break;
506
459
}
507
460
 
508
 
// Display body bottom, debug info and close </html>:
 
461
// Display body bottom, debug info, close </html>
509
462
$AdminUI->disp_global_footer();
510
463
 
511
 
 
512
464
?>