Dotclear

source: plugins/widgets/_public.php @ 2772:df84ff8c6261

Revision 2772:df84ff8c6261, 4.9 KB checked in by franck <carnet.franck.paul@…>, 11 years ago (diff)

Add tpl:IfWidgets template block -> return true if at least one widget will be displayed

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_RC_PATH')) { return; }
13
14include dirname(__FILE__).'/_default_widgets.php';
15require_once dirname(__FILE__).'/_widgets_functions.php';
16
17$core->tpl->addValue('Widgets',array('publicWidgets','tplWidgets'));
18$core->tpl->addBlock('Widget',array('publicWidgets','tplWidget'));
19$core->tpl->addBlock('IfWidgets',array('publicWidgets','tplIfWidgets'));
20
21class publicWidgets
22{
23     public static function tplWidgets($attr)
24     {
25          $type = isset($attr['type']) ? $attr['type'] : '';
26
27          # widgets to disable
28          $disable = isset($attr['disable']) ? trim($attr['disable']) : '';
29
30          if ($type == '') {
31               $res = "publicWidgets::widgetsHandler('nav','".addslashes($disable)."');"."\n".
32                    "   publicWidgets::widgetsHandler('extra','".addslashes($disable)."');"."\n".
33                    "   publicWidgets::widgetsHandler('custom','".addslashes($disable)."');"."\n";
34          } else {
35               if (!in_array($type, array('nav','extra','custom'))) {
36                    $type = 'nav';
37               }
38               $res = "publicWidgets::widgetsHandler('".addslashes($type)."','".addslashes($disable)."');";
39          }
40          return '<?php '.$res.' ?>';
41     }
42
43     public static function widgetsHandler($type,$disable='')
44     {
45          $wtype = 'widgets_'.$type;
46          $GLOBALS['core']->blog->settings->addNameSpace('widgets');
47          $widgets = $GLOBALS['core']->blog->settings->widgets->{$wtype};
48
49          if (!$widgets) { // If widgets value is empty, get defaults
50               $widgets = self::defaultWidgets($type);
51          } else { // Otherwise, load widgets
52               $widgets = dcWidgets::load($widgets);
53          }
54
55          if ($widgets->isEmpty()) { // Widgets are empty, don't show anything
56               return;
57          }
58
59          $disable = preg_split('/\s*,\s*/',$disable,-1,PREG_SPLIT_NO_EMPTY);
60          $disable = array_flip($disable);
61
62          foreach ($widgets->elements() as $k => $w)
63          {
64               if (isset($disable[$w->id()])) {
65                    continue;
66               }
67               echo $w->call($k);
68          }
69     }
70
71     public static function tplIfWidgets($attr,$content)
72     {
73          $type = isset($attr['type']) ? $attr['type'] : '';
74
75          # widgets to disable
76          $disable = isset($attr['disable']) ? trim($attr['disable']) : '';
77
78          if ($type == '') {
79               $res = "publicWidgets::ifWidgetsHandler('nav','".addslashes($disable)."') &&"."\n".
80                    "   publicWidgets::ifWidgetsHandler('extra','".addslashes($disable)."') &&"."\n".
81                    "   publicWidgets::ifWidgetsHandler('custom','".addslashes($disable)."')"."\n";
82          } else {
83               if (!in_array($type, array('nav','extra','custom'))) {
84                    $type = 'nav';
85               }
86               $res = "publicWidgets::ifWidgetsHandler('".addslashes($type)."','".addslashes($disable)."')";
87          }
88          return '<?php if('.$res.') : ?>'.$content.'<?php endif; ?>';
89     }
90
91     public static function ifWidgetsHandler($type,$disable='')
92     {
93          $wtype = 'widgets_'.$type;
94          $GLOBALS['core']->blog->settings->addNameSpace('widgets');
95          $widgets = $GLOBALS['core']->blog->settings->widgets->{$wtype};
96
97          if (!$widgets) { // If widgets value is empty, get defaults
98               $widgets = self::defaultWidgets($type);
99          } else { // Otherwise, load widgets
100               $widgets = dcWidgets::load($widgets);
101          }
102
103          return (!$widgets->isEmpty());
104     }
105
106     private static function defaultWidgets($type)
107     {
108          $widgets = new dcWidgets();
109          $w = new dcWidgets();
110
111          if (isset($GLOBALS['__default_widgets'][$type])) {
112               $w = $GLOBALS['__default_widgets'][$type];
113          }
114
115          return $w;
116     }
117
118     public static function tplWidget($attr,$content)
119     {
120          if (!isset($attr['id']) || !($GLOBALS['__widgets']->{$attr['id']} instanceof dcWidget)) {
121               return;
122          }
123
124          # We change tpl:lang syntax, we need it
125          $content = preg_replace('/\{\{tpl:lang\s+(.*?)\}\}/msu','{tpl:lang $1}',$content);
126
127          # We remove every {{tpl:
128          $content = preg_replace('/\{\{tpl:.*?\}\}/msu','',$content);
129
130          return
131          "<?php publicWidgets::widgetHandler('".addslashes($attr['id'])."','".str_replace("'","\\'",$content)."'); ?>";
132     }
133
134     public static function widgetHandler($id,$xml)
135     {
136          $widgets =& $GLOBALS['__widgets'];
137
138          if (!($widgets->{$id} instanceof dcWidget)) {
139               return;
140          }
141
142          $xml = '<?xml version="1.0" encoding="utf-8" ?><widget>'.$xml.'</widget>';
143          $xml = @simplexml_load_string($xml);
144          if (!($xml instanceof SimpleXMLElement)) {
145               echo "Invalid widget XML fragment";
146               return;
147          }
148
149          $w = clone $widgets->{$id};
150
151          foreach ($xml->setting as $e)
152          {
153               if (empty($e['name'])) {
154                    continue;
155               }
156
157               $setting = (string) $e['name'];
158               if (count($e->children())>0) {
159                    $text = preg_replace('#^<setting[^>]*>(.*)</setting>$#msu','\1', (string)$e->asXML());
160               } else {
161                    $text=$e;
162               }
163               $w->{$setting} = preg_replace_callback('/\{tpl:lang (.*?)\}/msu',array('self','widgetL10nHandler'),$text);
164          }
165
166          echo $w->call(0);
167     }
168
169     private static function widgetL10nHandler($m)
170     {
171          return __($m[1]);
172     }
173}
Note: See TracBrowser for help on using the repository browser.

Sites map