Dotclear

source: inc/admin/class.dc.admincontext.php @ 1147:2e5cb79e4782

Revision 1147:2e5cb79e4782, 11.5 KB checked in by Dsls <dsls@…>, 13 years ago (diff)

Oops, I did it again (c).

Fist draft of merge from formfilters to dctwig. lists need to be broken up too.

From now all post lists are broken.

RevLine 
[992]1<?php
[1056]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; }
[992]13
[1056]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();
[1091]25     protected $memory = array();
[992]26     
[1056]27     public function __construct($core)
28     {
[992]29          $this->core = $core;
[1056]30         
31          # Globals editable via context
[1053]32          $this->globals = array();
[1056]33         
34          # Globals not editable via context
35          $this->protected_globals = array(
[1089]36               'messages' => array(
37                    'static' => array(),
38                    'lists' => array(),
39                    'alert' => '',
40                    'errors' => array()
41               ),
42               
43               'page_title'   => array(),
44               'page_global'  => false,
[1056]45               
46               'admin_url'    => DC_ADMIN_URL,
[1147]47               'theme_url'    => '',
[1070]48               'plugin_url'   => DC_ADMIN_URL.'index.php?pf=',
[1056]49               
50               'version'           => DC_VERSION,
[1061]51               'vendor_name'  => DC_VENDOR_NAME,
52               
[1071]53               'safe_mode'    => isset($_SESSION['sess_safe_mode']) && $_SESSION['sess_safe_mode'],
54               'debug_mode'   => DC_DEBUG
[1056]55          );
[992]56     }
57     
[1056]58     /**
59     Prevent call crash from template on method that return this class
60     */
61     public function __toString()
[992]62     {
[1056]63          return '';
[992]64     }
65     
[1056]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)
[992]73     {
[1056]74          return isset($this->globals[$name]);
[1053]75     }
76     
[1056]77     /**
78     Add a global variable
79     
[1089]80     @param string $name Name of the variable
81     @param mixed $value Value of the variable
[1056]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;
[1053]95     }
[1056]96     
97     /**
98     Get a global variable
99     
100     @param string $name Name of the variable
[1089]101     @return mixed Value of the variable or null
[1056]102     */
103     public function __get($name)
104     {
105          return isset($this->globals[$name]) ? $this->globals[$name] : null;
106     }
107     
[1089]108     /**
109     Returns a list of filters to add to the existing list.
110     
[1056]111     @return array An array of filters
[1089]112     */
[992]113     public function getFilters()
114     {
115          return array(
116               'trans' => new Twig_Filter_Function("__", array('is_safe' => array('html')))
117          );
118     }
[1056]119     
[1089]120     /**
121     Returns a list of functions to add to the existing list.
122     
123     @return array An array of functions
124     */
[1056]125     public function getFunctions()
126     {
127          return array(
[1071]128               '__'           => new Twig_Function_Function("__", array('is_safe' => array('html'))),
[1091]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')))
[1056]132          );
133     }
134     
[1089]135     /**
136     Returns a list of global variables to add to the existing list.
[1056]137     
138     This merges overloaded variables with defined variables.
[1089]139     
140     @return array An array of global variables
141     */
[1056]142     public function getGlobals()
143     {
[1060]144          $this->getBlogs();
145          $this->getCurrentBlog();
146          $this->getCurrentUser();
[1064]147          $this->getMenus();
[1060]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;
[1088]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          );
[1060]160         
[1056]161          # Keep protected globals safe
162          return array_merge($this->globals,$this->protected_globals);
163     }
164     
[1089]165     /**
166     Returns the name of the extension.
167     
168     @return string The extension name
169     */
[1056]170     public function getName()
171     {
172          return 'AdminContext';
173     }
174     
[1078]175     
176     /**
177     Add an informational message
178     
179     @param string $message A message
180     @return object self
181     */
[1061]182     public function setSafeMode($safe_mode)
183     {
184          $this->protected_globals['safe_mode'] = (boolean) $safe_mode;
185          return $this;
186     }
187     
[1056]188     /**
[1078]189     Add an informational message
190     
191     @param string $message A message
192     @return object self
193     */
194     public function addMessageStatic($message)
195     {
[1089]196          $this->protected_globals['messages']['static'][] = $message;
[1078]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     {
[1089]209          $this->protected_globals['messages']['lists'][$title] = $messages;
[1078]210          return $this;
211     }
212     
213     /**
214     Set an important message
[1056]215     
216     @param string $message A message
217     @return object self
218     */
[1089]219     public function setAlert($message)
[1056]220     {
[1089]221          $this->protected_globals['messages']['alert'] = $message;
[1056]222          return $this;
[992]223     }
[1089]224     
[1056]225     /**
226     Add an error message
227     
228     @param string Error message
229     @return object self
230     */
231     public function addError($error)
[992]232     {
[1089]233          $this->protected_globals['messages']['errors'][] = $error;
[992]234          return $this;
235     }
236     
[1056]237     /**
238     Check if there is an error message
239     
240     @return boolean
241     */
242     public function hasError()
[992]243     {
[1089]244          return !empty($this->protected_globals['messages']['errors']);
[992]245     }
246     
[1056]247     /**
[1089]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
[1056]258     */
[1089]259     public function fillPageTitle($title,$url='')
[992]260     {
[1089]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;
[992]275     }
276     
[1056]277     /**
[1070]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     /**
[1089]286     Get list of blogs
287     */
[1060]288     protected function getBlogs()
289     {
[1061]290          $blog_id = '';
291         
[1060]292          # Blogs list
293          $blogs = array();
294          if ($this->core->auth->blog_count > 1 && $this->core->auth->blog_count < 20) {
[1061]295               $blog_id = $this->core->blog->id;
[1060]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(
[1061]314                    new dcFieldCombo('switchblog',$blog_id,$blogs,array(
[1060]315                    'label' => __('Blogs:'))))
316               ->addField(
317                    new dcFieldSubmit('switchblog_submit',__('ok'),array(
318                    'action' => 'switchblog')))
319               ->setup();
320     }
321     
322     /**
[1089]323     Get current blog information
324     */
[1060]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     /**
[1089]348     Get current user information
349     */
[1060]350     protected function getCurrentUser()
351     {
[1087]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,
[1088]361               'lang' => 'en',
362               'options' => $this->core->userDefaults(),
363               'prefs' => array(),
364               'rights' => array(
365                    'media' => false
366               )
[1087]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(),
[1088]378                    'options' => $this->core->auth->getOptions(),
379                    'rights' => array(
380                         'media' => $this->core->auth->check('media,media_admin',$this->core->blog->id)
381                    )
[1060]382               );
[1087]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;
[1060]394     }
[1064]395     
[1089]396     /**
397     Get sidebar menus
398     */
[1064]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     }
[1071]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     }
[1091]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     }
[992]492}
[1056]493?>
Note: See TracBrowser for help on using the repository browser.

Sites map