Dotclear

source: inc/admin/class.dc.admincontext.php @ 1089:049be1a46863

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

Sites map