[0] | 1 | <?php |
---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
| 3 | # |
---|
| 4 | # This file is part of Dotclear 2. |
---|
| 5 | # |
---|
[1179] | 6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
---|
[0] | 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 | /** |
---|
[334] | 15 | * @ingroup DC_CORE |
---|
| 16 | * @brief Error class |
---|
[2566] | 17 | * |
---|
[334] | 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. |
---|
[0] | 21 | */ |
---|
| 22 | class dcError |
---|
| 23 | { |
---|
[334] | 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"; |
---|
[2566] | 32 | |
---|
[0] | 33 | /** |
---|
[334] | 34 | * Object string representation. Returns errors stack. |
---|
[2566] | 35 | * |
---|
[334] | 36 | * @return string |
---|
[0] | 37 | */ |
---|
| 38 | public function __toString() |
---|
| 39 | { |
---|
| 40 | $res = ''; |
---|
[2566] | 41 | |
---|
[0] | 42 | foreach ($this->errors as $msg) |
---|
| 43 | { |
---|
| 44 | $res .= $msg."\n"; |
---|
| 45 | } |
---|
[2566] | 46 | |
---|
[0] | 47 | return $res; |
---|
| 48 | } |
---|
[2566] | 49 | |
---|
[0] | 50 | /** |
---|
[334] | 51 | * Adds an error to stack. |
---|
[2566] | 52 | * |
---|
[334] | 53 | * @param string $msg Error message |
---|
[0] | 54 | */ |
---|
| 55 | public function add($msg) |
---|
| 56 | { |
---|
| 57 | $this->flag = true; |
---|
| 58 | $this->errors[] = $msg; |
---|
| 59 | } |
---|
[2566] | 60 | |
---|
[0] | 61 | /** |
---|
[334] | 62 | * Returns the value of <var>flag</var> property. True if errors stack is not empty |
---|
[2566] | 63 | * |
---|
[334] | 64 | * @return boolean |
---|
[0] | 65 | */ |
---|
| 66 | public function flag() |
---|
| 67 | { |
---|
| 68 | return $this->flag; |
---|
| 69 | } |
---|
[2566] | 70 | |
---|
[0] | 71 | /** |
---|
[334] | 72 | * Resets errors stack. |
---|
[0] | 73 | */ |
---|
| 74 | public function reset() |
---|
| 75 | { |
---|
| 76 | $this->flag = false; |
---|
| 77 | $this->errors = array(); |
---|
| 78 | } |
---|
[2566] | 79 | |
---|
[0] | 80 | /** |
---|
[334] | 81 | * Returns <var>errors</var> property. |
---|
[2566] | 82 | * |
---|
[334] | 83 | * @return array |
---|
[0] | 84 | */ |
---|
| 85 | public function getErrors() |
---|
| 86 | { |
---|
| 87 | return $this->errors; |
---|
| 88 | } |
---|
[2566] | 89 | |
---|
[0] | 90 | /** |
---|
[334] | 91 | * Sets <var>list</var> and <var>item</var> properties. |
---|
[2566] | 92 | * |
---|
[334] | 93 | * @param string $list HTML errors list pattern |
---|
| 94 | * @param string $item HTML error item pattern |
---|
[0] | 95 | */ |
---|
| 96 | public function setHTMLFormat($list,$item) |
---|
| 97 | { |
---|
| 98 | $this->html_list = $list; |
---|
| 99 | $this->html_item = $item; |
---|
| 100 | } |
---|
[2566] | 101 | |
---|
[0] | 102 | /** |
---|
[334] | 103 | * Returns errors stack as HTML. |
---|
[2566] | 104 | * |
---|
[334] | 105 | * @return string |
---|
[0] | 106 | */ |
---|
| 107 | public function toHTML() |
---|
| 108 | { |
---|
| 109 | $res = ''; |
---|
[2566] | 110 | |
---|
[0] | 111 | if ($this->flag) |
---|
| 112 | { |
---|
| 113 | foreach ($this->errors as $msg) |
---|
| 114 | { |
---|
| 115 | $res .= sprintf($this->html_item,$msg); |
---|
| 116 | } |
---|
[2566] | 117 | |
---|
[0] | 118 | $res = sprintf($this->html_list,$res); |
---|
| 119 | } |
---|
[2566] | 120 | |
---|
[0] | 121 | return $res; |
---|
| 122 | } |
---|
| 123 | } |
---|