Dotclear

source: plugins/widgets/index.php @ 2509:5ab9027dee05

Revision 2509:5ab9027dee05, 11.6 KB checked in by franck <carnet.franck.paul@…>, 12 years ago (diff)

Add success messages in widgets/index.php, fixes #1834

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
7# Licensed under the GPL version 2.0 license.
8# See LICENSE file or
9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10#
11# -- END LICENSE BLOCK -----------------------------------------
12if (!defined('DC_CONTEXT_ADMIN')) { return; }
13
14include dirname(__FILE__).'/_default_widgets.php';
15
16# Loading navigation, extra widgets and custom widgets
17$widgets_nav = null;
18if ($core->blog->settings->widgets->widgets_nav) {
19     $widgets_nav = dcWidgets::load($core->blog->settings->widgets->widgets_nav);
20}
21$widgets_extra = null;
22if ($core->blog->settings->widgets->widgets_extra) {
23     $widgets_extra = dcWidgets::load($core->blog->settings->widgets->widgets_extra);
24}
25$widgets_custom = null;
26if ($core->blog->settings->widgets->widgets_custom) {
27     $widgets_custom = dcWidgets::load($core->blog->settings->widgets->widgets_custom);
28}
29
30$append_combo = array(
31     '-' => 0,
32     __('navigation') => 'nav',
33     __('extra') => 'extra',
34     __('custom') => 'custom'
35);
36
37function literalNullString($v)
38{
39     if ($v == '') {
40          return '&lt;'.__('empty string').'&gt;';
41     }
42     return $v;
43}
44
45# Adding widgets to sidebars
46if (!empty($_POST['append']) && is_array($_POST['addw']))
47{
48     # Filter selection
49     $addw = array();
50     foreach ($_POST['addw'] as $k => $v) {
51          if (($v == 'extra' || $v == 'nav' || $v == 'custom') && $__widgets->{$k} !== null ) {
52               $addw[$k] = $v;
53          }
54     }
55
56     # Append 1 widget
57     $wid = false;
58     if( gettype($_POST['append']) == 'array' && count($_POST['append']) == 1 ) {
59          $wid = array_keys($_POST['append']);
60          $wid = $wid[0];
61     }
62
63     # Append widgets
64     if (!empty($addw))
65     {
66          if (!($widgets_nav instanceof dcWidgets)) {
67               $widgets_nav = new dcWidgets;
68          }
69          if (!($widgets_extra instanceof dcWidgets)) {
70               $widgets_extra = new dcWidgets();
71          }
72          if (!($widgets_custom instanceof dcWidgets)) {
73               $widgets_custom = new dcWidgets();
74          }
75
76          foreach ($addw as $k => $v)
77          {
78               if( !$wid || $wid == $k )
79               {
80                    switch ($v) {
81                         case 'nav':
82                              $widgets_nav->append($__widgets->{$k});
83                              break;
84                         case 'extra':
85                              $widgets_extra->append($__widgets->{$k});
86                              break;
87                         case 'custom':
88                              $widgets_custom->append($__widgets->{$k});
89                              break;
90                    }
91               }
92          }
93
94          try {
95               $core->blog->settings->addNamespace('widgets');
96               $core->blog->settings->widgets->put('widgets_nav',$widgets_nav->store());
97               $core->blog->settings->widgets->put('widgets_extra',$widgets_extra->store());
98               $core->blog->settings->widgets->put('widgets_custom',$widgets_custom->store());
99               $core->blog->triggerBlog();
100               http::redirect($p_url);
101          } catch (Exception $e) {
102               $core->error->add($e->getMessage());
103          }
104     }
105}
106
107# Removing ?
108$removing = false;
109if ( isset($_POST['w']) && is_array($_POST['w']) ) {
110     foreach ($_POST['w'] as $nsid => $nsw) {
111          foreach ($nsw as $i => $v) {
112               if (!empty($v['_rem'])) {
113                    $removing = true;
114                    break 2;
115               }
116          }
117     }
118}
119
120# Move ?
121$move = false;
122if ( isset($_POST['w']) && is_array($_POST['w']) ) {
123     foreach ($_POST['w'] as $nsid => $nsw) {
124          foreach ($nsw as $i => $v) {
125               if (!empty($v['_down'])) {
126                    $oldorder = $_POST['w'][$nsid][$i]['order'];
127                    $neworder = $oldorder + 1;
128                    if( isset($_POST['w'][$nsid][$neworder]) ) {
129                         $_POST['w'][$nsid][$i]['order'] = $neworder;
130                         $_POST['w'][$nsid][$neworder]['order'] = $oldorder;
131                         $move = true;
132                    }
133               }
134               if (!empty($v['_up'])) {
135                    $oldorder = $_POST['w'][$nsid][$i]['order'];
136                    $neworder = $oldorder - 1;
137                    if( isset($_POST['w'][$nsid][$neworder]) ) {
138                         $_POST['w'][$nsid][$i]['order'] = $neworder;
139                         $_POST['w'][$nsid][$neworder]['order'] = $oldorder;
140                         $move = true;
141                    }
142               }
143          }
144     }
145}
146
147# Update sidebars
148if (!empty($_POST['wup']) || $removing || $move )
149{
150     if (!isset($_POST['w']) || !is_array($_POST['w'])) {
151          $_POST['w'] = array();
152     }
153
154     try
155     {
156          # Removing mark as _rem widgets
157          foreach ($_POST['w'] as $nsid => $nsw) {
158               foreach ($nsw as $i => $v) {
159                    if (!empty($v['_rem'])) {
160                         unset($_POST['w'][$nsid][$i]);
161                         continue;
162                    }
163               }
164          }
165
166          if (!isset($_POST['w']['nav'])) {
167               $_POST['w']['nav'] = array();
168          }
169          if (!isset($_POST['w']['extra'])) {
170               $_POST['w']['extra'] = array();
171          }
172          if (!isset($_POST['w']['custom'])) {
173               $_POST['w']['custom'] = array();
174          }
175
176          $widgets_nav = dcWidgets::loadArray($_POST['w']['nav'],$__widgets);
177          $widgets_extra = dcWidgets::loadArray($_POST['w']['extra'],$__widgets);
178          $widgets_custom = dcWidgets::loadArray($_POST['w']['custom'],$__widgets);
179
180          $core->blog->settings->addNamespace('widgets');
181          $core->blog->settings->widgets->put('widgets_nav',$widgets_nav->store());
182          $core->blog->settings->widgets->put('widgets_extra',$widgets_extra->store());
183          $core->blog->settings->widgets->put('widgets_custom',$widgets_custom->store());
184          $core->blog->triggerBlog();
185
186          dcPage::addSuccessNotice(__('Sidebars and their widgets have been saved.'));
187          http::redirect($p_url);
188     }
189     catch (Exception $e)
190     {
191          $core->error->add($e->getMessage());
192     }
193}
194elseif (!empty($_POST['wreset']))
195{
196     try
197     {
198          $core->blog->settings->addNamespace('widgets');
199          $core->blog->settings->widgets->put('widgets_nav','');
200          $core->blog->settings->widgets->put('widgets_extra','');
201          $core->blog->settings->widgets->put('widgets_custom','');
202          $core->blog->triggerBlog();
203
204          dcPage::addSuccessNotice(__('Sidebars have been resetting.'));
205          http::redirect($p_url);
206     }
207     catch (Exception $e)
208     {
209          $core->error->add($e->getMessage());
210     }
211}
212?>
213<html>
214<head>
215  <title><?php echo __('Widgets'); ?></title>
216  <link type="text/css" rel="stylesheet" href="index.php?pf=widgets/style.css"/>
217  <?php
218          echo
219               dcPage::jsLoad('js/jquery/jquery-ui.custom.js').
220               dcPage::jsLoad('index.php?pf=widgets/widgets.js');
221  ?>
222  <?php
223     $core->auth->user_prefs->addWorkspace('accessibility');
224     $user_dm_nodragdrop = $core->auth->user_prefs->accessibility->nodragdrop;
225  ?>
226  <?php if (!$user_dm_nodragdrop) : ?>
227  <script type="text/javascript" src="index.php?pf=widgets/dragdrop.js"></script>
228  <?php endif; ?>
229  <script type="text/javascript">
230  //<![CDATA[
231  <?php echo dcPage::jsVar('dotclear.msg.confirm_widgets_reset',
232     __('Are you sure you want to reset sidebars?')); ?>
233  //]]>
234  </script>
235  <?php echo(dcPage::jsConfirmClose('sidebarsWidgets')); ?>
236</head>
237<body>
238<?php
239echo dcPage::breadcrumb(
240     array(
241          html::escapeHTML($core->blog->name) => '',
242          __('Widgets') => ''
243     )).
244     dcPage::notices();
245
246# All widgets
247echo
248'<form id="listWidgets" action="'.$p_url.'" method="post"  class="widgets">'.
249'<h3>'.__('Available widgets').'</h3>'.
250'<p>'.__('Drag widgets from this list to one of the sidebars, for add.').'</p>'.
251'<ul id="widgets-ref">';
252
253$j = 0;
254foreach ($__widgets->elements(true) as $w) {
255     echo
256     '<li>'.form::hidden(array('w[void][0][id]'),html::escapeHTML($w->id())).
257     '<p class="widget-name">'.form::field(array('w[void][0][order]'),2,3,0,'hide','',0,'title="'.__('order').'"').' '.$w->name().
258     ($w->desc() != '' ? ' <span class="form-note">'.__($w->desc()).'</span>' : '').'</p>'.
259     '<p class="manual-move remove-if-drag"><label class="classic">'.__('Append to:').'</label> '.
260     form::combo(array('addw['.$w->id().']'),$append_combo).
261     '<input type="submit" name="append['.$w->id().']" value="'.__('Add').'" /></p>'.
262     '<div class="widgetSettings hidden-if-drag">'.$w->formSettings('w[void][0]',$j).'</div>'.
263     '</li>';
264     $j++;
265}
266
267echo
268'</ul>'.
269'<p>'.$core->formNonce().'</p>'.
270'<p class="remove-if-drag"><input type="submit" name="append" value="'.__('Add widgets to sidebars').'" /></p>'.
271'</form>';
272
273echo '<form id="sidebarsWidgets" action="'.$p_url.'" method="post">';
274# Nav sidebar
275echo
276'<div id="sidebarNav" class="widgets fieldset">'.
277sidebarWidgets('dndnav',__('Navigation sidebar'),$widgets_nav,'nav',$__default_widgets['nav'],$j);
278echo '</div>';
279
280# Extra sidebar
281echo
282'<div id="sidebarExtra" class="widgets fieldset">'.
283sidebarWidgets('dndextra',__('Extra sidebar'),$widgets_extra,'extra',$__default_widgets['extra'],$j);
284echo '</div>';
285
286# Custom sidebar
287echo
288'<div id="sidebarCustom" class="widgets fieldset">'.
289sidebarWidgets('dndcustom',__('Custom sidebar'),$widgets_custom,'custom',$__default_widgets['custom'],$j);
290echo '</div>';
291
292echo
293'<p id="sidebarsControl">'.
294$core->formNonce().
295'<input type="submit" name="wup" value="'.__('Update sidebars').'" /> '.
296'<input type="submit" class="reset" name="wreset" value="'.__('Reset sidebars').'" /></p>'.
297'</form>';
298
299$widget_elements = new stdClass;
300$widget_elements->content = '<dl>';
301foreach ($__widgets->elements() as $w)
302{
303     $widget_elements->content .=
304     '<dt><strong>'.html::escapeHTML($w->name()).'</strong> ('.
305     __('Widget ID:').' <strong>'.html::escapeHTML($w->id()).'</strong>)'.
306     ($w->desc() != '' ? ' <span class="form-note">'.__($w->desc()).'</span>' : '').'</dt>'.
307     '<dd>';
308
309     $w_settings = $w->settings();
310     if (empty($w_settings))
311     {
312          $widget_elements->content .= '<p>'.__('No setting for this widget').'</p>';
313     }
314     else
315     {
316          $widget_elements->content .= '<ul>';
317          foreach ($w->settings() as $n => $s)
318          {
319               switch ($s['type']) {
320                    case 'check':
321                         $s_type = __('boolean').", ".__('possible values:')." <code>0</code> ".__('or')." <code>1</code>";
322                         break;
323                    case 'combo':
324                         $s['options'] = array_map("literalNullString", $s['options']);
325                         $s_type = __('listitem').", ".__('possible values:')." <code>".implode('</code>, <code>',$s['options'])."</code>";
326                         break;
327                    case 'text':
328                    case 'textarea':
329                    default:
330                         $s_type = __('string');
331                         break;
332               }
333
334               $widget_elements->content .=
335               '<li>'.
336               __('Setting name:').' <strong>'.html::escapeHTML($n).'</strong>'.
337               ' ('.$s_type.')'.
338               '</li>';
339          }
340          $widget_elements->content .= '</ul>';
341     }
342     $widget_elements->content .= '</dd>';
343}
344$widget_elements->content .= '</dl></div>';
345
346dcPage::helpBlock('widgets',$widget_elements);
347
348function sidebarWidgets($id,$title,$widgets,$pr,$default_widgets,&$j)
349{
350     $res = '<h3>'.$title.'</h3>';
351
352     if (!($widgets instanceof dcWidgets))
353     {
354          $widgets = $default_widgets;
355     }
356
357     $res .= '<ul id="'.$id.'" class="connected">';
358
359     $res .= '<li class="empty-widgets" '.(!$widgets->isEmpty() ? 'style="display: none;"' : '').'>'.__('No widget as far.').'</li>';
360
361     $i = 0;
362     foreach ($widgets->elements() as $w)
363     {
364          $upDisabled = $i == 0 ? ' disabled" src="images/disabled_' : '" src="images/';
365          $downDisabled = $i == count($widgets->elements())-1 ? ' disabled" src="images/disabled_' : '" src="images/';
366          $altUp = $i == 0 ? ' alt=""' : ' alt="'.__('Up the widget').'"';
367          $altDown = $i == count($widgets->elements())-1 ? ' alt=""' : ' alt="'.__('Down the widget').'"';
368
369          $iname = 'w['.$pr.']['.$i.']';
370
371          $res .=
372          '<li>'.form::hidden(array($iname.'[id]'),html::escapeHTML($w->id())).
373          '<p class="widget-name">'.form::field(array($iname.'[order]'),2,3,(string) $i,'hidden','',0,'title="'.__('order').'"').
374          ' '.$w->name().
375          ($w->desc() != '' ? ' <span class="form-note">'.__($w->desc()).'</span>' : '').
376          '<span class="toolsWidget remove-if-drag">'.
377          '<input type="image" class="upWidget'.$upDisabled.'up.png" name="'.$iname.'[_up]" value="'.__('Up the widget').'"'.$altUp.' /> '.
378          '<input type="image" class="downWidget'.$downDisabled.'down.png" name="'.$iname.'[_down]" value="'.__('Down the widget').'"'.$altDown.' /> '.' '.
379          '<input type="image" class="removeWidget" src="images/trash.png" name="'.$iname.'[_rem]" value="'.__('Remove widget').'" alt="'.__('Remove the widget').'" />'.
380          '</span>'.
381          '<br class="clear"/></p>'.
382          '<div class="widgetSettings hidden-if-drag">'.$w->formSettings($iname,$j).'</div>'.
383          '</li>';
384
385          $i++;
386          $j++;
387     }
388
389     $res .= '</ul>';
390
391     $res .= '<ul class="sortable-delete"'.($i > 0 ? '':' style="display: none;"').'><li class="sortable-delete-placeholder">'.
392               __('Drag widgets here to remove.').'</li></ul>';
393
394     return $res;
395}
396?>
397</body>
398</html>
Note: See TracBrowser for help on using the repository browser.

Sites map