]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/IO/PdfAttendanceSheet.php
67605b2b1aaf2e13815a4903277f6c25b0aeb947
[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 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.9.0dev - 2016-02-21
36 */
37
38 namespace Galette\IO;
39
40 use Galette\Core\Db;
41 use Galette\Core\Preferences;
42 use Galette\Core\PrintLogo;
43 use Galette\Entity\PdfModel;
44 use Analog\Analog;
45
46 /**
47 * Attendance sheet
48 *
49 * @category IO
50 * @name PDF
51 * @package Galette
52 * @abstract Class for expanding TCPDF.
53 * @author Johan Cwiklinski <johan@x-tnd.be>
54 * @copyright 2016 The Galette Team
55 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
56 * @link http://galette.tuxfamily.org
57 * @since Available since 0.9.0dev - 2016-02-21
58 */
59
60 class PdfAttendanceSheet extends Pdf
61 {
62 const SHEET_FONT = self::FONT_SIZE-2;
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->SetFont(Pdf::FONT, '', self::SHEET_FONT);
79 $head_title = $this->doc_title;
80 if ($this->sheet_title !== null) {
81 $head_title .= ' - ' . $this->sheet_title;
82 }
83 if ($this->sheet_sub_title !== null) {
84 $head_title .= ' - ' . $this->sheet_sub_title;
85 }
86 if ($this->sheet_date !== null) {
87 $head_title .= ' - ' . $this->sheet_date->format(__("Y-m-d"));
88 }
89 $this->Cell(0, 10, $head_title, 1, false, 'C', 0, '', 0, false, 'M', 'M');
90 }
91 }
92
93 /**
94 * Main constructor, set creator and author
95 *
96 * @param Db $zdb Database instance
97 * @param Preferences $prefs Preferences
98 * @param array $data Data to set
99 */
100 public function __construct(Db $zdb, Preferences $prefs, $data = [])
101 {
102 $class = PdfModel::getTypeClass(__CLASS__);
103 $model = new $class($zdb, $prefs, PdfModel::MAIN_MODEL);
104
105 // Set document and model information
106 $this->doc_title = $data['doc_title'];
107 $this->SetTitle($data['doc_title']);
108
109 if (isset($data['title']) && trim($data['title']) != '') {
110 $this->sheet_title = $data['title'];
111 $model->title = $this->sheet_title;
112 }
113 if (isset($data['subtitle']) && trim($data['subtitle']) != '') {
114 $this->sheet_sub_title = $data['subtitle'];
115 $model->subtitle = $this->sheet_sub_title;
116 }
117 if (isset($data['sheet_date']) && trim($data['sheet_date']) != '') {
118 $dformat = __("Y-m-d");
119 $date = \DateTime::createFromFormat(
120 $dformat,
121 $data['sheet_date']
122 );
123 $this->sheet_date = $date;
124 }
125
126 parent::__construct($prefs, $model);
127 $this->init();
128 }
129 /**
130 * Initialize PDF
131 *
132 * @return void
133 */
134 private function init()
135 {
136 // Set document information
137 $this->SetSubject(_T("Generated by Galette"));
138 $this->SetKeywords(_T("Attendance sheet"));
139
140 // No hearders and footers
141 $this->setHeaderMargin(10);
142
143 // Set colors
144 $this->SetDrawColor(160, 160, 160);
145 $this->SetTextColor(0);
146 $this->SetFont(Pdf::FONT, '', Pdf::FONT_SIZE-2);
147 }
148
149 /**
150 * Draw members cards
151 *
152 * @param array $members Members
153 * @param string $doc_title Document title
154 *
155 * @return void
156 */
157 public function drawSheet($members, $doc_title)
158 {
159 $this->Open();
160 $this->AddPage();
161 $this->PageHeader($doc_title);
162
163 if ($this->sheet_date) {
164 $date_fmt = null;
165 if (PHP_OS === 'Linux') {
166 $format = _T("%A, %B %#d%O %Y");
167 $format = str_replace(
168 '%O',
169 date('S', $this->sheet_date->getTimestamp()),
170 $format
171 );
172 $date_fmt = strftime($format, $this->sheet_date->getTimestamp());
173 } else {
174 $format = __("Y-m-d");
175 $date_fmt = $this->sheet_date->format($format);
176 }
177 $this->Cell(190, 7, $date_fmt, 0, 1, 'C');
178 }
179
180 // Header
181 $this->SetFont('', 'B');
182 $this->SetFillColor(255, 255, 255);
183 $this->Cell(110, 7, _T("Name"), 1, 0, 'C', 1);
184 $this->Cell(80, 7, _T("Signature"), 1, 1, 'C', 1);
185
186 // Data
187 $this->SetFont('');
188 $mcount = 0;
189 foreach ($members as $m) {
190 $mcount++;
191 $this->Cell(10, 16, $mcount, 'LTB', 0, 'R');
192
193 if ($m->hasPicture() && $this->wimages) {
194 $p = $m->picture->getPath();
195
196 // Set logo size to max width 30 mm or max height 25 mm
197 $ratio = $m->picture->getWidth()/$m->picture->getHeight();
198 if ($ratio < 1) {
199 if ($m->picture->getHeight() > 14) {
200 $hlogo = 14;
201 } else {
202 $hlogo = $m->picture->getHeight();
203 }
204 $wlogo = round($hlogo*$ratio);
205 } else {
206 if ($m->picture->getWidth() > 14) {
207 $wlogo = 14;
208 } else {
209 $wlogo = $m->picture->getWidth();
210 }
211 $hlogo = round($wlogo/$ratio);
212 }
213
214 $y = $this->getY() + 1;
215 $x = $this->getX() + 1;
216 $this->Cell($wlogo+2, 16, '', 'LTB', 0);
217 $this->Image($p, $x, $y, $wlogo, $hlogo);
218 } else {
219 $x = $this->getX() + 1;
220 $this->Cell(1, 16, '', 'LTB', 0);
221 }
222
223 $xs = $this->getX() - $x + 1;
224 $this->Cell(100 - $xs, 16, $m->sname, 'RTB', 0, 'L');
225 $this->Cell(80, 16, '', 1, 1, 'L');
226 }
227 $this->Cell(190, 0, '', 'T');
228 }
229
230 /**
231 * Add images to file
232 *
233 * @return PdfAttendanceSheet
234 */
235 public function withImages()
236 {
237 $this->wimages = true;
238 }
239 }