forked from chadsmith/OpenVBX-Plugin-Subscriptions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriptions.php
301 lines (301 loc) · 10.2 KB
/
subscriptions.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
set_time_limit(0);
$user = OpenVBX::getCurrentUser();
$tenant_id = $user->values['tenant_id'];
$ci =& get_instance();
$queries = explode(';', file_get_contents(dirname(__FILE__) . '/db.sql'));
foreach($queries as $query)
if(trim($query))
$ci->db->query($query);
if(!empty($_POST['remove'])) {
$remove = intval($_POST['remove']);
if(!empty($_POST['list'])) {
$list = intval($_POST['list']);
if($ci->db->query(sprintf('SELECT id FROM subscribers_lists WHERE id = %d AND tenant = %d', $list, $tenant_id))->num_rows())
$ci->db->delete('subscribers', array('id' => $remove, 'list' => $list));
}
else {
$ci->db->delete('subscribers_lists', array('id' => $remove, 'tenant' => $tenant_id));
if($ci->db->affected_rows())
$ci->db->delete('subscribers', array('list' => $remove));
}
die;
}
if(!empty($_POST['type'])) {
$list = intval($_POST['list']);
if($ci->db->query(sprintf('SELECT id FROM subscribers_lists WHERE id = %d AND tenant = %d', $list, $tenant_id))->num_rows())
$subscribers = $ci->db->query(sprintf('SELECT value FROM subscribers WHERE list = %d', $list))->result();
else
$subscribers = array();
$type = $_POST['type'];
$callerId = normalize_phone_to_E164($_POST['callerId']);
$account = OpenVBX::getAccount();
if('sms' == $type && !empty($_POST['message'])) {
if(count($subscribers))
foreach($subscribers as $subscriber)
$account->sms_messages->create($callerId, $subscriber->value, $_POST['message']);
}
elseif('call' == $type) {
$flow = OpenVBX::getFlows(array('id' => $_POST['flow'], 'tenant_id' => $tenant_id));
if($flow && count($subscribers) && $flow[0]->values['data'])
foreach($subscribers as $subscriber)
$account->calls->create($callerId, $subscriber->value, site_url('twiml/start/voice/' . $flow[0]->values['id']));
}
}
if(!empty($_POST['numbers']) && preg_match_all('/\d{10,15}\b/', preg_replace('/[^0-9\n]+/', '', $_POST['numbers']), $numbers)) {
$list = intval($_POST['list']);
if($ci->db->query(sprintf('SELECT id FROM subscribers_lists WHERE id = %d AND tenant = %d', $list, $tenant_id))->num_rows()) {
$subscribers = $ci->db->query(sprintf('SELECT value FROM subscribers WHERE list = %d', $list))->result();
foreach($subscribers as &$subscriber)
$subscriber = $subscriber->value;
$numbers = array_diff(array_unique(array_map('normalize_phone_to_E164', $numbers[0])), $subscribers);
foreach($numbers as $number)
$ci->db->insert('subscribers', array(
'list' => $list,
'value' => $number,
'joined' => time()
));
}
}
if(!empty($_POST['export'])) {
$list = intval($_POST['export']);
if($list = $ci->db->query(sprintf('SELECT id, name FROM subscribers_lists WHERE id = %d AND tenant = %d', $list, $tenant_id))->row()) {
$subscribers = $ci->db->query(sprintf('SELECT value, joined FROM subscribers WHERE list = %d', $list->id))->result();
ob_clean();
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename=' . preg_replace('/\W/', '', $list->name) . '.csv');
?>"Number","Subscribed"
<?php foreach($subscribers as $subscriber): ?>
"<?php echo $subscriber->value; ?>","<?php echo date('d-M-Y', $subscriber->joined); ?>"
<?php endforeach;
die;
}
}
if(!empty($_POST['name']))
$ci->db->insert('subscribers_lists', array(
'tenant' => $tenant_id,
'name' => htmlentities($_POST['name'])
));
$lists = $ci->db->query(sprintf('SELECT id, name FROM subscribers_lists WHERE tenant = %d', $tenant_id))->result();
$flows = OpenVBX::getFlows(array('tenant_id' => $tenant_id));
OpenVBX::addJS('subscriptions.js');
?>
<style>
.vbx-subscriptions h3 {
font-size: 16px;
font-weight: bold;
margin-top: 0;
}
.vbx-subscriptions .list,
.vbx-subscriptions .subscriber {
clear: both;
width: 95%;
overflow: hidden;
margin: 0 auto;
padding: 5px 0;
border-bottom: 1px solid #eee;
}
.vbx-subscriptions .subscriber {
display: none;
background: #ccc;
}
.vbx-subscriptions .list span,
.vbx-subscriptions .subscriber span {
display: inline-block;
width: 20%;
text-align: center;
float: left;
vertical-align: middle;
line-height: 24px;
}
.vbx-subscriptions .list a {
text-decoration: none;
color: #111;
}
.vbx-subscriptions form {
display: none;
padding: 20px 5%;
background: #eee;
border-bottom: 1px solid #ccc;
}
.vbx-subscriptions a.sms,
.vbx-subscriptions a.call,
.vbx-subscriptions a.delete {
display: inline-block;
height: 24px;
width: 24px;
text-indent: -999em;
background: transparent url(<?php echo asset_url('/assets/i/standard-icons-sprite.png'); ?>) no-repeat 0 0;
}
.vbx-subscriptions a.sms {
background-position: -34px 0;
}
.vbx-subscriptions a.delete {
background: transparent url(<?php echo asset_url('/assets/i/action-icons-sprite.png'); ?>) no-repeat -68px 0;
}
</style>
<div class="vbx-content-main">
<div class="vbx-content-menu vbx-content-menu-top">
<h2 class="vbx-content-heading">Subscription Lists</h2>
<ul class="vbx-menu-items-right">
<li class="menu-item">
<button id="button-import-list" class="inline-button submit-button"><span>Import</span></button>
</li>
<li class="menu-item">
<button id="button-export-list" class="inline-button submit-button"><span>Export</span></button>
</li>
<li class="menu-item">
<button id="button-add-list" class="inline-button add-button"><span>Add List</span></button>
</li>
</ul>
</div>
<div class="vbx-table-section vbx-subscriptions">
<form class="add add-list" method="post" action="">
<h3>Add List</h3>
<fieldset class="vbx-input-container">
<label class="field-label">
<input type="text" class="medium" name="name" />
</label>
<p><button type="submit" class="submit-button"><span>Save</span></button></p>
</fieldset>
</form>
<form class="add import-list" method="post" action="">
<h3>Add Numbers</h3>
<fieldset class="vbx-input-container">
<?php if(count($lists)): ?>
<p>
<label class="field-label">
<select name="list" class="medium">
<?php foreach($lists as $list): ?>
<option value="<?php echo $list->id; ?>"><?php echo $list->name; ?></option>
<?php endforeach; ?>
</select>
</label>
</p>
<p>
<label class="field-label">Numbers
<textarea rows="20" cols="100" name="numbers" class="medium"></textarea>
</label>
</p>
<p><button type="submit" class="submit-button"><span>Import</span></button></p>
<?php else: ?>
<p>You do not have any lists!</p>
<?php endif; ?>
</fieldset>
</form>
<form class="export-list" method="post" action="">
<h3>Export List</h3>
<fieldset class="vbx-input-container">
<?php if(count($lists)): ?>
<p>
<label class="field-label">
<select name="export" class="medium">
<?php foreach($lists as $list): ?>
<option value="<?php echo $list->id; ?>"><?php echo $list->name; ?></option>
<?php endforeach; ?>
</select>
</label>
</p>
<p><button type="submit" class="submit-button"><span>Export</span></button></p>
<?php else: ?>
<p>You do not have any lists!</p>
<?php endif; ?>
</fieldset>
</form>
<form class="update update-sms" method="post" action="">
<h3>Send update to <span></span></h3>
<fieldset class="vbx-input-container">
<?php if(count($callerid_numbers)): ?>
<p>
<label class="field-label">Caller ID<br/>
<select name="callerId" class="medium">
<?php foreach($callerid_numbers as $number): ?>
<option value="<?php echo $number->phone; ?>"><?php echo $number->name; ?></option>
<?php endforeach; ?>
</select>
</label>
</p>
<p><input type="hidden" name="type" value="sms" /><input type="hidden" name="list" /></p>
<p>
<label class="field-label">Message
<textarea rows="20" cols="100" name="message" class="medium"></textarea>
</label>
</p>
<p><button type="submit" class="submit-button"><span>Send</span></button></p>
<?php else: ?>
<p>You do not have any phone numbers!</p>
<?php endif; ?>
</fieldset>
</form>
<form class="update update-dial" method="post" action="">
<h3>Auto dial <span></span></h3>
<fieldset class="vbx-input-container">
<?php if(count($callerid_numbers)): ?>
<?php if(count($flows)): ?>
<p>
<label class="field-label">Flow<br/>
<select name="flow" class="medium">
<?php foreach($flows as $flow): ?>
<option value="<?php echo $flow->values['id']; ?>"><?php echo $flow->values['name']; ?></option>
<?php endforeach; ?>
</select>
</label>
</p>
<p>
<label class="field-label">Caller ID<br/>
<select name="callerId" class="medium">
<?php foreach($callerid_numbers as $number): ?>
<option value="<?php echo $number->phone; ?>"><?php echo $number->name; ?></option>
<?php endforeach; ?>
</select>
</label>
</p>
<p><input type="hidden" name="type" value="call" /><input type="hidden" name="list" /></p>
<p><button type="submit" class="submit-button"><span>Call</span></button></p>
<?php else: ?>
<p>You do not have any flows!</p>
<?php endif; ?>
<?php else: ?>
<p>You do not have any phone numbers!</p>
<?php endif; ?>
</fieldset>
</form>
<?php if(count($lists)): ?>
<div class="list">
<h3>
<span>Name</span>
<span>Subscribed</span>
<span>SMS</span>
<span>Call</span>
<span>Delete</span>
</h3>
</div>
<?php foreach($lists as $list):
$subscribers = $ci->db->query(sprintf('SELECT id, value, joined FROM subscribers WHERE list = %d', $list->id))->result();
?>
<div class="list" id="list_<?php echo $list->id; ?>">
<p>
<span><?php echo $list->name; ?></span>
<span><a href="" class="subscribers"><?php echo count($subscribers); ?></a></span>
<span><a href="" class="sms">SMS</a></span>
<span><a href="" class="call">Call</a></span>
<span><a href="" class="delete">X</a></span>
</p>
</div>
<?php if(count($subscribers)): ?>
<?php foreach($subscribers as $subscriber): ?>
<div class="subscriber list_<?php echo $list->id; ?>" id="subscriber_<?php echo $list->id; ?>_<?php echo $subscriber->id; ?>">
<p>
<span><?php echo $subscriber->value; ?></span>
<span><?php echo date('d-M-Y', $subscriber->joined); ?></span>
<span> </span>
<span> </span>
<span><a href="" class="delete">X</a></span>
</p>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>