Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%class>
- has 'dstart' => (isa => 'Str', required => 0);
- has 'istart' => (isa => 'Str', required => 0);
- has 'iend' => (isa => 'Str', required => 0);
- has 'mfstart' => (isa => 'Str', required => 0);
- has 'wfstart' => (isa => 'Str', required => 0);
- has 'afstart' => (isa => 'Str', required => 0);
- has 'dfstart' => (isa => 'Str', required => 0);
- has 'display_csv' => (isa => 'Str', required => 0);
- </%class>
- <%init>
- use Data::Dumper;
- use Time::Local;
- use POSIX qw/strftime/;
- use Text::CSV_XS;
- my ($start, $end, $item, $s, $r, @table, $t2);
- my $count = 1;
- my $j = 1;
- my $ms = $.istart;
- my $ds = $.dstart;
- my $currentyear = (localtime)[5] + 1900;
- my $lenght = $currentyear - 2004; #2004 date 1st created item
- $lenght++; #10 years
- #print STDERR Dumper ("lenght : ".$lenght);
- #print STDERR Dumper ("-----------------------------------------------------------------");
- #print STDERR Dumper ($.dstart);
- #print STDERR Dumper ($.istart);
- #print STDERR Dumper ($.mend);
- if ($.dstart){ #day
- $count = 1;
- }
- if ($.istart){ #intervale date
- $count = 1;
- }
- if ($.dfstart){ #daily
- $count = 400 * $lenght;# last 4 years in weeks
- }
- if ($.wfstart){ #weekly
- $count = 52 * $lenght;# last 10 years in weeks
- }
- if ($.mfstart){ #monthly
- $count = 12 * $lenght;# last 10 years in weeks
- }
- if ($.afstart){ #annually
- #$count = 52; #53 * 7 * 4 ; # last 4 years in days
- }
- while( $count >= 1) { # week breakdown for a year
- print STDERR Dumper ($count);
- if ($.dstart || $.iend || $.dfstart || $.wfstart || $.mfstart || $.mfstart)
- {
- if ($j<=1){
- print STDERR Dumper ("j = " .$j);
- if ($.dstart) {
- $s = $.dstart;
- $start = $.dstart;
- $ds = $.dstart;
- }
- elsif ($.iend) {
- $s = $.iend;
- $start = $.istart;
- $ds= $.istart;
- # print STDERR Dumper ("mend");
- }
- elsif ($.dfstart) {
- $s = $.dfstart;
- $start = $.dfstart;
- $ds = $.dfstart;
- }
- elsif ($.wfstart) {
- $s = $.wfstart;
- $start = $.wfstart;
- $ds = $.wfstart;
- }
- elsif ($.mfstart) {
- $s = $.mfstart;
- $start = $.mfstart;
- $ds = $.mfstart;
- }
- elsif ($.afstart) {
- $s = $.afstart;
- $start = $.afstart;
- $ds = $.afstart;
- }
- }
- else {
- $s= $r;
- $start = $r;
- print STDERR Dumper ("in the loop" .$count.", s : ".$end);
- }
- # print STDERR Dumper ("**************************************************************************");
- my @tval2 = ( $s =~ m|(\d{4})-(\d{2})-(\d{2})| ); #2012-07-29
- my $tstr2 = $tval2[1]."/".$tval2[2]."/".$tval2[0]; #29/02/2012
- print STDERR Dumper ("str2 : ".$tstr2);
- my @tval3 = ( $tstr2 =~ m|(\d{2})/(\d{2})/(\d{4})| );
- my $time2 = timelocal( 0, 0, 0, $tval3[1], $tval3[0] -1, $tval3[2] );
- print STDERR Dumper ("time2 : ".$time2);
- if ($.dstart){ #day
- $t2 = strftime( "%m/%d/%Y", localtime( $time2 + 24 * 3600)); #24*7 = 168
- }
- if ($.istart){ #intervale
- $t2 = strftime( "%m/%d/%Y", localtime( $time2 + 24 * 3600));
- }
- if ($.dfstart){ #daily
- $t2 = strftime( "%m/%d/%Y", localtime( $time2 + 24 * 3600));
- }
- if ($.wfstart){ #weekly
- $t2 = strftime( "%m/%d/%Y", localtime( $time2 + 168 * 3600)); #24*7 = 168
- }
- if ($.mfstart){ #monthly
- print STDERR Dumper ("t2 : ".$t2);
- $t2 = strftime( "%m/%d/%Y", localtime( $time2 + 756 * 3600)); #168 * 4 = 672
- }
- #print STDERR Dumper ( $s);
- print STDERR Dumper ($t2);
- my @t3 = ( $t2 =~ m|(\d{2})/(\d{2})/(\d{4})| );
- $end = $t3[2]."-".$t3[0]."-".$t3[1];
- print STDERR Dumper (" start: ".$start.", end : ".$end);
- }
- elsif ($.istart and $.iend)
- {
- $start = $.istart;
- #$end = $.iend; #2012-07-29
- }
- $item = $db->do(q{
- select ?::varchar as date_end, ?::varchar as date_start, it.display_name, count(it.id) as num_item
- from items i
- left join item_item_types iit on (iit.item_id = i.id)
- left join item_types it on (it.id = iit.item_type_id)
- left join item_sites ist on (ist.item_id = i.id)
- where i.live and i.published_at between ? and ?
- group by it.display_name order by it.display_name, num_item desc
- }, $end, $start, $start, $end);
- $table[$count]= $item;
- print STDERR Dumper ("end: ".$end);
- $j++;
- $count--;
- $r = $end;
- }
- if ($.display_csv){
- $m->clear_buffer;
- my @rows;
- my $row;
- my $csv = Text::CSV_XS->new({
- 'quote_char' => '"',
- 'escape_char' => '"',
- 'sep_char' => ',',
- 'binary' => 1
- });
- $csv->types([Text::CSV_XS::PV(),Text::CSV_XS::IV()]);
- # print array of resultrow
- for my $i (@table) {
- while ($i && $i->next) { # print each resultrow
- @rows=($i->{'date_start'},$i->{'date_end'},$i->{'display_name'},$i->{'num_item'});
- $csv->combine(@rows);
- $m->print($csv->string."\n");
- }
- }
- print STDERR Dumper ($ds);
- $m->res->content_type('text/csv');
- $m->res->header( 'Content-disposition' =>'attachment; filename=stat_'.$ds.'_'.$end.'.csv' );
- $m->abort;
- }
- </%init>
- <div class="left">
- <& /stats/index.mc &>
- <div>
- % if ($.dstart) {
- <h4>Between : <% $.dstart %> and <% $.dstart %></h4>
- % }
- % else {
- <h4>Between : <% $.istart %> and <% $.iend %></h4>
- % }
- </div>
- <div class="clear"><br></div>
- </div>
- <table>
- <thead>
- <tr>
- <th>Item Type Name</th>
- <th>Quantity </th>
- </tr>
- </thead>
- % while ($item && $item->next) {
- <tbody>
- % if ($item->{'num_item'} > 0)
- % {
- <td><% $item->{'display_name'} %></td>
- <td><% $item->{'num_item'} %> </td>
- % }
- </tr>
- % }
- </tbody>
- </table>
- <div class="clear"><br></div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement