Dotclear

source: plugins/widgets/index.php @ 2778:fe7e025273c4

Revision 2778:fe7e025273c4, 11.7 KB checked in by franck <carnet.franck.paul@…>, 11 years ago (diff)

Add "offline" option on widgets, closes #1945

RevLine 
[0]1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
[1179]6# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
[0]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';
[2403]15
[665]16# Loading navigation, extra widgets and custom widgets
[0]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}
[665]25$widgets_custom = null;
26if ($core->blog->settings->widgets->widgets_custom) {
27     $widgets_custom = dcWidgets::load($core->blog->settings->widgets->widgets_custom);
28}
[0]29
30$append_combo = array(
31     '-' => 0,
32     __('navigation') => 'nav',
[665]33     __('extra') => 'extra',
34     __('custom') => 'custom'
[0]35);
36
[902]37function literalNullString($v)
38{
39     if ($v == '') {
40          return '&lt;'.__('empty string').'&gt;';
41     }
42     return $v;
43}
44
[0]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) {
[665]51          if (($v == 'extra' || $v == 'nav' || $v == 'custom') && $__widgets->{$k} !== null ) {
[0]52               $addw[$k] = $v;
53          }
54     }
[2509]55
[2395]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     }
[2509]62
[0]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          }
[665]72          if (!($widgets_custom instanceof dcWidgets)) {
73               $widgets_custom = new dcWidgets();
74          }
[2509]75
[0]76          foreach ($addw as $k => $v)
77          {
[2395]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                    }
[0]91               }
92          }
[2509]93
[0]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());
[665]98               $core->blog->settings->widgets->put('widgets_custom',$widgets_custom->store());
[0]99               $core->blog->triggerBlog();
100               http::redirect($p_url);
101          } catch (Exception $e) {
102               $core->error->add($e->getMessage());
103          }
104     }
105}
106
[2400]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
[2401]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) {
[2408]125               if (!empty($v['_down'])) {
[2401]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               }
[2408]134               if (!empty($v['_up'])) {
[2401]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
[0]147# Update sidebars
[2401]148if (!empty($_POST['wup']) || $removing || $move )
[0]149{
150     if (!isset($_POST['w']) || !is_array($_POST['w'])) {
151          $_POST['w'] = array();
152     }
[2509]153
[0]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          }
[2509]165
[0]166          if (!isset($_POST['w']['nav'])) {
167               $_POST['w']['nav'] = array();
168          }
169          if (!isset($_POST['w']['extra'])) {
170               $_POST['w']['extra'] = array();
171          }
[665]172          if (!isset($_POST['w']['custom'])) {
173               $_POST['w']['custom'] = array();
174          }
[2509]175
[0]176          $widgets_nav = dcWidgets::loadArray($_POST['w']['nav'],$__widgets);
177          $widgets_extra = dcWidgets::loadArray($_POST['w']['extra'],$__widgets);
[665]178          $widgets_custom = dcWidgets::loadArray($_POST['w']['custom'],$__widgets);
[2509]179
[0]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());
[665]183          $core->blog->settings->widgets->put('widgets_custom',$widgets_custom->store());
[0]184          $core->blog->triggerBlog();
[2509]185
186          dcPage::addSuccessNotice(__('Sidebars and their widgets have been saved.'));
[0]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','');
[665]201          $core->blog->settings->widgets->put('widgets_custom','');
[0]202          $core->blog->triggerBlog();
[2509]203
204          dcPage::addSuccessNotice(__('Sidebars have been resetting.'));
[0]205          http::redirect($p_url);
206     }
207     catch (Exception $e)
208     {
209          $core->error->add($e->getMessage());
210     }
211}
[2403]212?>
[0]213<html>
214<head>
215  <title><?php echo __('Widgets'); ?></title>
[2448]216  <link type="text/css" rel="stylesheet" href="index.php?pf=widgets/style.css"/>
[1669]217  <?php
218          echo
219               dcPage::jsLoad('js/jquery/jquery-ui.custom.js').
[2495]220               dcPage::jsLoad('js/jquery/jquery.ui.touch-punch.js').
[1669]221               dcPage::jsLoad('index.php?pf=widgets/widgets.js');
222  ?>
[2509]223  <?php
224     $core->auth->user_prefs->addWorkspace('accessibility');
[183]225     $user_dm_nodragdrop = $core->auth->user_prefs->accessibility->nodragdrop;
226  ?>
227  <?php if (!$user_dm_nodragdrop) : ?>
[0]228  <script type="text/javascript" src="index.php?pf=widgets/dragdrop.js"></script>
[183]229  <?php endif; ?>
[0]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>
[2702]236  <?php echo $core->callBehavior('adminPostEditor');?>
[0]237  <?php echo(dcPage::jsConfirmClose('sidebarsWidgets')); ?>
238</head>
239<body>
[2403]240<?php
[1358]241echo dcPage::breadcrumb(
[1339]242     array(
243          html::escapeHTML($core->blog->name) => '',
[2166]244          __('Widgets') => ''
[2256]245     )).
246     dcPage::notices();
[0]247
248# All widgets
249echo
250'<form id="listWidgets" action="'.$p_url.'" method="post"  class="widgets">'.
[1483]251'<h3>'.__('Available widgets').'</h3>'.
[2395]252'<p>'.__('Drag widgets from this list to one of the sidebars, for add.').'</p>'.
[1659]253'<ul id="widgets-ref">';
[0]254
[117]255$j = 0;
[0]256foreach ($__widgets->elements(true) as $w) {
257     echo
[1648]258     '<li>'.form::hidden(array('w[void][0][id]'),html::escapeHTML($w->id())).
[1659]259     '<p class="widget-name">'.form::field(array('w[void][0][order]'),2,3,0,'hide','',0,'title="'.__('order').'"').' '.$w->name().
[1650]260     ($w->desc() != '' ? ' <span class="form-note">'.__($w->desc()).'</span>' : '').'</p>'.
[1659]261     '<p class="manual-move remove-if-drag"><label class="classic">'.__('Append to:').'</label> '.
[2395]262     form::combo(array('addw['.$w->id().']'),$append_combo).
263     '<input type="submit" name="append['.$w->id().']" value="'.__('Add').'" /></p>'.
[1657]264     '<div class="widgetSettings hidden-if-drag">'.$w->formSettings('w[void][0]',$j).'</div>'.
[1648]265     '</li>';
[117]266     $j++;
[0]267}
268
269echo
[1648]270'</ul>'.
[2442]271'<p>'.$core->formNonce().'</p>'.
[1657]272'<p class="remove-if-drag"><input type="submit" name="append" value="'.__('Add widgets to sidebars').'" /></p>'.
[0]273'</form>';
274
275echo '<form id="sidebarsWidgets" action="'.$p_url.'" method="post">';
276# Nav sidebar
277echo
[1483]278'<div id="sidebarNav" class="widgets fieldset">'.
[1669]279sidebarWidgets('dndnav',__('Navigation sidebar'),$widgets_nav,'nav',$__default_widgets['nav'],$j);
280echo '</div>';
[0]281
282# Extra sidebar
283echo
[1483]284'<div id="sidebarExtra" class="widgets fieldset">'.
[1669]285sidebarWidgets('dndextra',__('Extra sidebar'),$widgets_extra,'extra',$__default_widgets['extra'],$j);
286echo '</div>';
[0]287
[665]288# Custom sidebar
289echo
[1483]290'<div id="sidebarCustom" class="widgets fieldset">'.
[1669]291sidebarWidgets('dndcustom',__('Custom sidebar'),$widgets_custom,'custom',$__default_widgets['custom'],$j);
292echo '</div>';
[665]293
[0]294echo
295'<p id="sidebarsControl">'.
296$core->formNonce().
[665]297'<input type="submit" name="wup" value="'.__('Update sidebars').'" /> '.
298'<input type="submit" class="reset" name="wreset" value="'.__('Reset sidebars').'" /></p>'.
[0]299'</form>';
300
301$widget_elements = new stdClass;
[2369]302$widget_elements->content = '<dl>';
[0]303foreach ($__widgets->elements() as $w)
304{
305     $widget_elements->content .=
306     '<dt><strong>'.html::escapeHTML($w->name()).'</strong> ('.
[954]307     __('Widget ID:').' <strong>'.html::escapeHTML($w->id()).'</strong>)'.
308     ($w->desc() != '' ? ' <span class="form-note">'.__($w->desc()).'</span>' : '').'</dt>'.
[0]309     '<dd>';
[2509]310
[0]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':
[902]323                         $s_type = __('boolean').", ".__('possible values:')." <code>0</code> ".__('or')." <code>1</code>";
[0]324                         break;
325                    case 'combo':
[902]326                         $s['options'] = array_map("literalNullString", $s['options']);
327                         $s_type = __('listitem').", ".__('possible values:')." <code>".implode('</code>, <code>',$s['options'])."</code>";
[0]328                         break;
329                    case 'text':
330                    case 'textarea':
331                    default:
[902]332                         $s_type = __('string');
[0]333                         break;
334               }
[2509]335
[0]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
[2369]348dcPage::helpBlock('widgets',$widget_elements);
[0]349
[117]350function sidebarWidgets($id,$title,$widgets,$pr,$default_widgets,&$j)
[0]351{
[1648]352     $res = '<h3>'.$title.'</h3>';
[2509]353
[0]354     if (!($widgets instanceof dcWidgets))
355     {
356          $widgets = $default_widgets;
357     }
[2509]358
[1648]359     $res .= '<ul id="'.$id.'" class="connected">';
[2509]360
[2418]361     $res .= '<li class="empty-widgets" '.(!$widgets->isEmpty() ? 'style="display: none;"' : '').'>'.__('No widget as far.').'</li>';
[2509]362
[0]363     $i = 0;
364     foreach ($widgets->elements() as $w)
365     {
[2442]366          $upDisabled = $i == 0 ? ' disabled" src="images/disabled_' : '" src="images/';
367          $downDisabled = $i == count($widgets->elements())-1 ? ' disabled" src="images/disabled_' : '" src="images/';
[2418]368          $altUp = $i == 0 ? ' alt=""' : ' alt="'.__('Up the widget').'"';
369          $altDown = $i == count($widgets->elements())-1 ? ' alt=""' : ' alt="'.__('Down the widget').'"';
[2778]370
[0]371          $iname = 'w['.$pr.']['.$i.']';
[2509]372
[0]373          $res .=
[1650]374          '<li>'.form::hidden(array($iname.'[id]'),html::escapeHTML($w->id())).
[2401]375          '<p class="widget-name">'.form::field(array($iname.'[order]'),2,3,(string) $i,'hidden','',0,'title="'.__('order').'"').
376          ' '.$w->name().
[2400]377          ($w->desc() != '' ? ' <span class="form-note">'.__($w->desc()).'</span>' : '').
[2401]378          '<span class="toolsWidget remove-if-drag">'.
[2418]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').'" />'.
[2401]382          '</span>'.
[2400]383          '<br class="clear"/></p>'.
[1657]384          '<div class="widgetSettings hidden-if-drag">'.$w->formSettings($iname,$j).'</div>'.
[1648]385          '</li>';
[2509]386
[0]387          $i++;
[117]388          $j++;
[0]389     }
[2509]390
[1648]391     $res .= '</ul>';
[2509]392
[2395]393     $res .= '<ul class="sortable-delete"'.($i > 0 ? '':' style="display: none;"').'><li class="sortable-delete-placeholder">'.
394               __('Drag widgets here to remove.').'</li></ul>';
[2509]395
[0]396     return $res;
397}
398?>
399</body>
[2509]400</html>
Note: See TracBrowser for help on using the repository browser.

Sites map