]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/IO/PdfAttendanceSheet.php
Rework PDF footer so any height will be ok
[galette.git] / galette / lib / Galette / IO / PdfAttendanceSheet.php
1 <?php
2
3 /**
4 * Copyright © 2003-2024 The Galette Team
5 *
6 * This file is part of Galette (https://galette.eu).
7 *
8 * Galette is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * Galette is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 namespace Galette\IO;
23
24 use Galette\Core\Db;
25 use Galette\Core\Preferences;
26 use Galette\Entity\Adherent;
27 use Galette\Entity\PdfModel;
28
29 /**
30 * Attendance sheet
31 *
32 * @author Johan Cwiklinski <johan@x-tnd.be>
33 */
34
35 class PdfAttendanceSheet extends Pdf
36 {
37 public const SHEET_FONT = self::FONT_SIZE - 2;
38 public const ATT_SHEET_MODEL = 100;
39
40 public ?string $doc_title = null;
41 public ?string $sheet_title = null;
42 public ?string $sheet_sub_title = null;
43 public ?\DateTime $sheet_date = null;
44 private bool $wimages = false;
45
46 /**
47 * Page header
48 *
49 * @return void
50 */
51 public function Header(): void // phpcs:ignore PSR1.Methods.CamelCapsMethodName
52 {
53 if ($this->PageNo() > 1) {
54 $this->setTopMargin(15);
55 $this->setY(10);
56 $this->SetFont(Pdf::FONT, '', self::SHEET_FONT);
57 $head_title = $this->doc_title;
58 if ($this->sheet_title !== null) {
59 $head_title .= ' - ' . $this->sheet_title;
60 }
61 /* Removed to prevent long lines */
62 /*if ($this->sheet_sub_title !== null) {
63 $head_title .= ' - ' . $this->sheet_sub_title;
64 }*/
65 if ($this->sheet_date !== null) {
66 $head_title .= ' - ' . $this->sheet_date->format(__("Y-m-d"));
67 }
68 $this->Cell(0, 10, $head_title, 0, 0, 'C', false, '', 0, false, 'M', 'M');
69 }
70 }
71
72 /**
73 * Main constructor, set creator and author
74 *
75 * @param Db $zdb Database instance
76 * @param Preferences $prefs Preferences
77 * @param array<string,mixed> $data Data to set
78 */
79 public function __construct(Db $zdb, Preferences $prefs, array $data = [])
80 {
81 $this->filename = __('attendance_sheet') . '.pdf';
82 $class = PdfModel::getTypeClass(self::ATT_SHEET_MODEL);
83 $model = new $class($zdb, $prefs);
84
85 // Set document and model information
86 $this->doc_title = $data['doc_title'];
87 $this->SetTitle($data['doc_title']);
88
89 if (isset($data['title']) && trim($data['title']) != '') {
90 $this->sheet_title = $data['title'];
91 $model->title = $this->sheet_title;
92 }
93 if (isset($data['subtitle']) && trim($data['subtitle']) != '') {
94 $this->sheet_sub_title = $data['subtitle'];
95 $model->subtitle = $this->sheet_sub_title;
96 }
97 if (isset($data['sheet_date']) && trim($data['sheet_date']) != '') {
98 $dformat = __("Y-m-d");
99 $date = \DateTime::createFromFormat(
100 $dformat,
101 $data['sheet_date']
102 );
103 $this->sheet_date = $date;
104 }
105
106 parent::__construct($prefs, $model);
107 // Enable Auto Page breaks
108 $this->SetAutoPageBreak(true, $this->footer_height + 10);
109 }
110
111 /**
112 * Initialize PDF
113 *
114 * @return void
115 */
116 public function init(): void
117 {
118 // Set document information
119 $this->SetSubject(_T("Generated by Galette"));
120 $this->SetKeywords(_T("Attendance sheet"));
121
122 $this->setHeaderMargin(10);
123
124 // Set colors
125 $this->SetDrawColor(160, 160, 160);
126 $this->SetTextColor(0);
127 $this->SetFont(Pdf::FONT, '', Pdf::FONT_SIZE - 2);
128
129 //enable pagination
130 $this->showPagination();
131
132 parent::init();
133 }
134
135 /**
136 * Draw members cards
137 *
138 * @param array<Adherent> $members Members
139 *
140 * @return void
141 */
142 public function drawSheet(array $members): void
143 {
144 $this->PageHeader($this->doc_title);
145
146 if ($this->sheet_date) {
147 $format = __("MMMM, EEEE d y");
148 $formatter = new \IntlDateFormatter(
149 $this->i18n->getLongID(),
150 \IntlDateFormatter::FULL,
151 \IntlDateFormatter::NONE,
152 \date_default_timezone_get(),
153 \IntlDateFormatter::GREGORIAN,
154 $format
155 );
156 $datetime = new \DateTimeImmutable($this->sheet_date->format('Y-m-d'));
157 $date = \DateTime::createFromImmutable($datetime);
158 $date_fmt = mb_convert_case($formatter->format($date), MB_CASE_TITLE);
159 $this->Cell(190, 7, $date_fmt, 0, 1, 'C');
160 }
161
162 // Header
163 $this->SetFont('', 'B');
164 $this->SetFillColor(255, 255, 255);
165 $this->Cell(110, 7, _T("Name"), 1, 0, 'C', true);
166 $this->Cell(80, 7, _T("Signature"), 1, 1, 'C', true);
167
168 // Data
169 $this->SetFont('');
170 $mcount = 0;
171 foreach ($members as $m) {
172 $mcount++;
173 $this->Cell(10, 16, (string)$mcount, ($this->i18n->isRTL() ? 'R' : 'L') . 'TB', 0, 'R');
174
175 if ($m->hasPicture() && $this->wimages) {
176 $p = $m->picture->getPath();
177
178 // Set logo size to max width 30 mm or max height 25 mm
179 $ratio = $m->picture->getWidth() / $m->picture->getHeight();
180 if ($ratio < 1) {
181 if ($m->picture->getHeight() > 14) {
182 $hlogo = 14;
183 } else {
184 $hlogo = $m->picture->getHeight();
185 }
186 $wlogo = round($hlogo * $ratio);
187 } else {
188 if ($m->picture->getWidth() > 14) {
189 $wlogo = 14;
190 } else {
191 $wlogo = $m->picture->getWidth();
192 }
193 $hlogo = round($wlogo / $ratio);
194 }
195
196 $y = $this->getY() + 1;
197 $x = $this->getX() + 1;
198 $ximg = $x;
199 if ($this->i18n->isRTL()) {
200 $ximg = $this->getPageWidth() - $x - $wlogo;
201 }
202 $this->Cell($wlogo + 2, 16, '', ($this->i18n->isRTL() ? 'R' : 'L') . 'TB', 0);
203 $this->Image($p, $ximg, $y, $wlogo, $hlogo);
204 } else {
205 $x = $this->getX() + 1;
206 $this->Cell(1, 16, '', ($this->i18n->isRTL() ? 'R' : 'L') . 'TB', 0);
207 }
208
209 $xs = $this->getX() - $x + 1;
210 $this->Cell(100 - $xs, 16, $m->sname, ($this->i18n->isRTL() ? 'L' : 'R') . 'TB', 0, ($this->i18n->isRTL() ? 'R' : 'L'));
211 $this->Cell(80, 16, '', 1, 1, ($this->i18n->isRTL() ? 'R' : 'L'));
212 }
213 $this->Cell(190, 0, '', 'T');
214 }
215
216 /**
217 * Add images to file
218 *
219 * @return self
220 */
221 public function withImages(): self
222 {
223 $this->wimages = true;
224 return $this;
225 }
226 }