| 1 | <?php | 
|---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- | 
|---|
| 3 | # | 
|---|
| 4 | # This file is part of Dotclear 2. | 
|---|
| 5 | # | 
|---|
| 6 | # Copyright (c) 2003-2011 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 | /** | 
|---|
| 15 |  * @ingroup DC_CORE | 
|---|
| 16 |  * @brief Template environment. | 
|---|
| 17 |  * | 
|---|
| 18 |  * This class provides same Twig environment for both admin and public side.  | 
|---|
| 19 |  * It adds methods for backwards compatibility with old template engine. | 
|---|
| 20 |  * | 
|---|
| 21 |  * default-templates path must be added from admin|public/prepend.php with: | 
|---|
| 22 |  * $core->tpl->getLoader()->addPath('PATH_TO/default-templates'); | 
|---|
| 23 |  * Selected theme path must be added with: | 
|---|
| 24 |  * $core->tpl->getLoader()->prependPath('PATH_TO/MY_THEME'); | 
|---|
| 25 |  */ | 
|---|
| 26 | class dcTemplate extends Twig_Environment | 
|---|
| 27 | { | 
|---|
| 28 |      protected $dc_blocks = array(); | 
|---|
| 29 |      protected $dc_values = array(); | 
|---|
| 30 |       | 
|---|
| 31 |      private $core; | 
|---|
| 32 |       | 
|---|
| 33 |      /** | 
|---|
| 34 |       * Create template environment (Twig_Environment instance). | 
|---|
| 35 |       *  | 
|---|
| 36 |       * We keep same signature as the old dcTemplate class. | 
|---|
| 37 |       */ | 
|---|
| 38 |      public function __construct($cache_dir,$self_name,$core) | 
|---|
| 39 |      { | 
|---|
| 40 |           $this->core =& $core; | 
|---|
| 41 |            | 
|---|
| 42 |           # Check cache directory | 
|---|
| 43 |           $cache_dir = path::real($cache_dir.'/twtpl',false); | 
|---|
| 44 |           if (!is_dir($cache_dir)) { | 
|---|
| 45 |                try { | 
|---|
| 46 |                     files::makeDir($cache_dir); | 
|---|
| 47 |                } catch (Exception $e) { | 
|---|
| 48 |                     $cache_dir = false; | 
|---|
| 49 |                } | 
|---|
| 50 |           } | 
|---|
| 51 |            | 
|---|
| 52 |           # Create Twig environment | 
|---|
| 53 |           parent::__construct( | 
|---|
| 54 |                new Twig_Loader_Filesystem(dirname(__FILE__).'/../swf'), | 
|---|
| 55 |                array( | 
|---|
| 56 |                     'auto_reload' => true, | 
|---|
| 57 |                     'autoescape' => false, | 
|---|
| 58 |                     'base_template_class' => 'Twig_Template', | 
|---|
| 59 |                     'cache' => $cache_dir,  | 
|---|
| 60 |                     'charset' => 'UTF-8', | 
|---|
| 61 |                     'debug' => DC_DEBUG, | 
|---|
| 62 |                     'optimizations' => -1, | 
|---|
| 63 |                     'strict_variables' => 0 //DC_DEBUG // Please fix undefined variables! | 
|---|
| 64 |                ) | 
|---|
| 65 |           ); | 
|---|
| 66 |            | 
|---|
| 67 |           # Add extensions | 
|---|
| 68 |           $this->addExtension(new dcFormExtension($this)); | 
|---|
| 69 |           $this->addExtension(new dcTabExtension($this)); | 
|---|
| 70 |      } | 
|---|
| 71 |       | 
|---|
| 72 |      /// @name Old template engine compatibility methods | 
|---|
| 73 |      //@{ | 
|---|
| 74 |      /** | 
|---|
| 75 |       * Old style to add a template block. | 
|---|
| 76 |       * | 
|---|
| 77 |       * @ignore | 
|---|
| 78 |       * @deprecated | 
|---|
| 79 |       */ | 
|---|
| 80 |      public function addBlock($name,$callback) | 
|---|
| 81 |      { | 
|---|
| 82 |           if (!is_callable($callback)) { | 
|---|
| 83 |                throw new Exception('No valid callback for '.$name); | 
|---|
| 84 |           } | 
|---|
| 85 |            | 
|---|
| 86 |           $this->dc_blocks[$name] = $callback; | 
|---|
| 87 |      } | 
|---|
| 88 |       | 
|---|
| 89 |      /** | 
|---|
| 90 |       * Old style to add a template value. | 
|---|
| 91 |       * | 
|---|
| 92 |       * @ignore | 
|---|
| 93 |       * @deprecated | 
|---|
| 94 |       */ | 
|---|
| 95 |      public function addValue($name,$callback) | 
|---|
| 96 |      { | 
|---|
| 97 |           if (!is_callable($callback)) { | 
|---|
| 98 |                throw new Exception('No valid callback for '.$name); | 
|---|
| 99 |           } | 
|---|
| 100 |            | 
|---|
| 101 |           $this->dc_values[$name] = $callback; | 
|---|
| 102 |      } | 
|---|
| 103 |       | 
|---|
| 104 |      /** | 
|---|
| 105 |       * Old style to check if a template block exists. | 
|---|
| 106 |       * | 
|---|
| 107 |       * @ignore | 
|---|
| 108 |       * @deprecated | 
|---|
| 109 |       */ | 
|---|
| 110 |      public function blockExists($name) | 
|---|
| 111 |      { | 
|---|
| 112 |           return isset($this->dc_blocks[$name]); | 
|---|
| 113 |      } | 
|---|
| 114 |       | 
|---|
| 115 |       | 
|---|
| 116 |      /** | 
|---|
| 117 |       * Old style to check if a template value exists. | 
|---|
| 118 |       * | 
|---|
| 119 |       * @ignore | 
|---|
| 120 |       * @deprecated | 
|---|
| 121 |       */ | 
|---|
| 122 |      public function valueExists($name) | 
|---|
| 123 |      { | 
|---|
| 124 |           return isset($this->dc_values[$name]); | 
|---|
| 125 |      } | 
|---|
| 126 |       | 
|---|
| 127 |       | 
|---|
| 128 |      /** | 
|---|
| 129 |       * Old style to check if a template tag exists. | 
|---|
| 130 |       * | 
|---|
| 131 |       * @ignore | 
|---|
| 132 |       * @deprecated | 
|---|
| 133 |       */ | 
|---|
| 134 |      public function tagExists($name) | 
|---|
| 135 |      { | 
|---|
| 136 |           return $this->blockExists($name) || $this->valueExists($name); | 
|---|
| 137 |      } | 
|---|
| 138 |       | 
|---|
| 139 |       | 
|---|
| 140 |      /** | 
|---|
| 141 |       * Old style to get a template value callback. | 
|---|
| 142 |       * | 
|---|
| 143 |       * @ignore | 
|---|
| 144 |       * @deprecated | 
|---|
| 145 |       */ | 
|---|
| 146 |      public function getValueCallback($name) | 
|---|
| 147 |      { | 
|---|
| 148 |           if ($this->valueExists($name)) | 
|---|
| 149 |           { | 
|---|
| 150 |                return $this->dc_values[$name]; | 
|---|
| 151 |           } | 
|---|
| 152 |            | 
|---|
| 153 |           return false; | 
|---|
| 154 |      } | 
|---|
| 155 |       | 
|---|
| 156 |      /** | 
|---|
| 157 |       * Old style to get a template block callback. | 
|---|
| 158 |       * | 
|---|
| 159 |       * @ignore | 
|---|
| 160 |       * @deprecated | 
|---|
| 161 |       */ | 
|---|
| 162 |      public function getBlockCallback($name) | 
|---|
| 163 |      { | 
|---|
| 164 |           if ($this->blockExists($name)) | 
|---|
| 165 |           { | 
|---|
| 166 |                return $this->dc_blocks[$name]; | 
|---|
| 167 |           } | 
|---|
| 168 |            | 
|---|
| 169 |           return false; | 
|---|
| 170 |      } | 
|---|
| 171 |       | 
|---|
| 172 |      /** | 
|---|
| 173 |       * Old style to get list of template blocks. | 
|---|
| 174 |       * | 
|---|
| 175 |       * @ignore | 
|---|
| 176 |       * @deprecated | 
|---|
| 177 |       */ | 
|---|
| 178 |      public function getBlocksList() | 
|---|
| 179 |      { | 
|---|
| 180 |           return array_keys($this->dc_blocks); | 
|---|
| 181 |      } | 
|---|
| 182 |       | 
|---|
| 183 |      /** | 
|---|
| 184 |       * Old style to get list of template values. | 
|---|
| 185 |       * | 
|---|
| 186 |       * @ignore | 
|---|
| 187 |       * @deprecated | 
|---|
| 188 |       */ | 
|---|
| 189 |      public function getValuesList() | 
|---|
| 190 |      { | 
|---|
| 191 |           return array_keys($this->dc_values); | 
|---|
| 192 |      } | 
|---|
| 193 |       | 
|---|
| 194 |      /** | 
|---|
| 195 |       * Old style to add template path. | 
|---|
| 196 |       * | 
|---|
| 197 |       * Many plugins and themes use this method, so we keep it. | 
|---|
| 198 |       * | 
|---|
| 199 |       * @ignore | 
|---|
| 200 |       * @deprecated | 
|---|
| 201 |       */ | 
|---|
| 202 |      public function setPath() | 
|---|
| 203 |      { | 
|---|
| 204 |           $path = array(); | 
|---|
| 205 |            | 
|---|
| 206 |           foreach (func_get_args() as $v) | 
|---|
| 207 |           { | 
|---|
| 208 |                if (is_array($v)) { | 
|---|
| 209 |                     $path = array_merge($path,array_values($v)); | 
|---|
| 210 |                } else { | 
|---|
| 211 |                     $path[] = $v; | 
|---|
| 212 |                } | 
|---|
| 213 |           } | 
|---|
| 214 |            | 
|---|
| 215 |           foreach ($path as $k => $v) | 
|---|
| 216 |           { | 
|---|
| 217 |                if (($v = path::real($v)) === false) { | 
|---|
| 218 |                     unset($path[$k]); | 
|---|
| 219 |                } | 
|---|
| 220 |           } | 
|---|
| 221 |            | 
|---|
| 222 |           $path = array_unique($path); | 
|---|
| 223 |            | 
|---|
| 224 |           $this->getLoader()->setPaths($path); | 
|---|
| 225 |      } | 
|---|
| 226 |       | 
|---|
| 227 |      /** | 
|---|
| 228 |       * Old style to get template path. | 
|---|
| 229 |       * | 
|---|
| 230 |       * Many plugins and themes use this method, so we keep it. | 
|---|
| 231 |       * | 
|---|
| 232 |       * @ignore | 
|---|
| 233 |       * @deprecated | 
|---|
| 234 |       */ | 
|---|
| 235 |      public function getPath() | 
|---|
| 236 |      { | 
|---|
| 237 |           return $this->getLoader()->getPaths(); | 
|---|
| 238 |      } | 
|---|
| 239 |      //@} | 
|---|
| 240 | } | 
|---|
| 241 | ?> | 
|---|