Dotclear

source: inc/admin/class.dc.admincontext.php @ 1078:35968ae8dae1

Revision 1078:35968ae8dae1, 9.8 KB checked in by JcDenis, 13 years ago (diff)

Add static messages to context and main layout

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_messages_static'   => array(),
36               'page_messages_lists'    => array(),
37               'page_message' => '',
38               'page_errors'  => array(),
39               'page_title'   => '',
40               
41               'admin_url'    => DC_ADMIN_URL,
42               'theme_url'    => DC_ADMIN_URL.'index.php?tf=',
43               'plugin_url'   => DC_ADMIN_URL.'index.php?pf=',
44               
45               'version'           => DC_VERSION,
46               'vendor_name'  => DC_VENDOR_NAME,
47               
48               'safe_mode'    => isset($_SESSION['sess_safe_mode']) && $_SESSION['sess_safe_mode'],
49               'debug_mode'   => DC_DEBUG
50          );
51     }
52     
53     /**
54     Prevent call crash from template on method that return this class
55     */
56     public function __toString()
57     {
58          return '';
59     }
60     
61     /**
62     Test a global variable
63     
64     @param string $name Name of the variable to test
65     @return boolean
66     */
67     public function __isset($name)
68     {
69          return isset($this->globals[$name]);
70     }
71     
72     /**
73     Add a global variable
74     
75    @param string $name Name of the variable
76    @param mixed $value Value of the variable
77     */
78     public function __set($name,$value)
79     {
80/*
81          # Overload protect
82          if ($value === null && isset($this->globals[$name])) {
83               unset($this->globals[$name]);
84          }
85          elseif (!isset($this->globals[$name])) {
86               throw new Exception('Modification of overloaded globals has no effect');
87          }
88//*/
89          $this->globals[$name] = $value;
90     }
91     
92     /**
93     Get a global variable
94     
95     @param string $name Name of the variable
96    @return mixed Value of the variable or null
97     */
98     public function __get($name)
99     {
100          return isset($this->globals[$name]) ? $this->globals[$name] : null;
101     }
102     
103    /**
104    Returns a list of filters to add to the existing list.
105   
106     @return array An array of filters
107    */
108     public function getFilters()
109     {
110          return array(
111               'trans' => new Twig_Filter_Function("__", array('is_safe' => array('html')))
112          );
113     }
114     
115    /**
116    Returns a list of functions to add to the existing list.
117   
118    @return array An array of functions
119    */
120     public function getFunctions()
121     {
122          return array(
123               '__'           => new Twig_Function_Function("__", array('is_safe' => array('html'))),
124               'debug_info' => new Twig_Function_Method($this, 'getDebugInfo', array('is_safe' => array('html')))
125               //,'page_menu' => new Twig_Function_Method($this, 'pageMenu', array('is_safe' => array('html')))
126          );
127     }
128     
129    /**
130    Returns a list of global variables to add to the existing list.
131     
132     This merges overloaded variables with defined variables.
133   
134    @return array An array of global variables
135    */
136     public function getGlobals()
137     {
138          $this->getBlogs();
139          $this->getCurrentBlog();
140          $this->getCurrentUser();
141          $this->getMenus();
142         
143          # Additional globals
144          $p = path::info($_SERVER['REQUEST_URI']);
145          $this->protected_globals['current_page'] = $p['base'];
146          $this->protected_globals['blog_count'] = $this->core->auth->blog_count;
147         
148          # Keep protected globals safe
149          return array_merge($this->globals,$this->protected_globals);
150     }
151     
152    /**
153     * Returns the name of the extension.
154     *
155     * @return string The extension name
156     */
157     public function getName()
158     {
159          return 'AdminContext';
160     }
161     
162     
163     /**
164     Add an informational message
165     
166     @param string $message A message
167     @return object self
168     */
169     public function setSafeMode($safe_mode)
170     {
171          $this->protected_globals['safe_mode'] = (boolean) $safe_mode;
172          return $this;
173     }
174     
175     /**
176     Add an informational message
177     
178     @param string $message A message
179     @return object self
180     */
181     public function addMessageStatic($message)
182     {
183          $this->protected_globals['page_messages_static'][] = $message;
184          return $this;
185     }
186     
187     /**
188     Add a list of informational messages
189     
190     @param string $message A title
191     @param array $message A list of messages
192     @return object self
193     */
194     public function addMessagesList($title,$messages)
195     {
196          $this->protected_globals['page_messages_lists'][$title] = $messages;
197          return $this;
198     }
199     
200     /**
201     Set an important message
202     
203     @param string $message A message
204     @return object self
205     */
206     public function setMessage($message)
207     {
208          $this->protected_globals['page_message'] = $message;
209          return $this;
210     }
211
212     /**
213     Add an error message
214     
215     @param string Error message
216     @return object self
217     */
218     public function addError($error)
219     {
220          $this->protected_globals['page_errors'][] = $error;
221          return $this;
222     }
223     
224     /**
225     Check if there is an error message
226     
227     @return boolean
228     */
229     public function hasError()
230     {
231          return !empty($this->protected_globals['page_errors']);
232     }
233     
234     /**
235     Add page title
236     */
237     public function setPageTitle($title)
238     {
239          $this->protected_globals['page_title'] = $title;
240     }
241     
242     /**
243     Check if a page title is set
244     */
245     public function hasPageTitle()
246     {
247          return !empty($this->protected_globals['page_title']);
248     }
249     
250     /**
251      * Get list of blogs
252      */
253     protected function getBlogs()
254     {
255          $blog_id = '';
256         
257          # Blogs list
258          $blogs = array();
259          if ($this->core->auth->blog_count > 1 && $this->core->auth->blog_count < 20) {
260               $blog_id = $this->core->blog->id;
261               $rs_blogs = $this->core->getBlogs(array('order'=>'LOWER(blog_name)','limit'=>20));
262               while ($rs_blogs->fetch()) {
263                    $blogs[$rs_blogs->blog_id] = $rs_blogs->blog_name.' - '.$rs_blogs->blog_url;
264                    $this->protected_globals['blogs'][$rs_blogs->blog_id] = array(
265                         'id'      => $rs_blogs->blog_id,
266                         'name'    => $rs_blogs->blog_name,
267                         'desc'    => $rs_blogs->blog_desc,
268                         'url'     => $rs_blogs->blog_url,
269                         'creadt'  => $rs_blogs->blog_creadt,
270                         'upddt'   => $rs_blogs->blog_upddt
271                    );
272               }
273          }
274         
275          # Switch blog form
276          $form = new dcForm($this->core,'switchblog_menu','index.php');
277          $form
278               ->addField(
279                    new dcFieldCombo('switchblog',$blog_id,$blogs,array(
280                    'label' => __('Blogs:'))))
281               ->addField(
282                    new dcFieldSubmit('switchblog_submit',__('ok'),array(
283                    'action' => 'switchblog')))
284               ->setup();
285     }
286     
287     /**
288      * Get current blog information
289      */
290     protected function getCurrentBlog()
291     {
292          $this->protected_globals['current_blog'] = $this->core->auth->blog_count ?
293               array(
294                    'id'      => $this->core->blog->id,
295                    'name'    => $this->core->blog->name,
296                    'desc'    => $this->core->blog->desc,
297                    'url'     => $this->core->blog->url,
298                    'host'    => $this->core->blog->host,
299                    'creadt'  => $this->core->blog->creadt,
300                    'upddt'   => $this->core->blog->upddt
301               ) : array(
302                    'id'      => '',
303                    'name'    => '',
304                    'desc'    => '',
305                    'url'     => '',
306                    'host'    => '',
307                    'creadt'  => '',
308                    'upddt'   => ''
309               );
310     }
311     
312     /**
313      * Get current user information
314      */
315     protected function getCurrentUser()
316     {
317          $this->protected_globals['current_user'] = $this->core->auth->userID() ?
318               array(
319                    'id'      => $this->core->auth->userID(),
320                    'admin'   => $this->core->auth->getInfo('user_admin'),
321                    'name'    => $this->core->auth->getInfo('user_name'),
322                    'firstname'    => $this->core->auth->getInfo('user_firstname'),
323                    'displayname'  => $this->core->auth->getInfo('user_displayname'),
324                    'url'     => $this->core->auth->getInfo('user_url'),
325                    'blog'    => $this->core->auth->getInfo('user_default_blog'),
326                    'lang'    => $this->core->auth->getInfo('user_lang'),
327                    'tz'      => $this->core->auth->getInfo('user_tz'),
328                    'creadt'  => $this->core->auth->getInfo('user_creadt'),
329                    'cn'      => $this->core->auth->getInfo('user_cn')
330               ) :
331               array(
332                    'id'      => '',
333                    'admin'   => '',
334                    'name'    => '',
335                    'firstname'    => '',
336                    'displayname'  => '',
337                    'url'     => '',
338                    'blog'    => '',
339                    'lang'    => 'en',
340                    'tz'      => '',
341                    'creadt'  => '',
342                    'cn'      => '',
343               );
344     }
345     
346     protected function getMenus()
347     {
348          global $_menu;
349         
350          $this->protected_globals['menus'] = array();
351         
352          if (!isset($_menu)) {
353               return;
354          }
355         
356          foreach($_menu as $m) {
357               $this->protected_globals['menus'][] = array(
358                    'id'           => $m->getID(),
359                    'title'        => $m->getTitle(),
360                    'separator'    => $m->getSeparator(),
361                    'items'        => $m->getItems()
362               );
363          }
364     }
365     
366     /**
367     Get an array of debug/dev infos
368     */
369     public function getDebugInfo()
370     {
371          if (!DC_DEBUG) {
372               return array();
373          }
374         
375          $di = array(
376               'global_vars' => implode(', ',array_keys($GLOBALS)),
377               'memory' => array(
378                    'usage' => memory_get_usage(),
379                    'size' => files::size(memory_get_usage())
380               ),
381               'xdebug' => array()
382          );
383         
384          if (function_exists('xdebug_get_profiler_filename')) {
385         
386               $url = http::getSelfURI();
387               $url .= strpos($url,'?') === false ? '?' : '&';
388               $url .= 'XDEBUG_PROFILE';
389               
390               $di['xdebug'] = array(
391                    'elapse_time' => xdebug_time_index(),
392                    'profiler_file' => xdebug_get_profiler_filename(),
393                    'profiler_url' =>  $url
394               );
395               
396               /* xdebug configuration:
397               zend_extension = /.../xdebug.so
398               xdebug.auto_trace = On
399               xdebug.trace_format = 0
400               xdebug.trace_options = 1
401               xdebug.show_mem_delta = On
402               xdebug.profiler_enable = 0
403               xdebug.profiler_enable_trigger = 1
404               xdebug.profiler_output_dir = /tmp
405               xdebug.profiler_append = 0
406               xdebug.profiler_output_name = timestamp
407               */
408          }
409         
410          return $di;
411     }
412}
413?>
Note: See TracBrowser for help on using the repository browser.

Sites map