CallBackery

 view release on metacpan or  search on metacpan

lib/CallBackery/GuiPlugin/AbstractTable.pm  view on Meta::CPAN



=head2 getData ('tableData|tableRowCount',tableDataRequest);

Return the requested table data and pass other types of request on to the upper levels.

=cut

sub getData {
    my $self = shift;
    my $type = shift // '';
    if ($type eq 'tableData'){
        return $self->getTableData(@_);
    }
    elsif ($type eq 'tableRowCount'){
        return $self->getTableRowCount(@_);
    }
    else {
        return $self->SUPER::getData($type,@_);
    }
}

=head2 getTableData({formData=>{},firstRow=>{},lastRow=>{},sortColumn=>'key',sortDesc=>true})

return data appropriate for the remote table widget

=cut

sub getTableData {
    return [{}];
}

=head2 getTableRowCount({formData=>{}})

return the number of rows matching the given formData

=cut

sub getTableRowCount {
    return 0;
}

=head2 makeExportAction(type => 'XLSX', filename => 'export-"now"', label => 'Export')

Create export button.
The default type is XLSX, also available is CSV.

=cut

sub makeExportAction {
    my $self = shift;
    my %args = @_;
    my $type = $args{type} // 'XLSX';
    my $label = $args{label} // trm("Export %1", $type);
    my $filename = $args{filename}
        // localtime->strftime('export-%Y-%m-%d-%H-%M-%S.').lc($type);

    return  {
        label            => $label,
        action           => 'download',
        addToContextMenu => true,
        key              => 'export_csv',
        actionHandler    => sub {
            my $self = shift;
            my $args = shift;
            my $data = $self->getTableData({
                formData => $args,
                firstRow => 0,
                lastRow => $self->getTableRowCount({ formData=>$args })
            });

            # Use the (translated) table headers in row 1.
            # Or the keys if undefined.
            my $loc = CallBackery::Translate->new(localeRoot=>$self->app->home->child("share"));
            $loc->setLocale($self->user->userInfo->{lang} // 'en');
            my $tCfg = $self->tableCfg;

            my @titles = map {
                $_->{label}
                    ? ((ref $_->{label} eq 'CallBackery::Translate')
                        ? $loc->tra($_->{label}[0])
                        : $_->{label})
                    : $_->{key};
            } @$tCfg;

            if ($type eq 'CSV') {
                my $csv = Text::CSV->new;
                $csv->combine(@titles);
                my $csv_str = $csv->string . "\n";
                for my $record (@$data) {
                    $csv->combine(map {
                        my $v = $record->{$_->{key}};
                        if ($_->{type} eq 'date') {
                            $v= localtime($v/1000)->strftime("%Y-%m-%d %H:%M:%S %z");
                        }
                        $v} @$tCfg);
                    $csv_str .= $csv->string . "\n";
                }
                my $asset = Mojo::Asset::Memory->new;
                $asset->add_chunk($csv_str);
                return {
                    asset    => $asset,
                    type     => 'text/csv',
                    filename => $filename,
                }
            }
            elsif ($type eq 'XLSX') {
                open my $xh, '>', \my $xlsx or die "failed to open xlsx fh: $!";
                my $workbook  = Excel::Writer::XLSX->new($xh);
                my $worksheet = $workbook->add_worksheet();

                my $col = 0;
                map {$worksheet->write(0, $col, $_); $col++} @titles;

                my $row = 2;
                my %date_format;
                for my $record (@$data) {
                    $col = 0;
                    for my $tc (@$tCfg) {
                        my $v = $record->{$tc->{key}};
                        if ($tc->{type} eq 'date') {



( run in 1.977 second using v1.01-cache-2.11-cpan-39bf76dae61 )