CallBackery
view release on metacpan or search on metacpan
lib/CallBackery/GuiPlugin/AbstractTable.pm view on Meta::CPAN
image.
=item insertAfter
Optional. The zero based index in L</tableCfg> the new column is placed after.
The indices of all extra columns refer to the original L</tableCfg>, so
several extra columns cannot shift each other around. Without it the column
follows the column named by C<content>, which is therefore mandatory for a
code reference content.
=item label
Optional column heading, translated like any other label. It defaults to the
heading of the column named by C<content>.
=item size
Optional edge length of the image in pixels, 100 by default. The row height
and the column width are adjusted to fit.
=back
C<extraCols> is ignored for a CSV export, which has nowhere to put an image.
A C<qrCode> column needs L<Text::QRCode>, which is loaded on first use so that
installations without QR code exports do not have to carry it.
=cut
# The rule xtr() applies in the frontend: an array is a message to
# translate, anything else is data. That covers a trm() made in this process
# and one that crossed JSON on its way here -- a status some other system
# reported, say -- which is a plain array by now. Handed to the writer as it
# was, the first came out in English and the second as "ARRAY(0x...)" in a
# csv, while Excel::Writer::XLSX spread it over the following cells and left
# the bare "%1" standing in this one. Arguments are rendered first, so a
# nested message gets its own msgid translated before it is substituted.
sub _renderMessage {
my $loc = shift;
my $value = shift;
return $value
unless ref $value eq 'CallBackery::Translate'
or ref $value eq 'ARRAY';
my ($str,@args) = @$value;
return '' unless defined $str and $str ne '';
return $loc->tra($str, map { _renderMessage($loc, $_) // '' } @args);
}
sub makeExportAction {
my $self = shift;
my %args = @_;
my $type = $args{type} // 'XLSX';
my $extraCols = $args{extraCols};
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 $tra = sub { _renderMessage($loc, shift) };
my @titles = map { $tra->($_->{label}) // $_->{key} } @$tCfg;
if ($type eq 'CSV') {
# Without binary mode Text::CSV refuses a line break or any
# character outside ASCII, and combine() then leaves an
# empty string behind: the row went missing without a word.
my $csv = Text::CSV->new({ binary => 1 });
my $line = sub {
$csv->combine(@_)
or die mkerror(9938, "failed to write csv line: "
. $csv->error_diag);
return $csv->string . "\n";
};
my $csv_str = $line->(@titles);
for my $record (@$data) {
$csv_str .= $line->(map {
my $v = $record->{$_->{key}};
# an empty date is an empty cell, as in the xlsx
# export, not the start of 1970
if ($_->{type} eq 'date' and $v) {
$v= localtime($v/1000)->strftime("%Y-%m-%d %H:%M:%S %z");
}
$tra->($v)} @$tCfg);
}
my $asset = Mojo::Asset::Memory->new;
# an asset holds bytes; the text has to be encoded first
$asset->add_chunk(encode('UTF-8', $csv_str));
return {
asset => $asset,
type => 'text/csv; charset=UTF-8',
filename => $filename,
}
}
elsif ($type eq 'XLSX') {
# a spreadsheet is the only export that can hold images, so
# the extra columns exist here and are ignored for CSV
my $plan = $self->_exportColumnPlan($tCfg, $extraCols);
open my $xh, '>', \my $xlsx or die "failed to open xlsx fh: $!";
my $workbook = Excel::Writer::XLSX->new($xh);
my $worksheet = $workbook->add_worksheet();
( run in 1.112 second using v1.01-cache-2.11-cpan-60884a8df76 )