1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2013 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 | * dcFavorites -- Favorites handling facilities |
---|
16 | * |
---|
17 | */ |
---|
18 | class dcFavorites |
---|
19 | { |
---|
20 | /** @var dcCore dotclear core instance */ |
---|
21 | protected $core; |
---|
22 | |
---|
23 | /** @var array list of favorite definitions */ |
---|
24 | protected $fav_defs; |
---|
25 | |
---|
26 | /** @var dcWorkspace current favorite landing workspace */ |
---|
27 | protected $ws; |
---|
28 | |
---|
29 | /** @var array list of user-defined favorite ids */ |
---|
30 | protected $local_prefs; |
---|
31 | |
---|
32 | /** @var array list of globally-defined favorite ids */ |
---|
33 | protected $global_prefs; |
---|
34 | |
---|
35 | /** @var array list of user preferences (either one of the 2 above, or not!) */ |
---|
36 | protected $user_prefs; |
---|
37 | |
---|
38 | /** |
---|
39 | * Class constructor |
---|
40 | * |
---|
41 | * @param mixed $core dotclear core |
---|
42 | * |
---|
43 | * @access public |
---|
44 | * |
---|
45 | * @return mixed Value. |
---|
46 | */ |
---|
47 | public function __construct($core) { |
---|
48 | $this->core = $core; |
---|
49 | $this->fav_defs = new ArrayObject(); |
---|
50 | $this->ws = $core->auth->user_prefs->addWorkspace('dashboard'); |
---|
51 | $this->user_prefs = array(); |
---|
52 | |
---|
53 | if ($this->ws->prefExists('favorites')) { |
---|
54 | $this->local_prefs = @unserialize($this->ws->getLocal('favorites')); |
---|
55 | $this->global_prefs = @unserialize($this->ws->getGlobal('favorites')); |
---|
56 | // Since we never know what user puts through user:preferences ... |
---|
57 | if (!is_array($this->local_prefs)) { |
---|
58 | $this->local_prefs = array(); |
---|
59 | } |
---|
60 | if (!is_array($this->global_prefs)) { |
---|
61 | $this->global_prefs = array(); |
---|
62 | } |
---|
63 | } else { |
---|
64 | // No favorite defined ? Huhu, let's go for a migration |
---|
65 | $this->migrateFavorites(); |
---|
66 | } |
---|
67 | defaultFavorites::initDefaultFavorites($this); |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | /** |
---|
72 | * setup - sets up favorites, fetch user favorites (against his permissions) |
---|
73 | * This method is to be called after loading plugins |
---|
74 | * |
---|
75 | * @access public |
---|
76 | * |
---|
77 | */ |
---|
78 | public function setup() { |
---|
79 | $this->legacyFavorites(); |
---|
80 | $this->core->callBehavior('adminDashboardFavorites', $this->core, $this); |
---|
81 | $this->setUserPrefs(); |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * getFavorite - retrieves a favorite (complete description) from its id. |
---|
86 | * |
---|
87 | * @param string $id the favorite id, or an array having 1 key 'name' set to id, ther keys are merged to favorite. |
---|
88 | * |
---|
89 | * @access public |
---|
90 | * |
---|
91 | * @return array the favorite, false if not found (or not permitted) |
---|
92 | */ |
---|
93 | public function getFavorite($p) { |
---|
94 | if (is_array($p)) { |
---|
95 | $fname=$p['name']; |
---|
96 | if (!isset($this->fav_defs[$fname])) { |
---|
97 | return false; |
---|
98 | } |
---|
99 | $fattr = $p; |
---|
100 | unset($fattr['name']); |
---|
101 | $fattr = array_merge($this->fav_defs[$fname],$fattr); |
---|
102 | } else { |
---|
103 | if (!isset($this->fav_defs[$p])) { |
---|
104 | return false; |
---|
105 | } |
---|
106 | $fattr = $this->fav_defs[$p]; |
---|
107 | } |
---|
108 | $fattr = array_merge (array('id' => null,'class'=>null),$fattr); |
---|
109 | if (!isset($fattr['permissions']) || $this->core->auth->check($fattr['permissions'],$this->core->blog->id)) { |
---|
110 | return $fattr; |
---|
111 | } else { |
---|
112 | return false; |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * getFavorites - retrieves a list of favorites. |
---|
118 | * |
---|
119 | * @param string $ids an array of ids, as defined in getFavorite. |
---|
120 | * |
---|
121 | * @access public |
---|
122 | * |
---|
123 | * @return array array of favorites, can be empty if ids are not found (or not permitted) |
---|
124 | */ |
---|
125 | public function getFavorites($ids) { |
---|
126 | $prefs = array(); |
---|
127 | foreach ($ids as $id) { |
---|
128 | $f = $this->getFavorite($id); |
---|
129 | if ($f !== false) { |
---|
130 | $prefs[$id]=$f; |
---|
131 | } |
---|
132 | } |
---|
133 | return $prefs; |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * setUserPrefs - get user favorites from settings. These are complete favorites, not ids only |
---|
138 | * returned favorites are the first non-empty list from : |
---|
139 | * * user-defined favorites |
---|
140 | * * globally-defined favorites |
---|
141 | * * a failback list "new post" (shall never be empty) |
---|
142 | * This method is called by ::setup() |
---|
143 | * @access protected |
---|
144 | * |
---|
145 | */ |
---|
146 | protected function setUserPrefs() { |
---|
147 | $this->user_prefs = $this->getFavorites($this->local_prefs); |
---|
148 | if (!count($this->user_prefs)) { |
---|
149 | $this->user_prefs = $this->getFavorites($this->global_prefs); |
---|
150 | } |
---|
151 | if (!count($this->user_prefs)) { |
---|
152 | $this->user_prefs = $this->getFavorites(array('new_post')); |
---|
153 | } |
---|
154 | $u = explode('?',$_SERVER['REQUEST_URI']); |
---|
155 | // Loop over prefs to enable active favorites |
---|
156 | foreach ($this->user_prefs as $k=>&$v) { |
---|
157 | if (isset($v['active_cb']) && is_callable($v['active_cb'])) { |
---|
158 | // Use callback if defined to match whether favorite is active or not |
---|
159 | $v['active'] = call_user_func($v['active_cb'],$u[0],$_REQUEST); |
---|
160 | } else { |
---|
161 | // Failback active detection. We test against URI name & parameters |
---|
162 | $v['active']=true; // true until something proves it is false |
---|
163 | $u = explode('?',$v['url'],2); |
---|
164 | if (!preg_match('/'.preg_quote($u[0],"/").'/',$_SERVER['REQUEST_URI'])) { |
---|
165 | $v['active'] = false; // no URI match |
---|
166 | } |
---|
167 | if (count($u) == 2) { |
---|
168 | parse_str($u[1],$p); |
---|
169 | // test against each request parameter. |
---|
170 | foreach ($p as $k2 => $v2) { |
---|
171 | if (!isset($_REQUEST[$k2]) || $_REQUEST[$k2] !== $v2) { |
---|
172 | $v['active'] = false; |
---|
173 | } |
---|
174 | } |
---|
175 | } |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * migrateFavorites - migrate dc < 2.6 favorites to new format |
---|
183 | * |
---|
184 | * @access protected |
---|
185 | * |
---|
186 | */ |
---|
187 | protected function migrateFavorites() { |
---|
188 | $fav_ws = $this->core->auth->user_prefs->addWorkspace('favorites'); |
---|
189 | $this->local_prefs=array(); |
---|
190 | $this->global_prefs=array(); |
---|
191 | foreach ($fav_ws->dumpPrefs() as $k => $v) { |
---|
192 | $fav = @unserialize($v['value']); |
---|
193 | if (is_array($fav)) { |
---|
194 | if ($v['global']) { |
---|
195 | $this->global_prefs[] = $fav['name']; |
---|
196 | } else { |
---|
197 | $this->local_prefs[] = $fav['name']; |
---|
198 | } |
---|
199 | } |
---|
200 | } |
---|
201 | $this->ws->put('favorites',serialize($this->global_prefs),'string','User favorites',true,true); |
---|
202 | $this->ws->put('favorites',serialize($this->local_prefs)); |
---|
203 | $this->user_prefs = $this->getFavorites($this->local_prefs); |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | |
---|
208 | /** |
---|
209 | * legacyFavorites - handle legacy favorites using adminDashboardFavs behavior |
---|
210 | * |
---|
211 | * @access protected |
---|
212 | * |
---|
213 | */ |
---|
214 | protected function legacyFavorites() { |
---|
215 | $f = new ArrayObject(); |
---|
216 | $this->core->callBehavior('adminDashboardFavs', $this->core, $f); |
---|
217 | foreach ($f as $k => $v) { |
---|
218 | $fav = array ( |
---|
219 | 'title' => __($v[1]), |
---|
220 | 'url' => $v[2], |
---|
221 | 'small-icon' => $v[3], |
---|
222 | 'large-icon' => $v[4], |
---|
223 | 'id' => $v[5], |
---|
224 | 'class' => $v[6] |
---|
225 | ); |
---|
226 | $this->register ($v[0], $fav); |
---|
227 | } |
---|
228 | |
---|
229 | } |
---|
230 | |
---|
231 | /** |
---|
232 | * getUserFavorites - returns favorites that correspond to current user |
---|
233 | * (may be local, global, or failback favorites) |
---|
234 | * |
---|
235 | * @access public |
---|
236 | * |
---|
237 | * @return array array of favorites (enriched) |
---|
238 | */ |
---|
239 | public function getUserFavorites() { |
---|
240 | return $this->user_prefs; |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | /** |
---|
245 | * getFavoriteIDs - returns user-defined or global favorites ids list |
---|
246 | * shall not be called outside preferences.php... |
---|
247 | * |
---|
248 | * @param boolean $global if true, retrieve global favs, user favs otherwise |
---|
249 | * |
---|
250 | * @access public |
---|
251 | * |
---|
252 | * @return array array of favorites ids (only ids, not enriched) |
---|
253 | */ |
---|
254 | public function getFavoriteIDs ($global=false) { |
---|
255 | return $global?$this->global_prefs:$this->local_prefs; |
---|
256 | } |
---|
257 | |
---|
258 | /** |
---|
259 | * setFavoriteIDs - stores user-defined or global favorites ids list |
---|
260 | * shall not be called outside preferences.php... |
---|
261 | * |
---|
262 | * @param array $ids list of fav ids |
---|
263 | * @param boolean $global if true, retrieve global favs, user favs otherwise |
---|
264 | * |
---|
265 | * @access public |
---|
266 | */ |
---|
267 | public function setFavoriteIDs($ids,$global=false) { |
---|
268 | $this->ws->put('favorites',serialize($ids),null,null,true,$global); |
---|
269 | } |
---|
270 | |
---|
271 | /** |
---|
272 | * getAvailableFavoritesIDs - returns all available fav ids |
---|
273 | * |
---|
274 | * @access public |
---|
275 | * |
---|
276 | * @return array array of favorites ids (only ids, not enriched) |
---|
277 | */ |
---|
278 | public function getAvailableFavoritesIDs () { |
---|
279 | return array_keys($this->fav_defs->getArrayCopy()); |
---|
280 | } |
---|
281 | |
---|
282 | /** |
---|
283 | * appendMenuTitle - adds favorites section title to sidebar menu |
---|
284 | * shall not be called outside admin/prepend.php... |
---|
285 | * |
---|
286 | * @param dcMenu $menu admin menu instance |
---|
287 | * |
---|
288 | * @access public |
---|
289 | */ |
---|
290 | public function appendMenuTitle($menu) { |
---|
291 | $menu['Favorites'] = new dcMenu('favorites-menu','My favorites'); |
---|
292 | $menu['Favorites']->title = __('My favorites'); |
---|
293 | } |
---|
294 | |
---|
295 | /** |
---|
296 | * appendMenu - adds favorites items title to sidebar menu |
---|
297 | * shall not be called outside admin/prepend.php... |
---|
298 | * |
---|
299 | * @param dcMenu $menu admin menu instance |
---|
300 | * |
---|
301 | * @access public |
---|
302 | */ |
---|
303 | public function appendMenu($menu) { |
---|
304 | foreach ($this->user_prefs as $k=>$v) { |
---|
305 | $menu['Favorites']->addItem( |
---|
306 | $v['title'], |
---|
307 | $v['url'], |
---|
308 | $v['small-icon'], |
---|
309 | $v['active'], |
---|
310 | true, |
---|
311 | $v['id'], |
---|
312 | $v['class'] |
---|
313 | ); |
---|
314 | } |
---|
315 | } |
---|
316 | |
---|
317 | /** |
---|
318 | * appendDashboardIcons - adds favorites icons to index page |
---|
319 | * shall not be called outside admin/index.php... |
---|
320 | * |
---|
321 | * @param array $icons dashboard icon list to enrich |
---|
322 | * |
---|
323 | * @access public |
---|
324 | */ |
---|
325 | public function appendDashboardIcons($icons) { |
---|
326 | foreach ($this->user_prefs as $k=>$v) { |
---|
327 | if (isset($v['dashboard_cb']) && is_callable($v['dashboard_cb'])) { |
---|
328 | $v = new ArrayObject($v); |
---|
329 | call_user_func($v['dashboard_cb'],$this->core,$v); |
---|
330 | } |
---|
331 | $icons[$k]=new ArrayObject(array($v['title'],$v['url'],$v['large-icon'])); |
---|
332 | $this->core->callBehavior('adminDashboardFavsIcon',$this->core,$k,$icons[$k]); |
---|
333 | } |
---|
334 | } |
---|
335 | |
---|
336 | /** |
---|
337 | * register - registers a new favorite definition |
---|
338 | * |
---|
339 | * @param string $id favorite id |
---|
340 | * @param array $data favorite information. Array keys are : |
---|
341 | * 'title' => favorite title (localized) |
---|
342 | * 'url' => favorite URL, |
---|
343 | * 'small-icon' => favorite small icon (for menu) |
---|
344 | * 'large-icon' => favorite large icon (for dashboard) |
---|
345 | * 'permissions' => (optional) comma-separated list of permissions for thie fav, if not set : no restriction |
---|
346 | * 'dashboard_cb' => (optional) callback to modify title if dynamic, if not set : title is taken as is |
---|
347 | * 'active_cb' => (optional) callback to tell whether current page matches favorite or not, for complex pages |
---|
348 | * |
---|
349 | * @access public |
---|
350 | */ |
---|
351 | public function register($id,$data) { |
---|
352 | $this->fav_defs[$id] = $data; |
---|
353 | return $this; |
---|
354 | } |
---|
355 | |
---|
356 | /** |
---|
357 | * registerMultiple - registers a list of favorites definition |
---|
358 | * |
---|
359 | * @param array an array defining all favorites key is the id, value is the data. |
---|
360 | * see register method for data format |
---|
361 | * @access public |
---|
362 | */ |
---|
363 | public function registerMultiple($data) { |
---|
364 | foreach ($data as $k=>$v) { |
---|
365 | $this->register($k,$v); |
---|
366 | } |
---|
367 | return $this; |
---|
368 | } |
---|
369 | |
---|
370 | /** |
---|
371 | * exists - tells whether a fav definition exists or not |
---|
372 | * |
---|
373 | * @param string $id : the fav id to test |
---|
374 | * |
---|
375 | * @access public |
---|
376 | * |
---|
377 | * @return true if the fav definition exists, false otherwise |
---|
378 | */ |
---|
379 | public function exists($id) { |
---|
380 | return isset($this->fav_defs[$id]); |
---|
381 | } |
---|
382 | |
---|
383 | } |
---|
384 | |
---|
385 | |
---|
386 | /** |
---|
387 | * defaultFavorites -- default favorites definition |
---|
388 | * |
---|
389 | */ |
---|
390 | class defaultFavorites |
---|
391 | { |
---|
392 | public static function initDefaultFavorites($favs) { |
---|
393 | $favs->registerMultiple(array( |
---|
394 | 'prefs' => array( |
---|
395 | 'title' => __('My preferences'), |
---|
396 | 'url' => 'preferences.php', |
---|
397 | 'small-icon' => 'images/menu/user-pref.png', |
---|
398 | 'large-icon' => 'images/menu/user-pref-b.png'), |
---|
399 | 'new_post' => array( |
---|
400 | 'title' => __('New entry'), |
---|
401 | 'url' => 'post.php', |
---|
402 | 'small-icon' => 'images/menu/edit.png', |
---|
403 | 'large-icon' => 'images/menu/edit-b.png', |
---|
404 | 'permissions' =>'usage,contentadmin', |
---|
405 | 'id' => 'menu-new-post'), |
---|
406 | 'posts' => array( |
---|
407 | 'title' => __('Entries'), |
---|
408 | 'url' => 'posts.php', |
---|
409 | 'small-icon' => 'images/menu/entries.png', |
---|
410 | 'large-icon' => 'images/menu/entries-b.png', |
---|
411 | 'permissions' => 'usage,contentadmin', |
---|
412 | 'dashboard_cb' => array('defaultFavorites','postsDashboard')), |
---|
413 | 'comments' => array( |
---|
414 | 'title' => __('Comments'), |
---|
415 | 'url' => 'comments.php', |
---|
416 | 'small-icon' => 'images/menu/comments.png', |
---|
417 | 'large-icon' => 'images/menu/comments-b.png', |
---|
418 | 'permissions' => 'usage,contentadmin', |
---|
419 | 'dashboard_cb' => array('defaultFavorites','commentsDashboard')), |
---|
420 | 'search' => array( |
---|
421 | 'title' => __('Search'), |
---|
422 | 'url' => 'search.php', |
---|
423 | 'small-icon' => 'images/menu/search.png', |
---|
424 | 'large-icon' => 'images/menu/search-b.png', |
---|
425 | 'permissions' => 'usage,contentadmin'), |
---|
426 | 'categories' => array( |
---|
427 | 'title' => __('Categories'), |
---|
428 | 'url' => 'categories.php', |
---|
429 | 'small-icon' => 'images/menu/categories.png', |
---|
430 | 'large-icon' => 'images/menu/categories-b.png', |
---|
431 | 'permissions' =>'categories'), |
---|
432 | 'media' => array( |
---|
433 | 'title' => __('Media manager'), |
---|
434 | 'url' => 'media.php', |
---|
435 | 'small-icon' => 'images/menu/media.png', |
---|
436 | 'large-icon' => 'images/menu/media-b.png', |
---|
437 | 'permissions' => 'media, media_admin'), |
---|
438 | 'blog_pref' => array( |
---|
439 | 'title' => __('Blog settings'), |
---|
440 | 'url' => 'blog_pref.php', |
---|
441 | 'small-icon' => 'images/menu/blog-pref.png', |
---|
442 | 'large-icon' => 'images/menu/blog-pref-b.png', |
---|
443 | 'permissions' => 'admin'), |
---|
444 | 'blog_theme' => array( |
---|
445 | 'title' => __('Blog appearance'), |
---|
446 | 'url' => 'blog_theme.php', |
---|
447 | 'small-icon' => 'images/menu/themes.png', |
---|
448 | 'large-icon' => 'images/menu/blog-theme-b.png', |
---|
449 | 'permissions' => 'admin'), |
---|
450 | 'blogs' => array( |
---|
451 | 'title' => __('Blogs'), |
---|
452 | 'url' => 'blogs.php', |
---|
453 | 'small-icon' => 'images/menu/blogs.png', |
---|
454 | 'large-icon' => 'images/menu/blogs-b.png', |
---|
455 | 'permissions' =>'usage,contentadmin'), |
---|
456 | 'users' => array( |
---|
457 | 'title' => __('Users'), |
---|
458 | 'url' => 'users.php', |
---|
459 | 'small-icon' => 'images/menu/users.png', |
---|
460 | 'large-icon' => 'images/menu/users-b.png'), |
---|
461 | 'plugins' => array( |
---|
462 | 'title' => __('Plugins management'), |
---|
463 | 'url' => 'plugins.php', |
---|
464 | 'small-icon' => 'images/menu/plugins.png', |
---|
465 | 'large-icon' => 'images/menu/plugins-b.png'), |
---|
466 | 'langs' => array( |
---|
467 | 'title' => __('Languages'), |
---|
468 | 'url' => 'langs.php', |
---|
469 | 'small-icon' => 'images/menu/langs.png', |
---|
470 | 'large-icon' => 'images/menu/langs-b.png'), |
---|
471 | 'help' => array( |
---|
472 | 'title' => __('Global help'), |
---|
473 | 'url' => 'help.php', |
---|
474 | 'small-icon' => 'images/menu/help.png', |
---|
475 | 'large-icon' => 'images/menu/help-b.png') |
---|
476 | )); |
---|
477 | } |
---|
478 | |
---|
479 | public static function postsDashboard($core,$v) { |
---|
480 | $post_count = $core->blog->getPosts(array(),true)->f(0); |
---|
481 | $str_entries = __('%d entry', '%d entries',$post_count); |
---|
482 | $v['title'] = sprintf($str_entries,$post_count); |
---|
483 | } |
---|
484 | |
---|
485 | public static function commentsDashboard($core,$v) { |
---|
486 | $comment_count = $core->blog->getComments(array(),true)->f(0); |
---|
487 | $str_comments = __('%d comments', '%d comments',$comment_count); |
---|
488 | $v['title']= sprintf($str_comments,$comment_count); |
---|
489 | } |
---|
490 | |
---|
491 | |
---|
492 | } |
---|