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