added on local at 2026-07-01 21:47:16
| 1 | #!/usr/bin/perl | |
| 2 | #====================================================================== | |
| 3 | # RePricer -- User preferences (notifications + UI prefs). | |
| 4 | # | |
| 5 | # GET /preferences.cgi | |
| 6 | # POST action=save_notifications notif_sale_alerts, notif_platform_health, | |
| 7 | # notif_review_digest, notif_ab_winners, | |
| 8 | # notif_weekly_summary, notif_marketing | |
| 9 | # | |
| 10 | # Created 2026-06-20 by splitting the legacy /settings.cgi: account | |
| 11 | # identity stayed on /profile.cgi, notifications + UI prefs landed | |
| 12 | # here. This file is the customer-facing twin of PTMatrix's | |
| 13 | # /preferences.cgi. | |
| 14 | #====================================================================== | |
| 15 | use strict; | |
| 16 | use warnings; | |
| 17 | ||
| 18 | use lib '/var/www/vhosts/3dshawn.com/repricer.3dshawn.com'; | |
| 19 | use CGI; | |
| 20 | use MODS::Template; | |
| 21 | use MODS::DBConnect; | |
| 22 | use MODS::Login; | |
| 23 | use MODS::RePricer::Config; | |
| 24 | use MODS::RePricer::Wrapper; | |
| 25 | use MODS::RePricer::Billing; | |
| 26 | ||
| 27 | my $q = CGI->new; | |
| 28 | my $form = $q->Vars; | |
| 29 | my $auth = MODS::Login->new; | |
| 30 | my $wrap = MODS::RePricer::Wrapper->new; | |
| 31 | my $tfile = MODS::Template->new; | |
| 32 | my $db = MODS::DBConnect->new; | |
| 33 | my $cfg = MODS::RePricer::Config->new; | |
| 34 | my $bill = MODS::RePricer::Billing->new; | |
| 35 | my $DB = $cfg->settings('database_name'); | |
| 36 | ||
| 37 | $|=1; | |
| 38 | my $userinfo = $auth->login_verify(); | |
| 39 | if (!$userinfo) { | |
| 40 | print "Status: 302 Found\nLocation: /login.cgi\n\n"; | |
| 41 | exit; | |
| 42 | } | |
| 43 | my $uid = $userinfo->{user_id}; | |
| 44 | $uid =~ s/[^0-9]//g; | |
| 45 | ||
| 46 | my $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. | |
| 52 | my @NOTIF_KEYS = qw( | |
| 53 | sale_alerts | |
| 54 | platform_health | |
| 55 | review_digest | |
| 56 | ab_winners | |
| 57 | weekly_summary | |
| 58 | marketing | |
| 59 | ); | |
| 60 | my %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 ------------------------------------------------- | |
| 70 | my $saved_msg = ''; | |
| 71 | ||
| 72 | sub _esc { | |
| 73 | my $s = shift; | |
| 74 | $s //= ''; | |
| 75 | $s =~ s/\\/\\\\/g; | |
| 76 | $s =~ s/'/''/g; | |
| 77 | return $s; | |
| 78 | } | |
| 79 | ||
| 80 | sub _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. | |
| 86 | foreach my $k (@NOTIF_KEYS) { | |
| 87 | if ($raw =~ /"\Q$k\E"\s*:\s*(true|false|0|1)/i) { | |
| 88 | my $v = lc $1; | |
| 89 | $out{$k} = ($v eq 'true' || $v eq '1') ? 1 : 0; | |
| 90 | } | |
| 91 | } | |
| 92 | return \%out; | |
| 93 | } | |
| 94 | ||
| 95 | sub _build_notif_json { | |
| 96 | my ($prefs) = @_; | |
| 97 | my @parts; | |
| 98 | foreach my $k (@NOTIF_KEYS) { | |
| 99 | my $v = $prefs->{$k} ? 1 : 0; | |
| 100 | push @parts, qq{"$k":$v}; | |
| 101 | } | |
| 102 | return '{' . join(',', @parts) . '}'; | |
| 103 | } | |
| 104 | ||
| 105 | if (($form->{action} || '') eq 'save_notifications') { | |
| 106 | # Each toggle posts its key as '1' when on, missing when off. | |
| 107 | my %prefs; | |
| 108 | foreach my $k (@NOTIF_KEYS) { | |
| 109 | $prefs{$k} = ($form->{"notif_$k"} ? 1 : 0); | |
| 110 | } | |
| 111 | my $json = _build_notif_json(\%prefs); | |
| 112 | ||
| 113 | # Upsert user_settings row. The PK is user_id so INSERT ON DUPLICATE | |
| 114 | # is the safe shape here. | |
| 115 | $db->db_readwrite($dbh, qq~ | |
| 116 | INSERT INTO `${DB}`.user_settings | |
| 117 | SET user_id='$uid', | |
| 118 | notification_prefs='~ . _esc($json) . qq~', | |
| 119 | marketing_opt_in='~ . ($prefs{marketing} ? 1 : 0) . qq~' | |
| 120 | ON DUPLICATE KEY UPDATE | |
| 121 | notification_prefs=VALUES(notification_prefs), | |
| 122 | marketing_opt_in=VALUES(marketing_opt_in) | |
| 123 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 124 | $saved_msg = 'Notification preferences saved.'; | |
| 125 | } | |
| 126 | ||
| 127 | # ---- Load current state -------------------------------------------- | |
| 128 | my $s = $db->db_readwrite($dbh, qq~ | |
| 129 | SELECT notification_prefs, marketing_opt_in | |
| 130 | FROM `${DB}`.user_settings | |
| 131 | WHERE user_id='$uid' | |
| 132 | ~, $ENV{SCRIPT_NAME}, __LINE__) || {}; | |
| 133 | my $prefs = _parse_notif_json($s->{notification_prefs}); | |
| 134 | # marketing_opt_in is the source of truth for the marketing toggle. | |
| 135 | $prefs->{marketing} = $s->{marketing_opt_in} ? 1 : 0 if exists $s->{marketing_opt_in}; | |
| 136 | ||
| 137 | # ---- Plan entitlement state for the "Available Features" tab -------- | |
| 138 | # user_feature_entitlements returns one row per catalog feature with | |
| 139 | # {key, name, blurb, icon, module, included, locked, plan_label}. | |
| 140 | my @entitlements = $bill->user_feature_entitlements($db, $dbh, $DB, $uid); | |
| 141 | my $plan_label_for_features = (@entitlements && $entitlements[0]->{plan_label}) | |
| 142 | || 'Free'; | |
| 143 | my @feature_rows; | |
| 144 | my %ICON_SVG = ( | |
| 145 | send => '<path d="M22 2 11 13"/><path d="m22 2-7 20-4-9-9-4 20-7Z"/>', | |
| 146 | store => '<path d="m3 9 1-5h16l1 5"/><path d="M5 9v11a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1V9"/>', | |
| 147 | activity => '<path d="M22 12h-4l-3 9L9 3l-3 9H2"/>', | |
| 148 | chart => '<path d="M3 3v18h18"/><path d="m7 14 4-4 4 4 5-5"/>', | |
| 149 | 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"/>', | |
| 150 | ); | |
| 151 | foreach my $f (@entitlements) { | |
| 152 | push @feature_rows, { | |
| 153 | key => $f->{key}, | |
| 154 | name => $f->{name}, | |
| 155 | blurb => $f->{blurb}, | |
| 156 | icon_svg => $ICON_SVG{ $f->{icon} } || $ICON_SVG{chart}, | |
| 157 | included => $f->{included} ? 1 : 0, | |
| 158 | locked => $f->{locked} ? 1 : 0, | |
| 159 | module_href => '/' . ($f->{module} || 'dashboard') . '.cgi', | |
| 160 | }; | |
| 161 | } | |
| 162 | ||
| 163 | # Owner-only? (the "Available Features" panel is plan-scoped, so we | |
| 164 | # only show it to the account owner; team members on a sub-account | |
| 165 | # would otherwise see entitlements that aren't theirs to manage.) | |
| 166 | my $is_owner = (!$userinfo->{owner_user_id}) ? 1 : 0; | |
| 167 | ||
| 168 | $db->db_disconnect($dbh); | |
| 169 | ||
| 170 | my $tvars = { | |
| 171 | user_id => $uid, | |
| 172 | saved_msg => $saved_msg, | |
| 173 | has_saved_msg => $saved_msg ? 1 : 0, | |
| 174 | ||
| 175 | notif_sale_alerts_on => $prefs->{sale_alerts} ? 'on' : '', | |
| 176 | notif_platform_health_on => $prefs->{platform_health} ? 'on' : '', | |
| 177 | notif_review_digest_on => $prefs->{review_digest} ? 'on' : '', | |
| 178 | notif_ab_winners_on => $prefs->{ab_winners} ? 'on' : '', | |
| 179 | notif_weekly_summary_on => $prefs->{weekly_summary} ? 'on' : '', | |
| 180 | notif_marketing_on => $prefs->{marketing} ? 'on' : '', | |
| 181 | ||
| 182 | notif_sale_alerts_checked => $prefs->{sale_alerts} ? 'checked' : '', | |
| 183 | notif_platform_health_checked => $prefs->{platform_health} ? 'checked' : '', | |
| 184 | notif_review_digest_checked => $prefs->{review_digest} ? 'checked' : '', | |
| 185 | notif_ab_winners_checked => $prefs->{ab_winners} ? 'checked' : '', | |
| 186 | notif_weekly_summary_checked => $prefs->{weekly_summary} ? 'checked' : '', | |
| 187 | notif_marketing_checked => $prefs->{marketing} ? 'checked' : '', | |
| 188 | ||
| 189 | is_owner => $is_owner, | |
| 190 | plan_label_for_features => $plan_label_for_features, | |
| 191 | features => \@feature_rows, | |
| 192 | has_features => scalar(@feature_rows) ? 1 : 0, | |
| 193 | }; | |
| 194 | ||
| 195 | print "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"; | |
| 196 | ||
| 197 | my $body = join('', $tfile->template('repricer_preferences.html', $tvars, $userinfo)); | |
| 198 | ||
| 199 | $wrap->render({ | |
| 200 | userinfo => $userinfo, | |
| 201 | page_key => 'preferences', | |
| 202 | title => 'Preferences', | |
| 203 | body => $body, | |
| 204 | }); |