Diff -- /var/www/vhosts/3dshawn.com/affiliate.3dshawn.com/dashboard.cgi
Diff
/var/www/vhosts/3dshawn.com/affiliate.3dshawn.com/dashboard.cgi
added on local at 2026-07-01 13:47:18
Added
+173
lines
Removed
-0
lines
Context
0
unchanged
Blobs
from
to 6d5a9089ac43
to 6d5a9089ac43
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 | # AffSoft - signed-in dashboard (was welcome.cgi). | |
| 4 | # | |
| 5 | # Shows the user both roles at once: | |
| 6 | # - As a merchant: programs they own + total clicks/conversions/earnings | |
| 7 | # - As an affiliate: programs they've joined + clicks/conversions/earnings | |
| 8 | # | |
| 9 | # Empty states nudge to /program_new.cgi (start a program) and | |
| 10 | # /discover.cgi (find one to join). | |
| 11 | #====================================================================== | |
| 12 | use strict; | |
| 13 | use warnings; | |
| 14 | ||
| 15 | use lib '/var/www/vhosts/3dshawn.com/affiliate.3dshawn.com'; | |
| 16 | use CGI; | |
| 17 | use MODS::DBConnect; | |
| 18 | use MODS::Login; | |
| 19 | use MODS::AffSoft::Config; | |
| 20 | use MODS::AffSoft::Wrapper; | |
| 21 | ||
| 22 | my $q = CGI->new; | |
| 23 | my $auth = MODS::Login->new; | |
| 24 | my $wrap = MODS::AffSoft::Wrapper->new; | |
| 25 | my $db = MODS::DBConnect->new; | |
| 26 | my $cfg = MODS::AffSoft::Config->new; | |
| 27 | my $DB = $cfg->settings('database_name'); | |
| 28 | ||
| 29 | my $userinfo = $auth->login_verify; | |
| 30 | unless ($userinfo) { | |
| 31 | print "Status: 302 Found\r\nLocation: /login.cgi\r\n\r\n"; | |
| 32 | exit; | |
| 33 | } | |
| 34 | my $uid = $userinfo->{user_id} + 0; | |
| 35 | my $name = $userinfo->{display_name} || $userinfo->{email} || 'there'; | |
| 36 | ||
| 37 | my $dbh = $db->db_connect; | |
| 38 | ||
| 39 | my $merchant = $db->db_readwrite($dbh, qq~ | |
| 40 | SELECT | |
| 41 | (SELECT COUNT(*) FROM ${DB}.affiliate_programs WHERE merchant_user_id = $uid) AS programs, | |
| 42 | (SELECT COUNT(*) FROM ${DB}.affiliate_programs WHERE merchant_user_id = $uid AND status='active') AS active_programs, | |
| 43 | (SELECT COUNT(*) FROM ${DB}.affiliate_clicks WHERE program_id IN (SELECT id FROM ${DB}.affiliate_programs WHERE merchant_user_id = $uid)) AS clicks_total, | |
| 44 | (SELECT COUNT(*) FROM ${DB}.affiliate_clicks WHERE program_id IN (SELECT id FROM ${DB}.affiliate_programs WHERE merchant_user_id = $uid) AND occurred_at >= NOW() - INTERVAL 1 DAY) AS clicks_24h, | |
| 45 | (SELECT COUNT(*) FROM ${DB}.affiliate_conversions WHERE program_id IN (SELECT id FROM ${DB}.affiliate_programs WHERE merchant_user_id = $uid) AND status='pending') AS conv_pending, | |
| 46 | (SELECT COALESCE(SUM(commission_cents),0) FROM ${DB}.affiliate_conversions WHERE program_id IN (SELECT id FROM ${DB}.affiliate_programs WHERE merchant_user_id = $uid) AND status IN ('approved','paid')) AS commission_payable_cents | |
| 47 | ~, $ENV{SCRIPT_NAME}, __LINE__) // {}; | |
| 48 | ||
| 49 | my $affiliate = $db->db_readwrite($dbh, qq~ | |
| 50 | SELECT | |
| 51 | (SELECT COUNT(*) FROM ${DB}.affiliate_memberships WHERE affiliate_user_id = $uid AND status='approved') AS memberships, | |
| 52 | (SELECT COUNT(*) FROM ${DB}.affiliate_links WHERE affiliate_user_id = $uid) AS links, | |
| 53 | (SELECT COUNT(*) FROM ${DB}.affiliate_clicks WHERE affiliate_user_id = $uid) AS clicks_total, | |
| 54 | (SELECT COUNT(*) FROM ${DB}.affiliate_clicks WHERE affiliate_user_id = $uid AND occurred_at >= NOW() - INTERVAL 1 DAY) AS clicks_24h, | |
| 55 | (SELECT COUNT(*) FROM ${DB}.affiliate_conversions WHERE affiliate_user_id = $uid) AS conv_total, | |
| 56 | (SELECT COALESCE(SUM(commission_cents),0) FROM ${DB}.affiliate_conversions WHERE affiliate_user_id = $uid AND status IN ('approved','paid')) AS commission_earned_cents | |
| 57 | ~, $ENV{SCRIPT_NAME}, __LINE__) // {}; | |
| 58 | ||
| 59 | $db->db_disconnect($dbh); | |
| 60 | ||
| 61 | print "Content-Type: text/html; charset=utf-8\r\nCache-Control: no-store, no-cache, must-revalidate, max-age=0\r\nPragma: no-cache\r\nExpires: 0\r\n\r\n"; | |
| 62 | ||
| 63 | my $name_h = _h($name); | |
| 64 | ||
| 65 | my $m_programs = $merchant->{programs} // 0; | |
| 66 | my $m_active = $merchant->{active_programs} // 0; | |
| 67 | my $m_clicks24 = $merchant->{clicks_24h} // 0; | |
| 68 | my $m_clicksT = $merchant->{clicks_total} // 0; | |
| 69 | my $m_pending = $merchant->{conv_pending} // 0; | |
| 70 | my $m_payable = sprintf('$%.2f', ($merchant->{commission_payable_cents} // 0) / 100); | |
| 71 | ||
| 72 | my $a_members = $affiliate->{memberships} // 0; | |
| 73 | my $a_links = $affiliate->{links} // 0; | |
| 74 | my $a_clicks24 = $affiliate->{clicks_24h} // 0; | |
| 75 | my $a_clicksT = $affiliate->{clicks_total} // 0; | |
| 76 | my $a_convs = $affiliate->{conv_total} // 0; | |
| 77 | my $a_earned = sprintf('$%.2f', ($affiliate->{commission_earned_cents} // 0) / 100); | |
| 78 | ||
| 79 | my $merchant_section; | |
| 80 | if ($m_programs == 0) { | |
| 81 | $merchant_section = <<'HTML'; | |
| 82 | <div style="padding:36px 28px;border:1px dashed rgba(102,99,255,.35);background:rgba(102,99,255,.05);border-radius:14px"> | |
| 83 | <div style="color:#a78bfa;font-size:11px;text-transform:uppercase;letter-spacing:.08em;font-weight:600;margin-bottom:6px">As a merchant</div> | |
| 84 | <h2 style="margin:0 0 8px;font-size:20px;color:#fff">Launch your first program</h2> | |
| 85 | <p style="color:#aaa;margin:0 0 20px;max-width:500px">A program is the offer affiliates promote. Set the destination URL, commission, and attribution window, and AffSoft mints tracking links and reports conversions for you.</p> | |
| 86 | <a href="/program_new.cgi" class="btn btn-primary">Create your first program</a> | |
| 87 | </div> | |
| 88 | HTML | |
| 89 | } else { | |
| 90 | $merchant_section = qq~ | |
| 91 | <div> | |
| 92 | <div style="display:flex;justify-content:space-between;align-items:baseline;gap:16px;margin-bottom:14px;flex-wrap:wrap"> | |
| 93 | <div> | |
| 94 | <div style="color:#a78bfa;font-size:11px;text-transform:uppercase;letter-spacing:.08em;font-weight:600">As a merchant</div> | |
| 95 | <h2 style="margin:2px 0 0;font-size:20px;color:#fff">$m_programs program@{[ $m_programs==1 ? '' : 's']} · $m_active active</h2> | |
| 96 | </div> | |
| 97 | <div style="display:flex;gap:10px"><a href="/programs.cgi" class="btn btn-sm" style="background:rgba(255,255,255,.08);color:#fff;border:1px solid rgba(255,255,255,.12)">Manage</a><a href="/program_new.cgi" class="btn btn-primary btn-sm">+ New</a></div> | |
| 98 | </div> | |
| 99 | <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(150px,1fr));gap:10px"> | |
| 100 | @{[ _stat_card('Clicks (24h)', $m_clicks24) ]} | |
| 101 | @{[ _stat_card('Clicks total', $m_clicksT) ]} | |
| 102 | @{[ _stat_card('Pending conversions', $m_pending) ]} | |
| 103 | @{[ _stat_card('Payable to affiliates', $m_payable) ]} | |
| 104 | </div> | |
| 105 | </div> | |
| 106 | ~; | |
| 107 | } | |
| 108 | ||
| 109 | my $affiliate_section; | |
| 110 | if ($a_members == 0) { | |
| 111 | $affiliate_section = <<'HTML'; | |
| 112 | <div style="padding:36px 28px;border:1px dashed rgba(34,197,94,.30);background:rgba(34,197,94,.05);border-radius:14px"> | |
| 113 | <div style="color:#86efac;font-size:11px;text-transform:uppercase;letter-spacing:.08em;font-weight:600;margin-bottom:6px">As an affiliate</div> | |
| 114 | <h2 style="margin:0 0 8px;font-size:20px;color:#fff">Join a program to earn</h2> | |
| 115 | <p style="color:#aaa;margin:0 0 20px;max-width:500px">Browse the catalog of public programs, join the ones that fit your audience, and AffSoft hands you a tracking link the moment you join.</p> | |
| 116 | <a href="/discover.cgi" class="btn" style="background:#22c55e;color:#000;border:0">Browse programs</a> | |
| 117 | </div> | |
| 118 | HTML | |
| 119 | } else { | |
| 120 | $affiliate_section = qq~ | |
| 121 | <div> | |
| 122 | <div style="display:flex;justify-content:space-between;align-items:baseline;gap:16px;margin-bottom:14px;flex-wrap:wrap"> | |
| 123 | <div> | |
| 124 | <div style="color:#86efac;font-size:11px;text-transform:uppercase;letter-spacing:.08em;font-weight:600">As an affiliate</div> | |
| 125 | <h2 style="margin:2px 0 0;font-size:20px;color:#fff">$a_members program@{[ $a_members==1 ? '' : 's']} joined · $a_links link@{[ $a_links==1 ? '' : 's']}</h2> | |
| 126 | </div> | |
| 127 | <div style="display:flex;gap:10px"><a href="/my_links.cgi" class="btn btn-sm" style="background:rgba(255,255,255,.08);color:#fff;border:1px solid rgba(255,255,255,.12)">Your links</a><a href="/discover.cgi" class="btn btn-sm" style="background:rgba(34,197,94,.20);color:#86efac;border:1px solid rgba(34,197,94,.4)">Find more</a></div> | |
| 128 | </div> | |
| 129 | <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(150px,1fr));gap:10px"> | |
| 130 | @{[ _stat_card('Clicks (24h)', $a_clicks24) ]} | |
| 131 | @{[ _stat_card('Clicks total', $a_clicksT) ]} | |
| 132 | @{[ _stat_card('Conversions', $a_convs) ]} | |
| 133 | @{[ _stat_card('Earned', $a_earned) ]} | |
| 134 | </div> | |
| 135 | </div> | |
| 136 | ~; | |
| 137 | } | |
| 138 | ||
| 139 | my $body = <<HTML; | |
| 140 | <div class="page-pad" style="padding:0"> | |
| 141 | <h1 style="margin:0 0 4px;font-size:30px;letter-spacing:-.01em">Welcome back, $name_h</h1> | |
| 142 | <p style="color:#888;margin:0 0 36px">Here's your AffSoft activity across both sides of the platform.</p> | |
| 143 | ||
| 144 | <div style="display:grid;grid-template-columns:1fr;gap:24px"> | |
| 145 | $merchant_section | |
| 146 | $affiliate_section | |
| 147 | </div> | |
| 148 | </div> | |
| 149 | HTML | |
| 150 | ||
| 151 | $wrap->render({ | |
| 152 | userinfo => $userinfo, | |
| 153 | title => 'Dashboard', | |
| 154 | body => $body, | |
| 155 | }); | |
| 156 | ||
| 157 | #---------------------------------------------------------------------- | |
| 158 | sub _h { | |
| 159 | my $s = shift // ''; | |
| 160 | $s =~ s/&/&/g; $s =~ s/</</g; $s =~ s/>/>/g; | |
| 161 | $s =~ s/"/"/g; $s =~ s/'/'/g; | |
| 162 | return $s; | |
| 163 | } | |
| 164 | ||
| 165 | sub _stat_card { | |
| 166 | my ($label, $value) = @_; | |
| 167 | return qq~ | |
| 168 | <div style="padding:14px 16px;border:1px solid rgba(255,255,255,.08);border-radius:10px;background:rgba(20,20,30,.4)"> | |
| 169 | <div style="color:#666;font-size:10px;text-transform:uppercase;letter-spacing:.05em;margin-bottom:4px">$label</div> | |
| 170 | <div style="font-size:22px;color:#fff;font-variant-numeric:tabular-nums">$value</div> | |
| 171 | </div> | |
| 172 | ~; | |
| 173 | } |