Dotclear

source: inc/admin/class.dc.admincontext.php @ 1064:0c887ef58692

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

Sites map