1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package toolkit |
4
|
|
|
*/ |
5
|
|
|
/** |
6
|
|
|
* The Alert class drives the standard Symphony notices that |
7
|
|
|
* appear at the top of the backend pages to alert the user of |
8
|
|
|
* something. Their are three default alert styles, notice, error |
9
|
|
|
* and success. |
10
|
|
|
*/ |
11
|
|
|
class Alert |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Represents a notice, usually used for non blocking alerts, |
15
|
|
|
* just to inform that user that something has happened and |
16
|
|
|
* they need to aware of it |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
const NOTICE = 'notice'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Represents an error, used when something has gone wrong during |
23
|
|
|
* the previous action. It is blocking, in that the action has |
24
|
|
|
* not completed successfully. |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
const ERROR = 'error'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Represents success, used when an action has completed successfully |
31
|
|
|
* with no errors |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
const SUCCESS = 'success'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The message for this Alert, this text will be displayed to the user |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $message; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The Alert constant to represent the style that this alert should |
44
|
|
|
* take on. Defaults to `Alert::NOTICE`. |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $type; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Constructor for the Alert class initialises some default |
51
|
|
|
* variables |
52
|
|
|
* |
53
|
|
|
* @param string $message |
54
|
|
|
* This text will be displayed to the user |
55
|
|
|
* @param string $type |
56
|
|
|
* The type of alert this is. Defaults to NOTICE, available |
57
|
|
|
* values are `Alert::NOTICE`, `Alert::ERROR`, `Alert::SUCCESS` |
58
|
|
|
*/ |
59
|
|
|
public function __construct($message, $type = self::NOTICE) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$this->message = $message; |
62
|
|
|
$this->type = $type; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Magic accessor function to get the private variables from |
67
|
|
|
* an Alert instance |
68
|
|
|
* |
69
|
|
|
* @param string $name |
70
|
|
|
* The name of the variable, message or type are the valid |
71
|
|
|
* values |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function __get($name) |
75
|
|
|
{ |
76
|
|
|
return $this->{"$name"}; |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Magic setter function to set the private variables of |
81
|
|
|
* an Alert instance |
82
|
|
|
* |
83
|
|
|
* @param string $name |
84
|
|
|
* The name of the variable, message or type are the valid values |
85
|
|
|
* @param string $value |
86
|
|
|
* The value of the variable that is being set |
87
|
|
|
*/ |
88
|
|
|
public function __set($name, $value) |
89
|
|
|
{ |
90
|
|
|
$this->{"$name"} = $value; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Magic isset function to check if a variable is set by ensuring |
95
|
|
|
* it's not null |
96
|
|
|
* |
97
|
|
|
* @param string $name |
98
|
|
|
* The name of the variable to check, message or type are the valid |
99
|
|
|
* values |
100
|
|
|
* @return boolean |
101
|
|
|
* true when set, false when not set. |
102
|
|
|
*/ |
103
|
|
|
public function __isset($name) |
104
|
|
|
{ |
105
|
|
|
return isset($this->{"$name"}); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Generates as XMLElement representation of this Alert |
110
|
|
|
* |
111
|
|
|
* @return XMLElement |
112
|
|
|
*/ |
113
|
|
|
public function asXML() |
114
|
|
|
{ |
115
|
|
|
$p = new XMLElement('p', $this->message, array('role' => 'alert')); |
116
|
|
|
$p->setAttribute('class', 'notice'); |
117
|
|
|
|
118
|
|
|
if ($this->type !== self::NOTICE) { |
119
|
|
|
$p->setAttribute('class', 'notice ' . $this->type); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $p; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|