forked from rzatkv/PopUpJSClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.php
152 lines (133 loc) · 6.14 KB
/
hooks.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
<?php
use WHMCS\Module\Addon\popupjsclient\ModuleController as AnnouncementController;
use WHMCS\Module\Addon\popupjsclient\Model as Announcement;
if (!defined("WHMCS")) {
die("This file cannot be accessed directly");
}
add_hook('ClientAreaFooterOutput', 1, 'ClientAreaFooterOutput_ModuleView');
/**
* @throws Exception
*/
function ClientAreaFooterOutput_ModuleView($vars)
{
// Check if the user is logged in
if (!isset($vars['loggedin']) || !$vars['loggedin']) {
return; // If you are not logged in, the popup does not display
}
$lang = AnnouncementController::getLang($vars);
$userGroup = $vars['clientsdetails']['groupid'] ?? null;
$announcement = Announcement::getLatest($userGroup);
if ($announcement && !empty($announcement->announcement)) {
Announcement::incrementViews($announcement->id);
$announcementText = $announcement->announcement; // We don't escape here, we will do it in JavaScript
$interactionLink = htmlspecialchars($lang['interaction_link'], ENT_QUOTES, 'UTF-8');
$announcementId = (int)$announcement->id;
$isMultimedia = $announcement->is_multimedia ? 'true' : 'false';
$url = htmlspecialchars($announcement->url, ENT_QUOTES, 'UTF-8');
$seeMore = htmlspecialchars($lang['see_more'], ENT_QUOTES, 'UTF-8');
echo "
<style>
.areaModal {
-webkit-box-align: center;
align-items: center;
background-color: rgba(74, 74, 74, 0.8);
display: flex;
height: 100vh;
min-height: 40vh;
-webkit-box-pack: center;
justify-content: center;
left: 0px;
position: fixed;
top: 0px;
width: 100vw;
z-index: 999;
}
.areaModalVal {
background-color: rgb(255, 255, 255);
box-sizing: border-box;
display: flex;
flex-direction: column;
max-height: 440px;
max-width: 800px;
min-height: 100px;
overflow: hidden;
position: relative;
width: 100%;
}
.areaModalButton {
position: absolute;
right: 0;
margin: 10px;
top: -5px;
background: #e2f4fb;
padding: 1px 15px;
font-size: 14px;
color: #235498;
font-weight: bold;
font-family: 'Plus Jakarta Sans';
}
.modalcontent {
background: #f1f1f1;
}
</style>
<script>
(function() {
var announcementData = {
id: $announcementId,
isMultimedia: $isMultimedia,
url: '$url',
text: " . json_encode($announcementText) . ",
seeMore: '$seeMore',
interactionLink: '$interactionLink'
};
window.addEventListener('load', function() {
var cookieName = 'modalDimissID_' + announcementData.id;
// Checks if the cookie already exists
if (getCookie(cookieName)) {
return; // Does not display if cookie is already set
}
// Creating the popup
var popup = document.createElement('div');
popup.innerHTML =
'<div id=\"areaModal\" class=\"areaModal\">' +
'<div id=\"areaModalVal\" class=\"areaModalVal\">' +
(announcementData.isMultimedia
? '<a href=\"' + announcementData.url + '\" target=\"_blank\"><img src=\"' + announcementData.text + '\" alt=\"Announcement\" /></a>'
: '<div class=\"p-5\"><span class=\"badge badge-info\">Info</span><div class=\"modalcontent mt-5 p-2 rounded\">' + parseAndSanitizeHTML(announcementData.text) + '</div><div class=\"linkmodalContent mt-5\"><a href=\"' + announcementData.url + '\" class=\"p-1 btn-primary rounded\" target=\"_blank\">' + announcementData.seeMore +'</a></div></div>') +
'<a href=\"#\" class=\"areaModalButton\" onclick=\"dismissAnnouncement(' + announcementData.id + '); return false;\">' + announcementData.interactionLink + '</a>' +
'</div>' +
'</div>';
document.body.appendChild(popup);
});
function dismissAnnouncement(announcementId) {
var cookieName = 'modalDimissID_' + announcementId;
setCookie(cookieName, '1', 1); // Sets the cookie for 1 day (24 hours)
document.getElementById('areaModal').remove();
}
function setCookie(name, value, days) {
var date = new Date();
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
document.cookie = name + '=' + value + '; path=/; expires=' + date.toUTCString();
}
function getCookie(name) {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i].trim();
if (cookie.startsWith(name + '=')) {
return cookie.substring(name.length + 1);
}
}
return null;
}
function parseAndSanitizeHTML(html) {
var parser = new DOMParser();
var doc = parser.parseFromString(html, 'text/html');
return doc.body.innerHTML;
}
// Make dismissAnnouncement globally accessible
window.dismissAnnouncement = dismissAnnouncement;
})();
</script>
";
}
}