Diff -- /var/www/vhosts/3dshawn.com/crm.3dshawn.com/save_sidebar.cgi
Diff

/var/www/vhosts/3dshawn.com/crm.3dshawn.com/save_sidebar.cgi

added on local at 2026-07-01 15:03:01

Added
+102
lines
Removed
-0
lines
Context
0
unchanged
Blobs
from
to 423e82791e4f
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 - Save the per-user sidebar-collapsed preference.
4#
5# Called by the toggle button in the wrapper template (fetch POST with
6# keepalive:true). Writes user_settings.sidebar_collapsed for the
7# logged-in user. Idempotent -- safe to call repeatedly.
8#======================================================================
9use strict;
10use warnings;
11
12use lib '/var/www/vhosts/3dshawn.com/crm.3dshawn.com';
13use CGI;
14use MODS::DBConnect;
15use MODS::Login;
16use MODS::ContactForge::Config;
17
18my $q = CGI->new;
19my $form = $q->Vars;
20my $auth = MODS::Login->new;
21my $db = MODS::DBConnect->new;
22my $cfg = MODS::ContactForge::Config->new;
23my $DB = $cfg->settings('database_name');
24
25$| = 1;
26
27# POST only -- prevents drive-by GETs from changing user prefs.
28if (($ENV{REQUEST_METHOD} || '') ne 'POST') {
29 print "Status: 405 Method Not Allowed\nContent-Type: text/plain\n\nPOST required\n";
30 exit;
31}
32
33my $userinfo = $auth->login_verify();
34if (!$userinfo || !$userinfo->{user_id}) {
35 print "Status: 401 Unauthorized\nContent-Type: text/plain\n\nlogin required\n";
36 exit;
37}
38
39my $uid = $userinfo->{user_id};
40$uid =~ s/[^0-9]//g;
41
42my $dbh = $db->db_connect();
43
44# Verify the table columns we want to write actually exist. The
45# wrapper renderer also guards this; a fresh DB without the migration
46# would otherwise 500 here too. sidebar_mobile_open was added later
47# than sidebar_collapsed -- accept it if present, otherwise silently
48# drop the mobile field.
49my $has_collapsed = $db->db_readwrite($dbh, qq~
50 SELECT COUNT(*) AS n FROM information_schema.columns
51 WHERE table_schema='$DB'
52 AND table_name='user_settings'
53 AND column_name='sidebar_collapsed'
54~, $ENV{SCRIPT_NAME}, __LINE__);
55my $has_mobile = $db->db_readwrite($dbh, qq~
56 SELECT COUNT(*) AS n FROM information_schema.columns
57 WHERE table_schema='$DB'
58 AND table_name='user_settings'
59 AND column_name='sidebar_mobile_open'
60~, $ENV{SCRIPT_NAME}, __LINE__);
61
62if (!$has_collapsed || !$has_collapsed->{n}) {
63 $db->db_disconnect($dbh);
64 print "Status: 503 Service Unavailable\nContent-Type: text/plain\n\nmigration pending\n";
65 exit;
66}
67
68# Build the UPSERT only over fields the client actually sent. A desktop
69# toggle does NOT touch the mobile column and vice versa, so each
70# breakpoint can drive its own state without stomping the other.
71my @ins_cols = ('user_id');
72my @ins_vals = ("'$uid'");
73my @upd_clause;
74
75if (exists $form->{sidebar_collapsed}) {
76 my $v = ($form->{sidebar_collapsed} eq '1') ? 1 : 0;
77 push @ins_cols, 'sidebar_collapsed';
78 push @ins_vals, "'$v'";
79 push @upd_clause, "sidebar_collapsed='$v'";
80}
81if ($has_mobile && $has_mobile->{n} && exists $form->{sidebar_mobile_open}) {
82 my $v = ($form->{sidebar_mobile_open} eq '1') ? 1 : 0;
83 push @ins_cols, 'sidebar_mobile_open';
84 push @ins_vals, "'$v'";
85 push @upd_clause, "sidebar_mobile_open='$v'";
86}
87
88if (@upd_clause) {
89 push @upd_clause, 'updated_at=NOW()';
90 my $cols_sql = join(',', @ins_cols);
91 my $vals_sql = join(',', @ins_vals);
92 my $upd_sql = join(',', @upd_clause);
93 $db->db_readwrite($dbh, qq~
94 INSERT INTO `${DB}`.user_settings ($cols_sql)
95 VALUES ($vals_sql)
96 ON DUPLICATE KEY UPDATE $upd_sql
97 ~, $ENV{SCRIPT_NAME}, __LINE__);
98}
99
100$db->db_disconnect($dbh);
101
102print "Content-Type: text/plain\nCache-Control: no-store\n\nok\n";
Keyboard: j next diff k previous diff g top G bottom r restore c compile-check ? help