Skip to content

Commit d812677

Browse files
Update from code review
Move PDF functionality to `GADS::PDFGenerator` class Remove unneccessary `use` statement in `GADS::ResultSet::Report` Add title to report download where it was removed in error
1 parent f4c5a1f commit d812677

File tree

5 files changed

+200
-234
lines changed

5 files changed

+200
-234
lines changed

lib/GADS.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1951,7 +1951,7 @@ any qr{/(record|history|purge|purgehistory)/([0-9]+)} => require_login sub {
19511951
my $site = var 'site'
19521952
or error __"No site configured";
19531953
my $pdf = $record->pdf($site)->content;
1954-
return send_file( \$pdf, content_type => 'application/pdf', );
1954+
return send_file( \$pdf, content_type => 'application/pdf', filename => "Record-".$record->current_id.".pdf");
19551955
}
19561956

19571957
if (query_parameters->get('report'))

lib/GADS/PDFGenerator.pm

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package GADS::PDFGenerator;
2+
3+
use strict;
4+
use warnings;
5+
6+
use Moo;
7+
8+
use GADS::Config;
9+
10+
use CtrlO::PDF;
11+
12+
has site => (
13+
is => 'ro',
14+
required => 1,
15+
);
16+
17+
has layouts => (
18+
is => 'ro',
19+
required => 1,
20+
);
21+
22+
has record => (
23+
is => 'ro',
24+
required => 1
25+
);
26+
27+
has user => (
28+
is => 'ro',
29+
required => 1,
30+
);
31+
32+
has security_marking => (
33+
is => 'ro',
34+
);
35+
36+
has _default_marking => (
37+
is => 'lazy',
38+
);
39+
40+
sub _build__default_marking {
41+
return shift->site->read_security_marking;
42+
}
43+
44+
has _logo => (
45+
is => 'lazy',
46+
);
47+
48+
sub _build__logo {
49+
return shift->site->create_temp_logo;
50+
}
51+
52+
sub build {
53+
my ($self, %options) = @_;
54+
55+
my $marking = $self->security_marking || $self->_default_marking;
56+
my $logo = $self->_logo;
57+
58+
my $pdf;
59+
my $topmargin = 0;
60+
61+
if ($logo)
62+
{
63+
$pdf = CtrlO::PDF->new(
64+
header => $marking,
65+
footer => $marking,
66+
logo => $logo,
67+
);
68+
69+
# Adjust the top margin to allow for the logo - 30px allows the table (below the logo) to not encroach on the logo when rendered
70+
# This is used rather than overcomplicating and using image size to centre the header, and then having to "drop" the table down to avoid the logo
71+
$topmargin = -30;
72+
}
73+
else
74+
{
75+
$pdf = CtrlO::PDF->new(
76+
header => $marking,
77+
footer => $marking,
78+
);
79+
}
80+
81+
$pdf->add_page;
82+
83+
$pdf->heading($options{title}, topmargin => $topmargin);
84+
$pdf->text($options{subtitle}, size => 14) if $options{subtitle};
85+
86+
my $hdr_props = {
87+
repeat => 0,
88+
justify => 'center',
89+
font_size => 12,
90+
bg_color => '#007c88',
91+
fg_color => '#ffffff',
92+
};
93+
94+
my $result = $self->layouts;
95+
96+
my @cols = $self->record->presentation_map_columns(columns => $result);
97+
my @topics = $self->record->get_topics(\@cols);
98+
99+
my $i = 0;
100+
foreach my $topic (@topics)
101+
{
102+
my $topic_name = $topic->{topic} ? $topic->{topic}->name : 'Other';
103+
my $fields = [ [$topic_name] ];
104+
105+
my $width = 0;
106+
foreach my $col (@{ $topic->{columns} })
107+
{
108+
if ($col->{data}->{selected_values})
109+
{
110+
my $first = 1;
111+
foreach my $c (@{ $col->{data}->{selected_values} })
112+
{
113+
my $values = $c->{values};
114+
$width =
115+
$width < (scalar(@$values) + 1)
116+
? scalar(@$values) + 1
117+
: $width;
118+
push @$fields, [ $first ? $col->{name} : '', @$values ];
119+
$first = 0;
120+
}
121+
}
122+
else
123+
{
124+
if ($col->{data}->{value})
125+
{
126+
push @$fields,
127+
[ $col->{name}, $col->{data}->{value} || "" ];
128+
}
129+
else
130+
{
131+
push @$fields, [ $col->{name}, $col->{data}->{grade} ];
132+
}
133+
$width = 2 if $width < 2;
134+
}
135+
}
136+
137+
my $cell_props = [];
138+
foreach my $d (@$fields)
139+
{
140+
my $has = @$d;
141+
142+
# $max_fields does not include field name
143+
my $gap = $width - $has + 1;
144+
push @$d, undef for (1 .. $gap);
145+
push @$cell_props,
146+
[ (undef) x ($has - 1), { colspan => $gap + 1 }, ];
147+
}
148+
149+
$pdf->table(
150+
data => $fields,
151+
header_props => $hdr_props,
152+
border_c => '#007C88',
153+
h_border_w => 1,
154+
cell_props => $cell_props,
155+
size => '4cm *',
156+
);
157+
}
158+
159+
my $now = DateTime->now;
160+
my $format = GADS::Config->instance->dateformat;
161+
$pdf->text(
162+
'Last edited by '
163+
. $self->record->edited_user->as_string . ' on '
164+
. $self->record->edited_time->as_string,
165+
size => 10
166+
);
167+
$pdf->text(
168+
'Report generated by '
169+
. $self->user->value . ' on '
170+
. $now->format_cldr($format) . ' at '
171+
. $now->hms,
172+
size => 10
173+
);
174+
175+
return $pdf;
176+
}
177+
178+
1;

lib/GADS/Record.pm

Lines changed: 9 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,127 +2723,19 @@ sub for_code
27232723
sub pdf
27242724
{ my ($self, $site) = @_;
27252725

2726-
my $config = GADS::Config->instance;
2727-
my $header = $config && $config->gads && $config->gads->{header};
2728-
my $marking = $site->read_security_marking;
2729-
my $logo = $site->create_temp_logo;
2730-
2731-
my $pdf;
2732-
my $topmargin = 0;
2733-
2734-
if ($logo)
2735-
{
2736-
$pdf = CtrlO::PDF->new(
2737-
header => $marking,
2738-
footer => $marking,
2739-
logo => $logo,
2740-
);
2741-
2742-
# Adjust the top margin to allow for the logo - 30px allows the table (below the logo) to not encroach on the logo when rendered
2743-
# This is used rather than overcomplicating and using image size to centre the header, and then having to "drop" the table down to avoid the logo
2744-
$topmargin = -30;
2745-
}
2746-
else
2747-
{
2748-
$pdf = CtrlO::PDF->new(
2749-
header => $marking,
2750-
footer => $marking,
2751-
);
2752-
}
2753-
2754-
$pdf->add_page;
2755-
$pdf->heading("Default Report", topmargin => $topmargin);
2756-
2757-
my $hdr_props = {
2758-
repeat => 0,
2759-
justify => 'center',
2760-
font_size => 12,
2761-
bg_color => '#007c88',
2762-
fg_color => '#ffffff',
2763-
};
2764-
27652726
my $result = [$self->layout->all_user_read];
27662727

2767-
my @cols = $self->presentation_map_columns(columns => $result);
2768-
my @topics = $self->get_topics(\@cols);
2769-
2770-
my $i = 0;
2771-
foreach my $topic (@topics)
2772-
{
2773-
my $topic_name = $topic->{topic} ? $topic->{topic}->name : 'Other';
2774-
my $fields = [ [$topic_name] ];
2775-
2776-
my $width = 0;
2777-
foreach my $col (@{ $topic->{columns} })
2778-
{
2779-
if ($col->{data}->{selected_values})
2780-
{
2781-
my $first = 1;
2782-
foreach my $c (@{ $col->{data}->{selected_values} })
2783-
{
2784-
my $values = $c->{values};
2785-
$width =
2786-
$width < (scalar(@$values) + 1)
2787-
? scalar(@$values) + 1
2788-
: $width;
2789-
push @$fields, [ $first ? $col->{name} : '', @$values ];
2790-
$first = 0;
2791-
}
2792-
}
2793-
else
2794-
{
2795-
if ($col->{data}->{value})
2796-
{
2797-
push @$fields,
2798-
[ $col->{name}, $col->{data}->{value} || "" ];
2799-
}
2800-
else
2801-
{
2802-
push @$fields, [ $col->{name}, $col->{data}->{grade} ];
2803-
}
2804-
$width = 2 if $width < 2;
2805-
}
2806-
}
2807-
2808-
my $cell_props = [];
2809-
foreach my $d (@$fields)
2810-
{
2811-
my $has = @$d;
2812-
2813-
# $max_fields does not include field name
2814-
my $gap = $width - $has + 1;
2815-
push @$d, undef for (1 .. $gap);
2816-
push @$cell_props,
2817-
[ (undef) x ($has - 1), { colspan => $gap + 1 }, ];
2818-
}
2819-
2820-
$pdf->table(
2821-
data => $fields,
2822-
header_props => $hdr_props,
2823-
border_c => '#007C88',
2824-
h_border_w => 1,
2825-
cell_props => $cell_props,
2826-
size => '4cm *',
2827-
);
2828-
}
2829-
2830-
my $now = DateTime->now;
2831-
my $format = GADS::Config->instance->dateformat;
2832-
$pdf->text(
2833-
'Last edited by '
2834-
. $self->edited_user->as_string . ' on '
2835-
. $self->edited_time->as_string,
2836-
size => 10
2837-
);
2838-
$pdf->text(
2839-
'Report generated by '
2840-
. $self->user->value . ' on '
2841-
. $now->format_cldr($format) . ' at '
2842-
. $now->hms,
2843-
size => 10
2728+
my $generator = GADS::PDFGenerator->new(
2729+
site => $site,
2730+
layouts => $result,
2731+
record => $self,
2732+
user => $self->user,
2733+
security_marking => $self->layout->security_marking
28442734
);
28452735

2846-
$pdf;
2736+
my $pdf = $generator->build(title => "Record-" . $self->current_id . ".pdf");
2737+
2738+
return $pdf;
28472739
}
28482740

28492741
sub get_report

0 commit comments

Comments
 (0)