|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Core\Modules; |
|
4
|
|
|
/** |
|
5
|
|
|
* Gets and sets the alert box message to be sent to the front |
|
6
|
|
|
* Class AlertBox |
|
7
|
|
|
* @package Core\Modules |
|
8
|
|
|
*/ |
|
9
|
|
|
class AlertBox extends Module |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var array the diffrent allowed alert types |
|
13
|
|
|
*/ |
|
14
|
|
|
private $allowedTypes = [ |
|
15
|
|
|
'success', |
|
16
|
|
|
'info', |
|
17
|
|
|
'warning', |
|
18
|
|
|
'error' |
|
19
|
|
|
]; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* sets our alert message |
|
23
|
|
|
* @param string $message the alert message |
|
24
|
|
|
* @param string $type the type of alert |
|
25
|
|
|
* @throws \Exception |
|
26
|
|
|
*/ |
|
27
|
|
|
public function setAlert(string $message, $type = 'success') |
|
28
|
|
|
{ |
|
29
|
|
|
//make sure we have the right type or throw an error |
|
30
|
|
|
|
|
31
|
|
|
if (!in_array($type, $this->allowedTypes)) { |
|
32
|
|
|
throw new \Exception("Invalid toastr alert type " . $type); |
|
33
|
|
|
} |
|
34
|
|
|
$message = [ |
|
35
|
|
|
'type' => $type, |
|
36
|
|
|
'message' => $message |
|
37
|
|
|
]; |
|
38
|
|
|
$session = $this->container->getSession(); |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
$alert = $session->get('alert_messages'); |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
$alert[] = $message; |
|
45
|
|
|
|
|
46
|
|
|
$session->set('alert_messages', $alert); |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Checks if we have any unsent alerts |
|
53
|
|
|
* @return bool |
|
54
|
|
|
*/ |
|
55
|
|
|
public function alertsPending() |
|
56
|
|
|
{ |
|
57
|
|
|
$session = $this->container->getSession(); |
|
58
|
|
|
|
|
59
|
|
|
return $session->isParamSet('alert_messages'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* grabs the pending alerts from teh session and returns the array |
|
64
|
|
|
* It then empties the alerts to avoid showing the same alert twice |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getAlerts(): array |
|
68
|
|
|
{ |
|
69
|
|
|
if (!$this->alertsPending()) { |
|
70
|
|
|
return []; |
|
71
|
|
|
} |
|
72
|
|
|
$session = $this->container->getSession(); |
|
73
|
|
|
$alerts = $session->get('alert_messages'); |
|
74
|
|
|
//could return null and need to send back an array. this should never happen since we checked the alerts pending but scrutinizer complains ! |
|
75
|
|
|
if (is_null($alerts)) { |
|
76
|
|
|
$alerts = []; |
|
77
|
|
|
} |
|
78
|
|
|
$session->remove('alert_messages'); |
|
79
|
|
|
return $alerts; |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
} |