Dotclear

source: inc/admin/class.dc.favorites.php @ 2247:931dddd634d0

Revision 2247:931dddd634d0, 14.3 KB checked in by franck <carnet.franck.paul@…>, 12 years ago (diff)

menu-new-post id is no more useful

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

Sites map