Dotclear

source: plugins/widgets/index.php @ 3021:4018240bced6

Revision 3021:4018240bced6, 11.7 KB checked in by Nicolas <nikrou77@…>, 10 years ago (diff)

Add css without cache counterpart : cssLoad
Use jsLoad and cssLoad for official plugins

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  <?php echo dcPage::cssLoad(dcPage::getPF('widgets/style.css'));?>
217  <?php
218          echo
219               dcPage::jsLoad('js/jquery/jquery-ui.custom.js').
220               dcPage::jsLoad('js/jquery/jquery.ui.touch-punch.js').
221               dcPage::jsLoad(dcPage::getPF('widgets/widgets.js'));
222  ?>
223  <?php
224     $core->auth->user_prefs->addWorkspace('accessibility');
225     $user_dm_nodragdrop = $core->auth->user_prefs->accessibility->nodragdrop;
226  ?>
227  <?php if (!$user_dm_nodragdrop) : ?>
228  <?php echo dcPage::jsLoad(dcPage::getPF('widgets/dragdrop.js'));?>
229  <?php endif; ?>
230  <script type="text/javascript">
231  //<![CDATA[
232  <?php echo dcPage::jsVar('dotclear.msg.confirm_widgets_reset',
233     __('Are you sure you want to reset sidebars?')); ?>
234  //]]>
235  </script>
236  <?php echo $core->callBehavior('adminPostEditor');?>
237  <?php echo(dcPage::jsConfirmClose('sidebarsWidgets')); ?>
238</head>
239<body>
240<?php
241echo dcPage::breadcrumb(
242     array(
243          html::escapeHTML($core->blog->name) => '',
244          __('Widgets') => ''
245     )).
246     dcPage::notices();
247
248# All widgets
249echo
250'<form id="listWidgets" action="'.$p_url.'" method="post"  class="widgets">'.
251'<h3>'.__('Available widgets').'</h3>'.
252'<p>'.__('Drag widgets from this list to one of the sidebars, for add.').'</p>'.
253'<ul id="widgets-ref">';
254
255$j = 0;
256foreach ($__widgets->elements(true) as $w) {
257     echo
258     '<li>'.form::hidden(array('w[void][0][id]'),html::escapeHTML($w->id())).
259     '<p class="widget-name">'.form::field(array('w[void][0][order]'),2,3,0,'hide','',0,'title="'.__('order').'"').' '.$w->name().
260     ($w->desc() != '' ? ' <span class="form-note">'.__($w->desc()).'</span>' : '').'</p>'.
261     '<p class="manual-move remove-if-drag"><label class="classic">'.__('Append to:').'</label> '.
262     form::combo(array('addw['.$w->id().']'),$append_combo).
263     '<input type="submit" name="append['.$w->id().']" value="'.__('Add').'" /></p>'.
264     '<div class="widgetSettings hidden-if-drag">'.$w->formSettings('w[void][0]',$j).'</div>'.
265     '</li>';
266     $j++;
267}
268
269echo
270'</ul>'.
271'<p>'.$core->formNonce().'</p>'.
272'<p class="remove-if-drag"><input type="submit" name="append" value="'.__('Add widgets to sidebars').'" /></p>'.
273'</form>';
274
275echo '<form id="sidebarsWidgets" action="'.$p_url.'" method="post">';
276# Nav sidebar
277echo
278'<div id="sidebarNav" class="widgets fieldset">'.
279sidebarWidgets('dndnav',__('Navigation sidebar'),$widgets_nav,'nav',$__default_widgets['nav'],$j);
280echo '</div>';
281
282# Extra sidebar
283echo
284'<div id="sidebarExtra" class="widgets fieldset">'.
285sidebarWidgets('dndextra',__('Extra sidebar'),$widgets_extra,'extra',$__default_widgets['extra'],$j);
286echo '</div>';
287
288# Custom sidebar
289echo
290'<div id="sidebarCustom" class="widgets fieldset">'.
291sidebarWidgets('dndcustom',__('Custom sidebar'),$widgets_custom,'custom',$__default_widgets['custom'],$j);
292echo '</div>';
293
294echo
295'<p id="sidebarsControl">'.
296$core->formNonce().
297'<input type="submit" name="wup" value="'.__('Update sidebars').'" /> '.
298'<input type="submit" class="reset" name="wreset" value="'.__('Reset sidebars').'" /></p>'.
299'</form>';
300
301$widget_elements = new stdClass;
302$widget_elements->content = '<dl>';
303foreach ($__widgets->elements() as $w)
304{
305     $widget_elements->content .=
306     '<dt><strong>'.html::escapeHTML($w->name()).'</strong> ('.
307     __('Widget ID:').' <strong>'.html::escapeHTML($w->id()).'</strong>)'.
308     ($w->desc() != '' ? ' <span class="form-note">'.__($w->desc()).'</span>' : '').'</dt>'.
309     '<dd>';
310
311     $w_settings = $w->settings();
312     if (empty($w_settings))
313     {
314          $widget_elements->content .= '<p>'.__('No setting for this widget').'</p>';
315     }
316     else
317     {
318          $widget_elements->content .= '<ul>';
319          foreach ($w->settings() as $n => $s)
320          {
321               switch ($s['type']) {
322                    case 'check':
323                         $s_type = __('boolean').", ".__('possible values:')." <code>0</code> ".__('or')." <code>1</code>";
324                         break;
325                    case 'combo':
326                         $s['options'] = array_map("literalNullString", $s['options']);
327                         $s_type = __('listitem').", ".__('possible values:')." <code>".implode('</code>, <code>',$s['options'])."</code>";
328                         break;
329                    case 'text':
330                    case 'textarea':
331                    default:
332                         $s_type = __('string');
333                         break;
334               }
335
336               $widget_elements->content .=
337               '<li>'.
338               __('Setting name:').' <strong>'.html::escapeHTML($n).'</strong>'.
339               ' ('.$s_type.')'.
340               '</li>';
341          }
342          $widget_elements->content .= '</ul>';
343     }
344     $widget_elements->content .= '</dd>';
345}
346$widget_elements->content .= '</dl></div>';
347
348dcPage::helpBlock('widgets',$widget_elements);
349
350function sidebarWidgets($id,$title,$widgets,$pr,$default_widgets,&$j)
351{
352     $res = '<h3>'.$title.'</h3>';
353
354     if (!($widgets instanceof dcWidgets))
355     {
356          $widgets = $default_widgets;
357     }
358
359     $res .= '<ul id="'.$id.'" class="connected">';
360
361     $res .= '<li class="empty-widgets" '.(!$widgets->isEmpty() ? 'style="display: none;"' : '').'>'.__('No widget as far.').'</li>';
362
363     $i = 0;
364     foreach ($widgets->elements() as $w)
365     {
366          $upDisabled = $i == 0 ? ' disabled" src="images/disabled_' : '" src="images/';
367          $downDisabled = $i == count($widgets->elements())-1 ? ' disabled" src="images/disabled_' : '" src="images/';
368          $altUp = $i == 0 ? ' alt=""' : ' alt="'.__('Up the widget').'"';
369          $altDown = $i == count($widgets->elements())-1 ? ' alt=""' : ' alt="'.__('Down the widget').'"';
370
371          $iname = 'w['.$pr.']['.$i.']';
372
373          $res .=
374          '<li>'.form::hidden(array($iname.'[id]'),html::escapeHTML($w->id())).
375          '<p class="widget-name">'.form::field(array($iname.'[order]'),2,3,(string) $i,'hidden','',0,'title="'.__('order').'"').
376          ' '.$w->name().
377          ($w->desc() != '' ? ' <span class="form-note">'.__($w->desc()).'</span>' : '').
378          '<span class="toolsWidget remove-if-drag">'.
379          '<input type="image" class="upWidget'.$upDisabled.'up.png" name="'.$iname.'[_up]" value="'.__('Up the widget').'"'.$altUp.' /> '.
380          '<input type="image" class="downWidget'.$downDisabled.'down.png" name="'.$iname.'[_down]" value="'.__('Down the widget').'"'.$altDown.' /> '.' '.
381          '<input type="image" class="removeWidget" src="images/trash.png" name="'.$iname.'[_rem]" value="'.__('Remove widget').'" alt="'.__('Remove the widget').'" />'.
382          '</span>'.
383          '<br class="clear"/></p>'.
384          '<div class="widgetSettings hidden-if-drag">'.$w->formSettings($iname,$j).'</div>'.
385          '</li>';
386
387          $i++;
388          $j++;
389     }
390
391     $res .= '</ul>';
392
393     $res .= '<ul class="sortable-delete"'.($i > 0 ? '':' style="display: none;"').'><li class="sortable-delete-placeholder">'.
394               __('Drag widgets here to remove.').'</li></ul>';
395
396     return $res;
397}
398?>
399</body>
400</html>
Note: See TracBrowser for help on using the repository browser.

Sites map