Dotclear

source: inc/admin/class.dc.admincontext.php @ 1091:d32eaf9fac5e

Revision 1091:d32eaf9fac5e, 11.5 KB checked in by JcDenis, 13 years ago (diff)

Added javascript helpers to load only once scripts files

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

Sites map