Dotclear

source: plugins/widgets/index.php @ 2495:dc8c7974ff0b

Revision 2495:dc8c7974ff0b, 12.5 KB checked in by franck <carnet.franck.paul@…>, 12 years ago (diff)

Add touch capacity for drag'n'drop gestures on screen, addresses #1802

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          http::redirect($p_url);
187     }
188     catch (Exception $e)
189     {
190          $core->error->add($e->getMessage());
191     }
192}
193elseif (!empty($_POST['wreset']))
194{
195     try
196     {
197          $core->blog->settings->addNamespace('widgets');
198          $core->blog->settings->widgets->put('widgets_nav','');
199          $core->blog->settings->widgets->put('widgets_extra','');
200          $core->blog->settings->widgets->put('widgets_custom','');
201          $core->blog->triggerBlog();
202
203          http::redirect($p_url);
204     }
205     catch (Exception $e)
206     {
207          $core->error->add($e->getMessage());
208     }
209}
210?>
211<html>
212<head>
213  <title><?php echo __('Widgets'); ?></title>
214  <style type="text/css">
215  <?php echo file_get_contents(dirname(__FILE__).'/style.css'); ?>
216  </style>
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('index.php?pf=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  <script type="text/javascript" src="index.php?pf=widgets/dragdrop.js"></script>
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(dcPage::jsConfirmClose('sidebarsWidgets')); ?>
237</head>
238<body>
239<?php
240echo dcPage::breadcrumb(
241     array(
242          html::escapeHTML($core->blog->name) => '',
243          __('Widgets') => ''
244     )).
245     dcPage::notices();
246
247# All widgets
248echo
249'<form id="listWidgets" action="'.$p_url.'" method="post"  class="widgets">'.
250'<h3>'.__('Available widgets').'</h3>'.
251'<p>'.__('Drag widgets from this list to one of the sidebars, for add.').'</p>'.
252'<ul id="widgets-ref">';
253
254$j = 0;
255foreach ($__widgets->elements(true) as $w) {
256     echo
257     '<li>'.form::hidden(array('w[void][0][id]'),html::escapeHTML($w->id())).
258     '<p class="widget-name">'.form::field(array('w[void][0][order]'),2,3,0,'hide','',0,'title="'.__('order').'"').' '.$w->name().
259     ($w->desc() != '' ? ' <span class="form-note">'.__($w->desc()).'</span>' : '').'</p>'.
260     '<p class="manual-move remove-if-drag"><label class="classic">'.__('Append to:').'</label> '.
261     form::combo(array('addw['.$w->id().']'),$append_combo).
262     '<input type="submit" name="append['.$w->id().']" value="'.__('Add').'" /></p>'.
263     '<div class="widgetSettings hidden-if-drag">'.$w->formSettings('w[void][0]',$j).'</div>'.
264     '</li>';
265     $j++;
266}
267
268echo
269'</ul>'.
270$core->formNonce().
271'<p class="remove-if-drag"><input type="submit" name="append" value="'.__('Add widgets to sidebars').'" /></p>'.
272'</form>';
273
274echo '<form id="sidebarsWidgets" action="'.$p_url.'" method="post">';
275# Nav sidebar
276echo
277'<div id="sidebarNav" class="widgets fieldset">'.
278sidebarWidgets('dndnav',__('Navigation sidebar'),$widgets_nav,'nav',$__default_widgets['nav'],$j);
279echo '</div>';
280
281# Extra sidebar
282echo
283'<div id="sidebarExtra" class="widgets fieldset">'.
284sidebarWidgets('dndextra',__('Extra sidebar'),$widgets_extra,'extra',$__default_widgets['extra'],$j);
285echo '</div>';
286
287# Custom sidebar
288echo
289'<div id="sidebarCustom" class="widgets fieldset">'.
290sidebarWidgets('dndcustom',__('Custom sidebar'),$widgets_custom,'custom',$__default_widgets['custom'],$j);
291echo '</div>';
292
293echo
294'<p id="sidebarsControl">'.
295$core->formNonce().
296'<input type="submit" name="wup" value="'.__('Update sidebars').'" /> '.
297'<input type="submit" class="reset" name="wreset" value="'.__('Reset sidebars').'" /></p>'.
298'</form>';
299
300$widget_elements = new stdClass;
301$widget_elements->content =
302'<h4>'.__('Use of widgets').'</h4>'.
303'<p>'.__('Widgets may be used to add various blocks of content to be displayed on your public pages. To add a widget, '.
304'drag it from the Available widgets list on the left to one of the sidebars on the right of this page. You can order '.
305'your widgets in a sidebar by dragging them up or down. You must update sidebars to apply your changes.').'</p>'.
306'<p>'.__('Once included in a sidebar, widgets have configuration options that you can reach by clicking on the arrow next '.
307'to their name.').'</p>'.
308'<p>'.__('Reset sidebars to get back to default widgets installation.').'</p>'.
309'<h4>'.__('Widget templates tags').'</h4>'.
310'<div id="widgets-tpl">'.
311'<p>'.__('If you are allowed to edit your theme templates, you can directly add widgets as '.
312'templates tags, with their own configuration.').'</p>'.
313'<p>'.__('To add a widget in your template, you need to write code like this:').'</p>'.
314'<pre>&lt;tpl:Widget id="<strong>'.__('Widget ID').'</strong>"&gt;
315  &lt;setting name="<strong>'.__('Setting name').'</strong>"&gt;<strong>'.__('Setting value').'</strong>&lt;/setting&gt;
316  ...
317&lt;/tpl:Widget&gt;</pre>'.
318'<p>'.__('Here are the following available widgets for your blog:').'</p>';
319
320$widget_elements->content .= '<dl>';
321foreach ($__widgets->elements() as $w)
322{
323     $widget_elements->content .=
324     '<dt><strong>'.html::escapeHTML($w->name()).'</strong> ('.
325     __('Widget ID:').' <strong>'.html::escapeHTML($w->id()).'</strong>)'.
326     ($w->desc() != '' ? ' <span class="form-note">'.__($w->desc()).'</span>' : '').'</dt>'.
327     '<dd>';
328
329     $w_settings = $w->settings();
330     if (empty($w_settings))
331     {
332          $widget_elements->content .= '<p>'.__('No setting for this widget').'</p>';
333     }
334     else
335     {
336          $widget_elements->content .= '<ul>';
337          foreach ($w->settings() as $n => $s)
338          {
339               switch ($s['type']) {
340                    case 'check':
341                         $s_type = __('boolean').", ".__('possible values:')." <code>0</code> ".__('or')." <code>1</code>";
342                         break;
343                    case 'combo':
344                         $s['options'] = array_map("literalNullString", $s['options']);
345                         $s_type = __('listitem').", ".__('possible values:')." <code>".implode('</code>, <code>',$s['options'])."</code>";
346                         break;
347                    case 'text':
348                    case 'textarea':
349                    default:
350                         $s_type = __('string');
351                         break;
352               }
353
354               $widget_elements->content .=
355               '<li>'.
356               __('Setting name:').' <strong>'.html::escapeHTML($n).'</strong>'.
357               ' ('.$s_type.')'.
358               '</li>';
359          }
360          $widget_elements->content .= '</ul>';
361     }
362     $widget_elements->content .= '</dd>';
363}
364$widget_elements->content .= '</dl></div>';
365
366dcPage::helpBlock($widget_elements);
367
368function sidebarWidgets($id,$title,$widgets,$pr,$default_widgets,&$j)
369{
370     $res = '<h3>'.$title.'</h3>';
371
372     if (!($widgets instanceof dcWidgets))
373     {
374          $widgets = $default_widgets;
375     }
376
377     $res .= '<ul id="'.$id.'" class="connected">';
378
379     $res .= '<li class="empty-widgets" '.(!$widgets->isEmpty() ? 'style="display: none;"' : '').'>'.__('No widget for now.').'</li>';
380
381     $i = 0;
382     foreach ($widgets->elements() as $w)
383     {
384          $upDisabled = $i == 0 ? '" disabled="" src="images/disabled_' : '" src="images/';
385          $downDisabled = $i == count($widgets->elements())-1 ? '" disabled="" src="images/disabled_' : '" src="images/';
386
387          $iname = 'w['.$pr.']['.$i.']';
388
389          $res .=
390          '<li>'.form::hidden(array($iname.'[id]'),html::escapeHTML($w->id())).
391          '<p class="widget-name">'.form::field(array($iname.'[order]'),2,3,(string) $i,'hidden','',0,'title="'.__('order').'"').
392          ' '.$w->name().
393          ($w->desc() != '' ? ' <span class="form-note">'.__($w->desc()).'</span>' : '').
394          '<span class="toolsWidget remove-if-drag">'.
395          '<input type="image" class="upWidget'.$upDisabled.'up.png" name="'.$iname.'[_up]" value="'.__('Up the widget').'" />'.
396          '<input type="image" class="downWidget'.$downDisabled.'down.png" name="'.$iname.'[_down]" value="'.__('Down the widget').'" />'.' '.
397          '<input type="image" class="removeWidget" src="images/trash.png" name="'.$iname.'[_rem]" value="'.__('Remove widget').'" />'.
398          '</span>'.
399          '<br class="clear"/></p>'.
400          '<div class="widgetSettings hidden-if-drag">'.$w->formSettings($iname,$j).'</div>'.
401          '</li>';
402
403          $i++;
404          $j++;
405     }
406
407     $res .= '</ul>';
408
409     $res .= '<ul class="sortable-delete"'.($i > 0 ? '':' style="display: none;"').'><li class="sortable-delete-placeholder">'.
410               __('Drag widgets here to remove.').'</li></ul>';
411
412     return $res;
413}
414?>
415</body>
416</html>
Note: See TracBrowser for help on using the repository browser.

Sites map