Dotclear

source: inc/admin/class.dc.favorites.php @ 3731:3770620079d4

Revision 3731:3770620079d4, 17.6 KB checked in by franck <carnet.franck.paul@…>, 7 years ago (diff)

Simplify licence block at the beginning of each file

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

Sites map