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 | * @ingroup DC_CORE |
---|
16 | * @brief Error class |
---|
17 | * |
---|
18 | * dcError is a very simple error class, with a stack. Call dcError::add to |
---|
19 | * add an error in stack. In administration area, errors are automatically |
---|
20 | * displayed. |
---|
21 | */ |
---|
22 | class dcError |
---|
23 | { |
---|
24 | /** @var array Errors stack */ |
---|
25 | protected $errors = array(); |
---|
26 | /** @var boolean True if stack is not empty */ |
---|
27 | protected $flag = false; |
---|
28 | /** @var string HTML errors list pattern */ |
---|
29 | protected $html_list = "<ul>\n%s</ul>\n"; |
---|
30 | /** @var string HTML error item pattern */ |
---|
31 | protected $html_item = "<li>%s</li>\n"; |
---|
32 | /** @var string HTML error single pattern */ |
---|
33 | protected $html_single = "<p>%s</p>\n"; |
---|
34 | |
---|
35 | /** |
---|
36 | * Object string representation. Returns errors stack. |
---|
37 | * |
---|
38 | * @return string |
---|
39 | */ |
---|
40 | public function __toString() |
---|
41 | { |
---|
42 | $res = ''; |
---|
43 | |
---|
44 | foreach ($this->errors as $msg) |
---|
45 | { |
---|
46 | $res .= $msg."\n"; |
---|
47 | } |
---|
48 | |
---|
49 | return $res; |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * Adds an error to stack. |
---|
54 | * |
---|
55 | * @param string $msg Error message |
---|
56 | */ |
---|
57 | public function add($msg) |
---|
58 | { |
---|
59 | $this->flag = true; |
---|
60 | $this->errors[] = $msg; |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * Returns the value of <var>flag</var> property. True if errors stack is not empty |
---|
65 | * |
---|
66 | * @return boolean |
---|
67 | */ |
---|
68 | public function flag() |
---|
69 | { |
---|
70 | return $this->flag; |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * Resets errors stack. |
---|
75 | */ |
---|
76 | public function reset() |
---|
77 | { |
---|
78 | $this->flag = false; |
---|
79 | $this->errors = array(); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Returns <var>errors</var> property. |
---|
84 | * |
---|
85 | * @return array |
---|
86 | */ |
---|
87 | public function getErrors() |
---|
88 | { |
---|
89 | return $this->errors; |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * Sets <var>list</var> and <var>item</var> properties. |
---|
94 | * |
---|
95 | * @param string $list HTML errors list pattern |
---|
96 | * @param string $item HTML error item pattern |
---|
97 | */ |
---|
98 | public function setHTMLFormat($list,$item,$single=null) |
---|
99 | { |
---|
100 | $this->html_list = $list; |
---|
101 | $this->html_item = $item; |
---|
102 | if ($single) { |
---|
103 | $this->html_single = $single; |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * Returns errors stack as HTML. |
---|
109 | * |
---|
110 | * @return string |
---|
111 | */ |
---|
112 | public function toHTML() |
---|
113 | { |
---|
114 | $res = ''; |
---|
115 | |
---|
116 | if ($this->flag) |
---|
117 | { |
---|
118 | if (count($this->errors == 1)) { |
---|
119 | $res = sprintf($this->html_single,$this->errors[0]); |
---|
120 | } else { |
---|
121 | foreach ($this->errors as $msg) { |
---|
122 | $res .= sprintf($this->html_item,$msg); |
---|
123 | } |
---|
124 | $res = sprintf($this->html_list,$res); |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | return $res; |
---|
129 | } |
---|
130 | } |
---|