[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 |
---|
| 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. |
---|
[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"; |
---|
[0] | 32 | |
---|
| 33 | /** |
---|
[334] | 34 | * Object constructor. |
---|
[0] | 35 | */ |
---|
| 36 | public function __construct() |
---|
| 37 | { |
---|
| 38 | $this->code = 0; |
---|
| 39 | $this->msg = ''; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | /** |
---|
[334] | 43 | * Object string representation. Returns errors stack. |
---|
| 44 | * |
---|
| 45 | * @return string |
---|
[0] | 46 | */ |
---|
| 47 | public function __toString() |
---|
| 48 | { |
---|
| 49 | $res = ''; |
---|
| 50 | |
---|
| 51 | foreach ($this->errors as $msg) |
---|
| 52 | { |
---|
| 53 | $res .= $msg."\n"; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | return $res; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | /** |
---|
[334] | 60 | * Adds an error to stack. |
---|
| 61 | * |
---|
| 62 | * @param string $msg Error message |
---|
[0] | 63 | */ |
---|
| 64 | public function add($msg) |
---|
| 65 | { |
---|
| 66 | $this->flag = true; |
---|
| 67 | $this->errors[] = $msg; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | /** |
---|
[334] | 71 | * Returns the value of <var>flag</var> property. True if errors stack is not empty |
---|
| 72 | * |
---|
| 73 | * @return boolean |
---|
[0] | 74 | */ |
---|
| 75 | public function flag() |
---|
| 76 | { |
---|
| 77 | return $this->flag; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | /** |
---|
[334] | 81 | * Resets errors stack. |
---|
[0] | 82 | */ |
---|
| 83 | public function reset() |
---|
| 84 | { |
---|
| 85 | $this->flag = false; |
---|
| 86 | $this->errors = array(); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | /** |
---|
[334] | 90 | * Returns <var>errors</var> property. |
---|
| 91 | * |
---|
| 92 | * @return array |
---|
[0] | 93 | */ |
---|
| 94 | public function getErrors() |
---|
| 95 | { |
---|
| 96 | return $this->errors; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /** |
---|
[334] | 100 | * Sets <var>list</var> and <var>item</var> properties. |
---|
| 101 | * |
---|
| 102 | * @param string $list HTML errors list pattern |
---|
| 103 | * @param string $item HTML error item pattern |
---|
[0] | 104 | */ |
---|
| 105 | public function setHTMLFormat($list,$item) |
---|
| 106 | { |
---|
| 107 | $this->html_list = $list; |
---|
| 108 | $this->html_item = $item; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /** |
---|
[334] | 112 | * Returns errors stack as HTML. |
---|
| 113 | * |
---|
| 114 | * @return string |
---|
[0] | 115 | */ |
---|
| 116 | public function toHTML() |
---|
| 117 | { |
---|
| 118 | $res = ''; |
---|
| 119 | |
---|
| 120 | if ($this->flag) |
---|
| 121 | { |
---|
| 122 | foreach ($this->errors as $msg) |
---|
| 123 | { |
---|
| 124 | $res .= sprintf($this->html_item,$msg); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | $res = sprintf($this->html_list,$res); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | return $res; |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | ?> |
---|