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