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

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_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'));
[2772]19$core->tpl->addBlock('IfWidgets',array('publicWidgets','tplIfWidgets'));
[0]20
21class publicWidgets
22{
23     public static function tplWidgets($attr)
24     {
[936]25          $type = isset($attr['type']) ? $attr['type'] : '';
26
[0]27          # widgets to disable
28          $disable = isset($attr['disable']) ? trim($attr['disable']) : '';
[2566]29
[936]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.' ?>';
[0]41     }
[2566]42
[0]43     public static function widgetsHandler($type,$disable='')
44     {
45          $wtype = 'widgets_'.$type;
[671]46          $GLOBALS['core']->blog->settings->addNameSpace('widgets');
[0]47          $widgets = $GLOBALS['core']->blog->settings->widgets->{$wtype};
[2566]48
[0]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          }
[2566]54
[0]55          if ($widgets->isEmpty()) { // Widgets are empty, don't show anything
56               return;
57          }
[2566]58
[0]59          $disable = preg_split('/\s*,\s*/',$disable,-1,PREG_SPLIT_NO_EMPTY);
60          $disable = array_flip($disable);
[2566]61
[0]62          foreach ($widgets->elements() as $k => $w)
63          {
64               if (isset($disable[$w->id()])) {
65                    continue;
66               }
67               echo $w->call($k);
68          }
69     }
[2566]70
[2772]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
[0]106     private static function defaultWidgets($type)
107     {
108          $widgets = new dcWidgets();
109          $w = new dcWidgets();
[2566]110
[0]111          if (isset($GLOBALS['__default_widgets'][$type])) {
112               $w = $GLOBALS['__default_widgets'][$type];
113          }
[2566]114
[0]115          return $w;
116     }
[2566]117
[0]118     public static function tplWidget($attr,$content)
119     {
120          if (!isset($attr['id']) || !($GLOBALS['__widgets']->{$attr['id']} instanceof dcWidget)) {
121               return;
122          }
[2566]123
[0]124          # We change tpl:lang syntax, we need it
125          $content = preg_replace('/\{\{tpl:lang\s+(.*?)\}\}/msu','{tpl:lang $1}',$content);
[2566]126
[0]127          # We remove every {{tpl:
128          $content = preg_replace('/\{\{tpl:.*?\}\}/msu','',$content);
[2566]129
[0]130          return
131          "<?php publicWidgets::widgetHandler('".addslashes($attr['id'])."','".str_replace("'","\\'",$content)."'); ?>";
132     }
[2566]133
[0]134     public static function widgetHandler($id,$xml)
135     {
136          $widgets =& $GLOBALS['__widgets'];
[2566]137
[0]138          if (!($widgets->{$id} instanceof dcWidget)) {
139               return;
140          }
[2566]141
[0]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          }
[2566]148
[0]149          $w = clone $widgets->{$id};
[2566]150
[0]151          foreach ($xml->setting as $e)
152          {
153               if (empty($e['name'])) {
154                    continue;
155               }
[2566]156
[0]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          }
[2566]165
[0]166          echo $w->call(0);
167     }
[2566]168
[0]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