-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_service.php
122 lines (97 loc) · 2.95 KB
/
message_service.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
namespace SmartHistoryTourManager;
/**
* Represents a message that is shown to the user on the top of the page.
*/
class Message {
public $type;
public $text;
public function __construct($text, $type = MessageService::INFO) {
$this->type = $type;
$this->text = $text;
}
public function get_label() {
return MessageService::message_label($this->type);
}
public function get_prefix() {
return MessageService::message_prefix($this->type);
}
}
/**
* This singleton class keeps a bunch of messages, that can be added from
* anywhere in the application. These messages are then automatically shown to
* the user on page rendering.
*/
class MessageService {
// some constants to distinguish between message types
const SUCCESS = 0;
const INFO = 10;
const WARNING = 20;
const ERROR = 30;
public $messages;
private function __construct() {
$this->messages = array();
}
public function add_success($msg_str) {
$this->messages[] = new Message($msg_str, self::SUCCESS);
}
public function add_info($msg_str) {
$this->messages[] = new Message($msg_str, self::INFO);
}
public function add_warning($msg_str) {
$this->messages[] = new Message($msg_str, self::WARNING);
}
public function add_error($msg_str) {
$this->messages[] = new Message($msg_str, self::ERROR);
}
public function add($message) {
$this->messages[] = $message;
}
public function add_all($messages) {
$this->messages = array_merge($this->messages, $messages);
}
/**
* If the model has messages attacthed that indicate invalidity these are
* added as messages of the specified type.
* NOTE: This does not perform validity checking itself (left to the user)
*/
public function add_model_messages($model, $type = self::WARNING) {
if(!empty($model->messages)) {
foreach ($model->messages as $msg => $bool) {
$this->messages[] = new Message($msg, $type);
}
}
}
public static function message_label($msg_type) {
switch($msg_type) {
case self::SUCCESS:
return 'success';
case self::INFO:
return 'info';
case self::WARNING:
return 'warning';
case self::ERROR:
return 'error';
}
}
public static function message_prefix($msg_type) {
switch($msg_type) {
case self::SUCCESS:
return 'OK';
case self::INFO:
return 'Info';
case self::WARNING:
return 'Warnung';
case self::ERROR:
return 'Fehler';
}
}
public static function instance() {
static $instance = null;
if($instance === null) {
$instance = new self();
}
return $instance;
}
}
?>