Diff -- /var/www/vhosts/webstls.com/httpdocs/preferences.cgi
Diff

/var/www/vhosts/webstls.com/httpdocs/preferences.cgi

added on WebSTLs (webstls.com) at 2026-07-01 22:26:44

Added
+207
lines
Removed
-0
lines
Context
0
unchanged
Blobs
from
to 50b07ba400ae
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# WebSTLs - Preferences (notifications + module visibility)
4#
5# Notification toggles (sale alerts, platform health, review digest,
6# A/B winners, weekly summary, marketing) + the "Available Features"
7# panel that shows which features the current plan includes and which
8# are locked behind a higher tier. Matches the PTMatrix /preferences.cgi
9# convention.
10#
11# Split off from the legacy /settings.cgi (which combined account +
12# notifications + module visibility in a single tabbed page). The
13# account / identity portion now lives at /profile.cgi.
14#======================================================================
15use strict;
16use warnings;
17
18use lib '/var/www/vhosts/webstls.com/httpdocs';
19use CGI;
20use MODS::Template;
21use MODS::DBConnect;
22use MODS::Login;
23use MODS::WebSTLs::Config;
24use MODS::WebSTLs::Wrapper;
25use MODS::WebSTLs::Billing;
26
27my $q = CGI->new;
28my $form = $q->Vars;
29my $auth = MODS::Login->new;
30my $wrap = MODS::WebSTLs::Wrapper->new;
31my $tfile = MODS::Template->new;
32my $db = MODS::DBConnect->new;
33my $cfg = MODS::WebSTLs::Config->new;
34my $bill = MODS::WebSTLs::Billing->new;
35my $DB = $cfg->settings('database_name');
36
37$|=1;
38my $userinfo = $auth->login_verify();
39if (!$userinfo) {
40 print "Status: 302 Found\nLocation: /login.cgi\n\n";
41 exit;
42}
43my $uid = $userinfo->{user_id};
44$uid =~ s/[^0-9]//g;
45
46my $dbh = $db->db_connect();
47
48# ---- Notification prefs schema (mirrored into JSON in user_settings) -
49# Toggle key => default. The UI shows each as a labelled switch and the
50# saved row stores `1`/`0`. New keys can be added without a migration --
51# the column is LONGTEXT JSON.
52my @NOTIF_KEYS = qw(
53 sale_alerts
54 platform_health
55 review_digest
56 ab_winners
57 weekly_summary
58 marketing
59);
60my %NOTIF_DEFAULT = (
61 sale_alerts => 1,
62 platform_health => 1,
63 review_digest => 1,
64 ab_winners => 1,
65 weekly_summary => 1,
66 marketing => 0,
67);
68
69# ---- POST handlers -------------------------------------------------
70my $saved_msg = '';
71
72sub _esc {
73 my $s = shift;
74 $s //= '';
75 $s =~ s/\\/\\\\/g;
76 $s =~ s/'/''/g;
77 return $s;
78}
79
80sub _parse_notif_json {
81 my ($raw) = @_;
82 my %out = %NOTIF_DEFAULT;
83 return \%out unless $raw && $raw =~ /\S/;
84 # Minimal JSON parse: key:0/1. We control writes, so no need for
85 # a full parser (and the vanilla-only rule rules out JSON::PP if
86 # not core -- it is core 5.14+, but we still keep this trivial).
87 foreach my $k (@NOTIF_KEYS) {
88 if ($raw =~ /"\Q$k\E"\s*:\s*(true|false|0|1)/i) {
89 my $v = lc $1;
90 $out{$k} = ($v eq 'true' || $v eq '1') ? 1 : 0;
91 }
92 }
93 return \%out;
94}
95
96sub _build_notif_json {
97 my ($prefs) = @_;
98 my @parts;
99 foreach my $k (@NOTIF_KEYS) {
100 my $v = $prefs->{$k} ? 1 : 0;
101 push @parts, qq{"$k":$v};
102 }
103 return '{' . join(',', @parts) . '}';
104}
105
106if (($form->{action} || '') eq 'save_notifications') {
107 # Each toggle posts its key as '1' when on, missing when off.
108 my %prefs;
109 foreach my $k (@NOTIF_KEYS) {
110 $prefs{$k} = ($form->{"notif_$k"} ? 1 : 0);
111 }
112 my $json = _build_notif_json(\%prefs);
113
114 # Upsert user_settings row. The PK is user_id so INSERT ON DUPLICATE
115 # is the safe shape here.
116 $db->db_readwrite($dbh, qq~
117 INSERT INTO `${DB}`.user_settings
118 SET user_id='$uid',
119 notification_prefs='~ . _esc($json) . qq~',
120 marketing_opt_in='~ . ($prefs{marketing} ? 1 : 0) . qq~'
121 ON DUPLICATE KEY UPDATE
122 notification_prefs=VALUES(notification_prefs),
123 marketing_opt_in=VALUES(marketing_opt_in)
124 ~, $ENV{SCRIPT_NAME}, __LINE__);
125 $saved_msg = 'Notification preferences saved.';
126}
127
128# ---- Load current state --------------------------------------------
129my $s = $db->db_readwrite($dbh, qq~
130 SELECT notification_prefs, marketing_opt_in
131 FROM `${DB}`.user_settings
132 WHERE user_id='$uid'
133~, $ENV{SCRIPT_NAME}, __LINE__) || {};
134my $prefs = _parse_notif_json($s->{notification_prefs});
135# marketing_opt_in is the source of truth for the marketing toggle.
136$prefs->{marketing} = $s->{marketing_opt_in} ? 1 : 0 if exists $s->{marketing_opt_in};
137
138# ---- Plan entitlement state for the "Available Features" panel ------
139# user_feature_entitlements returns one row per catalog feature with
140# {key, name, blurb, icon, module, included, locked, plan_label}.
141# Pre-build the bullet HTML in Perl so the template can emit the list
142# with a single [loop:].
143my @entitlements = $bill->user_feature_entitlements($db, $dbh, $DB, $uid);
144my $plan_label_for_features = (@entitlements && $entitlements[0]->{plan_label})
145 || 'Free';
146my @feature_rows;
147my %ICON_SVG = (
148 send => '<path d="M22 2 11 13"/><path d="m22 2-7 20-4-9-9-4 20-7Z"/>',
149 store => '<path d="m3 9 1-5h16l1 5"/><path d="M5 9v11a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1V9"/>',
150 activity => '<path d="M22 12h-4l-3 9L9 3l-3 9H2"/>',
151 chart => '<path d="M3 3v18h18"/><path d="m7 14 4-4 4 4 5-5"/>',
152 people => '<path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/>',
153);
154foreach my $f (@entitlements) {
155 push @feature_rows, {
156 key => $f->{key},
157 name => $f->{name},
158 blurb => $f->{blurb},
159 icon_svg => $ICON_SVG{ $f->{icon} } || $ICON_SVG{chart},
160 included => $f->{included} ? 1 : 0,
161 locked => $f->{locked} ? 1 : 0,
162 module_href => '/' . ($f->{module} || 'dashboard') . '.cgi',
163 };
164}
165
166# Only the account owner sees the Available Features panel -- team
167# members don't pay for the subscription so showing them the
168# locked/included grid would be misleading.
169my $is_owner = !($userinfo->{owner_user_id});
170
171$db->db_disconnect($dbh);
172
173my $tvars = {
174 user_id => $uid,
175 saved_msg => $saved_msg,
176 has_saved_msg => $saved_msg ? 1 : 0,
177 is_owner => $is_owner ? 1 : 0,
178
179 notif_sale_alerts_on => $prefs->{sale_alerts} ? 'on' : '',
180 notif_platform_health_on => $prefs->{platform_health} ? 'on' : '',
181 notif_review_digest_on => $prefs->{review_digest} ? 'on' : '',
182 notif_ab_winners_on => $prefs->{ab_winners} ? 'on' : '',
183 notif_weekly_summary_on => $prefs->{weekly_summary} ? 'on' : '',
184 notif_marketing_on => $prefs->{marketing} ? 'on' : '',
185
186 notif_sale_alerts_checked => $prefs->{sale_alerts} ? 'checked' : '',
187 notif_platform_health_checked => $prefs->{platform_health} ? 'checked' : '',
188 notif_review_digest_checked => $prefs->{review_digest} ? 'checked' : '',
189 notif_ab_winners_checked => $prefs->{ab_winners} ? 'checked' : '',
190 notif_weekly_summary_checked => $prefs->{weekly_summary} ? 'checked' : '',
191 notif_marketing_checked => $prefs->{marketing} ? 'checked' : '',
192
193 plan_label_for_features => $plan_label_for_features,
194 features => \@feature_rows,
195 has_features => scalar(@feature_rows) ? 1 : 0,
196};
197
198print "Content-Type: text/html; charset=utf-8\nCache-Control: no-store, no-cache, must-revalidate, max-age=0\nPragma: no-cache\nExpires: 0\n\n";
199
200my $body = join('', $tfile->template('webstls_preferences.html', $tvars, $userinfo));
201
202$wrap->render({
203 userinfo => $userinfo,
204 page_key => 'preferences',
205 title => 'Preferences',
206 body => $body,
207});
Keyboard: j next diff k previous diff g top G bottom r restore c compile-check ? help