| 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 | |
|---|
| 33 | /** |
|---|
| 34 | * Object constructor. |
|---|
| 35 | */ |
|---|
| 36 | public function __construct() |
|---|
| 37 | { |
|---|
| 38 | $this->code = 0; |
|---|
| 39 | $this->msg = ''; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * Object string representation. Returns errors stack. |
|---|
| 44 | * |
|---|
| 45 | * @return string |
|---|
| 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 | /** |
|---|
| 60 | * Adds an error to stack. |
|---|
| 61 | * |
|---|
| 62 | * @param string $msg Error message |
|---|
| 63 | */ |
|---|
| 64 | public function add($msg) |
|---|
| 65 | { |
|---|
| 66 | $this->flag = true; |
|---|
| 67 | $this->errors[] = $msg; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | /** |
|---|
| 71 | * Returns the value of <var>flag</var> property. True if errors stack is not empty |
|---|
| 72 | * |
|---|
| 73 | * @return boolean |
|---|
| 74 | */ |
|---|
| 75 | public function flag() |
|---|
| 76 | { |
|---|
| 77 | return $this->flag; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | /** |
|---|
| 81 | * Resets errors stack. |
|---|
| 82 | */ |
|---|
| 83 | public function reset() |
|---|
| 84 | { |
|---|
| 85 | $this->flag = false; |
|---|
| 86 | $this->errors = array(); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /** |
|---|
| 90 | * Returns <var>errors</var> property. |
|---|
| 91 | * |
|---|
| 92 | * @return array |
|---|
| 93 | */ |
|---|
| 94 | public function getErrors() |
|---|
| 95 | { |
|---|
| 96 | return $this->errors; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | /** |
|---|
| 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 |
|---|
| 104 | */ |
|---|
| 105 | public function setHTMLFormat($list,$item) |
|---|
| 106 | { |
|---|
| 107 | $this->html_list = $list; |
|---|
| 108 | $this->html_item = $item; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | /** |
|---|
| 112 | * Returns errors stack as HTML. |
|---|
| 113 | * |
|---|
| 114 | * @return string |
|---|
| 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 | } |
|---|