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