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