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 ----------------------------------------- |
---|
12 | if (!defined('DC_RC_PATH')) { return; } |
---|
13 | |
---|
14 | /** |
---|
15 | @ingroup DC_CORE |
---|
16 | @brief Template extension for admin context |
---|
17 | |
---|
18 | This extends template environment with tools required in admin context. |
---|
19 | */ |
---|
20 | class 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_message' => '', |
---|
36 | 'page_errors' => array(), |
---|
37 | 'page_title' => '', |
---|
38 | |
---|
39 | 'admin_url' => DC_ADMIN_URL, |
---|
40 | 'theme_url' => DC_ADMIN_URL.'index.php?tf=', |
---|
41 | |
---|
42 | 'version' => DC_VERSION, |
---|
43 | 'vendor_name' => DC_VENDOR_NAME, |
---|
44 | |
---|
45 | # Blogs list (not available yet) |
---|
46 | 'blogs' => array(), |
---|
47 | |
---|
48 | # Current blog (not available yet and never available in auth.php) |
---|
49 | 'blog' => array( |
---|
50 | 'id' => '', |
---|
51 | 'host' => '', |
---|
52 | 'url' => '', |
---|
53 | 'name' => '' |
---|
54 | ) |
---|
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 | '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 | # Blogs list |
---|
143 | if ($this->core->auth->blog_count > 1 && $this->core->auth->blog_count < 20) { |
---|
144 | $rs_blogs = $core->getBlogs(array('order'=>'LOWER(blog_name)','limit'=>20)); |
---|
145 | while ($rs_blogs->fetch()) { |
---|
146 | $this->protected_globals['blogs'][html::escapeHTML($rs_blogs->blog_name.' - '.$rs_blogs->blog_url)] = $rs_blogs->blog_id; |
---|
147 | } |
---|
148 | } |
---|
149 | # Current blog |
---|
150 | if ($this->core->auth->blog_count) { |
---|
151 | $this->protected_globals['blog'] = array( |
---|
152 | 'id' => $this->core->blog->id, |
---|
153 | 'host' => $this->core->blog->host, |
---|
154 | 'url' => $this->core->blog->url, |
---|
155 | 'name' => $this->core->blog->name |
---|
156 | ); |
---|
157 | } |
---|
158 | # Keep protected globals safe |
---|
159 | return array_merge($this->globals,$this->protected_globals); |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * Returns the name of the extension. |
---|
164 | * |
---|
165 | * @return string The extension name |
---|
166 | */ |
---|
167 | public function getName() |
---|
168 | { |
---|
169 | return 'AdminContext'; |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | Set information message |
---|
174 | |
---|
175 | @param string $message A message |
---|
176 | @return object self |
---|
177 | */ |
---|
178 | public function setMessage($message) |
---|
179 | { |
---|
180 | $this->protected_globals['page_message'] = $message; |
---|
181 | return $this; |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | Add an error message |
---|
186 | |
---|
187 | @param string Error message |
---|
188 | @return object self |
---|
189 | */ |
---|
190 | public function addError($error) |
---|
191 | { |
---|
192 | $this->protected_globals['page_errors'][] = $error; |
---|
193 | return $this; |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | Check if there is an error message |
---|
198 | |
---|
199 | @return boolean |
---|
200 | */ |
---|
201 | public function hasError() |
---|
202 | { |
---|
203 | return !empty($this->protected_globals['page_errors']); |
---|
204 | } |
---|
205 | |
---|
206 | /** |
---|
207 | Add page title |
---|
208 | */ |
---|
209 | public function setPageTitle($title) |
---|
210 | { |
---|
211 | $this->protected_globals['page_title'] = $title; |
---|
212 | } |
---|
213 | |
---|
214 | /** |
---|
215 | pageMenu |
---|
216 | */ |
---|
217 | public function pageMenu() |
---|
218 | { |
---|
219 | $menu =& $GLOBALS['_menu']; |
---|
220 | foreach ($menu as $k => $v) { |
---|
221 | echo $menu[$k]->draw(); |
---|
222 | } |
---|
223 | } |
---|
224 | } |
---|
225 | ?> |
---|