Diff -- /var/www/vhosts/3dshawn.com/affiliate.3dshawn.com/admin_email_schedules.cgi
Diff

/var/www/vhosts/3dshawn.com/affiliate.3dshawn.com/admin_email_schedules.cgi

added on local at 2026-07-01 13:46:58

Added
+413
lines
Removed
-0
lines
Context
0
unchanged
Blobs
from
to c98a7011726d
Restore this content →
Unified diff
Naive LCS-based line diff. Additions in emerald, deletions in rose. Both sides decompressed live from BLOB_STORE.
1#!/usr/bin/perl
2#======================================================================
3# AffSoft -- /admin_email_schedules.cgi
4#
5# Configure when each scheduled email fires. One row per (template,
6# trigger_event, offset). Edit offset_days / offset_hours / hour-of-day
7# inline; toggle active per row; add new schedule; delete schedule.
8#
9# Super-admin only.
10#======================================================================
11use strict;
12use warnings;
13
14use lib '/var/www/vhosts/3dshawn.com/affiliate.3dshawn.com';
15use CGI;
16use MODS::DBConnect;
17use MODS::AffSoft::Wrapper;
18use MODS::AffSoft::Config;
19use MODS::Login;
20
21my $q = CGI->new;
22my $form = $q->Vars;
23my $wrap = MODS::AffSoft::Wrapper->new;
24my $db = MODS::DBConnect->new;
25my $cfg = MODS::AffSoft::Config->new;
26my $DB = $cfg->settings('database_name');
27
28my $auth = MODS::Login->new;
29my $u = $auth->login_verify;
30unless ($u) { print "Status: 302 Found\nLocation: /login.cgi\n\n"; exit; }
31require MODS::AffSoft::Permissions;
32MODS::AffSoft::Permissions->new->require_super_admin($u);
33my $uid = $u->{user_id} + 0;
34
35my $dbh = $db->db_connect;
36my $act = $form->{act} || '';
37my $flash = '';
38
39my @EVENTS = qw(trial_end_at signup invoice_paid invoice_failed password_reset);
40my %EVENT_DESC = (
41 trial_end_at => "Relative to companies.trial_ends_at",
42 signup => "Relative to user signup",
43 invoice_paid => "Relative to a successful invoice payment",
44 invoice_failed => "Relative to a failed invoice charge",
45 password_reset => "When a password reset is requested",
46);
47
48if (($ENV{REQUEST_METHOD} || '') eq 'POST') {
49 if ($act eq 'save') {
50 my $sid = ($form->{schedule_id} || 0) + 0;
51 my $off_d = ($form->{offset_days} || 0) + 0;
52 my $off_h = ($form->{offset_hours} || 0) + 0;
53 my $hr = $form->{send_at_local_hour};
54 my $hr_sql = (defined $hr && $hr =~ /^\d+$/ && $hr >= 0 && $hr <= 23) ? "'$hr'" : 'NULL';
55 my $ev = $form->{trigger_event} || 'trial_end_at';
56 $ev = 'trial_end_at' unless grep { $_ eq $ev } @EVENTS;
57 my $active = $form->{is_active} ? 1 : 0;
58 my $notes = substr($form->{notes} || '', 0, 250); $notes =~ s/'/''/g;
59 $db->db_readwrite($dbh, qq~
60 UPDATE ${DB}.email_schedules
61 SET trigger_event='$ev', offset_days='$off_d', offset_hours='$off_h',
62 send_at_local_hour=$hr_sql, is_active='$active', notes='$notes',
63 updated_by_user_id='$uid'
64 WHERE id='$sid'
65 ~, $0, __LINE__);
66 $flash = 'Schedule updated.';
67 }
68 elsif ($act eq 'add') {
69 my $tid = ($form->{template_id} || 0) + 0;
70 my $ev = $form->{trigger_event} || 'trial_end_at';
71 $ev = 'trial_end_at' unless grep { $_ eq $ev } @EVENTS;
72 my $off_d = ($form->{offset_days} || 0) + 0;
73 my $off_h = ($form->{offset_hours} || 0) + 0;
74 if ($tid) {
75 $db->db_readwrite($dbh, qq~
76 INSERT INTO ${DB}.email_schedules
77 (template_id, trigger_event, offset_days, offset_hours, is_active, created_at, updated_by_user_id)
78 VALUES ('$tid', '$ev', '$off_d', '$off_h', 1, NOW(), '$uid')
79 ~, $0, __LINE__);
80 $flash = 'Schedule added.';
81 }
82 }
83 elsif ($act eq 'delete') {
84 my $sid = ($form->{schedule_id} || 0) + 0;
85 if ($sid) {
86 $db->db_readwrite($dbh, qq~DELETE FROM ${DB}.email_schedules WHERE id='$sid'~, $0, __LINE__);
87 $flash = 'Schedule deleted.';
88 }
89 }
90}
91
92my @scheds = $db->db_readwrite_multiple($dbh, qq~
93 SELECT s.id, s.template_id, s.trigger_event, s.offset_days, s.offset_hours,
94 s.send_at_local_hour, s.is_active, s.notes, s.updated_at,
95 t.slug, t.name AS template_name, t.category
96 FROM ${DB}.email_schedules s
97 JOIN ${DB}.email_templates t ON t.id = s.template_id
98 ORDER BY s.trigger_event, s.offset_days, t.slug
99~, $0, __LINE__);
100
101my @templates = $db->db_readwrite_multiple($dbh, qq~
102 SELECT id, slug, name, category FROM ${DB}.email_templates ORDER BY category, slug
103~, $0, __LINE__);
104
105$db->db_disconnect($dbh);
106
107print "Content-Type: text/html; charset=utf-8\r\nCache-Control: no-store\r\n\r\n";
108
109my $flash_html = $flash ? qq~<div style="padding:10px 14px;background:rgba(34,197,94,.12);border:1px solid rgba(34,197,94,.3);border-radius:8px;color:#86efac;font-size:13px;margin-bottom:18px">$flash</div>~ : '';
110
111my $rows = '';
112foreach my $s (@scheds) {
113 my $hr_val = defined($s->{send_at_local_hour}) ? $s->{send_at_local_hour} : '';
114 my $checked = $s->{is_active} ? 'checked' : '';
115 my $name = _h($s->{template_name});
116 my $notes = _h($s->{notes} || '');
117 my $ev_opts = '';
118 foreach my $e (@EVENTS) {
119 my $sel = ($e eq $s->{trigger_event}) ? ' selected' : '';
120 $ev_opts .= qq~<option value="$e"$sel>$e</option>~;
121 }
122 my $timing = ($s->{offset_days} == 0 && $s->{offset_hours} == 0) ? 'on event'
123 : ($s->{offset_days} < 0 || $s->{offset_hours} < 0) ? "before"
124 : "after";
125 my $timing_color = $timing eq 'before' ? '#5aa9ff'
126 : $timing eq 'after' ? '#fbbf24'
127 : '#86efac';
128 my $timing_label = uc($timing);
129 $rows .= qq~
130 <tr style="border-top:1px solid rgba(255,255,255,.05)">
131 <td style="padding:14px 14px;font-size:13px;color:#e2e8f0;vertical-align:top;width:180px">
132 <div style="font-weight:600">$name</div>
133 <div style="font-size:11px;color:#94a3b8;font-family:ui-monospace,Consolas,Menlo,monospace;margin-top:4px">$s->{slug}</div>
134 </td>
135 <td style="padding:14px 14px;vertical-align:top">
136 <form method="POST" action="/admin_email_schedules.cgi" style="display:grid;grid-template-columns:1fr 1fr;gap:10px;margin:0;align-items:start">
137 <input type="hidden" name="act" value="save">
138 <input type="hidden" name="schedule_id" value="$s->{id}">
139
140 <div>
141 <label style="display:block;font-size:10px;color:#94a3b8;text-transform:uppercase;letter-spacing:.06em;margin-bottom:4px">Trigger event</label>
142 <select name="trigger_event" class="select" style="font-size:13px;padding:6px 8px;width:100%">$ev_opts</select>
143 </div>
144
145 <div>
146 <label style="display:block;font-size:10px;color:#94a3b8;text-transform:uppercase;letter-spacing:.06em;margin-bottom:4px">Notes</label>
147 <textarea class="input grow-on-focus" name="notes" rows="1" placeholder="optional note shown only on this admin page" style="font-size:13px;padding:6px 8px;width:100%;height:32px;resize:none;overflow:hidden;transition:height .15s ease;font-family:inherit;line-height:1.4" onfocus="this.style.height='120px';this.style.overflow='auto'" onblur="this.style.height='32px';this.style.overflow='hidden'">$notes</textarea>
148 </div>
149
150 <div style="display:flex;gap:10px;flex-wrap:wrap;align-items:flex-start">
151 <div style="border:1px solid rgba(90,169,255,.18);background:rgba(90,169,255,.04);border-radius:8px;padding:8px 12px">
152 <label style="display:block;font-size:10px;color:#94a3b8;text-transform:uppercase;letter-spacing:.06em;margin-bottom:4px">Offset <span style="color:$timing_color;font-weight:600">&middot; $timing_label</span></label>
153 <div style="display:flex;gap:6px;align-items:center;font-size:12px;color:#94a3b8">
154 <input class="input" name="offset_days" type="number" value="$s->{offset_days}" style="width:60px;padding:6px 8px;text-align:center"> <span>days</span>
155 <input class="input" name="offset_hours" type="number" value="$s->{offset_hours}" style="width:60px;padding:6px 8px;text-align:center"> <span>hours</span>
156 </div>
157 </div>
158 <div style="border:1px solid rgba(34,197,94,.18);background:rgba(34,197,94,.04);border-radius:8px;padding:8px 12px;display:flex;gap:14px;align-items:flex-end;flex-wrap:wrap">
159 <div>
160 <label style="display:block;font-size:10px;color:#94a3b8;text-transform:uppercase;letter-spacing:.06em;margin-bottom:4px">Send at local hour</label>
161 <div style="display:flex;gap:6px;align-items:center;font-size:12px;color:#94a3b8">
162 <input class="input" name="send_at_local_hour" type="number" min="0" max="23" value="$hr_val" placeholder="any" style="width:60px;padding:6px 8px;text-align:center"> <span>:00</span>
163 </div>
164 </div>
165 <label style="font-size:13px;color:#cbd5e1;display:flex;align-items:center;gap:8px;margin:0 0 6px 0;cursor:pointer">
166 <input type="checkbox" name="is_active" value="1" $checked style="width:16px;height:16px">
167 <span>Active</span>
168 </label>
169 </div>
170 </div>
171
172 <div style="display:flex;gap:10px;justify-content:flex-end;align-items:flex-start">
173 <button type="submit" class="btn btn-primary" style="font-size:13px;padding:8px 18px">Save changes</button>
174 <button type="button"
175 class="js-delete-schedule"
176 data-id="$s->{id}"
177 data-name="$name"
178 data-slug="$s->{slug}"
179 style="font-size:13px;padding:8px 18px;background:rgba(239,68,68,.10);color:#fca5a5;border:1px solid rgba(239,68,68,.35);border-radius:6px;cursor:pointer;font-weight:600">
180 Delete
181 </button>
182 </div>
183 </form>
184 </td>
185 </tr>~;
186}
187
188my $tpl_opts = '';
189foreach my $t (@templates) {
190 my $n = _h($t->{name});
191 $tpl_opts .= qq~<option value="$t->{id}">[$t->{category}] $n ($t->{slug})</option>~;
192}
193my $ev_opts = '';
194foreach my $e (@EVENTS) {
195 my $desc = _h($EVENT_DESC{$e});
196 $ev_opts .= qq~<option value="$e">$e &ndash; $desc</option>~;
197}
198
199my $tabs_html = _email_setup_tabs('schedules');
200
201my $body = qq~
202<style>
203 /* ---------- Confirmation modal: in-app, dark theme, animated ---------- */
204 .cmodal-back {
205 position: fixed; inset: 0; z-index: 9000;
206 background: rgba(8, 10, 16, .75);
207 backdrop-filter: blur(4px);
208 -webkit-backdrop-filter: blur(4px);
209 display: none; align-items: center; justify-content: center;
210 padding: 24px;
211 opacity: 0;
212 transition: opacity .18s ease;
213 }
214 .cmodal-back.is-open { display: flex; opacity: 1; }
215 .cmodal {
216 width: 100%; max-width: 460px;
217 background: linear-gradient(180deg, #161b29 0%, #0d1119 100%);
218 border: 1px solid rgba(255,255,255,.10);
219 border-radius: 16px;
220 box-shadow: 0 24px 70px rgba(0,0,0,.55), 0 0 0 1px rgba(255,255,255,.02) inset;
221 overflow: hidden;
222 transform: translateY(8px) scale(.98);
223 transition: transform .18s ease;
224 }
225 .cmodal-back.is-open .cmodal { transform: translateY(0) scale(1); }
226 .cmodal-head {
227 padding: 22px 24px 14px;
228 display: flex; align-items: flex-start; gap: 14px;
229 }
230 .cmodal-icon {
231 flex: 0 0 auto; width: 40px; height: 40px;
232 border-radius: 10px;
233 background: linear-gradient(135deg, rgba(239,68,68,.20), rgba(239,68,68,.08));
234 border: 1px solid rgba(239,68,68,.35);
235 display: flex; align-items: center; justify-content: center;
236 color: #fca5a5;
237 }
238 .cmodal-titlewrap { flex: 1; }
239 .cmodal-title { font-size: 17px; font-weight: 700; color: #f1f5f9; line-height: 1.3; margin: 4px 0 0; }
240 .cmodal-body {
241 padding: 0 24px 16px 78px;
242 font-size: 13.5px; color: #cbd5e1; line-height: 1.6;
243 }
244 .cmodal-body strong { color: #e2e8f0; }
245 .cmodal-body .cmodal-target {
246 display: inline-flex; gap: 8px; align-items: center;
247 margin-top: 10px;
248 padding: 8px 12px;
249 background: rgba(255,255,255,.03);
250 border: 1px solid rgba(255,255,255,.06);
251 border-radius: 8px;
252 font-size: 12.5px;
253 }
254 .cmodal-body .cmodal-target code {
255 font-family: ui-monospace,Consolas,Menlo,monospace;
256 font-size: 11.5px; color: #c4b5fd;
257 }
258 .cmodal-foot {
259 padding: 14px 22px 22px;
260 display: flex; justify-content: flex-end; gap: 10px;
261 background: rgba(0,0,0,.18);
262 border-top: 1px solid rgba(255,255,255,.05);
263 }
264 .cmodal-btn {
265 border: 0; border-radius: 9px;
266 padding: 10px 18px;
267 font-size: 13.5px; font-weight: 600;
268 cursor: pointer;
269 transition: background .12s, transform .08s;
270 font-family: inherit;
271 }
272 .cmodal-btn:active { transform: translateY(1px); }
273 .cmodal-btn-cancel {
274 background: rgba(255,255,255,.06);
275 color: #cbd5e1;
276 border: 1px solid rgba(255,255,255,.10);
277 }
278 .cmodal-btn-cancel:hover { background: rgba(255,255,255,.10); }
279 .cmodal-btn-danger {
280 background: linear-gradient(180deg, #dc2626, #b91c1c);
281 color: #fff;
282 border: 1px solid rgba(127,29,29,.6);
283 box-shadow: 0 1px 0 rgba(255,255,255,.10) inset, 0 6px 16px rgba(220,38,38,.30);
284 }
285 .cmodal-btn-danger:hover { background: linear-gradient(180deg, #ef4444, #dc2626); }
286</style>
287
288<div class="page-pad">
289 <h1 style="margin:0 0 6px;font-size:28px;color:#fff">Email Setup</h1>
290 <p style="color:#94a3b8;margin:0 0 18px;font-size:14px">Templates, schedules, and spam-protection controls.</p>
291 $tabs_html
292 $flash_html
293 <h2 style="font-size:18px;color:#fff;margin:0 0 6px">Schedules</h2>
294 <p style="color:#94a3b8;margin:0 0 14px;font-size:13px">When each lifecycle email fires. Negative offset = before the trigger event, positive = after.</p>
295
296 <div style="border:1px solid rgba(255,255,255,.08);border-radius:12px;overflow:hidden;background:rgba(20,20,30,.4);margin-bottom:32px">
297 <table style="width:100%;border-collapse:collapse">
298 <thead><tr>
299 <th style="padding:12px 14px;text-align:left;font-size:10.5px;color:#94a3b8;text-transform:uppercase;letter-spacing:.08em;width:180px">Template</th>
300 <th style="padding:12px 14px;text-align:left;font-size:10.5px;color:#94a3b8;text-transform:uppercase;letter-spacing:.08em">Trigger + offset</th>
301 </tr></thead>
302 <tbody>$rows</tbody>
303 </table>
304 </div>
305
306 <h2 style="font-size:18px;color:#fff;margin:0 0 14px">Add new schedule</h2>
307 <form method="POST" action="/admin_email_schedules.cgi" style="display:grid;grid-template-columns:1fr 1fr 100px 100px auto;gap:10px;align-items:end;padding:16px;border:1px solid rgba(91,33,182,.30);background:rgba(91,33,182,.05);border-radius:10px">
308 <input type="hidden" name="act" value="add">
309 <div><label style="display:block;font-size:11px;color:#cbd5e1;margin-bottom:4px;text-transform:uppercase">Template</label>
310 <select name="template_id" class="select" required>$tpl_opts</select></div>
311 <div><label style="display:block;font-size:11px;color:#cbd5e1;margin-bottom:4px;text-transform:uppercase">Trigger</label>
312 <select name="trigger_event" class="select">$ev_opts</select></div>
313 <div><label style="display:block;font-size:11px;color:#cbd5e1;margin-bottom:4px;text-transform:uppercase">Days</label>
314 <input class="input" name="offset_days" type="number" value="0" style="text-align:center"></div>
315 <div><label style="display:block;font-size:11px;color:#cbd5e1;margin-bottom:4px;text-transform:uppercase">Hours</label>
316 <input class="input" name="offset_hours" type="number" value="0" style="text-align:center"></div>
317 <div><button type="submit" class="btn btn-primary">+ Add schedule</button></div>
318 </form>
319</div>
320
321<!-- ===== Confirmation modal (hidden by default) ===== -->
322<div class="cmodal-back" id="cmodal-back" role="dialog" aria-modal="true" aria-labelledby="cmodal-title">
323 <div class="cmodal">
324 <div class="cmodal-head">
325 <div class="cmodal-icon">
326 <svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
327 <path d="M3 6h18"/><path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/>
328 <path d="M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"/>
329 <line x1="10" y1="11" x2="10" y2="17"/><line x1="14" y1="11" x2="14" y2="17"/>
330 </svg>
331 </div>
332 <div class="cmodal-titlewrap">
333 <div style="font-size:11px;text-transform:uppercase;letter-spacing:.08em;color:#fca5a5;font-weight:600">Confirm deletion</div>
334 <h2 class="cmodal-title" id="cmodal-title">Delete this schedule?</h2>
335 </div>
336 </div>
337 <div class="cmodal-body">
338 The template itself stays in place &mdash; only this scheduled send rule is removed. Other schedules for the same template are unaffected.
339 <div class="cmodal-target" id="cmodal-target">
340 <svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="#94a3b8" stroke-width="2"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>
341 <span id="cmodal-target-name" style="color:#e2e8f0;font-weight:500"></span>
342 <code id="cmodal-target-slug"></code>
343 </div>
344 </div>
345 <div class="cmodal-foot">
346 <button type="button" class="cmodal-btn cmodal-btn-cancel" id="cmodal-cancel">Cancel</button>
347 <button type="button" class="cmodal-btn cmodal-btn-danger" id="cmodal-confirm">Delete schedule</button>
348 </div>
349 </div>
350</div>
351
352<!-- Hidden form that the modal submits when the user confirms -->
353<form id="cmodal-form" method="POST" action="/admin_email_schedules.cgi" style="display:none">
354 <input type="hidden" name="act" value="delete">
355 <input type="hidden" name="schedule_id" id="cmodal-form-id" value="">
356</form>
357
358<script>
359(function () {
360 var back = document.getElementById('cmodal-back');
361 var nameEl = document.getElementById('cmodal-target-name');
362 var slugEl = document.getElementById('cmodal-target-slug');
363 var idEl = document.getElementById('cmodal-form-id');
364 var form = document.getElementById('cmodal-form');
365 var btnC = document.getElementById('cmodal-cancel');
366 var btnD = document.getElementById('cmodal-confirm');
367
368 function open(name, slug, id) {
369 nameEl.textContent = name;
370 slugEl.textContent = slug;
371 idEl.value = id;
372 back.classList.add('is-open');
373 document.body.style.overflow = 'hidden';
374 setTimeout(function(){ btnC.focus(); }, 50);
375 }
376 function close() {
377 back.classList.remove('is-open');
378 document.body.style.overflow = '';
379 }
380 document.querySelectorAll('.js-delete-schedule').forEach(function (b) {
381 b.addEventListener('click', function (ev) {
382 ev.preventDefault();
383 open(b.getAttribute('data-name'), b.getAttribute('data-slug'), b.getAttribute('data-id'));
384 });
385 });
386 btnC.addEventListener('click', close);
387 btnD.addEventListener('click', function () { form.submit(); });
388 back.addEventListener('click', function (ev) { if (ev.target === back) close(); });
389 document.addEventListener('keydown', function (ev) {
390 if (ev.key === 'Escape' && back.classList.contains('is-open')) close();
391 });
392})();
393</script>~;
394
395$wrap->render({ userinfo => $u, page_key => 'email_setup', title => 'Email Setup', body => $body });
396
397sub _email_setup_tabs {
398 my ($active) = @_;
399 my %t = (templates => 'Templates', schedules => 'Schedules', controls => 'Controls');
400 my %h = (templates => '/admin_emails.cgi', schedules => '/admin_email_schedules.cgi', controls => '/admin_email_controls.cgi');
401 my $html = '<div style="display:flex;gap:0;border-bottom:1px solid rgba(255,255,255,.08);margin:0 0 22px">';
402 foreach my $k (qw(templates schedules controls)) {
403 my $is_active = ($k eq $active);
404 my $color = $is_active ? '#5aa9ff' : '#94a3b8';
405 my $border = $is_active ? '#5aa9ff' : 'transparent';
406 my $weight = $is_active ? '600' : '500';
407 $html .= qq~<a href="$h{$k}" style="padding:10px 20px;color:$color;text-decoration:none;font-size:13px;border-bottom:2px solid $border;font-weight:$weight;letter-spacing:.02em">$t{$k}</a>~;
408 }
409 $html .= '</div>';
410 return $html;
411}
412
413sub _h { my $s = shift // ''; $s =~ s/&/&amp;/g; $s =~ s/</&lt;/g; $s =~ s/>/&gt;/g; $s =~ s/"/&quot;/g; $s =~ s/'/&#39;/g; return $s; }
Keyboard: j next diff k previous diff g top G bottom r restore c compile-check ? help