Dotclear

source: inc/admin/class.dc.notices.php @ 3954:315a5aa7a8b7

Revision 3954:315a5aa7a8b7, 4.7 KB checked in by franck <carnet.franck.paul@…>, 7 years ago (diff)

dcNotices method don't need to be static

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 * dcNotices -- Backend notices handling facilities
14 *
15 */
16class dcNotices
17{
18    /** @var dcCore dotclear core instance */
19    protected $core;
20
21    private $N_TYPES = [
22        // id → CSS class
23        "success" => "success",
24        "warning" => "warning-msg",
25        "error"   => "error",
26        "message" => "message",
27        "static"  => "static-msg"];
28
29    private $error_displayed = false;
30
31    /**
32     * Class constructor
33     *
34     * @param mixed  $core   dotclear core
35     *
36     * @access public
37     *
38     * @return mixed Value.
39     */
40    public function __construct($core)
41    {
42        $this->core = $core;
43    }
44
45    /* Session based notices */
46
47    public function getNotices()
48    {
49        $res = '';
50
51        // return error messages if any
52        if ($this->core->error->flag() && !$this->error_displayed) {
53
54            # --BEHAVIOR-- adminPageNotificationError
55            $notice_error = $this->core->callBehavior('adminPageNotificationError', $this->core, $this->core->error);
56
57            if (isset($notice_error) && !empty($notice_error)) {
58                $res .= $notice_error;
59            } else {
60                $res .= '<div class="error"><p>' .
61                '<strong>' . (count($this->core->error->getErrors()) > 1 ? __('Errors:') : __('Error:')) . '</strong>' .
62                '</p>' . $this->core->error->toHTML() . '</div>';
63            }
64            $this->error_displayed = true;
65        }
66
67        // return notices if any
68        if (isset($_SESSION['notifications'])) {
69            foreach ($_SESSION['notifications'] as $notification) {
70                # --BEHAVIOR-- adminPageNotification
71                $notice = $this->core->callBehavior('adminPageNotification', $this->core, $notification);
72
73                $res .= (isset($notice) && !empty($notice) ? $notice : $this->getNotification($notification));
74            }
75            unset($_SESSION['notifications']);
76            // unset seems not to be sufficient, so initialize to an empty array
77            $_SESSION['notifications'] = [];
78        }
79        return $res;
80    }
81
82    public function addNotice($type, $message, $options = [])
83    {
84        if (isset($this->N_TYPES[$type])) {
85            $class = $this->N_TYPES[$type];
86        } else {
87            $class = $type;
88        }
89        if (isset($_SESSION['notifications']) && is_array($_SESSION['notifications'])) {
90            $notifications = $_SESSION['notifications'];
91        } else {
92            $notifications = [];
93        }
94
95        $n = array_merge($options, ['class' => $class, 'ts' => time(), 'text' => $message]);
96        if ($type != "static") {
97            $notifications[] = $n;
98        } else {
99            array_unshift($notifications, $n);
100        }
101        $_SESSION['notifications'] = $notifications;
102    }
103
104    public function addSuccessNotice($message, $options = [])
105    {
106        $this->addNotice("success", $message, $options);
107    }
108
109    public function addWarningNotice($message, $options = [])
110    {
111        $this->addNotice("warning", $message, $options);
112    }
113
114    public function addErrorNotice($message, $options = [])
115    {
116        $this->addNotice("error", $message, $options);
117    }
118
119    protected function getNotification($n)
120    {
121        $tag = (isset($n['divtag']) && $n['divtag']) ? 'div' : 'p';
122        $ts  = '';
123        if (!isset($n['with_ts']) || ($n['with_ts'] == true)) {
124            $ts = dt::str(__('[%H:%M:%S]'), $n['ts'], $this->core->auth->getInfo('user_tz')) . ' ';
125        }
126        $res = '<' . $tag . ' class="' . $n['class'] . '" role="alert">' . $ts . $n['text'] . '</' . $tag . '>';
127        return $res;
128    }
129
130    /* Direct messages, usually immediately displayed */
131
132    public function message($msg, $timestamp = true, $div = false, $echo = true, $class = 'message')
133    {
134        $res = '';
135        if ($msg != '') {
136            $res = ($div ? '<div class="' . $class . '">' : '') . '<p' . ($div ? '' : ' class="' . $class . '"') . '>' .
137                ($timestamp ? dt::str(__('[%H:%M:%S]'), null, $this->core->auth->getInfo('user_tz')) . ' ' : '') . $msg .
138                '</p>' . ($div ? '</div>' : '');
139            if ($echo) {
140                echo $res;
141            }
142        }
143        return $res;
144    }
145
146    public function success($msg, $timestamp = true, $div = false, $echo = true)
147    {
148        return $this->message($msg, $timestamp, $div, $echo, "success");
149    }
150
151    public function warning($msg, $timestamp = true, $div = false, $echo = true)
152    {
153        return $this->message($msg, $timestamp, $div, $echo, "warning-msg");
154    }
155}
Note: See TracBrowser for help on using the repository browser.

Sites map