-
Notifications
You must be signed in to change notification settings - Fork 0
/
SendOutWarlert.module
141 lines (119 loc) · 4.2 KB
/
SendOutWarlert.module
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php namespace ProcessWire;
/**
* SendOutWarlert Module
*
* Takes Care of sending out Messages for Co2 Alerts and Warnings
* Simply extend the send method whith additional senders.
*
*
*/
class SendOutWarlert extends WireData implements Module {
/**
* getModuleInfo required by module interface
* @return array
*/
public static function getModuleInfo() {
return array(
'title' => 'Send out alerts and warnings',
'version' => "0.1.1",
'summary' => 'Takes care of sending out all alerts and warnings.',
'singular' => false,
'autoload' => false,
//'icon' => 'smile-o',
);
}
/**
* Initialize the module
* can be used as a substitute for _construct, but with module settings activated
*/
public function init() {
// No hooks today
}
///////////////////////////////////
// Additional Class Vars
/** @var WireObject $room Room to handle*/
protected $room;
/** @var array $targets Targets to send to.*/
protected $targets;
/** @var string $headline Title to send*/
protected $headline = "";
/** @var string $text Text to send*/
protected $text = "";
/** @var string $level use "warn" or "alert" */
protected $level = "warn";
/**
*
*
* @param WireObject $room
* @param string $text
* @param string $level
* @return void
*/
public function send($headline, $text, $room, $level) {
$this->room = $room;
$this->level = $level;
$this->text = $text;
$this->headline = $headline;
$this->findTargets();
if (!empty($targets)) {
//$this->sendAll();
}
else
$this->error("Cannot send Warning no targets defined for Rooom: { $room->title}");
return $this->targets;
}
/**
* Find the Targets to send to
*/
protected function findTargets() {
// do we have instructors for this room ?
if ($this->room->room_instructor->count() != 0) {
foreach ($this->room->room_instructor as $inst) {
$this->targets[$inst->id] = array();
$this->targets[$inst->id]['teams'] = empty($inst->teams_id) ? "" : $inst->teams_id;
$this->targets[$inst->id]['phone'] = empty($inst->phone_number) ? "" : $inst->phone_number;
$this->targets[$inst->id]['email'] = empty($inst->email) ? "" : $inst->email;
}
}
// if we are on an alert , we inform all managers too
if ($this->level=="alert") {
$managers = $this->users->find('roles=manager');
print_r($managers);
if ($managers->count() != 0) {
foreach ($managers as $mana) {
// instructors and user never can have the same page id
$this->targets[$mana->id] = array();
$this->targets[$mana->id]['teams'] = empty($mana->teams_id) ? "" : $mana->teams_id;
$this->targets[$mana->id]['phone'] = empty($mana->phone_number) ? "" : $mana->phone_number;
$this->targets[$mana->id]['email'] = empty($mana->email) ? "" : $mana->email;
}
}
}
}
/**
* Send to all available targets
*/
protected function sendAll() {
foreach ($this->targets as $reciever){
// send email
if (!empty($reciever['email'])){
// fetch object from module itself
$sender = wireMail();
$sender ->to($reciever['email'])
->subject($this->headline)
->body($this->text)
->send();
}
// Send SMS
if (!empty($reciever['phone'])){
// SMS has no title field so combine
$text = $this->headline."\n\n";
// fetch from module factory
$sender = $modules->SendSms;
// Do sending
$response = $sender->sendMessage($text, $reciever['phone']);
}
// No teams yet as teams is not prepared
}
}
}