Project

General

Profile

wb-archiv283 / branches / 2.8.x / wb / framework / msgQueue.php @ 2134

1
<?php
2

    
3
/*
4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19

    
20
/**
21
 * msgQueue.php
22
 *
23
 * @category     Core
24
 * @package      Core_Helpers
25
 * @copyright    Manuela v.d.Decken <manuela@isteam.de>
26
 * @author       Manuela v.d.Decken <manuela@isteam.de>
27
 * @license      http://www.gnu.org/licenses/gpl.html   GPL License
28
 * @version      0.0.1
29
 * @revision     $Revision: 2134 $
30
 * @link         $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/msgQueue.php $
31
 * @lastmodified $Date: 2015-08-31 23:57:34 +0200 (Mon, 31 Aug 2015) $
32
 * @since        File available since 09.05.2015
33
 * @description  provides a message queue to display system messages on each screens
34
 */
35
class msgQueue {
36

    
37
/** define type of retval */
38
    const RETVAL_ARRAY  = 0;
39
    const RETVAL_STRING = 1; // (default)
40
/** */
41
    const LOG      = 0;
42
    const ERROR    = 1; // (default)
43
    const SUCCESS  = 2;
44
    const OK       = 2;
45
    const WARN     = 4;
46
    const SHOW_ALL = 0;
47
    const ALL      = 0;
48
/**  */
49
    private static $_instance;
50
/**  */
51
    private $aLogs    = array();
52
/**
53
 * constructor
54
 */
55
    protected function __construct() {
56
        $this->aLogs = array(
57
            self::ERROR   => array(),
58
            self::SUCCESS => array(),
59
            self::WARN    => array(),
60
            self::LOG     => array()
61
        );
62
    }
63
/** disable cloning */
64
    private function __clone() { }
65
/**
66
 * get handle for active instance
67
 */
68
    public static function getInstance()
69
    {
70
        if (!isset(self::$_instance)) {
71
            $c = __CLASS__;
72
            self::$_instance = new $c;
73
        }
74
        return self::$_instance;
75
    }
76
/**
77
 * push new message in queue
78
 * @param string $sMessage
79
 * @param int $iStatus (default: self::FAIL)
80
 */
81
    public static function add($sMessage = '', $iStatus = self::ERROR)
82
    {
83
        $iStatus =
84
            ($iStatus === true ? self::OK : ($iStatus === false ? self::ERROR : $iStatus)) ?: self::ERROR;
85
        self::getInstance()->aLogs[self::LOG][] = array('status'=>self::WARN, 'msg'=>$sMessage);
86
        self::getInstance()->aLogs[$iStatus][]  = $sMessage;
87
    }
88
/**
89
 * clear error lists
90
 * @param int $iStatus defiones which list should be cleared (one or all)
91
 * @return void
92
 */
93
    public static function clear($iStatus = self::ALL)
94
    {
95
        if ($iStatus == self::ALL) {
96
            self::getInstance()->aLogs = array(
97
                self::ERROR   => array(),
98
                self::SUCCESS => array(),
99
                self::WARN    => array(),
100
                self::LOG     => array()
101
            );
102
        } else {
103
            if (isset(self::getInstance()->aLogs[$iStatus])) {
104
                self::getInstance()->aLogs[$iStatus] = array();
105
            }
106
        }
107
    }
108
/**
109
 *
110
 * @param int $iStatus (default: self::ALL)
111
 * @return bool
112
 */
113
    public static function isEmpty($iStatus = self::ALL)
114
    {
115
        if ($iStatus == self::ALL) {
116
            return (sizeof(self::getInstance()->aLogs[self::LOG]) == 0);
117
        } else {
118
            return (isset(self::getInstance()->aLogs[$iStatus]) == false);
119
        }
120
    }
121
/**
122
 * returns selected kind of messages
123
 * @param integer $iStatus  which messages
124
 * @param integer $iRetvalType  return as string or array(default)
125
 * @return mixed  string|array
126
 * @description  msgQueue::SHOW_ALL returns a multidimensional array as  $x[Type][Messages]
127
 *                all others return a string of messages concated by \n
128
 */
129
    public static function getMessages($iStatus = self::ERROR, $iRetvalType = self::RETVAL_ARRAY)
130
    {
131
        $aRetval = array();
132
        if ($iStatus == self::SHOW_ALL) {
133
            return self::getInstance()->aLogs[self::LOG];
134
        } else {
135
            if (isset(self::getInstance()->aLogs[$iStatus])) {
136
                foreach (self::getInstance()->aLogs[$iStatus] as $aItem) {
137
                    $aRetval[] = $aItem['msg'];
138
                }
139
            }
140
        }
141
        return ($iRetvalType == self::RETVAL_STRING ? implode("\n", $aRetval) : $aRetval);
142
    }
143
/**
144
 *
145
 * @param int $iType
146
 * @return mixed
147
 * @deprecated set deprecated since 2.8.4 and removed in next version
148
 */
149
    public static function getError($iType = self::RETVAL_STRING)
150
    {
151
        trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED);
152
        return self::getMessages(self::ERROR, $iType);
153
    }
154
/**
155
 *
156
 * @param int $iType
157
 * @return mixed
158
 * @deprecated set deprecated since 2.8.4 and removed in next version
159
 */
160
    public static function getSuccess($iType = self::RETVAL_STRING)
161
    {
162
        trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED);
163
        return self::getMessages(self::OK, $iType);
164
}
165
/**
166
 *
167
 * @return array
168
 * @deprecated set deprecated since 2.8.4 and removed in next version
169
 */
170
    public static function getLoglist()
171
    {
172
        trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED);
173
        return self::getMessages(self::LOG, self::RETVAL_ARRAY);
174
    }
175

    
176
} // end of class msgQueue