#!/usr/bin/perl -wT
# -*- Mode: perl; tab-width: 4; indent-tabs-mode: nil; -*-
#
# Referrer Tracking: Stats

use strict;
use CGI::Carp;
use HTML::Entities;
use lib 'lib';

use vars '@ISA';
@ISA = qw(TheCommon);
require TheCommon;

sub main {
    my $self = shift;

    # /stats
    #    show the top 10 referrers and other stats
    
    my $data = $self->{database}->getTopReferralStats($self->{query}->param('count') || 10);

    $self->{http}->page('Statistics');

    if (@$data == 0) {
        $self->{http}->print('<p>No referrals yet.</p>');
    } else {
        $self->{http}->print(<<eof);
 <table>
  <thead>
   <tr>
    <th>Name</th>
    <th>Clickthroughs</th>
    <th>Downloads</th>
    <th>Registrations</th>
    <th>Total Referrals</th>
   </tr>
  </head>
  <tbody>
eof
        foreach my $datum (@$data) {
            foreach (values %$datum) {
                $_ = '' unless defined $_;
                $_ = encode_entities($_);
            }
            $self->{http}->print(<<eof);
   <tr>
    <td>$datum->{name}</td>
    <td>$datum->{a}</td>
    <td>$datum->{b}</td>
    <td>$datum->{c}</td>
    <td>$datum->{total}</td>
   </tr>
eof
            }
        $self->{http}->print(<<eof);
  </tbody>
 </table>
eof
    }
}
