added on local at 2026-07-01 22:09:43
| 1 | #!/usr/bin/perl | |
| 2 | #====================================================================== | |
| 3 | # ShopCart - Preferences (notifications + UI behavior) | |
| 4 | # | |
| 5 | # Notification toggles (sale alerts, platform health, review digest, | |
| 6 | # A/B winners, weekly summary, marketing). Split out of the old tabbed | |
| 7 | # settings.cgi so URL conventions match PTMatrix | |
| 8 | # (/profile.cgi = account identity, /preferences.cgi = notifications + UI). | |
| 9 | # | |
| 10 | # Body lives in TEMPLATES/shopcart_preferences.html. | |
| 11 | #====================================================================== | |
| 12 | use strict; | |
| 13 | use warnings; | |
| 14 | ||
| 15 | use lib '/var/www/vhosts/3dshawn.com/shop.3dshawn.com'; | |
| 16 | use CGI; | |
| 17 | use MODS::Template; | |
| 18 | use MODS::DBConnect; | |
| 19 | use MODS::Login; | |
| 20 | use MODS::ShopCart::Config; | |
| 21 | use MODS::ShopCart::Wrapper; | |
| 22 | ||
| 23 | my $q = CGI->new; | |
| 24 | my $form = $q->Vars; | |
| 25 | my $auth = MODS::Login->new; | |
| 26 | my $wrap = MODS::ShopCart::Wrapper->new; | |
| 27 | my $tfile = MODS::Template->new; | |
| 28 | my $db = MODS::DBConnect->new; | |
| 29 | my $cfg = MODS::ShopCart::Config->new; | |
| 30 | my $DB = $cfg->settings('database_name'); | |
| 31 | ||
| 32 | $|=1; | |
| 33 | my $userinfo = $auth->login_verify(); | |
| 34 | if (!$userinfo) { | |
| 35 | print "Status: 302 Found\nLocation: /login.cgi\n\n"; | |
| 36 | exit; | |
| 37 | } | |
| 38 | my $uid = $userinfo->{user_id}; | |
| 39 | $uid =~ s/[^0-9]//g; | |
| 40 | ||
| 41 | my $dbh = $db->db_connect(); | |
| 42 | ||
| 43 | # ---- Notification prefs schema (mirrored into JSON in user_settings) - | |
| 44 | my @NOTIF_KEYS = qw( | |
| 45 | sale_alerts | |
| 46 | platform_health | |
| 47 | review_digest | |
| 48 | ab_winners | |
| 49 | weekly_summary | |
| 50 | marketing | |
| 51 | ); | |
| 52 | my %NOTIF_DEFAULT = ( | |
| 53 | sale_alerts => 1, | |
| 54 | platform_health => 1, | |
| 55 | review_digest => 1, | |
| 56 | ab_winners => 1, | |
| 57 | weekly_summary => 1, | |
| 58 | marketing => 0, | |
| 59 | ); | |
| 60 | ||
| 61 | my $saved_msg = ''; | |
| 62 | ||
| 63 | sub _esc { | |
| 64 | my $s = shift; | |
| 65 | $s //= ''; | |
| 66 | $s =~ s/\\/\\\\/g; | |
| 67 | $s =~ s/'/''/g; | |
| 68 | return $s; | |
| 69 | } | |
| 70 | ||
| 71 | sub _parse_notif_json { | |
| 72 | my ($raw) = @_; | |
| 73 | my %out = %NOTIF_DEFAULT; | |
| 74 | return \%out unless $raw && $raw =~ /\S/; | |
| 75 | foreach my $k (@NOTIF_KEYS) { | |
| 76 | if ($raw =~ /"\Q$k\E"\s*:\s*(true|false|0|1)/i) { | |
| 77 | my $v = lc $1; | |
| 78 | $out{$k} = ($v eq 'true' || $v eq '1') ? 1 : 0; | |
| 79 | } | |
| 80 | } | |
| 81 | return \%out; | |
| 82 | } | |
| 83 | ||
| 84 | sub _build_notif_json { | |
| 85 | my ($prefs) = @_; | |
| 86 | my @parts; | |
| 87 | foreach my $k (@NOTIF_KEYS) { | |
| 88 | my $v = $prefs->{$k} ? 1 : 0; | |
| 89 | push @parts, qq{"$k":$v}; | |
| 90 | } | |
| 91 | return '{' . join(',', @parts) . '}'; | |
| 92 | } | |
| 93 | ||
| 94 | if (($form->{action} || '') eq 'save_notifications') { | |
| 95 | my %prefs; | |
| 96 | foreach my $k (@NOTIF_KEYS) { | |
| 97 | $prefs{$k} = ($form->{"notif_$k"} ? 1 : 0); | |
| 98 | } | |
| 99 | my $json = _build_notif_json(\%prefs); | |
| 100 | ||
| 101 | $db->db_readwrite($dbh, qq~ | |
| 102 | INSERT INTO `${DB}`.user_settings | |
| 103 | SET user_id='$uid', | |
| 104 | notification_prefs='~ . _esc($json) . qq~', | |
| 105 | marketing_opt_in='~ . ($prefs{marketing} ? 1 : 0) . qq~' | |
| 106 | ON DUPLICATE KEY UPDATE | |
| 107 | notification_prefs=VALUES(notification_prefs), | |
| 108 | marketing_opt_in=VALUES(marketing_opt_in) | |
| 109 | ~, $ENV{SCRIPT_NAME}, __LINE__); | |
| 110 | $saved_msg = 'Notification preferences saved.'; | |
| 111 | } | |
| 112 | ||
| 113 | # ---- Load current state -------------------------------------------- | |
| 114 | my $s = $db->db_readwrite($dbh, qq~ | |
| 115 | SELECT notification_prefs, marketing_opt_in | |
| 116 | FROM `${DB}`.user_settings | |
| 117 | WHERE user_id='$uid' | |
| 118 | ~, $ENV{SCRIPT_NAME}, __LINE__) || {}; | |
| 119 | my $prefs = _parse_notif_json($s->{notification_prefs}); | |
| 120 | $prefs->{marketing} = $s->{marketing_opt_in} ? 1 : 0 if exists $s->{marketing_opt_in}; | |
| 121 | ||
| 122 | $db->db_disconnect($dbh); | |
| 123 | ||
| 124 | my $tvars = { | |
| 125 | user_id => $uid, | |
| 126 | saved_msg => $saved_msg, | |
| 127 | has_saved_msg => $saved_msg ? 1 : 0, | |
| 128 | ||
| 129 | notif_sale_alerts_on => $prefs->{sale_alerts} ? 'on' : '', | |
| 130 | notif_platform_health_on => $prefs->{platform_health} ? 'on' : '', | |
| 131 | notif_review_digest_on => $prefs->{review_digest} ? 'on' : '', | |
| 132 | notif_ab_winners_on => $prefs->{ab_winners} ? 'on' : '', | |
| 133 | notif_weekly_summary_on => $prefs->{weekly_summary} ? 'on' : '', | |
| 134 | notif_marketing_on => $prefs->{marketing} ? 'on' : '', | |
| 135 | ||
| 136 | notif_sale_alerts_checked => $prefs->{sale_alerts} ? 'checked' : '', | |
| 137 | notif_platform_health_checked => $prefs->{platform_health} ? 'checked' : '', | |
| 138 | notif_review_digest_checked => $prefs->{review_digest} ? 'checked' : '', | |
| 139 | notif_ab_winners_checked => $prefs->{ab_winners} ? 'checked' : '', | |
| 140 | notif_weekly_summary_checked => $prefs->{weekly_summary} ? 'checked' : '', | |
| 141 | notif_marketing_checked => $prefs->{marketing} ? 'checked' : '', | |
| 142 | }; | |
| 143 | ||
| 144 | 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"; | |
| 145 | ||
| 146 | my $body = join('', $tfile->template('shopcart_preferences.html', $tvars, $userinfo)); | |
| 147 | ||
| 148 | $wrap->render({ | |
| 149 | userinfo => $userinfo, | |
| 150 | page_key => 'preferences', | |
| 151 | title => 'Preferences', | |
| 152 | body => $body, | |
| 153 | }); |