Diff -- /var/www/vhosts/3dshawn.com/shop.3dshawn.com/MODS/ShopCart/Wrapper.pm
Diff

/var/www/vhosts/3dshawn.com/shop.3dshawn.com/MODS/ShopCart/Wrapper.pm

added on local at 2026-07-01 22:09:41

Added
+599
lines
Removed
-0
lines
Context
0
unchanged
Blobs
from
to 10b542677459
Restore this content →
Unified diff
Naive LCS-based line diff. Additions in emerald, deletions in rose. Both sides decompressed live from BLOB_STORE.
1package MODS::ShopCart::Wrapper;
2#======================================================================
3# ShopCart — page wrapper.
4#
5# Renders the sidebar + topbar shell around a page's main content.
6# Uses MODS::Template for the shell template (shopcart_wrapper.html
7# for logged-in pages, shopcart_marketing.html for logged-out).
8#
9# Caller pattern:
10#
11# my $body = qq~ ...page body html... ~;
12# $wrap->render({
13# userinfo => $userinfo, # required for logged-in shell
14# page_key => 'dashboard', # marks the active sidebar item
15# title => 'Dashboard',
16# body => $body,
17# extra_js => ['/assets/javascript/graphs.js'], # optional
18# });
19#======================================================================
20
21use strict;
22use warnings;
23
24use MODS::Template;
25use MODS::DBConnect;
26use MODS::ShopCart::Config;
27
28my $tfile = MODS::Template->new;
29my $config = MODS::ShopCart::Config->new;
30my $db = MODS::DBConnect->new;
31
32# Sidebar nav: section headers + entries. Each entry has a key
33# matched against $page_key to set "active", an icon name (resolved
34# to inline SVG below), label, href, and optional badge.
35my @SIDEBAR = (
36 # Sidebar mirrors the profile dropdown in shopcart_wrapper.html --
37 # same sections, same wording, same hrefs, same ordering so the
38 # two menus read as one family. Order: Studio > Insights > Account
39 # > Admin (admin-only). Admin section contains every platform-wide
40 # control surface.
41 #
42 # Per-row gating attrs (all optional):
43 # feature = permission_features.slug -- hide row from team
44 # members who lack the slug. Owners always pass.
45 # owner_only = 1 -- only the account owner (no owner_user_id) sees
46 # this row. Team members never see it regardless of role.
47 # Used for billing + team management + "danger zone" entries.
48 # admin_only = 1 -- hides from non-admin users entirely.
49 { section => 'Workspace' },
50 { key => 'dashboard', label => 'Dashboard', href => '/dashboard.cgi', icon => 'welcome' },
51 { key => 'calendar', label => 'Calendar', href => '/calendar.cgi', icon => 'calendar' },
52 { key => 'storefront', label => 'My Storefront', href => '/storefront.cgi', icon => 'storefront', feature => 'view_storefront' },
53 { key => 'models', label => 'My Products', href => '/models.cgi', icon => 'models', feature => 'view_models' },
54 # The Upload-new-product action lives ON /models.cgi as a button now
55 # (was its own sidebar entry "New Listing"). The CGI itself is at
56 # /upload_model.cgi and reachable through the My Products CTA.
57 { key => 'categories', label => 'Categories', href => '/categories.cgi', icon => 'models', feature => 'edit_models' },
58 { key => 'bundles', label => 'Bundles', href => '/bundles.cgi', icon => 'models', feature => 'edit_models' },
59 { key => 'fulfillment', label => 'Fulfill Orders', href => '/fulfill.cgi', icon => 'billing', feature => 'view_orders' },
60 { key => 'shipping', label => 'Shipping Setup', href => '/shipping_settings.cgi', icon => 'analytics', feature => 'edit_models' },
61 { key => 'subscriptions', label => 'Subscription Plans',href => '/subscription_plans.cgi', icon => 'promotions', feature => 'edit_models' },
62 { key => 'promotions', label => 'Promotions', href => '/promotions.cgi', icon => 'promotions', feature => 'view_promotions' },
63 { key => 'storefront_seo', label => 'SEO', href => '/storefront_seo.cgi', icon => 'seo', feature => 'view_seo' },
64
65 # Reporting consolidated from 5 items -> 2 + Optimization.
66 # Sales Reports merges Sales Dashboard + Analytics + Audience.
67 # Traffic Statistics holds Heatmaps under its tab nav (no separate
68 # sidebar entry). Optimization stays separate -- A/B testing is a
69 # tool, not a report.
70 { section => 'Reporting' },
71 { key => 'sales_reports', label => 'Sales Reports', href => '/sales_reports.cgi', icon => 'dashboard', feature => 'view_reports' },
72 { key => 'orders', label => 'Orders', href => '/orders.cgi', icon => 'billing', feature => 'view_orders' },
73 { key => 'customers', label => 'Customers', href => '/customers.cgi', icon => 'team', feature => 'view_orders' },
74 { key => 'optimize', label => 'Optimization Testing', href => '/optimization.cgi', icon => 'optimize', feature => 'view_reports' },
75
76 # Help-section items (Tutorials, A/B & MVT tutorials, Product tour)
77 # live in the topbar Help-button dropdown, not the sidebar -- the
78 # dropdown is reachable from every page so help is always one click
79 # away without taking sidebar real estate. Source: shopcart_wrapper.html.
80
81 { section => 'Account' },
82 { key => 'migrate', label => 'Migrate & Get Paid', href => '/onboarding_migrate.cgi', icon => 'storefront' },
83 { key => 'my_profile', label => 'My Profile', href => '/profile.cgi', icon => 'profile' },
84 { key => 'billing', label => 'Billing & Plan', href => '/billing.cgi', icon => 'billing', feature => 'manage_billing' },
85 { key => 'team', label => 'Team Members', href => '/team.cgi', icon => 'team', owner_only => 1 },
86 { key => 'preferences', label => 'Preferences', href => '/preferences.cgi', icon => 'settings' },
87
88 { section => 'Admin', admin_only => 1 },
89 { key => 'admin_earnings', label => 'Earnings & Expenses', href => '/admin_earnings.cgi', icon => 'billing', super_admin_only => 1, tip => 'Revenue from paid invoices + manual expense ledger. Net + margin. Date range with this-month default.' },
90 { key => 'admin_funnels', label => 'Conversion Funnel', href => '/admin_funnels.cgi', icon => 'funnel',
91 admin_only => 1 },
92 { key => 'admin_traffic', label => 'Traffic Reports', href => '/admin_traffic.cgi', icon => 'analytics',
93 admin_only => 1, tip => 'Where visitors are coming from + globe + per-country drill.' },
94 { key => 'admin_messages', label => 'Support inbox', href => '/admin_messages.cgi', icon => 'messages',
95 admin_only => 1, feature => 'admin_view_messages' }, { key => 'admin_companies', label => 'All Companies', href => '/admin_companies.cgi', icon => 'building', admin_only => 1 },
96
97 { key => 'admin_users', label => 'All Users', href => '/admin_users.cgi', icon => 'admin',
98 admin_only => 1, feature => 'admin_view_users' },
99
100 { key => 'admin_teams', label => 'Team Members', href => '/admin_teams.cgi', icon => 'team',
101 admin_only => 1 },
102 { key => 'admin_billing', label => 'User Billing', href => '/admin_billing.cgi', icon => 'billing',
103 admin_only => 1, feature => 'admin_view_billing' },
104 # Pricing & Promotions is one sidebar entry covering BOTH the
105 # billing-packages catalogue and the platform-wide promotions list.
106 # The two pages share a tab nav at the top so the user can switch
107 # between them; both set page_key='admin_packages' so this entry
108 # stays highlighted across either URL.
109 { key => 'admin_packages', label => 'Pricing & Promotions', href => '/admin_packages.cgi', icon => 'billing',
110 admin_only => 1, super_admin_only => 1 },
111 # Tax: rates + marketplace-facilitator collection policy. Visible
112 # to all admins, but only super-admins can save changes (the CGI
113 # gates the POST handlers). Separate from Pricing & Promotions
114 # because tax is platform-wide compliance, not seller-facing
115 # pricing.
116 { key => 'admin_tax', label => 'Tax', href => '/admin_tax.cgi', icon => 'billing',
117 admin_only => 1 },
118 # Default category list -- the starter pack copied into every new
119 # seller's seller_categories at signup. Sellers can then edit their
120 # own copy freely without affecting the master defaults.
121 { key => 'admin_categories', label => 'Default Categories', href => '/admin_categories.cgi', icon => 'models',
122 admin_only => 1 },
123 { key => 'admin_seo', label => 'SEO', href => '/admin_seo.cgi', icon => 'seo',
124 admin_only => 1, feature => 'admin_view_seo' },
125 # Heatmaps now lives as a tab inside /admin_visitors.cgi (no
126 # separate sidebar entry). /admin_heatmap.cgi is still reachable
127 # at its direct URL and renders the same tab nav with Heatmaps
128 # highlighted.
129 { key => 'admin_chat', label => 'Chat', href => '/admin_chat.cgi', icon => 'chat',
130 admin_only => 1, feature => 'admin_chat' }, # Sandbox CCs for QA. Super-admin-only because a misused test card
131 # could be assigned to a real seller's account and silently
132 # "approve" their next charge. Staff with manage-billing get to see
133 # tests it; only the platform owner gets to create them.
134 { key => 'admin_test_cards', label => 'Test Credit Cards', href => '/admin_test_cards.cgi', icon => 'card',
135 admin_only => 1, super_admin_only => 1 },
136 { key => 'email_setup', label => 'Email Setup', href => '/admin_emails.cgi', icon => 'messages', admin_only => 1, super_admin_only => 1 },
137 # Platform-owner-only: edits Stripe keys / branding / email and
138 # other DB-backed settings. Regular admins do NOT see this row.
139 { key => 'admin_maintenance', label => 'Maintenance', href => '/admin_maintenance.cgi', icon => 'settings', super_admin_only => 1, tip => 'Lock the site for everyone except admins; edit the message users see while locked.' }, { key => 'admin_config', label => 'Configuration', href => '/admin_config.cgi', icon => 'settings',
140 admin_only => 1, super_admin_only => 1 },
141);
142
143sub new {
144 my ($class) = @_;
145 return bless({}, $class);
146}
147
148#----------------------------------------------------------------------
149# render(\%opts) — print the full page (Content-Type already sent
150# by the calling .cgi).
151#----------------------------------------------------------------------
152sub render {
153 my ($self, $opts) = @_;
154 $opts ||= {};
155
156 my $userinfo = $opts->{userinfo};
157 my $logged_in = $userinfo && $userinfo->{user_id} ? 1 : 0;
158
159 my $tvars = {
160 website_title => $config->settings('website_title'),
161 brand_name => $config->settings('brand_name'),
162 brand_tagline => $config->settings('brand_tagline'),
163 assets => $config->settings('assets'),
164 assets_css => $config->settings('assets_css'),
165 assets_js => $config->settings('assets_js'),
166 title => $opts->{title} || 'ShopCart',
167 body => $opts->{body} || '',
168 extra_js => _format_scripts($opts->{extra_js}),
169 extra_css => _format_styles($opts->{extra_css}),
170 # SEO meta block (built below). Renders into shopcart_wrapper.html
171 # / shopcart_marketing.html at $meta_tags so every page emits a
172 # consistent <meta> set without each CGI having to know.
173 meta_tags => _build_meta_tags($opts),
174 };
175
176 if ($logged_in) {
177 $tvars->{user_id} = $userinfo->{user_id};
178 $tvars->{display_name} = $userinfo->{display_name};
179 $tvars->{email} = $userinfo->{email};
180 $tvars->{plan_tier} = ucfirst($userinfo->{plan_tier} || 'free');
181 $tvars->{initials} = _initials($userinfo->{display_name} || $userinfo->{email});
182
183 my $is_admin = $userinfo->{is_admin} || $userinfo->{_admin_is_admin} || 0;
184 $tvars->{is_admin} = $is_admin;
185 # Unread comments/reviews drive the red dot on the bell icon and
186 # the Messages entry in both menus. Querying once here so the
187 # sidebar builder + the topbar template both read the same number.
188 # Topbar Messages pill was removed 2026-06-21 along with the
189 # cross-platform inbox (messages.cgi) deletion. Admins reach the
190 # Support inbox via the sidebar entry; sellers don't have a
191 # Messages surface (they reach support via /help.cgi).
192 my $unread = 0;
193 # Live chats awaiting an admin reply. Drives the topbar pill
194 # so admins can't miss a visitor mid-conversation no matter
195 # what page they're on. Pill always renders for admins; the
196 # idle (blue) vs waiting (green/pulsing) state is gated on
197 # count. Clicking the waiting pill jumps straight to the
198 # longest-waiting chat so admins skip the console list.
199 my $chat_data = $is_admin ? _pending_chats_data() : { n => 0, first_id => 0 };
200 $tvars->{admin_chats_pending} = $chat_data->{n};
201 $tvars->{has_admin_chats_pending} = $chat_data->{n} > 0 ? 1 : 0;
202 $tvars->{admin_chats_first_id} = $chat_data->{first_id} || 0;
203 $tvars->{admin_chats_href} = $chat_data->{first_id}
204 ? "/admin_chat.cgi?chat=$chat_data->{first_id}"
205 : '/admin_chat.cgi';
206 $tvars->{sidebar} = _build_sidebar($opts->{page_key} || '', $is_admin, $unread, $userinfo);
207 $tvars->{dropdown_links} = _build_dropdown_links($is_admin, $unread, $userinfo);
208 $tvars->{impersonation_banner} = _impersonation_banner($userinfo);
209
210 # Per-user sidebar pref: applied as a class on <html> so the
211 # final layout is in place on first paint (no flash). Guarded
212 # against the user_settings.sidebar_collapsed column not having
213 # been added yet -- a missing-column SELECT would 500 the page.
214 my ($collapsed, $mobile_open) = _sidebar_pref($userinfo->{user_id});
215 my @cls;
216 push @cls, 'sidebar-collapsed' if $collapsed;
217 push @cls, 'mobile-nav-open' if $mobile_open;
218 $tvars->{html_class} = join(' ', @cls);
219 }
220
221 # Heatmap-view mode: when /admin_heatmap.cgi loads a page in its
222 # preview iframe it appends `heatmap_view=1` to the URL. We render
223 # that request as if the visitor were logged OUT so the DOM
224 # matches exactly what a real visitor saw -- otherwise the admin
225 # session would inject the studio sidebar wrapper and every stored
226 # selector would resolve to a different element. We also suppress
227 # track.js so the admin's preview clicks don't pollute analytics.
228 my $heatmap_view = ($ENV{QUERY_STRING} || '') =~ /(?:^|&)heatmap_view=1(?:&|$)/ ? 1 : 0;
229 $tvars->{heatmap_view} = $heatmap_view;
230
231 # Tutorial-embed mode: a [frame:/url] block inside a tutorial body
232 # loads the target URL in an iframe with ?tut_embed=1 appended. We
233 # strip the sidebar/topbar (same as heatmap_view does) so the
234 # iframe shows just the page content, not the whole studio shell.
235 my $tut_embed = ($ENV{QUERY_STRING} || '') =~ /(?:^|&)tut_embed=1(?:&|$)/ ? 1 : 0;
236 $tvars->{tut_embed} = $tut_embed;
237
238 my $template_file = ($logged_in && !$heatmap_view && !$tut_embed)
239 ? 'shopcart_wrapper.html'
240 : 'shopcart_marketing.html';
241
242 my @html = $tfile->template($template_file, $tvars, $userinfo);
243 print "@html";
244 return;
245}
246
247#======================================================================
248# Helpers
249#======================================================================
250
251sub _initials {
252 my ($name) = @_;
253 return '?' unless $name;
254 my @parts = split /[\s\@\.]+/, $name;
255 my $i = '';
256 $i .= uc(substr($parts[0], 0, 1)) if $parts[0];
257 $i .= uc(substr($parts[1], 0, 1)) if $parts[1];
258 return $i || uc(substr($name, 0, 1));
259}
260
261sub _format_scripts {
262 my ($list) = @_;
263 return '' unless $list && ref($list) eq 'ARRAY';
264 return join("\n", map { qq~<script src="$_"></script>~ } @$list);
265}
266
267sub _format_styles {
268 my ($list) = @_;
269 return '' unless $list && ref($list) eq 'ARRAY';
270 return join("\n", map { qq~<link rel="stylesheet" href="$_">~ } @$list);
271}
272
273# Cache the column-existence check so we don't run information_schema
274# on every page load. Under plain CGI this only saves work within a
275# single request; under FCGI/mod_perl it survives across requests.
276our $_sidebar_col_checked;
277our $_sidebar_col_exists;
278
279# Returns ($collapsed, $mobile_open) for the user's sidebar prefs.
280# Both default to 0 (expanded desktop, closed mobile drawer) when no
281# row exists or the migration columns are missing. sidebar_mobile_open
282# was added in a later migration so we guard for its absence.
283sub _sidebar_pref {
284 my ($user_id) = @_;
285 return (0, 0) unless $user_id;
286 $user_id =~ s/[^0-9]//g;
287 return (0, 0) unless $user_id;
288
289 my $DB = $config->settings('database_name');
290 my $dbh = $db->db_connect();
291 return (0, 0) unless $dbh;
292
293 unless ($_sidebar_col_checked) {
294 my $r = $db->db_readwrite($dbh, qq~
295 SELECT COUNT(*) AS n FROM information_schema.columns
296 WHERE table_schema='$DB'
297 AND table_name='user_settings'
298 AND column_name='sidebar_collapsed'
299 ~, $ENV{SCRIPT_NAME}, __LINE__);
300 $_sidebar_col_exists = ($r && $r->{n}) ? 1 : 0;
301 $_sidebar_col_checked = 1;
302 }
303
304 unless ($_sidebar_col_exists) {
305 $db->db_disconnect($dbh);
306 return (0, 0);
307 }
308
309 # Probe the optional mobile column (cheap, cached per-request).
310 my $mob_check = $db->db_readwrite($dbh, qq~
311 SELECT COUNT(*) AS n FROM information_schema.columns
312 WHERE table_schema='$DB'
313 AND table_name='user_settings'
314 AND column_name='sidebar_mobile_open'
315 ~, $ENV{SCRIPT_NAME}, __LINE__);
316 my $mob_extra = ($mob_check && $mob_check->{n}) ? ', sidebar_mobile_open' : '';
317
318 my $r = $db->db_readwrite($dbh, qq~
319 SELECT sidebar_collapsed$mob_extra
320 FROM `${DB}`.user_settings
321 WHERE user_id='$user_id'
322 LIMIT 1
323 ~, $ENV{SCRIPT_NAME}, __LINE__);
324 $db->db_disconnect($dbh);
325
326 my $collapsed = ($r && $r->{sidebar_collapsed}) ? 1 : 0;
327 my $mobile_open = ($r && $r->{sidebar_mobile_open}) ? 1 : 0;
328 return ($collapsed, $mobile_open);
329}
330
331
332sub _build_sidebar {
333 my ($active_key, $is_admin, $unread_messages, $userinfo) = @_;
334 $unread_messages ||= 0;
335 my $html = '';
336 my $pending_section; # buffered header; emitted when a child row gets through
337 my $is_team_member = ($userinfo && $userinfo->{owner_user_id}) ? 1 : 0;
338
339 foreach my $row (@SIDEBAR) {
340 # Admin entries (links AND section headers) are gated -- sellers
341 # must never see them. Hide the rows entirely (not just disable)
342 # so the page keys do not leak in HTML for non-admins to find.
343 next if $row->{admin_only} && !$is_admin;
344
345 if ($row->{section}) {
346 # Buffer the header; we only commit it once a visible child
347 # follows. Prevents dangling "Reporting" labels when every
348 # child is hidden by role.
349 $pending_section = $row->{section};
350 next;
351 }
352
353 # owner_only rows are visible only to the actual account owner
354 # (billing, team management, ...). Team members and impersonated
355 # admins are blocked here so the menu cannot leak them.
356 next if $row->{owner_only} && $is_team_member;
357
358 # super_admin_only rows are visible only to the platform owner
359 # (users.is_super_admin=1). Regular admins managing users / billing
360 # still see all the other admin rows but never this one.
361 if ($row->{super_admin_only}) {
362 require MODS::ShopCart::Permissions;
363 next unless MODS::ShopCart::Permissions->new->is_super_admin($userinfo);
364 }
365
366 # feature gate: skip if user lacks the slug. _has_user_feature
367 # short-circuits for owners (NULL permission_groups_id).
368 next if $row->{feature} && !_has_user_feature($userinfo, $row->{feature});
369
370 if (defined $pending_section) {
371 $html .= qq~<div class="sidebar-section-label">$pending_section</div>\n~;
372 $pending_section = undef;
373 }
374
375 my $cls = ($row->{key} eq $active_key) ? 'sidebar-link active' : 'sidebar-link';
376 my $svg = _icon($row->{icon});
377 my $badge = $row->{badge} ? qq~<span class="badge">$row->{badge}</span>~ : '';
378 # Live unread dot on the Messages row only -- the dot is omitted
379 # when there are zero unread comments/reviews so the navigation
380 # never shows a permanent "alert" indicator.
381 my $unread_dot = '';
382 if ($row->{key} && $row->{key} eq 'messages' && $unread_messages > 0) {
383 $unread_dot = qq~<span class="menu-dot" title="$unread_messages unread"></span>~;
384 }
385 $html .= qq~<a class="$cls" href="$row->{href}">$svg<span>$row->{label}</span>$unread_dot$badge</a>\n~;
386 }
387 return $html;
388}
389
390# Build the profile-dropdown links from the SAME @SIDEBAR data the
391# left sidebar uses. This guarantees the two menus stay in lockstep
392# whenever items are added / renamed / removed -- never hand-edit the
393# dropdown HTML directly. Section headers become <div class="profile-
394# dropdown-section">, divider rows are inserted before each section
395# (except the first), and admin-only items get the .profile-admin
396# class so they render in the admin red/orange accent like before.
397sub _build_dropdown_links {
398 my ($is_admin, $unread_messages, $userinfo) = @_;
399 $unread_messages ||= 0;
400 my $html = '';
401 my $emitted_first_section = 0;
402 my $pending_section;
403 my $pending_divider = 0;
404 my $is_team_member = ($userinfo && $userinfo->{owner_user_id}) ? 1 : 0;
405
406 foreach my $row (@SIDEBAR) {
407 next if $row->{admin_only} && !$is_admin;
408
409 if ($row->{section}) {
410 # Buffer the header + its divider; emit only when a child
411 # row gets through. Otherwise the dropdown shows lone
412 # "Reporting" headers with nothing under them.
413 $pending_section = $row->{section};
414 $pending_divider = $emitted_first_section ? 1 : 0;
415 next;
416 }
417
418 next if $row->{owner_only} && $is_team_member;
419 if ($row->{super_admin_only}) {
420 require MODS::ShopCart::Permissions;
421 next unless MODS::ShopCart::Permissions->new->is_super_admin($userinfo);
422 }
423 next if $row->{feature} && !_has_user_feature($userinfo, $row->{feature});
424
425 if (defined $pending_section) {
426 $html .= qq~<div class="profile-dropdown-divider"></div>\n~ if $pending_divider;
427 $html .= qq~<div class="profile-dropdown-section">$pending_section</div>\n~;
428 $emitted_first_section = 1;
429 $pending_section = undef;
430 $pending_divider = 0;
431 }
432
433 my $svg = _icon($row->{icon});
434 my $admin_c = $row->{admin_only} ? ' class="profile-admin"' : '';
435 my $unread_dot = '';
436 if ($row->{key} && $row->{key} eq 'messages' && $unread_messages > 0) {
437 $unread_dot = qq~<span class="menu-dot" style="margin-left:auto" title="$unread_messages unread"></span>~;
438 }
439 $html .= qq~<a href="$row->{href}"$admin_c><span class="dropdown-icon">$svg</span> $row->{label}$unread_dot</a>\n~;
440 }
441 return $html;
442}
443
444# Thin wrapper around MODS::ShopCart::Permissions->has_feature so the
445# sidebar builders stay decoupled from the module loader. Loaded once.
446my $_perm_singleton;
447sub _has_user_feature {
448 my ($userinfo, $slug) = @_;
449 return 1 unless $slug;
450 return 0 unless $userinfo;
451 # Owner / unconstrained: skip the DB trip
452 return 1 if !defined($userinfo->{permission_groups_id}) || $userinfo->{permission_groups_id} eq '';
453 if (!$_perm_singleton) {
454 return 1 unless eval { require MODS::ShopCart::Permissions; 1 };
455 $_perm_singleton = MODS::ShopCart::Permissions->new;
456 }
457 return $_perm_singleton->has_feature($userinfo, $slug);
458}
459
460# Open visitor chats with at least one unread message for the admin.
461# Drives both the topbar pill state (idle/waiting) and its click
462# target (jump direct to the longest-waiting chat). Table existence
463# is guarded via information_schema so pre-migration deploys can't
464# 500 the wrapper. Returns { n => count, first_id => oldest_pending_chat_id }.
465sub _pending_chats_data {
466 require MODS::DBConnect;
467 my $db = MODS::DBConnect->new;
468 my $DB = $config->settings('database_name');
469 my $dbh = $db->db_connect();
470 return { n => 0, first_id => 0 } unless $dbh;
471 my $have = $db->db_readwrite($dbh, qq~
472 SELECT COUNT(*) AS n FROM information_schema.tables
473 WHERE table_schema='$DB' AND table_name='support_chats'
474 ~, $ENV{SCRIPT_NAME}, __LINE__);
475 unless ($have && $have->{n}) {
476 $db->db_disconnect($dbh);
477 return { n => 0, first_id => 0 };
478 }
479 # ORDER BY last_message_at ASC -- oldest unread first. Sending an
480 # admin to the chat that has been waiting longest is the most
481 # courteous default; if they want a different one they can use
482 # the console list as normal.
483 my @rows = $db->db_readwrite_multiple($dbh, qq~
484 SELECT id FROM `${DB}`.support_chats
485 WHERE status = 'open'
486 AND unread_for_admin > 0
487 ORDER BY last_message_at ASC
488 ~, $ENV{SCRIPT_NAME}, __LINE__);
489 $db->db_disconnect($dbh);
490 return {
491 n => scalar(@rows),
492 first_id => scalar(@rows) ? ($rows[0]->{id} + 0) : 0,
493 };
494}
495
496# Banner shown across the top of every authenticated page WHILE an
497# admin is impersonating another user. The "Stop" link is a POST to
498# /admin_action.cgi (form, not GET) so accidental link-followers can't
499# end an impersonation by sharing a URL.
500sub _impersonation_banner {
501 my ($userinfo) = @_;
502 return '' unless $userinfo && $userinfo->{_impersonating};
503
504 my $target_email = _html_escape($userinfo->{email} || '');
505 my $target_name = _html_escape($userinfo->{display_name} || $target_email);
506 my $admin_email = _html_escape($userinfo->{_admin_email} || '');
507
508 return qq~
509<div class="impersonation-banner">
510 <div class="impersonation-banner-msg">
511 <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10Z"/></svg>
512 <span><strong>Acting as</strong> $target_name &lt;$target_email&gt; &middot; admin: $admin_email</span>
513 </div>
514 <form method="POST" action="/admin_action.cgi" class="impersonation-banner-form">
515 <input type="hidden" name="act" value="stop_impersonation">
516 <button type="submit" class="impersonation-banner-stop">Stop &amp; return to admin</button>
517 </form>
518</div>
519~;
520}
521
522sub _html_escape {
523 my ($s) = @_;
524 return '' unless defined $s;
525 $s =~ s/&/&amp;/g;
526 $s =~ s/</&lt;/g;
527 $s =~ s/>/&gt;/g;
528 $s =~ s/"/&quot;/g;
529 $s =~ s/'/&#39;/g;
530 return $s;
531}
532
533# Inline SVG icon library — keeps the wrapper independent of any
534# external icon font / file. All icons are 24x24, stroke-based.
535my %ICONS = (
536 dashboard => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="7" height="9" rx="1"/><rect x="14" y="3" width="7" height="5" rx="1"/><rect x="14" y="12" width="7" height="9" rx="1"/><rect x="3" y="16" width="7" height="5" rx="1"/></svg>',
537 models => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 2 3 7v10l9 5 9-5V7l-9-5Z"/><path d="m3 7 9 5 9-5"/><path d="M12 22V12"/></svg>',
538 upload => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/></svg>',
539 storefront => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="m3 9 1-5h16l1 5"/><path d="M5 9v11a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1V9"/><path d="M9 21V13h6v8"/></svg>',
540 audience => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></svg>',
541 messages => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2Z"/></svg>',
542 funnel => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M22 3H2l8 9.46V19l4 2v-8.54L22 3z"/></svg>',
543 analytics => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M3 3v18h18"/><path d="m7 14 4-4 4 4 5-5"/></svg>',
544 optimize => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M22 12h-4l-3 9L9 3l-3 9H2"/></svg>',
545 settings => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 1 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 1 1-4 0v-.09a1.65 1.65 0 0 0-1-1.51 1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 1 1-2.83-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 1 1 0-4h.09a1.65 1.65 0 0 0 1.51-1 1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 1 1 2.83-2.83l.06.06a1.65 1.65 0 0 0 1.82.33 1.65 1.65 0 0 0 1-1.51V3a2 2 0 1 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 1 1 2.83 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82v0a1.65 1.65 0 0 0 1.51 1H21a2 2 0 1 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1Z"/></svg>',
546 building => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="2" width="18" height="20" rx="2"/><path d="M9 22V12h6v10"/><path d="M9 6h.01"/><path d="M9 10h.01"/><path d="M15 6h.01"/><path d="M15 10h.01"/></svg>',
547 calendar => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="4" width="18" height="18" rx="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/></svg>',
548 clock => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>',
549 admin => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10Z"/><path d="m9 12 2 2 4-4"/></svg>',
550 billing => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="2" y="5" width="20" height="14" rx="2"/><line x1="2" y1="10" x2="22" y2="10"/><line x1="6" y1="15" x2="10" y2="15"/></svg>',
551 promotions => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M20.59 13.41 13.42 20.58a2 2 0 0 1-2.83 0L2 12V2h10l8.59 8.59a2 2 0 0 1 0 2.82z"/><line x1="7" y1="7" x2="7.01" y2="7"/></svg>',
552 profile => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></svg>',
553 seo => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="11" cy="11" r="7"/><path d="m21 21-4.3-4.3"/><path d="M7 11h8"/><path d="M11 7v8"/></svg>',
554 visitors => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M1 12s4-7 11-7 11 7 11 7-4 7-11 7-11-7-11-7Z"/><circle cx="12" cy="12" r="3"/></svg>',
555 chat => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2Z"/></svg>',
556 welcome => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M3 12 12 3l9 9"/><path d="M5 10v10a1 1 0 0 0 1 1h4v-6h4v6h4a1 1 0 0 0 1-1V10"/></svg>',
557 heatmap => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="8" cy="9" r="3"/><circle cx="16" cy="14" r="4"/><circle cx="10" cy="17" r="2"/><rect x="2" y="2" width="20" height="20" rx="2"/></svg>',
558 team => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></svg>',
559 card => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="2" y="5" width="20" height="14" rx="2"/><line x1="2" y1="10" x2="22" y2="10"/><line x1="6" y1="15" x2="10" y2="15"/><line x1="14" y1="15" x2="18" y2="15"/></svg>',
560);
561
562sub _icon {
563 my ($name) = @_;
564 return $ICONS{$name} || '';
565}
566
567# Build the <meta> block injected into every page's <head>. Pulls
568# platform defaults from platform_settings, and if the caller passed
569# opts->{storefront} (e.g. /store.cgi), layers the per-storefront SEO
570# row over the top. Cached per-request via $_meta_cache so repeated
571# calls in one CGI don't re-query.
572our $_meta_cache;
573sub _build_meta_tags {
574 my ($opts) = @_;
575 return '' unless $opts;
576
577 require MODS::DBConnect;
578 require MODS::ShopCart::SEO;
579 my $db = MODS::DBConnect->new;
580 my $seo = MODS::ShopCart::SEO->new;
581 my $DB = $config->settings('database_name');
582 my $dbh = $db->db_connect();
583 return '' unless $dbh;
584
585 my $platform = $seo->get_platform_settings($db, $dbh, $DB);
586 my $store = $opts->{storefront} || {};
587
588 my $tags = $seo->render_tags(
589 platform => $platform,
590 storefront => $store,
591 page_title => $opts->{title} || '',
592 request_url => $opts->{canonical_url} || '',
593 );
594
595 $db->db_disconnect($dbh);
596 return $tags;
597}
598
5991;
Keyboard: j next diff k previous diff g top G bottom r restore c compile-check ? help