Project

General

Profile

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

1
<?php
2
/**
3
 * Description of class
4
 *
5
 * @author wkl
6
 */
7
class msgQueue {
8

    
9
/** define type of retval */
10
    const RETVAL_ARRAY  = 0;
11
    const RETVAL_STRING = 1; // (default)
12
/** */
13
    const LOG      = 0;
14
    const ERROR    = 1; // (default)
15
    const SUCCESS  = 2;
16
    const OK       = 2;
17
    const WARN     = 4;
18
    const SHOW_ALL = 0;
19
    const ALL      = 0;
20
/**  */
21
    private static $_instance;
22
/**  */
23
    private $aLogs    = array();
24
/**
25
 * constructor
26
 */
27
    protected function __construct() {
28
        $this->aLogs = array();
29
    }
30
/** disable cloning */
31
    private function __clone() { }
32
/**
33
 * get handle for active instance
34
 */
35
    public static function getInstance()
36
    {
37
        if (!isset(self::$_instance)) {
38
            $c = __CLASS__;
39
            self::$_instance = new $c;
40
        }
41
        return self::$_instance;
42
    }
43
/**
44
 * push new message in queue
45
 * @param string $sMessage
46
 * @param int $iStatus (default: self::FAIL)
47
 */
48
    public static function add($sMessage = '', $iStatus = self::ERROR)
49
    {
50
        $iStatus =
51
            ($iStatus === true ? self::OK : ($iStatus === false ? self::ERROR : $iStatus)) ?: self::ERROR;
52
        self::getInstance()->aLogs[self::LOG][] = array('status'=>self::WARN, 'msg'=>$sMessage);
53
        self::getInstance()->aLogs[$iStatus][]  = $sMessage;
54
    }
55
/**
56
 *
57
 * @param int $iStatus
58
 */
59
    public static function clear($iStatus = self::ALL)
60
    {
61
        if ($iStatus == self::ALL) {
62
            self::getInstance()->aLogs = array();
63
        } else {
64
            if (isset(self::getInstance()->aLogs[$iStatus])) {
65
                self::getInstance()->aLogs[$iStatus] = array();
66
            }
67
        }
68
    }
69
/**
70
 *
71
 * @param int $iStatus (default: self::ALL)
72
 * @return bool
73
 */
74
    public static function isEmpty($iStatus = self::ALL)
75
    {
76
        if ($iStatus == self::ALL) {
77
            return (sizeof(self::getInstance()->aLogs[self::LOG]) == 0);
78
        } else {
79
            return (isset(self::getInstance()->aLogs[$iStatus]) == false);
80
        }
81
    }
82
/**
83
 * returns selected kind of messages
84
 * @param type $iStatus  which messages
85
 * @param type $iRetvalType  return as string or array(default)
86
 * @return mixed  string|array
87
 */
88
    public static function getMessages($iStatus = self::ERROR, $iRetvalType = self::RETVAL_Array)
89
    {
90
        $aRetval = array();
91
        if ($iStatus == self::SHOW_ALL) {
92
            return self::getInstance()->aLogs[self::LOG];
93
        } else {
94
            if (isset(self::getInstance()->aLogs[$iStatus])) {
95
                foreach (self::getInstance()->aLogs[$iStatus] as $aItem) {
96
                    $aRetval[] = $aItem['msg'];
97
                }
98
            }
99
        }
100
        return ($iRetvalType == self::RETVAL_STRING ? implode("\n", $aRetval) : $aRetval);
101
    }
102
/**
103
 *
104
 * @param int $iType
105
 * @return mixed
106
 * @deprecated set deprecated since 2.8.4 and removed in next version
107
 */
108
    public static function getError($iType = self::RETVAL_STRING)
109
    {
110
        return self::getMessages(self::ERROR, $iType);
111
    }
112
/**
113
 *
114
 * @param int $iType
115
 * @return mixed
116
 * @deprecated set deprecated since 2.8.4 and removed in next version
117
 */
118
    public static function getSuccess($iType = self::RETVAL_STRING)
119
    {
120
        return self::getMessages(self::OK, $iType);
121
    }
122

    
123
} // end of class msgQueue