-
Notifications
You must be signed in to change notification settings - Fork 0
/
Missedcall.class.php
1482 lines (1347 loc) · 52.1 KB
/
Missedcall.class.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace FreePBX\modules;
// License for all code of this FreePBX module can be found in the license file inside the module directory
// Copyright 2015 Sangoma Technologies.
use BMO;
use FreePBX_Helpers;
use PDO;
use Exception;
class Missedcall extends FreePBX_Helpers implements BMO {
public const EMAIL_TYPE_HTML = 'html';
public const EMAIL_TYPE_TEXT = 'text';
public const EMAIL_SUBJECT = 'Missed call from {{calleridname}}';
private bool $licensed = false;
public function __construct($freepbx = null) {
if ($freepbx == null) {
throw new Exception("Not given a FreePBX Object");
}
$this->FreePBX = $freepbx;
$this->db = $freepbx->Database;
$this->userman = $freepbx->Userman;
$this->astman = $freepbx->astman;
}
public function getRightNav($request) {
if(!isset($request['view']) || $request['view'] != "form") {
return false;
}
return load_view(__DIR__."/views/rnav.php",[]);
}
//BMO Methods
//Required function - Called during module install
public function install() {
// Register FeatureCode - Activate
$fcc = new \featurecode('missedcall', 'missedcall_on');
$fcc->setDescription('Missed Call Notification Activate');
$fcc->setDefault('*56');
$fcc->update();
unset($fcc);
// Register FeatureCode - Deactivate
$fcc = new \featurecode('missedcall', 'missedcall_off');
$fcc->setDescription('Missed Call Notification Deactivate');
$fcc->setDefault('*57');
$fcc->update();
unset($fcc);
// Register FeatureCode - Toggle
$fcc = new \featurecode('missedcall', 'missedcall_toggle');
$fcc->setDescription('Missed Call Notification Toggle');
$fcc->setDefault('*58');
$fcc->update();
unset($fcc);
$users = $this->getUsers();
foreach($users as $id=>$ext){
if(!empty($ext)){
$response = $this->FreePBX->astman->database_get("AMPUSER","$ext/missedcall");
if($response != "enable") {
$response = $this->FreePBX->astman->database_put("AMPUSER","$ext/missedcall", "disable");
}
$this->update($id,$ext,0, 0, 0, 0);
}
}
}
// required function - called during module un-install
public function uninstall() {
$queries = [];
out(_('Removing the database table'));
$result = $this->deleteTable();
if($result === true){
out(_('Table Deleted'));
}else{
out(_('Something went wrong'));
out($result);
}
out(_('Removing missedcall keys from the asterisk database'));
$users = $this->getUsers();
foreach($users as $ext){
$this->FreePBX->astman->database_del("AMPUSER","$ext/missedcall");
}
// remove userman settings
$queries[] = "DELETE FROM userman_groups_settings WHERE `module`= 'missedcall'";
$queries[] = "DELETE FROM userman_groups_settings WHERE `module`= 'ucp|Missedcall'";
$queries[] = "DELETE FROM userman_users_settings WHERE `module`= 'missedcall'";
foreach($queries as $query){
$stmt = $this->db->prepare($query);
$stmt->execute();
}
}
public function getDeviceUser($ext){
$query = "select `user` from devices WHERE `id`= '$ext'";
$stmt = $this->db->prepare($query);
$stmt->execute();
$data = $stmt->fetch(\PDO::FETCH_ASSOC);
return $data['user'] ?? $ext;
}
// fetchall call belong to given linkedid
public function getallcalls($linkedid){
$query = "SELECT * FROM missedcalllog WHERE linkedid= '$linkedid'";
$stmt = $this->db->prepare($query);
$stmt->execute();
$data = $stmt->fetchAll(\PDO::FETCH_ASSOC);
return $data;
}
// Remove all call belong to given linkedid
public function removeAllCalls($linkedid){
$query = "DELETE FROM missedcalllog WHERE linkedid= '$linkedid'";
$stmt = $this->db->prepare($query);
$stmt->execute();
return;
}
public function sendEmail($mc_email='',$ext='',$mcexten='',$mcname="",$calltype='') {
$emailData = [];
// determine from email address for notification
if (function_exists('fetchFromEmail')) {
$fr_email = fetchFromEmail();
} else {
$from = $this->FreePBX->Config()->get('AMPUSERMANEMAILFROM');
$fr_email = !empty($from) ? $from : 'freepbx@freepbx.org';
}
$bname = $this->FreePBX->Config()->get('DASHBOARD_FREEPBX_BRAND');
if (!empty($bname)) {
$fr_name = $bname.": "._("Missed Call Notification");
} else {
$fr_name = _("Missed Call Notification");
}
$user = $this->userman->getUserByDefaultExtension($ext);
$timezone = $this->userman->getLocaleSpecificSettingByUID($user['id'],'timezone');
$date = date("Y-m-d H:i:s");
if(!empty($timezone)) {
$date = new \DateTime("now",new \DateTimeZone($timezone));
$date = $date->format('Y-m-d H:i:s');
}
$emailData['brand'] = $this->FreePBX->Config()->get('BRAND_FREEPBX_ALT_LEFT');
$emailData['extension'] = $ext;
$emailData['callerid'] = $mcexten;
$emailData['calleridname'] = $mcname;
$emailData['datetime'] = $date;
$emailData['calltype'] = $calltype;
// Get mail template
$emailTemplate = $this->getMailTemplate('notification_mail');
$emailType = !empty($emailTemplate['type']) ? $emailTemplate['type'] : self::EMAIL_TYPE_HTML;
$subject = !empty($emailTemplate['subject']) ? $emailTemplate['subject'] : self::EMAIL_SUBJECT;
$subject = $this->replaceTemplateVariables($subject, $emailData);
$body = !empty($emailTemplate['body']) ? $emailTemplate['body'] : file_get_contents(__DIR__ . '/views/mail.tpl');
$body = $this->replaceTemplateVariables($body, $emailData);
$body = $emailType == self::EMAIL_TYPE_HTML ? html_entity_decode((string) $body, ENT_QUOTES) : $body;
$em = new \CI_Email();
$em->from($fr_email, $fr_name);
$em->to($mc_email);
$em->subject($subject);
$em->set_mailtype($emailType);
$em->message($body);
$em->send();
dbug("Sending missed call notification to ".$mc_email);
}
//View called by page.misseccall.php
public function showPage(){
$subhead = null;
$email = $this->FreePBX->Config()->get('AMPUSERMANEMAILFROM');
$error = false;
if(empty($email)){
$error = true;
}
$content = load_view(__DIR__.'/views/grid.php');
echo load_view(__DIR__.'/views/default.php', ['subhead' => $subhead, 'content' => $content, "error" => $error]);
}
//add buttons to your page(s), buttons should not be added in html. Note this is a 13+ method.
public function getActionBar($request) {
$buttons = [];
switch($request['display']) {
//this is usually your module's rawname
case 'missedcall':
$buttons = ['delete' => ['name' => 'delete', 'id' => 'delete', 'value' => _('Delete')], 'reset' => ['name' => 'reset', 'id' => 'reset', 'value' => _('Reset')], 'submit' => ['name' => 'submit', 'id' => 'submit', 'value' => _('Submit')]];
//We hide the delete button if we are not editing an item. "id" should be whatever your unique element is.
if (empty($request['id'])) {
unset($buttons['delete']);
}
//If we are not in the form view lets 86 the buttons
if (empty($request['view'])){
unset($buttons);
}
break;
}
return $buttons;
}
public function checkFieldValidationForUserman($uid, $request){
$noError = true;
$message = '';
$notify = $this->getStatus($uid);
# check that missed call is enabled
if($notify == 1){
$noError = false;
$message = _("The user's email address is required. Because missed call notification is enabled. Please disable it and try again.");
}
return ["status" => $noError, "type" => $noError ? "" : "danger", "message" => $message];
}
//Ajax methods
//This method declares which are valid ajax commands...
public function ajaxRequest($req, &$setting) {
switch ($req) {
case "toggleMC":
case "get_status":
case "savebulk":
case "saveEmailSettings":
return true;
default:
return false;
}
}
public function ajaxHandler(){
switch ($_REQUEST['command']) {
case 'savebulk':
switch($_REQUEST["status"]){
case "enable":
foreach($_REQUEST["extensions"] as $key => $userid){
$this->updateOne($userid,'notification',1,[],true);
}
return ["status" => true, "message" => _("Success.")];
case "disable":
foreach($_REQUEST["extensions"] as $key => $userid){
$this->updateOne($userid,'notification',0,[],true);
}
return ["status" => true, "message" => _("Success.")];
default:
return ["status" => false, "message" => _("Unknown Status.")];
}
return false;
case 'toggleMC':
if($_REQUEST['state'] == 'enable'){
$state = true;
}
if($_REQUEST['state'] == 'disable'){
$state = false;
}
if(!isset($state) || !isset($_REQUEST['extdisplay'])){
return ['toggle' => 'invalid'];
}
$this->Toggle($_REQUEST['extdisplay']);
return ['toggle' => 'received'];
break;
case "get_status":
$users = $this->getUsers();
$list = [];
foreach($users as $id => $ext){
$mc_params = $this->get($id);
if(empty($mc_params['email'])){
continue;
}
$user = $this->userman->getUserByDefaultExtension($ext);
$mcenabled = $this->userman->getCombinedModuleSettingByID($id,'missedcall','mcenabled', false, true);
$internal = $mc_params['internal'] == "1" ? '<i class="fa fa-check-circle text-success"></i>' : '<i class="fa fa-times-circle text-danger"></i>' ;
$external = $mc_params['external'] == "1" ? '<i class="fa fa-check-circle text-success"></i>' : '<i class="fa fa-times-circle text-danger"></i>' ;
$queue = $mc_params['queue'] == "1" ? '<i class="fa fa-check-circle text-success"></i>' : '<i class="fa fa-times-circle text-danger"></i>' ;
$ringgroup = $mc_params['ringgroup']== "1" ? '<i class="fa fa-check-circle text-success"></i>' : '<i class="fa fa-times-circle text-danger"></i>' ;
$enabled = $mcenabled== "1" ? '<i class="fa fa-check-circle text-success"></i>' : '<i class="fa fa-times-circle text-danger"></i>' ;
$list[] = [
"userid" =>$id,
"username" =>$user['username'],
"extension" => $ext,
"email" => $mc_params['email'],
"internal" => $internal,
"external" => $external,
"queue" => $queue,
"ringgroup" => $ringgroup,
"notification" =>$enabled
];
}
return $list;
case "saveEmailSettings":
return $this->saveEmailSettings($_REQUEST);
default:
return false;
break;
}
}
public function usermanShowPage() {
global $version;
if(isset($_REQUEST['action'])) {
$error = "";
switch($_REQUEST['action']) {
case 'addgroup':
case 'showgroup':
$mcenabled = ($_REQUEST['action'] == "addgroup") ? true : $this->userman->getModuleSettingByGID($_REQUEST['group'],'missedcall','mcenabled');
$mcrg = ($_REQUEST['action'] == "addgroup") ? true : $this->userman->getModuleSettingByGID($_REQUEST['group'],'missedcall','mcrg');
$mcq = ($_REQUEST['action'] == "addgroup") ? true : $this->userman->getModuleSettingByGID($_REQUEST['group'],'missedcall','mcq');
$mci = ($_REQUEST['action'] == "addgroup") ? true : $this->userman->getModuleSettingByGID($_REQUEST['group'],'missedcall','mci');
$mcx = ($_REQUEST['action'] == "addgroup") ? true : $this->userman->getModuleSettingByGID($_REQUEST['group'],'missedcall','mcx');
return [["title" => _("Missed Call"), "rawname" => "missedcall", "content" => load_view(__DIR__.'/views/missedcall.php',["mode" => "group", "error" => $error, "mcenabled" => $mcenabled, "mcrg" => $mcrg, "mcq" => $mcq, "mci" =>$mci, "mcx"=>$mcx])]];
case 'adduser':
case 'showuser':
if(isset($_REQUEST['user'])) {
$user = $this->userman->getUserByID($_REQUEST['user']);
$mcenabled = $this->userman->getModuleSettingByID($user['id'],'missedcall','mcenabled',true);
$mcrg = $this->userman->getModuleSettingByID($user['id'],'missedcall','mcrg',true);
$mcq = $this->userman->getModuleSettingByID($user['id'],'missedcall','mcq',true);
$mci = $this->userman->getModuleSettingByID($user['id'],'missedcall','mci',true);
$mcx = $this->userman->getModuleSettingByID($user['id'],'missedcall','mcx',true);
}
return [["title" => _("Missed Call"), "rawname" => "missedcall", "content" => load_view(__DIR__.'/views/missedcall.php',["mode" => "user", "error" => $error, "mcenabled" => $mcenabled ?? '', "mcrg" => $mcrg ?? '', "mcq" => $mcq ?? '', "mci" =>$mci ?? '', "mcx"=>$mcx ?? ''])]];
default:
return [];
}
}
}
public function usermanDelGroup($id,$display,$data) {
}
public function usermanAddGroup($id, $display, $data) {
$this->usermanUpdateGroup($id,$display,$data);
}
/*update user by settings */
private function updateUserbysettins($users=[],$setting="",$value=""){
foreach ($users as $id){
if($setting == 'notification'){
$mcenabled= $this->userman->getCombinedModuleSettingByID($id,'missedcall','mcenabled');
if($mcenabled){
$this->updateOne($id,'notification',1);
} else {
$this->updateOne($id,'notification',0);
}
}
if($setting == 'ringgroup'){
$mcenabled= $this->userman->getCombinedModuleSettingByID($id,'missedcall','mcrg');
if($mcenabled){
$this->updateOne($id,'ringgroup',1);
} else {
$this->updateOne($id,'ringgroup',0);
}
}
if($setting == 'queue'){
$mcenabled= $this->userman->getCombinedModuleSettingByID($id,'missedcall','mcq');
if($mcenabled){
$this->updateOne($id,'queue',1);
} else {
$this->updateOne($id,'queue',0);
}
}
if($setting == 'internal'){
$mcenabled= $this->userman->getCombinedModuleSettingByID($id,'missedcall','mci');
if($mcenabled){
$this->updateOne($id,'internal',1);
} else {
$this->updateOne($id,'internal',0);
}
}
if($setting == 'external'){
$mcenabled= $this->userman->getCombinedModuleSettingByID($id,'missedcall','mcx');
if($mcenabled){
$this->updateOne($id,'external',1);
} else {
$this->updateOne($id,'external',0);
}
}
}
}
public function usermanUpdateGroup($id,$display,$data) {
if($display == 'userman' && isset($_POST['type']) && $_POST['type'] == 'group') {
if(isset($_POST['mcenabled'])) {
if($_POST['mcenabled'] == "true") {
$this->userman->setModuleSettingByGID($id,'missedcall','mcenabled',true);
$this->updateUserbysettins($data['users'],'notification',1);
} else {
$this->userman->setModuleSettingByGID($id,'missedcall','mcenabled',false);
$this->updateUserbysettins($data['users'],'notification',0);
}
}
if(isset($_POST['mcrg'])) {
if($_POST['mcrg'] == "true") {
$this->userman->setModuleSettingByGID($id,'missedcall','mcrg',true);
$this->updateUserbysettins($data['users'],'ringgroup',1);
} else {
$this->userman->setModuleSettingByGID($id,'missedcall','mcrg',false);
$this->updateUserbysettins($data['users'],'ringgroup',0);
}
}
if(isset($_POST['mcq'])) {
if($_POST['mcq'] == "true") {
$this->userman->setModuleSettingByGID($id,'missedcall','mcq',true);
$this->updateUserbysettins($data['users'],'queue',1);
} else {
$this->userman->setModuleSettingByGID($id,'missedcall','mcq',false);
$this->updateUserbysettins($data['users'],'queue',0);
}
}
if(isset($_POST['mci'])) {
if($_POST['mci'] == "true") {
$this->userman->setModuleSettingByGID($id,'missedcall','mci',true);
$this->updateUserbysettins($data['users'],'internal',1);
} else{
$this->userman->setModuleSettingByGID($id,'missedcall','mci',false);
$this->updateUserbysettins($data['users'],'internal',0);
}
}
if(isset($_POST['mcx'])) {
if($_POST['mcx'] == "true") {
$this->userman->setModuleSettingByGID($id,'missedcall','mcx',true);
$this->updateUserbysettins($data['users'],'external',1);
} else {
$this->userman->setModuleSettingByGID($id,'missedcall','mcx',false);
$this->updateUserbysettins($data['users'],'external',0);
}
}
}
}
/**
* Hook functionality from userman when a user is deleted
* @param {int} $id The userman user id
* @param {string} $display The display page name where this was executed
* @param {array} $data Array of data to be able to use
*/
public function usermanDelUser($id, $display, $data) {
$sql = "DELETE FROM `missedcall` WHERE `userid` = :userid";
$stmt = $this->db->prepare($sql);
$stmt->execute([':userid'=>$id]);dbug($sql);dbug($id);
}
/**
* Hook functionality from userman when a user is added
* @param {int} $id The userman user id
* @param {string} $display The display page name where this was executed
* @param {array} $data Array of data to be able to use
*/
public function usermanAddUser($id, $display, $data) {
$this->usermanUpdateUser($id, $display, $data);
}
/**
* Hook functionality from userman when a user is updated
* @param {int} $id The userman user id
* @param {string} $display The display page name where this was executed
* @param {array} $data Array of data to be able to use
*/
public function usermanUpdateUser($id, $display, $data) {
if($display == 'userman' && isset($_POST['type']) && $_POST['type'] == 'user') {
if(isset($_POST['mcenabled'])) {
if($_POST['mcenabled'] == "true") {
$this->userman->setModuleSettingByID($id,'missedcall','mcenabled',true);
$this->updateOne($id,'notification',1);
} elseif($_POST['mcenabled'] == "false") {
$this->userman->setModuleSettingByID($id,'missedcall','mcenabled',false);
$this->updateOne($id,'notification',0);
} else {
$this->userman->setModuleSettingByID($id,'missedcall','mcenabled',null);
//getcombined settings
$mcenabled= $this->userman->getCombinedModuleSettingByID($id,'missedcall','mcenabled');
if($mcenabled){
$this->updateOne($id,'notification',1);
} else {
$this->updateOne($id,'notification',0);
}
}
}
if(isset($_POST['mcrg'])) {
if($_POST['mcrg'] == "true") {
$this->userman->setModuleSettingByID($id,'missedcall','mcrg',true);
$this->updateOne($id,'ringgroup',1);
} elseif($_POST['mcrg'] == "false") {
$this->userman->setModuleSettingByID($id,'missedcall','mcrg',false);
$this->updateOne($id,'ringgroup',0);
} else {
$this->userman->setModuleSettingByID($id,'missedcall','mcrg',null);
//getcombined settings
$mcrg = $this->userman->getCombinedModuleSettingByID($id,'missedcall','mcrg');
if($mcrg){
$this->updateOne($id,'ringgroup',1);
} else {
$this->updateOne($id,'ringgroup',0);
}
}
}
if(isset($_POST['mcq'])) {
if($_POST['mcq'] == "true") {
$this->userman->setModuleSettingByID($id,'missedcall','mcq',true);
$this->updateOne($id,'queue',1);
} elseif($_POST['mcq'] == "false") {
$this->userman->setModuleSettingByID($id,'missedcall','mcq',false);
$this->updateOne($id,'queue',0);
} else {
$this->userman->setModuleSettingByID($id,'missedcall','mcq',null);
$mcq = $this->userman->getCombinedModuleSettingByID($id,'missedcall','mcq');
if($mcq){
$this->updateOne($id,'queue',1);
} else {
$this->updateOne($id,'queue',0);
}
}
}
if(isset($_POST['mci'])) {
if($_POST['mci'] == "true") {
$this->userman->setModuleSettingByID($id,'missedcall','mci',true);
$this->updateOne($id,'internal',1);
} elseif($_POST['mci'] == "false") {
$this->userman->setModuleSettingByID($id,'missedcall','mci',false);
$this->updateOne($id,'internal',0);
} else {
$this->userman->setModuleSettingByID($id,'missedcall','mci',null);
$mci = $this->userman->getCombinedModuleSettingByID($id,'missedcall','mci');
if($mci){
$this->updateOne($id,'internal',1);
} else {
$this->updateOne($id,'internal',0);
}
}
}
if(isset($_POST['mcx'])) {
if($_POST['mcx'] == "true") {
$this->userman->setModuleSettingByID($id,'missedcall','mcx',true);
$this->updateOne($id,'external',1);
} elseif($_POST['mcx'] == "false") {
$this->userman->setModuleSettingByID($id,'missedcall','mcx',false);
$this->updateOne($id,'external',0);
} else {
$this->userman->setModuleSettingByID($id,'missedcall','mcx',null);
$mcx = $this->userman->getCombinedModuleSettingByID($id,'missedcall','mcx');
if($mcx){
$this->updateOne($id,'external',1);
} else {
$this->updateOne($id,'external',0);
}
}
}
}
}
public function ucpDelGroup($id,$display,$data) {
}
public function ucpAddGroup($id, $display, $data) {
$this->ucpUpdateGroup($id,$display,$data);
}
public function ucpUpdateGroup($id,$display,$data) {
if($display == 'userman' && isset($_POST['type']) && $_POST['type'] == 'group') {
if($_POST['missedcall_enable'] == 'yes') {
$this->FreePBX->Ucp->setSettingByGID($id,'Missedcall','enabled',true);
} else {
$this->FreePBX->Ucp->setSettingByGID($id,'Missedcall','enabled',false);
}
if($_POST['mcenabled'] == 'yes') {
$this->FreePBX->Ucp->setSettingByGID($id,'Missedcall','mcenabled',true);
} else {
$this->FreePBX->Ucp->setSettingByGID($id,'Missedcall','mcenabled',false);
}
if($_POST['mcrg'] == 'yes') {
$this->FreePBX->Ucp->setSettingByGID($id,'Missedcall','mcrg',true);
} else {
$this->FreePBX->Ucp->setSettingByGID($id,'Missedcall','mcrg',false);
}
if($_POST['mcq'] == 'yes') {
$this->FreePBX->Ucp->setSettingByGID($id,'Missedcall','mcq',true);
} else {
$this->FreePBX->Ucp->setSettingByGID($id,'Missedcall','mcq',false);
}
}
}
/**
* Hook functionality from userman when a user is deleted
* @param {int} $id The userman user id
* @param {string} $display The display page name where this was executed
* @param {array} $data Array of data to be able to use
*/
public function ucpDelUser($id, $display, $ucpStatus, $data) {}
/**
* Hook functionality from userman when a user is added
* @param {int} $id The userman user id
* @param {string} $display The display page name where this was executed
* @param {array} $data Array of data to be able to use
*/
public function ucpAddUser($id, $display, $ucpStatus, $data) {
$this->ucpUpdateUser($id, $display, $ucpStatus, $data);
}
/**
* Hook functionality from userman when a user is updated
* @param {int} $id The userman user id
* @param {string} $display The display page name where this was executed
* @param {array} $data Array of data to be able to use
*/
public function ucpUpdateUser($id, $display, $ucpStatus, $data) {
if($display == 'userman' && isset($_POST['type']) && $_POST['type'] == 'user') {
if(isset($_POST['missedcall_enable']) && $_POST['missedcall_enable'] == 'yes') {
$this->FreePBX->Ucp->setSettingByID($id,'Missedcall','enabled',true);
} elseif(isset($_POST['missedcall_enable']) && $_POST['missedcall_enable'] == 'no') {
$this->FreePBX->Ucp->setSettingByID($id,'Missedcall','enabled',false);
} elseif(isset($_POST['missedcall_enable']) && $_POST['missedcall_enable'] == 'inherit') {
$this->FreePBX->Ucp->setSettingByID($id,'Missedcall','enabled',null);
}
}
}
public function ucpConfigPage($mode, $user, $action) {
if(empty($user)) {
$enabled = ($mode == 'group') ? true : null;
} else {
if($mode == 'group') {
$enabled = $this->FreePBX->Ucp->getSettingByGID($user['id'],'Missedcall','enabled');
$enabled = !($enabled) ? false : true;
} else {
$enabled = $this->FreePBX->Ucp->getSettingByID($user['id'],'Missedcall','enabled');
}
}
$html = [];
$html[0] = ["title" => _("Missed Call"), "rawname" => "missedcall", "content" => load_view(__DIR__."/views/ucp_config.php",["mode" => $mode, "enabled" => $enabled])];
return $html;
}
public function doConfigPageInit($page) {
$userid = $_REQUEST['userid']??'';
$extension = $_REQUEST['extension'] ?? '';
$internal = $_REQUEST['mcinternal'] ?? '';
$external = $_REQUEST['mcexternal'] ?? '';
$queue = $_REQUEST['mcqueue'] ?? '';
$ringgroup = $_REQUEST['mcringgroup'] ?? '';
$action = $_REQUEST['action'] ?? '';
$view = $_REQUEST['view'] ?? '';
//Handle form submissions
switch ($action) {
case 'submit':
$this->update($userid,$extension,$queue,$ringgroup,$internal,$external,'MMP');
break;
}
}
//Dialplan Methods
// This method required
Public function myDialplanHooks(){
// set priority for doDialplanHook, return true for default of 500 or set;
return 500;
}
// Method 'doDialplanHook' used to generate Asterisk dialplan
public function doDialplanHook(&$ext, $engine, $priority){
$modulename = 'missedcall';
// Retrieve module's feature codes
$fcc = new \featurecode($modulename, 'missedcall_on');
$mc_on = $fcc->getCodeActive();
unset($fcc);
$fcc = new \featurecode($modulename, 'missedcall_off');
$mc_off = $fcc->getCodeActive();
unset($fcc);
$fcc = new \featurecode($modulename, 'missedcall_toggle');
$mc_toggle = $fcc->getCodeActive();
unset($fcc);
$id = 'app-missedcall';
$ext->addInclude('from-internal-additional', $id); // Add the include to from-internal
$ext->add($id, $mc_on, '', new \ext_goto('1', 's', 'app-missedcall-on'));
$ext->add($id, $mc_off, '', new \ext_goto('1', 's', 'app-missedcall-off'));
$ext->add($id, $mc_toggle, '', new \ext_goto('1', 's', 'app-missedcall-toggle'));
$id = 'app-missedcall-on';
$c = 's';
$ext->add($id, $c, '', new \ext_macro('user-callerid'));
$ext->add($id, $c, '', new \ext_agi('missedcallnotify.php,${AMPUSER},enable'));
$ext->add($id, $c, 'hangup', new \ext_hangup());
$id = 'app-missedcall-off';
$c = 's';
$ext->add($id, $c, '', new \ext_macro('user-callerid'));
$ext->add($id, $c, '', new \ext_agi('missedcallnotify.php,${AMPUSER},disable'));
$ext->add($id, $c, 'hangup', new \ext_hangup());
$id = 'app-missedcall-toggle';
$c = 's';
$ext->add($id, $c, '', new \ext_macro('user-callerid'));
$ext->add($id, $c, '', new \ext_agi('missedcallnotify.php,${AMPUSER},toggle'));
$ext->add($id, $c, 'hangup', new \ext_hangup());
$id = 'app-missedcall-hangup';
$c = '_.';
$ext->add($id, $c, '', new \ext_noop('Dialed: ${EXTEN}'));
$ext->add($id, $c, '', new \ext_noop('Caller: ${MCEXTEN}'));
$ext->add($id, $c, '', new \ext_gotoif('$["${CHANNEL(LINKEDID)}"!="${CHANNEL(UNIQUEID)}" & "${EXTEN}"="s"]','exit'));
$ext->add($id, $c, '', new \ext_set('EXTENNUM','${CUT(EXTEN,@,1)}'));
$ext->add($id, $c, '', new \ext_set('FEXTENNUM', '${IF($[["${EXTENNUM:0:2}"="90"] || ["${EXTENNUM:0:2}"="98"]]?${EXTENNUM:2}:${EXTEN})}'));
$ext->add($id, $c, '', new \ext_gotoif('$[${DB_EXISTS(AMPUSER/${FEXTENNUM}/missedcall)} & "${DB(AMPUSER/${FEXTENNUM}/missedcall)}"="disable"]','exit'));
$ext->add($id, $c, '', new \ext_agi('missedcallnotify.php,${FEXTENNUM},,${FEXTENNUM},${DB_EXISTS(AMPUSER/${FEXTENNUM}/missedcall)},${DB(AMPUSER/${FEXTENNUM}/missedcall)},${CHANNEL},${DIALSTATUS},${MCQUEUE},${MCGROUP},${FMFM}'));
$ext->add($id, $c, 'exit', new \ext_return());
// need to set an inheritable channel variable so the dialing extension is known at hangup
$context = "macro-user-callerid";
$ext->splice($context, 's', "continue", new \ext_set('__MCORGCHAN','${CHANNEL}'),"",3,true);
$ext->splice($context, 's', "continue", new \ext_set('__MCEXTEN','${AMPUSER}'),"",3,true);
$ext->splice($context, 's', "continue", new \ext_set('__MCNAME','${CALLERID(name)}'),"",3,true);
$ext->splice($context, 's', "continue", new \ext_set('__MCNUM','${CALLERID(num)}'),"",3,true);
// splice hangup handler into dialplan, 'func-apply-sipheaders' gets run on every dial
$context = 'func-apply-sipheaders';
$ext->splice($context, "s", 1, new \ext_set('localchan','${CUT(CHANNEL,/,2)}'));
$ext->splice($context, "s", 2, new \ext_set('DialMCEXT','${CUT(localchan,-,1)}'));
$ext->splice($context, "s", 3, new \ext_set('CHANNEL(hangup_handler_push)','app-missedcall-hangup,${DialMCEXT},1'),"",1);
$context = 'macro-dial-one';
$ext->splice($context, "s", "", new \ext_set('__MCMULTI','${MD5(${DEXTEN}${FROMEXTEN})}'),"",1);
$ext->splice($context, "s", "", new \ext_set('__MCEXTTOCALL','${EXTTOCALL}'),"",1);
//dialOne-with-exten
$context = "dialOne-with-exten";
$ext->splice($context, "_X", 0, new \ext_set('CHANNEL(hangup_handler_push)','app-missedcall-hangup,${DialMCEXT},1'),"",1);
$ext->splice($context, "_[+-X].", 0, new \ext_set('CHANNEL(hangup_handler_push)','app-missedcall-hangup,${DialMCEXT},1'),"",1);
$context = 'macro-dial';
$priorities = ["ndloopbegin", "huntstart"];
foreach($priorities as $pri) {
$ext->splice($context, "s", $pri, new \ext_set('__MCEXTTOCALL','${EXTTOCALL}'),"",1);
}
$context = 'macro-hangupcall';
$exten = 's';
$ext->splice($context, $exten, "start", new \ext_set('__MCVMSTATUS','${VMSTATUS}'));
$ext->splice($context, $exten, 'start', new \ext_gosub(1, '${EXTEN}', 'app-missedcall-hangup'));
// splcie into FMFM
$context = 'followme-sub';
$ext->splice($context, '_X!', 0, new \ext_set('__FMFM','TRUE'));
// splice inheritable channel variable into each ring group
$context = 'ext-group';
$rgroups = $this->getRingGroups();
if (is_array($rgroups)) {
foreach ($rgroups as $exten) {
$ext->splice($context, $exten, 1, new \ext_set('__MCGROUP','${EXTEN}'));
}
}
// splice inheritable channel variable into each queue
$context = 'ext-queues';
$queues = $this->getQueues();
if (is_array($queues)) {
foreach ($queues as $exten) {
$ext->splice($context, $exten, 1, new \ext_set('__MCQUEUE','${EXTEN}'));
}
}
}
// Module specific methods
public function asm(){
return $this->astman;
}
/**
* privvate getRingGroups get a list of all ring groups on the system
* @param
* @return Returns 1D array of all ring group numbers or null if none.
**/
private function getRingGroups() {
$rg = [];
$ringgroup_list= $this->FreePBX->Ringgroups->listRinggroups(true);
foreach ($ringgroup_list as $item) {
$rg[] = $item['grpnum'];
}
if (is_array($rg)) {
return $rg;
} else {
return null;
}
}
/**
* private getQueues get a list of all queues on the system
* @param
* @return Returns 1D array of all queue numbers or null if none.
*/
private function getQueues() {
$result = null;
$retval = $this->FreePBX->Queues->search('',$result);
$queues = [];
if(!empty($result)){
foreach ($result as $queue) {
$pattern = "~^.*\((.*)\).*$~";
if(preg_match($pattern, (string) $queue['text'], $retval)) {
$queues[]=$retval[1];
}
}
}
if (is_array($queues)) {
return $queues;
} else {
return null;
}
}
/**
* getUsers get a list of all ampusers on the system
* @param
* @return Returns 1D array of all system ampusers or null if none.
*/
public function getUsers() {
$users = [];
$userman = $this->FreePBX->userman->getAllUsers();
foreach ($userman as $user) {
$users[$user['id']] = $user['default_extension'];
}
if (is_array($users)) {
return $users;
} else {
return null;
}
}
/**
* private getEmail returns email address associted with user set for primary extension
* @param string $exten
* @return Returns string with email address or null if none.
*/
private function getEmail($id) {
if ($id) {
$details = $this->FreePBX->Userman()->getUserByID($id);
if(is_array($details) && !empty($details['email'])){
$email = $details['email'];
}
}
if (!empty($email)) {
// should we validate if string is valid email address?
return $email;
} else {
return null;
}
}
/**
* getStatus retuns whether missed calls is enabled or disabled for a specific extension
* @param string $id
* @return Returns true or false
*/
public function getStatus($userid) {
$query = "SELECT * FROM missedcall WHERE userid= ?";
$stmt = $this->db->prepare($query);
$stmt->execute([$userid]);
$data = $stmt->fetch(\PDO::FETCH_ASSOC);
return $data['notification'] ?? 0;
}
/**
* Enable missed call notification for specific extension
* @param string $exten
* @return
*/
public function misscallEnable($userid,$dbinsert = false,$from = false) {
$user = $this->userman->getUserByID($userid);
$exten = $user['default_extension'];
// disable in DB
if($dbinsert){
$sql = 'INSERT INTO `missedcall` (`notification`, `userid`,`extension`) VALUES (:value,:userid, :ext)';
} else {
$sql = "UPDATE `missedcall` SET `extension`= :ext ,`notification` = :value WHERE `userid` = :userid";
}
$stmt = $this->db->prepare($sql);
$stmt->execute([':userid'=>$userid, ':ext'=>$exten, ':value'=>1]);
//update the Userman
if($from){
$this->userman->setModuleSettingByID($userid,'missedcall','mcenabled',true);
}
$response = $this->FreePBX->astman->database_put("AMPUSER","$exten/missedcall", "enable");
return $response;
}
/**
* Disable missed call notification for specific extension
* @param string $exten
* @return
*/
public function misscallDisable($userid,$dbinsert = false,$from = false) {
$user = $this->userman->getUserByID($userid);
$exten = $user['default_extension'];
// disable in DB
if($dbinsert){
$sql = 'INSERT INTO `missedcall` (`notification`, `userid`,`extension`) VALUES (:value,:userid, :ext)';
} else {
$sql = "UPDATE `missedcall` SET `extension`= :ext ,`notification` = :value WHERE `userid` = :userid";
}
$stmt = $this->db->prepare($sql);
$stmt->execute([':userid'=>$userid, ':ext'=>$exten, ':value'=>0]);
if($from){
$this->userman->setModuleSettingByID($userid,'missedcall','mcenabled',false);
}
$response = $this->FreePBX->astman->database_put("AMPUSER","$exten/missedcall", "disable");
return $response;
}
/**
* Toggle missed call notification for specific extension
* @param string $exten
* @return the status of the extension after the toggle string 'enable' or 'disable'
*/
public function Toggle($id) {
$status = $this->getStatus($id);
if ($status == 0) {
$resp = $this->misscallEnable($id,false,true);
return 'enable';
} else {
$resp = $this->misscallDisable($id,false,true);
return 'disable';
}
}
//Module getters
/**
* get Gets all missed call params for specific extension
* @param string $userid
* @param getby by userid or extension
* @return Returns array of all params.
*/
public function get($userid,$getby='userid'){
if($getby =='userid'){
$sql = "SELECT * FROM `missedcall` WHERE `userid` = :userid";
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':userid',$userid, \PDO::PARAM_INT);
}else {
$sql = "SELECT * FROM `missedcall` WHERE `extension` = :extension";
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':extension',$userid, \PDO::PARAM_INT);
}
$stmt->execute();
$ret = $stmt->fetch(\PDO::FETCH_ASSOC);
if (! empty($ret))
{
$ret['email'] = $this->getEmail($ret['userid']);
$ret['enable'] = $ret['notification'];
}
return $ret;
}
/**
* getAllUsers : Getting all users with their status from the database.
*
* @return array
*/
public function getAllUsers(){
$sql = "SELECT * FROM missedcall";
$stm = $this->db->prepare($sql);
$stm->execute();
$ret = $stm->fetchall(\PDO::FETCH_ASSOC);
return $ret;
}
/* $id : userman userid
$extension : userman users extension
$queue : Queue enabled
$ringgroup : ringgrroup enabled
$internal : internal enabled
$external : external enabled
$updatefrom : userman( value based on userman settings), MMS( Missedcall Module Setttins)
We need to sync the settings from userman to MMP and MMP to Userman
*/
public function update($id,$extension,$queue,$ringgroup,$internal,$external,$updatefrom='userman'){
// change bools to 1/0
$queue = $queue?1:0;
$ringgroup = $ringgroup?1:0;
$internal = $internal?1:0;