[992] | 1 | <?php |
---|
[1056] | 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; } |
---|
[992] | 13 | |
---|
[1056] | 14 | /** |
---|
| 15 | @ingroup DC_CORE |
---|
| 16 | @brief Template extension for admin context |
---|
| 17 | |
---|
| 18 | This extends template environment with tools required in admin context. |
---|
| 19 | */ |
---|
| 20 | class dcAdminContext extends Twig_Extension |
---|
| 21 | { |
---|
| 22 | protected $core; |
---|
| 23 | protected $globals = array(); |
---|
| 24 | protected $protected_globals = array(); |
---|
[992] | 25 | |
---|
[1056] | 26 | public function __construct($core) |
---|
| 27 | { |
---|
[992] | 28 | $this->core = $core; |
---|
[1056] | 29 | |
---|
| 30 | # Globals editable via context |
---|
[1053] | 31 | $this->globals = array(); |
---|
[1056] | 32 | |
---|
| 33 | # Globals not editable via context |
---|
| 34 | $this->protected_globals = array( |
---|
| 35 | 'page_message' => '', |
---|
| 36 | 'page_errors' => array(), |
---|
| 37 | 'page_title' => '', |
---|
| 38 | |
---|
| 39 | 'admin_url' => DC_ADMIN_URL, |
---|
| 40 | 'theme_url' => DC_ADMIN_URL.'index.php?tf=', |
---|
[1070] | 41 | 'plugin_url' => DC_ADMIN_URL.'index.php?pf=', |
---|
[1056] | 42 | |
---|
| 43 | 'version' => DC_VERSION, |
---|
[1061] | 44 | 'vendor_name' => DC_VENDOR_NAME, |
---|
| 45 | |
---|
| 46 | 'safe_mode' => isset($_SESSION['sess_safe_mode']) && $_SESSION['sess_safe_mode'] |
---|
[1056] | 47 | ); |
---|
[992] | 48 | } |
---|
| 49 | |
---|
[1056] | 50 | /** |
---|
| 51 | Prevent call crash from template on method that return this class |
---|
| 52 | */ |
---|
| 53 | public function __toString() |
---|
[992] | 54 | { |
---|
[1056] | 55 | return ''; |
---|
[992] | 56 | } |
---|
| 57 | |
---|
[1056] | 58 | /** |
---|
| 59 | Test a global variable |
---|
| 60 | |
---|
| 61 | @param string $name Name of the variable to test |
---|
| 62 | @return boolean |
---|
| 63 | */ |
---|
| 64 | public function __isset($name) |
---|
[992] | 65 | { |
---|
[1056] | 66 | return isset($this->globals[$name]); |
---|
[1053] | 67 | } |
---|
| 68 | |
---|
[1056] | 69 | /** |
---|
| 70 | Add a global variable |
---|
| 71 | |
---|
| 72 | @param string $name Name of the variable |
---|
| 73 | @param mixed $value Value of the variable |
---|
| 74 | */ |
---|
| 75 | public function __set($name,$value) |
---|
| 76 | { |
---|
| 77 | /* |
---|
| 78 | # Overload protect |
---|
| 79 | if ($value === null && isset($this->globals[$name])) { |
---|
| 80 | unset($this->globals[$name]); |
---|
| 81 | } |
---|
| 82 | elseif (!isset($this->globals[$name])) { |
---|
| 83 | throw new Exception('Modification of overloaded globals has no effect'); |
---|
| 84 | } |
---|
| 85 | //*/ |
---|
| 86 | $this->globals[$name] = $value; |
---|
[1053] | 87 | } |
---|
[1056] | 88 | |
---|
| 89 | /** |
---|
| 90 | Get a global variable |
---|
| 91 | |
---|
| 92 | @param string $name Name of the variable |
---|
| 93 | @return mixed Value of the variable or null |
---|
| 94 | */ |
---|
| 95 | public function __get($name) |
---|
| 96 | { |
---|
| 97 | return isset($this->globals[$name]) ? $this->globals[$name] : null; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | /** |
---|
| 101 | Returns a list of filters to add to the existing list. |
---|
| 102 | |
---|
| 103 | @return array An array of filters |
---|
| 104 | */ |
---|
[992] | 105 | public function getFilters() |
---|
| 106 | { |
---|
| 107 | return array( |
---|
| 108 | 'trans' => new Twig_Filter_Function("__", array('is_safe' => array('html'))) |
---|
| 109 | ); |
---|
| 110 | } |
---|
[1056] | 111 | |
---|
| 112 | /** |
---|
| 113 | Returns a list of functions to add to the existing list. |
---|
| 114 | |
---|
| 115 | @return array An array of functions |
---|
| 116 | */ |
---|
| 117 | public function getFunctions() |
---|
| 118 | { |
---|
| 119 | return array( |
---|
[1064] | 120 | '__' => new Twig_Function_Function("__", array('is_safe' => array('html'))) |
---|
| 121 | //,'page_menu' => new Twig_Function_Method($this, 'pageMenu', array('is_safe' => array('html'))) |
---|
[1056] | 122 | ); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | /** |
---|
| 126 | Returns a list of global variables to add to the existing list. |
---|
| 127 | |
---|
| 128 | This merges overloaded variables with defined variables. |
---|
| 129 | |
---|
| 130 | @return array An array of global variables |
---|
| 131 | */ |
---|
| 132 | public function getGlobals() |
---|
| 133 | { |
---|
[1060] | 134 | $this->getBlogs(); |
---|
| 135 | $this->getCurrentBlog(); |
---|
| 136 | $this->getCurrentUser(); |
---|
[1064] | 137 | $this->getMenus(); |
---|
[1060] | 138 | |
---|
| 139 | # Additional globals |
---|
| 140 | $p = path::info($_SERVER['REQUEST_URI']); |
---|
| 141 | $this->protected_globals['current_page'] = $p['base']; |
---|
| 142 | $this->protected_globals['blog_count'] = $this->core->auth->blog_count; |
---|
| 143 | |
---|
[1056] | 144 | # Keep protected globals safe |
---|
| 145 | return array_merge($this->globals,$this->protected_globals); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | /** |
---|
| 149 | * Returns the name of the extension. |
---|
| 150 | * |
---|
| 151 | * @return string The extension name |
---|
| 152 | */ |
---|
| 153 | public function getName() |
---|
| 154 | { |
---|
| 155 | return 'AdminContext'; |
---|
| 156 | } |
---|
| 157 | |
---|
[1061] | 158 | public function setSafeMode($safe_mode) |
---|
| 159 | { |
---|
| 160 | $this->protected_globals['safe_mode'] = (boolean) $safe_mode; |
---|
| 161 | return $this; |
---|
| 162 | } |
---|
| 163 | |
---|
[1056] | 164 | /** |
---|
| 165 | Set information message |
---|
| 166 | |
---|
| 167 | @param string $message A message |
---|
| 168 | @return object self |
---|
| 169 | */ |
---|
| 170 | public function setMessage($message) |
---|
| 171 | { |
---|
| 172 | $this->protected_globals['page_message'] = $message; |
---|
| 173 | return $this; |
---|
[992] | 174 | } |
---|
| 175 | |
---|
[1056] | 176 | /** |
---|
| 177 | Add an error message |
---|
| 178 | |
---|
| 179 | @param string Error message |
---|
| 180 | @return object self |
---|
| 181 | */ |
---|
| 182 | public function addError($error) |
---|
[992] | 183 | { |
---|
[1056] | 184 | $this->protected_globals['page_errors'][] = $error; |
---|
[992] | 185 | return $this; |
---|
| 186 | } |
---|
| 187 | |
---|
[1056] | 188 | /** |
---|
| 189 | Check if there is an error message |
---|
| 190 | |
---|
| 191 | @return boolean |
---|
| 192 | */ |
---|
| 193 | public function hasError() |
---|
[992] | 194 | { |
---|
[1056] | 195 | return !empty($this->protected_globals['page_errors']); |
---|
[992] | 196 | } |
---|
| 197 | |
---|
[1056] | 198 | /** |
---|
| 199 | Add page title |
---|
| 200 | */ |
---|
| 201 | public function setPageTitle($title) |
---|
[992] | 202 | { |
---|
[1056] | 203 | $this->protected_globals['page_title'] = $title; |
---|
[992] | 204 | } |
---|
| 205 | |
---|
[1056] | 206 | /** |
---|
[1070] | 207 | Check if a page title is set |
---|
| 208 | */ |
---|
| 209 | public function hasPageTitle() |
---|
| 210 | { |
---|
| 211 | return !empty($this->protected_globals['page_title']); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | /** |
---|
[1060] | 215 | * Get list of blogs |
---|
| 216 | */ |
---|
| 217 | protected function getBlogs() |
---|
| 218 | { |
---|
[1061] | 219 | $blog_id = ''; |
---|
| 220 | |
---|
[1060] | 221 | # Blogs list |
---|
| 222 | $blogs = array(); |
---|
| 223 | if ($this->core->auth->blog_count > 1 && $this->core->auth->blog_count < 20) { |
---|
[1061] | 224 | $blog_id = $this->core->blog->id; |
---|
[1060] | 225 | $rs_blogs = $this->core->getBlogs(array('order'=>'LOWER(blog_name)','limit'=>20)); |
---|
| 226 | while ($rs_blogs->fetch()) { |
---|
| 227 | $blogs[$rs_blogs->blog_id] = $rs_blogs->blog_name.' - '.$rs_blogs->blog_url; |
---|
| 228 | $this->protected_globals['blogs'][$rs_blogs->blog_id] = array( |
---|
| 229 | 'id' => $rs_blogs->blog_id, |
---|
| 230 | 'name' => $rs_blogs->blog_name, |
---|
| 231 | 'desc' => $rs_blogs->blog_desc, |
---|
| 232 | 'url' => $rs_blogs->blog_url, |
---|
| 233 | 'creadt' => $rs_blogs->blog_creadt, |
---|
| 234 | 'upddt' => $rs_blogs->blog_upddt |
---|
| 235 | ); |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | # Switch blog form |
---|
| 240 | $form = new dcForm($this->core,'switchblog_menu','index.php'); |
---|
| 241 | $form |
---|
| 242 | ->addField( |
---|
[1061] | 243 | new dcFieldCombo('switchblog',$blog_id,$blogs,array( |
---|
[1060] | 244 | 'label' => __('Blogs:')))) |
---|
| 245 | ->addField( |
---|
| 246 | new dcFieldSubmit('switchblog_submit',__('ok'),array( |
---|
| 247 | 'action' => 'switchblog'))) |
---|
| 248 | ->setup(); |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | /** |
---|
| 252 | * Get current blog information |
---|
| 253 | */ |
---|
| 254 | protected function getCurrentBlog() |
---|
| 255 | { |
---|
| 256 | $this->protected_globals['current_blog'] = $this->core->auth->blog_count ? |
---|
| 257 | array( |
---|
| 258 | 'id' => $this->core->blog->id, |
---|
| 259 | 'name' => $this->core->blog->name, |
---|
| 260 | 'desc' => $this->core->blog->desc, |
---|
| 261 | 'url' => $this->core->blog->url, |
---|
| 262 | 'host' => $this->core->blog->host, |
---|
| 263 | 'creadt' => $this->core->blog->creadt, |
---|
| 264 | 'upddt' => $this->core->blog->upddt |
---|
| 265 | ) : array( |
---|
| 266 | 'id' => '', |
---|
| 267 | 'name' => '', |
---|
| 268 | 'desc' => '', |
---|
| 269 | 'url' => '', |
---|
| 270 | 'host' => '', |
---|
| 271 | 'creadt' => '', |
---|
| 272 | 'upddt' => '' |
---|
| 273 | ); |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | /** |
---|
| 277 | * Get current user information |
---|
| 278 | */ |
---|
| 279 | protected function getCurrentUser() |
---|
| 280 | { |
---|
| 281 | $this->protected_globals['current_user'] = $this->core->auth->userID() ? |
---|
| 282 | array( |
---|
| 283 | 'id' => $this->core->auth->userID(), |
---|
| 284 | 'admin' => $this->core->auth->getInfo('user_admin'), |
---|
| 285 | 'name' => $this->core->auth->getInfo('user_name'), |
---|
| 286 | 'firstname' => $this->core->auth->getInfo('user_firstname'), |
---|
| 287 | 'displayname' => $this->core->auth->getInfo('user_displayname'), |
---|
| 288 | 'url' => $this->core->auth->getInfo('user_url'), |
---|
| 289 | 'blog' => $this->core->auth->getInfo('user_default_blog'), |
---|
| 290 | 'lang' => $this->core->auth->getInfo('user_lang'), |
---|
| 291 | 'tz' => $this->core->auth->getInfo('user_tz'), |
---|
| 292 | 'creadt' => $this->core->auth->getInfo('user_creadt'), |
---|
| 293 | 'cn' => $this->core->auth->getInfo('user_cn') |
---|
| 294 | ) : |
---|
| 295 | array( |
---|
| 296 | 'id' => '', |
---|
| 297 | 'admin' => '', |
---|
| 298 | 'name' => '', |
---|
| 299 | 'firstname' => '', |
---|
| 300 | 'displayname' => '', |
---|
| 301 | 'url' => '', |
---|
| 302 | 'blog' => '', |
---|
| 303 | 'lang' => 'en', |
---|
| 304 | 'tz' => '', |
---|
| 305 | 'creadt' => '', |
---|
| 306 | 'cn' => '', |
---|
| 307 | ); |
---|
| 308 | } |
---|
[1064] | 309 | |
---|
| 310 | protected function getMenus() |
---|
| 311 | { |
---|
| 312 | global $_menu; |
---|
| 313 | |
---|
| 314 | $this->protected_globals['menus'] = array(); |
---|
| 315 | |
---|
| 316 | if (!isset($_menu)) { |
---|
| 317 | return; |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | foreach($_menu as $m) { |
---|
| 321 | $this->protected_globals['menus'][] = array( |
---|
| 322 | 'id' => $m->getID(), |
---|
| 323 | 'title' => $m->getTitle(), |
---|
| 324 | 'separator' => $m->getSeparator(), |
---|
| 325 | 'items' => $m->getItems() |
---|
| 326 | ); |
---|
| 327 | } |
---|
| 328 | } |
---|
[992] | 329 | } |
---|
[1056] | 330 | ?> |
---|