Pluf Framework

Pluf Framework Git Source Tree

Root/src/Pluf/Error.php

1<?php
2/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3/*
4# ***** BEGIN LICENSE BLOCK *****
5# This file is part of Plume CMS, a website management application.
6# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
7#
8# Plume CMS is free software; you can redistribute it and/or modify
9# it under the terms of the GNU Lesser General Public License as published by
10# the Free Software Foundation; either version 2.1 of the License, or
11# (at your option) any later version.
12#
13# Plume CMS is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU Lesser General Public License for more details.
17#
18# You should have received a copy of the GNU Lesser General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21#
22# ***** END LICENSE BLOCK ***** */
23
24
25/**
26 * Error handling class.
27 *
28 * @credits Basis idea from Olivier Meunier
29 */
30class Pluf_Error
31{
32 private $error = array(); /**< Current errors. */
33
34
35 /**
36 * Reset the errors
37 */
38 function resetError()
39 {
40 $this->error = array();
41 }
42
43 /**
44 * Set an error.
45 *
46 * By convention the number is 4xx if the error is coming from
47 * the user or 5xx if coming from the system (database error for ex.)
48 *
49 * @param string Error message
50 * @param int Error number (0)
51 */
52 function setError($msg, $no=0)
53 {
54 $this->error[] = array($no,$msg);
55 }
56
57
58 /**
59 * Returns the errors.
60 *
61 * @param bool Errors as HTML list (false)
62 * @param bool Show numbers (true)
63 * @return mixed array of errors, HTML list, or false if no errors
64 */
65 function error($html=false, $with_nb=true)
66 {
67 if (count($this->error) > 0) {
68 if (!$html) {
69 return $this->error;
70 } else {
71 $res = '<ul>'."\n";
72 foreach($this->error as $v) {
73 $res .= '<li>'.
74 (($with_nb) ?
75 '<span class="errno">'.$v[0].'</span> - ' :
76 '').
77 '<span class="errmsg">'.$v[1].'</span></li>'."\n";
78 }
79 return $res."</ul>\n";
80 }
81 } else {
82 return false;
83 }
84 }
85
86 /**
87 * Helper function to set the error from the DB.
88 *
89 * @param string Error message from the DB
90 */
91 function setDbError($db_error_msg)
92 {
93 $this->setError(__('DB error:').' '.$db_error_msg, 500);
94 }
95
96 /**
97 * Bulk set errors.
98 *
99 * Used when you went to recopy the errors of one object into
100 * another. You can call that way:
101 * $object->bulkSetErrors($otherobject->error());
102 *
103 * @param array List of errors
104 * @return bool Success
105 */
106 function bulkSetError($errors)
107 {
108 if (!is_array($errors)) {
109 return false;
110 }
111 foreach ($errors as $e) {
112 $this->setError($e[1], $e[0]);
113 }
114 return true;
115 }
116
117}
118

Archive Download this file

Branches

Tags