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