Dotclear

source: inc/admin/class.dc.favorites.php @ 2229:d5e819e27bea

Revision 2229:d5e819e27bea, 14.1 KB checked in by Dsls, 12 years ago (diff)

reworked favorites handling (wip)

Line 
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 -----------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14/**
15* dcFavorites -- Favorites handling facilities
16*
17*/
18class 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->setUserPrefs();
81     }
82
83   /**
84     * getFavorite - retrieves a favorite (complete description) from its id.
85     *
86     * @param string  $id   the favorite id, or an array having 1 key 'name' set to id, ther keys are merged to favorite.
87     *
88     * @access public
89     *
90     * @return array the favorite, false if not found (or not permitted)
91     */
92     public function getFavorite($p) {
93          if (is_array($p)) {
94               $fname=$p['name'];
95               if (!isset($this->fav_defs[$fname])) {
96                    return false;
97               }
98               $fattr = $p;
99               unset($fattr['name']);
100               $fattr = array_merge($this->fav_defs[$fname],$fattr);
101          } else {
102               if (!isset($this->fav_defs[$p])) {
103                    return false;
104               }
105               $fattr = $this->fav_defs[$p];
106          }
107          $fattr = array_merge (array('id' => null,'class'=>null),$fattr);
108          if (!isset($fattr['permissions']) || $this->core->auth->check($fattr['permissions'],$this->core->blog->id)) {
109               return $fattr;
110          } else {
111               return false;
112          }
113     }
114     
115   /**
116     * getFavorites - retrieves a list of favorites.
117     *
118     * @param string  $ids   an array of ids, as defined in getFavorite.
119     *
120     * @access public
121     *
122     * @return array array of favorites, can be empty if ids are not found (or not permitted)
123     */
124     public function getFavorites($ids) {
125          $prefs = array();
126          foreach ($ids as $id) {
127               $f = $this->getFavorite($id);
128               if ($f !== false) {
129                    $prefs[$id]=$f;
130               }
131          }
132          return $prefs;
133     }
134         
135   /**
136     * setUserPrefs - get user favorites from settings. These are complete favorites, not ids only
137     *                   returned favorites are the first non-empty list from :
138      *                   * user-defined favorites
139      *                   * globally-defined favorites
140      *                   * a failback list "new post" (shall never be empty)
141      *                  This method is called by ::setup()
142     * @access protected
143     *
144     */
145     protected function setUserPrefs() {
146          $this->user_prefs = $this->getFavorites($this->local_prefs);
147          if (!count($this->user_prefs)) {
148               $this->user_prefs = $this->getFavorites($this->global_prefs);
149          }
150          if (!count($this->user_prefs)) {
151               $this->user_prefs = $this->getFavorites(array('new_post'));
152          }
153
154          // Loop over prefs to enable active favorites
155          foreach ($this->user_prefs as $k=>&$v) {
156               if (isset($v['active_cb']) && is_callable($v['active_cb'])) {
157                    // Use callback if defined to match whether favorite is active or not
158                    $v['active'] = call_user_func($v['active_cb'],$_SERVER['REQUEST_URI'],$_REQUEST);
159               } else {
160                    // Failback active detection. We test against URI name & parameters
161                    $v['active']=true; // true until something proves it is false
162                    $u = explode('?',$v['url'],2);
163                    if (!preg_match('/'.preg_quote($u[0],"/").'/',$_SERVER['REQUEST_URI'])) {
164                         $v['active'] = false; // no URI match
165                    }
166                    if (count($u) == 2) {
167                         parse_str($u[1],$p);
168                         // test against each request parameter.
169                         foreach ($p as $k2 => $v2) {
170                              if (!isset($_REQUEST[$k2]) || $_REQUEST[$k2] !== $v2) {
171                                   $v['active'] = false;
172                              }
173                         }
174                    }
175               }
176          }
177               
178     }
179     
180   /**
181     * migrateFavorites - migrate dc < 2.6 favorites to new format
182      *
183     * @access protected
184     *
185     */
186     protected function migrateFavorites() {
187          $fav_ws = $this->core->auth->user_prefs->addWorkspace('favorites');
188          $this->local_prefs=array();
189          $this->global_prefs=array();
190          foreach ($fav_ws->dumpPrefs() as $k => $v) {
191               $fav = @unserialize($v['value']);
192               if (is_array($fav)) {
193                    if ($v['global']) {
194                         $this->global_prefs[] = $fav['name'];
195                    } else {
196                         $this->local_prefs[] = $fav['name'];
197                    }
198               }
199          }
200          $this->ws->put('favorites',serialize($this->global_prefs),'string','User favorites',true,true);
201          $this->ws->put('favorites',serialize($this->local_prefs));
202          $this->user_prefs = $this->getFavorites($this->local_prefs);
203     }
204
205     
206     
207   /**
208     * legacyFavorites - handle legacy favorites using adminDashboardFavs behavior
209      *
210     * @access protected
211     *
212     */
213     protected function legacyFavorites() {
214          $f = new ArrayObject();
215          $this->core->callBehavior('adminDashboardFavs', $this->core, $f);
216          foreach ($f as $k => $v) {
217               $fav = array (
218                    'title' => __($v[1]),
219                    'url' => $v[2],
220                    'small-icon' => $v[3],
221                    'large-icon' => $v[4],
222                    'id' => $v[5],
223                    'class' => $v[6]
224               );
225               $this->register ($v[0], $fav);
226          }
227         
228     }
229     
230   /**
231     * getUserFavorites - returns favorites that correspond to current user
232      *   (may be local, global, or failback favorites)
233      *
234     * @access public
235     *
236     * @return array array of favorites (enriched)
237     */
238     public function getUserFavorites() {
239          return $this->user_prefs;
240     }
241     
242
243   /**
244     * getFavoriteIDs - returns user-defined or global favorites ids list
245      *                       shall not be called outside preferences.php...
246      *
247     * @param boolean  $global   if true, retrieve global favs, user favs otherwise
248      *
249     * @access public
250     *
251     * @return array array of favorites ids (only ids, not enriched)
252     */
253     public function getFavoriteIDs ($global=false) {
254          return $global?$this->global_prefs:$this->local_prefs;
255     }
256
257   /**
258     * setFavoriteIDs - stores user-defined or global favorites ids list
259      *                       shall not be called outside preferences.php...
260      *
261     * @param array  $ids   list of fav ids
262     * @param boolean  $global   if true, retrieve global favs, user favs otherwise
263      *
264     * @access public
265     */
266     public function setFavoriteIDs($ids,$global=false) {
267          $this->ws->put('favorites',serialize($ids),null,null,true,$global);
268     }
269     
270   /**
271     * getAvailableFavoritesIDs - returns all available fav ids
272      *
273     * @access public
274     *
275     * @return array array of favorites ids (only ids, not enriched)
276     */
277     public function getAvailableFavoritesIDs () {
278          return array_keys($this->fav_defs->getArrayCopy());
279     }
280
281   /**
282     * appendMenuTitle - adds favorites section title to sidebar menu
283      *                       shall not be called outside admin/prepend.php...
284      *
285     * @param dcMenu  $menu   admin menu instance
286      *
287     * @access public
288     */
289     public function appendMenuTitle($menu) {
290          $menu['Favorites'] = new dcMenu('favorites-menu','My favorites');
291          $menu['Favorites']->title = __('My favorites');
292     }
293
294   /**
295     * appendMenu - adds favorites items title to sidebar menu
296      *                       shall not be called outside admin/prepend.php...
297      *
298     * @param dcMenu  $menu   admin menu instance
299      *
300     * @access public
301     */
302     public function appendMenu($menu) {
303          foreach ($this->user_prefs as $k=>$v) {
304               $menu['Favorites']->addItem(
305                    $v['title'],
306                    $v['url'],
307                    $v['small-icon'],
308                    $v['active'],
309                    true,
310                    $v['id'],
311                    $v['class']
312               );
313          }
314     }
315     
316   /**
317     * appendDashboardIcons - adds favorites icons to index page
318      *                       shall not be called outside admin/index.php...
319      *
320     * @param array  $icons   dashboard icon list to enrich
321      *
322     * @access public
323     */
324     public function appendDashboardIcons($icons) {
325          foreach ($this->user_prefs as $k=>$v) {
326               if (isset($v['title_cb']) && is_callable($v['title_cb'])) {
327                    $title = call_user_func($v['title_cb'],$this->core);
328               } else {
329                    $title = $v['title'];
330               }
331               $icons[$k]=new ArrayObject(array($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      *   'title_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*/
390class 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                    'title_cb' => array('defaultFavorites','postsTitle')),
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                    'title_cb' => array('defaultFavorites','commentsTitle')),
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 postsTitle($core) {
480          $post_count = $core->blog->getPosts(array(),true)->f(0);
481          $str_entries = __('%d entry', '%d entries',$post_count);
482          return sprintf($str_entries,$post_count);
483     }
484     
485     public static function commentsTitle($core) {
486          $comment_count = $core->blog->getComments(array(),true)->f(0);
487          $str_comments = __('%d comments', '%d comments',$comment_count);
488          return sprintf($str_comments,$comment_count);
489     }
490
491
492}
Note: See TracBrowser for help on using the repository browser.

Sites map