Dotclear

source: plugins/widgets/index.php @ 2401:06f7d3a90bd0

Revision 2401:06f7d3a90bd0, 13.3 KB checked in by Lepeltier kévin <kevin@…>, 12 years ago (diff)

widgets : fléche pour réordonner sans drag&drop

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<html>
17<head>
18  <title><?php echo __('Widgets'); ?></title>
19  <style type="text/css">
20  <?php echo file_get_contents(dirname(__FILE__).'/style.css'); ?>
21  </style>
22  <?php
23          echo
24               dcPage::jsLoad('js/jquery/jquery-ui.custom.js').
25               dcPage::jsLoad('index.php?pf=widgets/widgets.js');
26  ?>
27  <?php 
28     $core->auth->user_prefs->addWorkspace('accessibility'); 
29     $user_dm_nodragdrop = $core->auth->user_prefs->accessibility->nodragdrop;
30  ?>
31  <?php if (!$user_dm_nodragdrop) : ?>
32  <script type="text/javascript" src="index.php?pf=widgets/dragdrop.js"></script>
33  <?php endif; ?>
34  <script type="text/javascript">
35  //<![CDATA[
36  <?php echo dcPage::jsVar('dotclear.msg.confirm_widgets_reset',
37     __('Are you sure you want to reset sidebars?')); ?>
38  //]]>
39  </script>
40  <?php echo(dcPage::jsConfirmClose('sidebarsWidgets')); ?>
41</head>
42<body>
43<?php
44# Loading navigation, extra widgets and custom widgets
45$widgets_nav = null;
46if ($core->blog->settings->widgets->widgets_nav) {
47     $widgets_nav = dcWidgets::load($core->blog->settings->widgets->widgets_nav);
48}
49$widgets_extra = null;
50if ($core->blog->settings->widgets->widgets_extra) {
51     $widgets_extra = dcWidgets::load($core->blog->settings->widgets->widgets_extra);
52}
53$widgets_custom = null;
54if ($core->blog->settings->widgets->widgets_custom) {
55     $widgets_custom = dcWidgets::load($core->blog->settings->widgets->widgets_custom);
56}
57
58$append_combo = array(
59     '-' => 0,
60     __('navigation') => 'nav',
61     __('extra') => 'extra',
62     __('custom') => 'custom'
63);
64
65function literalNullString($v)
66{
67     if ($v == '') {
68          return '&lt;'.__('empty string').'&gt;';
69     }
70     return $v;
71}
72
73# Adding widgets to sidebars
74if (!empty($_POST['append']) && is_array($_POST['addw']))
75{
76     # Filter selection
77     $addw = array();
78     foreach ($_POST['addw'] as $k => $v) {
79          if (($v == 'extra' || $v == 'nav' || $v == 'custom') && $__widgets->{$k} !== null ) {
80               $addw[$k] = $v;
81          }
82     }
83         
84     # Append 1 widget
85     $wid = false;
86     if( gettype($_POST['append']) == 'array' && count($_POST['append']) == 1 ) {
87          $wid = array_keys($_POST['append']);
88          $wid = $wid[0];
89     }
90     
91     # Append widgets
92     if (!empty($addw))
93     {
94          if (!($widgets_nav instanceof dcWidgets)) {
95               $widgets_nav = new dcWidgets;
96          }
97          if (!($widgets_extra instanceof dcWidgets)) {
98               $widgets_extra = new dcWidgets();
99          }
100          if (!($widgets_custom instanceof dcWidgets)) {
101               $widgets_custom = new dcWidgets();
102          }
103         
104          foreach ($addw as $k => $v)
105          {
106               if( !$wid || $wid == $k )
107               {
108                    switch ($v) {
109                         case 'nav':
110                              $widgets_nav->append($__widgets->{$k});
111                              break;
112                         case 'extra':
113                              $widgets_extra->append($__widgets->{$k});
114                              break;
115                         case 'custom':
116                              $widgets_custom->append($__widgets->{$k});
117                              break;
118                    }
119               }
120               
121          }
122         
123          try {
124               $core->blog->settings->addNamespace('widgets');
125               $core->blog->settings->widgets->put('widgets_nav',$widgets_nav->store());
126               $core->blog->settings->widgets->put('widgets_extra',$widgets_extra->store());
127               $core->blog->settings->widgets->put('widgets_custom',$widgets_custom->store());
128               $core->blog->triggerBlog();
129               http::redirect($p_url);
130          } catch (Exception $e) {
131               $core->error->add($e->getMessage());
132          }
133     }
134}
135
136# Removing ?
137$removing = false;
138if ( isset($_POST['w']) && is_array($_POST['w']) ) {
139     foreach ($_POST['w'] as $nsid => $nsw) {
140          foreach ($nsw as $i => $v) {
141               if (!empty($v['_rem'])) {
142                    $removing = true;
143                    break 2;
144               }
145          }
146     }
147}
148
149# Move ?
150$move = false;
151if ( isset($_POST['w']) && is_array($_POST['w']) ) {
152     foreach ($_POST['w'] as $nsid => $nsw) {
153          foreach ($nsw as $i => $v) {
154               if (!empty($v['down'])) {
155                    $oldorder = $_POST['w'][$nsid][$i]['order'];
156                    $neworder = $oldorder + 1;
157                    if( isset($_POST['w'][$nsid][$neworder]) ) {
158                         $_POST['w'][$nsid][$i]['order'] = $neworder;
159                         $_POST['w'][$nsid][$neworder]['order'] = $oldorder;
160                         $move = true;
161                    }
162               }
163               if (!empty($v['up'])) {
164                    $oldorder = $_POST['w'][$nsid][$i]['order'];
165                    $neworder = $oldorder - 1;
166                    if( isset($_POST['w'][$nsid][$neworder]) ) {
167                         $_POST['w'][$nsid][$i]['order'] = $neworder;
168                         $_POST['w'][$nsid][$neworder]['order'] = $oldorder;
169                         $move = true;
170                    }
171               }
172          }
173     }
174}
175
176# Update sidebars
177if (!empty($_POST['wup']) || $removing || $move )
178{
179     if (!isset($_POST['w']) || !is_array($_POST['w'])) {
180          $_POST['w'] = array();
181     }
182     
183     try
184     {
185         
186          # Removing mark as _rem widgets
187          foreach ($_POST['w'] as $nsid => $nsw) {
188               foreach ($nsw as $i => $v) {
189                    if (!empty($v['_rem'])) {
190                         unset($_POST['w'][$nsid][$i]);
191                         continue;
192                    }
193               }
194          }
195         
196          if (!isset($_POST['w']['nav'])) {
197               $_POST['w']['nav'] = array();
198          }
199          if (!isset($_POST['w']['extra'])) {
200               $_POST['w']['extra'] = array();
201          }
202          if (!isset($_POST['w']['custom'])) {
203               $_POST['w']['custom'] = array();
204          }
205         
206          $widgets_nav = dcWidgets::loadArray($_POST['w']['nav'],$__widgets);
207          $widgets_extra = dcWidgets::loadArray($_POST['w']['extra'],$__widgets);
208          $widgets_custom = dcWidgets::loadArray($_POST['w']['custom'],$__widgets);
209         
210          $core->blog->settings->addNamespace('widgets');
211          $core->blog->settings->widgets->put('widgets_nav',$widgets_nav->store());
212          $core->blog->settings->widgets->put('widgets_extra',$widgets_extra->store());
213          $core->blog->settings->widgets->put('widgets_custom',$widgets_custom->store());
214          $core->blog->triggerBlog();
215         
216          http::redirect($p_url);
217     }
218     catch (Exception $e)
219     {
220          $core->error->add($e->getMessage());
221     }
222}
223elseif (!empty($_POST['wreset']))
224{
225     try
226     {
227          $core->blog->settings->addNamespace('widgets');
228          $core->blog->settings->widgets->put('widgets_nav','');
229          $core->blog->settings->widgets->put('widgets_extra','');
230          $core->blog->settings->widgets->put('widgets_custom','');
231          $core->blog->triggerBlog();
232         
233          http::redirect($p_url);
234     }
235     catch (Exception $e)
236     {
237          $core->error->add($e->getMessage());
238     }
239}
240/*?>
241<html>
242<head>
243  <title><?php echo __('Widgets'); ?></title>
244  <style type="text/css">
245  <?php echo file_get_contents(dirname(__FILE__).'/style.css'); ?>
246  </style>
247  <?php
248          echo
249               dcPage::jsLoad('js/jquery/jquery-ui.custom.js').
250               dcPage::jsLoad('index.php?pf=widgets/widgets.js');
251  ?>
252  <?php
253     $core->auth->user_prefs->addWorkspace('accessibility');
254     $user_dm_nodragdrop = $core->auth->user_prefs->accessibility->nodragdrop;
255  ?>
256  <?php if (!$user_dm_nodragdrop) : ?>
257  <script type="text/javascript" src="index.php?pf=widgets/dragdrop.js"></script>
258  <?php endif; ?>
259  <script type="text/javascript">
260  //<![CDATA[
261  <?php echo dcPage::jsVar('dotclear.msg.confirm_widgets_reset',
262     __('Are you sure you want to reset sidebars?')); ?>
263  //]]>
264  </script>
265  <?php echo(dcPage::jsConfirmClose('sidebarsWidgets')); ?>
266</head>
267<body>
268<?php*/
269echo dcPage::breadcrumb(
270     array(
271          html::escapeHTML($core->blog->name) => '',
272          __('Widgets') => ''
273     )).
274     dcPage::notices();
275
276# All widgets
277echo
278'<form id="listWidgets" action="'.$p_url.'" method="post"  class="widgets">'.
279'<h3>'.__('Available widgets').'</h3>'.
280'<p>'.__('Drag widgets from this list to one of the sidebars, for add.').'</p>'.
281'<ul id="widgets-ref">';
282
283$j = 0;
284foreach ($__widgets->elements(true) as $w) {
285     echo
286     '<li>'.form::hidden(array('w[void][0][id]'),html::escapeHTML($w->id())).
287     '<p class="widget-name">'.form::field(array('w[void][0][order]'),2,3,0,'hide','',0,'title="'.__('order').'"').' '.$w->name().
288     ($w->desc() != '' ? ' <span class="form-note">'.__($w->desc()).'</span>' : '').'</p>'.
289     '<p class="manual-move remove-if-drag"><label class="classic">'.__('Append to:').'</label> '.
290     form::combo(array('addw['.$w->id().']'),$append_combo).
291     '<input type="submit" name="append['.$w->id().']" value="'.__('Add').'" /></p>'.
292     '<div class="widgetSettings hidden-if-drag">'.$w->formSettings('w[void][0]',$j).'</div>'.
293     '</li>';
294     $j++;
295}
296
297echo
298'</ul>'.
299$core->formNonce().
300'<p class="remove-if-drag"><input type="submit" name="append" value="'.__('Add widgets to sidebars').'" /></p>'.
301'</form>';
302
303echo '<form id="sidebarsWidgets" action="'.$p_url.'" method="post">';
304# Nav sidebar
305echo
306'<div id="sidebarNav" class="widgets fieldset">'.
307sidebarWidgets('dndnav',__('Navigation sidebar'),$widgets_nav,'nav',$__default_widgets['nav'],$j);
308echo '</div>';
309
310# Extra sidebar
311echo
312'<div id="sidebarExtra" class="widgets fieldset">'.
313sidebarWidgets('dndextra',__('Extra sidebar'),$widgets_extra,'extra',$__default_widgets['extra'],$j);
314echo '</div>';
315
316# Custom sidebar
317echo
318'<div id="sidebarCustom" class="widgets fieldset">'.
319sidebarWidgets('dndcustom',__('Custom sidebar'),$widgets_custom,'custom',$__default_widgets['custom'],$j);
320echo '</div>';
321
322echo
323'<p id="sidebarsControl">'.
324$core->formNonce().
325'<input type="submit" name="wup" value="'.__('Update sidebars').'" /> '.
326'<input type="submit" class="reset" name="wreset" value="'.__('Reset sidebars').'" /></p>'.
327'</form>';
328
329$widget_elements = new stdClass;
330$widget_elements->content =
331'<h4>'.__('Use of widgets').'</h4>'.
332'<p>'.__('Widgets may be used to add various blocks of content to be displayed on your public pages. To add a widget, '.
333'drag it from the Available widgets list on the left to one of the sidebars on the right of this page. You can order '.
334'your widgets in a sidebar by dragging them up or down. You must update sidebars to apply your changes.').'</p>'.
335'<p>'.__('Once included in a sidebar, widgets have configuration options that you can reach by clicking on the arrow next '.
336'to their name.').'</p>'.
337'<p>'.__('Reset sidebars to get back to default widgets installation.').'</p>'.
338'<h4>'.__('Widget templates tags').'</h4>'.
339'<div id="widgets-tpl">'.
340'<p>'.__('If you are allowed to edit your theme templates, you can directly add widgets as '.
341'templates tags, with their own configuration.').'</p>'.
342'<p>'.__('To add a widget in your template, you need to write code like this:').'</p>'.
343'<pre>&lt;tpl:Widget id="<strong>'.__('Widget ID').'</strong>"&gt;
344  &lt;setting name="<strong>'.__('Setting name').'</strong>"&gt;<strong>'.__('Setting value').'</strong>&lt;/setting&gt;
345  ...
346&lt;/tpl:Widget&gt;</pre>'.
347'<p>'.__('Here are the following available widgets for your blog:').'</p>';
348
349$widget_elements->content .= '<dl>';
350foreach ($__widgets->elements() as $w)
351{
352     $widget_elements->content .=
353     '<dt><strong>'.html::escapeHTML($w->name()).'</strong> ('.
354     __('Widget ID:').' <strong>'.html::escapeHTML($w->id()).'</strong>)'.
355     ($w->desc() != '' ? ' <span class="form-note">'.__($w->desc()).'</span>' : '').'</dt>'.
356     '<dd>';
357     
358     $w_settings = $w->settings();
359     if (empty($w_settings))
360     {
361          $widget_elements->content .= '<p>'.__('No setting for this widget').'</p>';
362     }
363     else
364     {
365          $widget_elements->content .= '<ul>';
366          foreach ($w->settings() as $n => $s)
367          {
368               switch ($s['type']) {
369                    case 'check':
370                         $s_type = __('boolean').", ".__('possible values:')." <code>0</code> ".__('or')." <code>1</code>";
371                         break;
372                    case 'combo':
373                         $s['options'] = array_map("literalNullString", $s['options']);
374                         $s_type = __('listitem').", ".__('possible values:')." <code>".implode('</code>, <code>',$s['options'])."</code>";
375                         break;
376                    case 'text':
377                    case 'textarea':
378                    default:
379                         $s_type = __('string');
380                         break;
381               }
382               
383               $widget_elements->content .=
384               '<li>'.
385               __('Setting name:').' <strong>'.html::escapeHTML($n).'</strong>'.
386               ' ('.$s_type.')'.
387               '</li>';
388          }
389          $widget_elements->content .= '</ul>';
390     }
391     $widget_elements->content .= '</dd>';
392}
393$widget_elements->content .= '</dl></div>';
394
395dcPage::helpBlock($widget_elements);
396
397function sidebarWidgets($id,$title,$widgets,$pr,$default_widgets,&$j)
398{
399     $res = '<h3>'.$title.'</h3>';
400     
401     if (!($widgets instanceof dcWidgets))
402     {
403          $widgets = $default_widgets;
404     }
405     
406     $res .= '<ul id="'.$id.'" class="connected">';
407     
408     $res .= '<li class="empty-widgets" '.(!$widgets->isEmpty() ? 'style="display: none;"' : '').'>'.__('No widget for now.').'</li>';
409     
410     $i = 0;
411     foreach ($widgets->elements() as $w)
412     {
413          $upDisabled = $i == 0 ? '" disabled="" src="images/disabled_' : '" src="images/';
414          $downDisabled = $i == count($widgets->elements())-1 ? '" disabled="" src="images/disabled_' : '" src="images/';
415         
416          $iname = 'w['.$pr.']['.$i.']';
417         
418          $res .=
419          '<li>'.form::hidden(array($iname.'[id]'),html::escapeHTML($w->id())).
420          '<p class="widget-name">'.form::field(array($iname.'[order]'),2,3,(string) $i,'hidden','',0,'title="'.__('order').'"').
421          ' '.$w->name().
422          ($w->desc() != '' ? ' <span class="form-note">'.__($w->desc()).'</span>' : '').
423          '<span class="toolsWidget remove-if-drag">'.
424          '<input type="image" class="upWidget'.$upDisabled.'up.png" name="'.$iname.'[up]" value="'.__('Up the widget').'" />'.
425          '<input type="image" class="downWidget'.$downDisabled.'down.png" name="'.$iname.'[down]" value="'.__('Down the widget').'" />'.' '.
426          '<input type="image" class="removeWidget" src="images/trash.png" name="'.$iname.'[_rem]" value="'.__('Remove widget').'" />'.
427          '</span>'.
428          '<br class="clear"/></p>'.
429          '<div class="widgetSettings hidden-if-drag">'.$w->formSettings($iname,$j).'</div>'.
430          '</li>';
431         
432          $i++;
433          $j++;
434     }
435     
436     $res .= '</ul>';
437     
438     $res .= '<ul class="sortable-delete"'.($i > 0 ? '':' style="display: none;"').'><li class="sortable-delete-placeholder">'.
439               __('Drag widgets here to remove.').'</li></ul>';
440     
441     return $res;
442}
443?>
444</body>
445</html>
Note: See TracBrowser for help on using the repository browser.

Sites map