Dotclear

source: inc/admin/class.dc.admincontext.php @ 1088:476af9ff8aed

Revision 1088:476af9ff8aed, 10.1 KB checked in by JcDenis, 13 years ago (diff)

Put reusable globals variables in admin context

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          $this->protected_globals['rtl'] = l10n::getTextDirection(
148               $this->protected_globals['current_user']['lang']) == 'rtl';
149          $this->protected_globals['session'] = array(
150               'id' => session_id(),
151               'uid' => isset($_SESSION['sess_browser_uid']) ? $_SESSION['sess_browser_uid'] : '',
152               'nonce' => $this->core->getNonce()
153          );
154         
155          # Keep protected globals safe
156          return array_merge($this->globals,$this->protected_globals);
157     }
158     
159    /**
160     * Returns the name of the extension.
161     *
162     * @return string The extension name
163     */
164     public function getName()
165     {
166          return 'AdminContext';
167     }
168     
169     
170     /**
171     Add an informational message
172     
173     @param string $message A message
174     @return object self
175     */
176     public function setSafeMode($safe_mode)
177     {
178          $this->protected_globals['safe_mode'] = (boolean) $safe_mode;
179          return $this;
180     }
181     
182     /**
183     Add an informational message
184     
185     @param string $message A message
186     @return object self
187     */
188     public function addMessageStatic($message)
189     {
190          $this->protected_globals['page_messages_static'][] = $message;
191          return $this;
192     }
193     
194     /**
195     Add a list of informational messages
196     
197     @param string $message A title
198     @param array $message A list of messages
199     @return object self
200     */
201     public function addMessagesList($title,$messages)
202     {
203          $this->protected_globals['page_messages_lists'][$title] = $messages;
204          return $this;
205     }
206     
207     /**
208     Set an important message
209     
210     @param string $message A message
211     @return object self
212     */
213     public function setMessage($message)
214     {
215          $this->protected_globals['page_message'] = $message;
216          return $this;
217     }
218
219     /**
220     Add an error message
221     
222     @param string Error message
223     @return object self
224     */
225     public function addError($error)
226     {
227          $this->protected_globals['page_errors'][] = $error;
228          return $this;
229     }
230     
231     /**
232     Check if there is an error message
233     
234     @return boolean
235     */
236     public function hasError()
237     {
238          return !empty($this->protected_globals['page_errors']);
239     }
240     
241     /**
242     Add page title
243     */
244     public function setPageTitle($title)
245     {
246          $this->protected_globals['page_title'] = $title;
247     }
248     
249     /**
250     Check if a page title is set
251     */
252     public function hasPageTitle()
253     {
254          return !empty($this->protected_globals['page_title']);
255     }
256     
257     /**
258      * Get list of blogs
259      */
260     protected function getBlogs()
261     {
262          $blog_id = '';
263         
264          # Blogs list
265          $blogs = array();
266          if ($this->core->auth->blog_count > 1 && $this->core->auth->blog_count < 20) {
267               $blog_id = $this->core->blog->id;
268               $rs_blogs = $this->core->getBlogs(array('order'=>'LOWER(blog_name)','limit'=>20));
269               while ($rs_blogs->fetch()) {
270                    $blogs[$rs_blogs->blog_id] = $rs_blogs->blog_name.' - '.$rs_blogs->blog_url;
271                    $this->protected_globals['blogs'][$rs_blogs->blog_id] = array(
272                         'id'      => $rs_blogs->blog_id,
273                         'name'    => $rs_blogs->blog_name,
274                         'desc'    => $rs_blogs->blog_desc,
275                         'url'     => $rs_blogs->blog_url,
276                         'creadt'  => $rs_blogs->blog_creadt,
277                         'upddt'   => $rs_blogs->blog_upddt
278                    );
279               }
280          }
281         
282          # Switch blog form
283          $form = new dcForm($this->core,'switchblog_menu','index.php');
284          $form
285               ->addField(
286                    new dcFieldCombo('switchblog',$blog_id,$blogs,array(
287                    'label' => __('Blogs:'))))
288               ->addField(
289                    new dcFieldSubmit('switchblog_submit',__('ok'),array(
290                    'action' => 'switchblog')))
291               ->setup();
292     }
293     
294     /**
295      * Get current blog information
296      */
297     protected function getCurrentBlog()
298     {
299          $this->protected_globals['current_blog'] = $this->core->auth->blog_count ?
300               array(
301                    'id'      => $this->core->blog->id,
302                    'name'    => $this->core->blog->name,
303                    'desc'    => $this->core->blog->desc,
304                    'url'     => $this->core->blog->url,
305                    'host'    => $this->core->blog->host,
306                    'creadt'  => $this->core->blog->creadt,
307                    'upddt'   => $this->core->blog->upddt
308               ) : array(
309                    'id'      => '',
310                    'name'    => '',
311                    'desc'    => '',
312                    'url'     => '',
313                    'host'    => '',
314                    'creadt'  => '',
315                    'upddt'   => ''
316               );
317     }
318     
319     /**
320      * Get current user information
321      */
322     protected function getCurrentUser()
323     {
324          $infos = array(
325               'pwd','name','firstname','displayname',
326               'email','url','default_blog','lang','tz',
327               'post_status','creadt','upddt','cn'
328          );
329         
330          $user = array(
331               'id' => '',
332               'super' => false,
333               'lang' => 'en',
334               'options' => $this->core->userDefaults(),
335               'prefs' => array(),
336               'rights' => array(
337                    'media' => false
338               )
339          );
340         
341          foreach($infos as $i) {
342               $user[$i] = '';
343          }
344         
345          if ($this->core->auth->userID()) {
346         
347               $user = array(
348                    'id' => $this->core->auth->userID(),
349                    'super' => $this->core->auth->isSuperAdmin(),
350                    'options' => $this->core->auth->getOptions(),
351                    'rights' => array(
352                         'media' => $this->core->auth->check('media,media_admin',$this->core->blog->id)
353                    )
354               );
355               
356               foreach($infos as $i) {
357                    $user[$i] = $this->core->auth->getInfo('user_'.$i);
358               }
359               
360               foreach($this->core->auth->user_prefs->dumpWorkspaces() as $ws => $prefs) {
361                    $user['prefs'][$ws] = $prefs->dumpPrefs();
362               }
363          }
364         
365          $this->protected_globals['current_user'] = $user;
366     }
367     
368     protected function getMenus()
369     {
370          global $_menu;
371         
372          $this->protected_globals['menus'] = array();
373         
374          if (!isset($_menu)) {
375               return;
376          }
377         
378          foreach($_menu as $m) {
379               $this->protected_globals['menus'][] = array(
380                    'id'           => $m->getID(),
381                    'title'        => $m->getTitle(),
382                    'separator'    => $m->getSeparator(),
383                    'items'        => $m->getItems()
384               );
385          }
386     }
387     
388     /**
389     Get an array of debug/dev infos
390     */
391     public function getDebugInfo()
392     {
393          if (!DC_DEBUG) {
394               return array();
395          }
396         
397          $di = array(
398               'global_vars' => implode(', ',array_keys($GLOBALS)),
399               'memory' => array(
400                    'usage' => memory_get_usage(),
401                    'size' => files::size(memory_get_usage())
402               ),
403               'xdebug' => array()
404          );
405         
406          if (function_exists('xdebug_get_profiler_filename')) {
407         
408               $url = http::getSelfURI();
409               $url .= strpos($url,'?') === false ? '?' : '&';
410               $url .= 'XDEBUG_PROFILE';
411               
412               $di['xdebug'] = array(
413                    'elapse_time' => xdebug_time_index(),
414                    'profiler_file' => xdebug_get_profiler_filename(),
415                    'profiler_url' =>  $url
416               );
417               
418               /* xdebug configuration:
419               zend_extension = /.../xdebug.so
420               xdebug.auto_trace = On
421               xdebug.trace_format = 0
422               xdebug.trace_options = 1
423               xdebug.show_mem_delta = On
424               xdebug.profiler_enable = 0
425               xdebug.profiler_enable_trigger = 1
426               xdebug.profiler_output_dir = /tmp
427               xdebug.profiler_append = 0
428               xdebug.profiler_output_name = timestamp
429               */
430          }
431         
432          return $di;
433     }
434}
435?>
Note: See TracBrowser for help on using the repository browser.

Sites map