]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/IO/PdfMembersCards.php
Add member number; closes #190
[galette.git] / galette / lib / Galette / IO / PdfMembersCards.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Member card PDF
7 *
8 * PHP version 5
9 *
10 * Copyright © 2021 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 2021 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.8.2dev - 2014-11-30
35 */
36
37 namespace Galette\IO;
38
39 use Galette\Core\Preferences;
40 use Galette\Core\PrintLogo;
41 use Analog\Analog;
42
43 /**
44 * Member card PDF
45 *
46 * @category IO
47 * @name PDF
48 * @package Galette
49 * @abstract Class for expanding TCPDF.
50 * @author Johan Cwiklinski <johan@x-tnd.be>
51 * @copyright 2021 The Galette Team
52 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
53 * @link http://galette.tuxfamily.org
54 * @since Available since 0.8.2dev - 2014-11-30
55 */
56
57 class PdfMembersCards extends Pdf
58 {
59 public const WIDTH = 75;
60 public const HEIGHT = 40;
61 public const COLS = 2;
62 public const ROWS = 6;
63
64 private $tcol;
65 private $scol;
66 private $bcol;
67 private $hcol;
68 private $xorigin;
69 private $yorigin;
70 private $wi;
71 private $he;
72 private $nbcol;
73 private $nbrow;
74 private $hspacing;
75 private $vspacing;
76 private $max_text_size;
77 private $year_font_size;
78 private $an_cot;
79 private $abrev;
80 private $wlogo;
81 private $hlogo;
82 private $logofile;
83
84 /**
85 * Main constructor, set creator and author
86 *
87 * @param Preferences $prefs Preferences
88 */
89 public function __construct(Preferences $prefs)
90 {
91 parent::__construct($prefs);
92 $this->setRTL(false);
93 $this->filename = __('cards') . '.pdf';
94 $this->init();
95 }
96 /**
97 * Initialize PDF
98 *
99 * @return void
100 */
101 private function init()
102 {
103 // Set document information
104 $this->SetTitle(_T("Member's Cards"));
105 $this->SetSubject(_T("Generated by Galette"));
106 $this->SetKeywords(_T("Cards"));
107
108 // No hearders and footers
109 $this->SetPrintHeader(false);
110 $this->SetPrintFooter(false);
111 $this->setFooterMargin(0);
112 $this->setHeaderMargin(0);
113
114 // Show full page
115 $this->SetDisplayMode('fullpage');
116
117 // Disable Auto Page breaks
118 $this->SetAutoPageBreak(false, 0);
119
120 // Set colors
121 $this->SetDrawColor(160, 160, 160);
122 $this->SetTextColor(0);
123 $this->tcol = $this->colorHex2Dec($this->preferences->pref_card_tcol);
124 $this->scol = $this->colorHex2Dec($this->preferences->pref_card_scol);
125 $this->bcol = $this->colorHex2Dec($this->preferences->pref_card_bcol);
126 $this->hcol = $this->colorHex2Dec($this->preferences->pref_card_hcol);
127
128 // Set margins
129 $this->SetMargins(
130 $this->preferences->pref_card_marges_h,
131 $this->preferences->pref_card_marges_v
132 );
133
134 // Set font
135 $this->SetFont(self::FONT);
136
137 // Set origin
138 // Top left corner
139 $this->xorigin = $this->preferences->pref_card_marges_h;
140 $this->yorigin = $this->preferences->pref_card_marges_v;
141
142 // Card width
143 $this->wi = self::getWidth();
144 // Card heigth
145 $this->he = self::getHeight();
146 // Number of colons
147 $this->nbcol = self::getCols();
148 // Number of rows
149 $this->nbrow = self::getRows();
150 // Spacing between cards
151 $this->hspacing = $this->preferences->pref_card_hspace;
152 $this->vspacing = $this->preferences->pref_card_vspace;
153
154 //maximum size for visible text. May vary with fonts.
155 $this->max_text_size = 80;
156 $this->year_font_size = 8;
157
158 // Get fixed data from preferences
159 $this->an_cot = $this->preferences->pref_card_year;
160 $this->abrev = $this->preferences->pref_card_abrev;
161
162 $print_logo = new PrintLogo();
163 $this->logofile = $print_logo->getPath();
164
165 // Set logo size to max width 30 mm or max height 25 mm
166 $ratio = $print_logo->getWidth() / $print_logo->getHeight();
167 if ($ratio < 1) {
168 if ($print_logo->getHeight() > 16) {
169 $this->hlogo = 20;
170 } else {
171 $this->hlogo = $print_logo->getHeight();
172 }
173 $this->wlogo = round($this->hlogo * $ratio);
174 } else {
175 if ($print_logo->getWidth() > 16) {
176 $this->wlogo = 30;
177 } else {
178 $this->wlogo = $print_logo->getWidth();
179 }
180 $this->hlogo = round($this->wlogo / $ratio);
181 }
182 }
183
184 /**
185 * Draw members cards
186 *
187 * @param array $members Members
188 *
189 * @return void
190 */
191 public function drawCards($members)
192 {
193 $nb_card = 0;
194 foreach ($members as $member) {
195 // Detect page breaks
196 if ($nb_card % ($this->nbcol * $this->nbrow) == 0) {
197 $this->AddPage();
198 }
199
200 // Compute card position on page
201 $col = $nb_card % $this->nbcol;
202 $row = ($nb_card / $this->nbcol) % $this->nbrow;
203 // Set origin
204 $x0 = $this->xorigin + $col * (round($this->wi) + round($this->hspacing));
205 $y0 = $this->yorigin + $row * (round($this->he) + round($this->vspacing));
206 // Logo X position
207 $xl = round($x0 + $this->wi - $this->wlogo);
208 // Get data
209 $email = '';
210 switch ($this->preferences->pref_card_address) {
211 case 0:
212 $email .= $member->email;
213 break;
214 case 5:
215 $email .= $member->zipcode . ' - ' . $member->town;
216 break;
217 case 6:
218 $email .= $member->nickname;
219 break;
220 case 7:
221 $email .= $member->job;
222 break;
223 case 8:
224 $email .= $member->number;
225 break;
226 }
227
228 // Select strip color according to status
229 switch ($member->status) {
230 case 1:
231 case 2:
232 case 3:
233 case 10:
234 $fcol = $this->bcol;
235 break;
236 case 5:
237 case 6:
238 $fcol = $this->hcol;
239 break;
240 default:
241 $fcol = $this->scol;
242 }
243
244 $nom_adh_ext = '';
245 if ($this->preferences->pref_bool_display_title) {
246 $nom_adh_ext .= $member->stitle;
247 }
248 $nom_adh_ext .= $member->sname;
249 $photo = $member->picture;
250 $photofile = $photo->getPath();
251
252 // Photo 100x130 and logo
253 $this->Image($photofile, $x0, $y0, 25);
254 $this->Image($this->logofile, $xl, $y0, round($this->wlogo));
255
256 // Color=#8C8C8C: Shadow of the year
257 $this->SetTextColor(140);
258 $this->SetFontSize($this->year_font_size);
259
260 $an_cot = $this->an_cot;
261 if ($an_cot === 'DEADLINE') {
262 //get current member deadline
263 $an_cot = $member->due_date;
264 }
265
266 $xan_cot = $x0 + $this->wi - $this->GetStringWidth(
267 $an_cot,
268 self::FONT,
269 'B',
270 $this->year_font_size
271 ) - 0.2;
272 $this->SetXY($xan_cot, $y0 + $this->hlogo - 0.3);
273 $this->writeHTML('<strong>' . $an_cot . '</strong>', false, 0);
274
275 // Colored Text (Big label, id, year)
276 $this->SetTextColor($fcol['R'], $fcol['G'], $fcol['B']);
277
278 $this->SetFontSize(8);
279
280 $xid = $x0 + $this->wi - $this->GetStringWidth($member->id, self::FONT, 'B', 8) - 0.2;
281 $this->SetXY($xid, $y0 + 28);
282 $this->writeHTML('<strong>' . $member->id . '</strong>', false, 0);
283 $this->SetFontSize($this->year_font_size);
284 $xan_cot = $xan_cot - 0.3;
285 $this->SetXY($xan_cot, $y0 + $this->hlogo - 0.3);
286 $this->writeHTML('<strong>' . $an_cot . '</strong>', false, 0);
287
288 // Abbrev: Adapt font size to text length
289 $this->fixSize(
290 $this->abrev,
291 $this->max_text_size,
292 12,
293 'B'
294 );
295 $this->SetXY($x0 + 27, $y0 + 12);
296 $this->writeHTML('<strong>' . $this->abrev . '</strong>', true, 0);
297
298 // Name: Adapt font size to text length
299 $this->SetTextColor(0);
300 $this->fixSize(
301 $nom_adh_ext,
302 $this->max_text_size,
303 8,
304 'B'
305 );
306 $this->SetXY($x0 + 27, $this->getY() + 4);
307 //$this->setX($x0 + 27);
308 $this->writeHTML('<strong>' . $nom_adh_ext . '</strong>', true, 0);
309
310 // Email (adapt too)
311 $this->fixSize(
312 $email,
313 $this->max_text_size,
314 6,
315 'B'
316 );
317 $this->setX($x0 + 27);
318 $this->writeHTML('<strong>' . $email . '</strong>', false, 0);
319
320 // Lower colored strip with long text
321 $this->SetFillColor($fcol['R'], $fcol['G'], $fcol['B']);
322 $this->SetTextColor(
323 $this->tcol['R'],
324 $this->tcol['G'],
325 $this->tcol['B']
326 );
327 $this->SetFont(self::FONT, 'B', 6);
328 $this->SetXY($x0, $y0 + 33);
329 $this->Cell(
330 $this->wi,
331 7,
332 $this->preferences->pref_card_strip,
333 0,
334 0,
335 'C',
336 1
337 );
338
339 // Draw a gray frame around the card
340 $this->Rect($x0, $y0, $this->wi, $this->he);
341 $nb_card++;
342 }
343 }
344
345 /**
346 * Get card width
347 *
348 * @return integer
349 */
350 public static function getWidth()
351 {
352 return defined('GALETTE_CARD_WIDTH') ? GALETTE_CARD_WIDTH : self::WIDTH;
353 }
354
355 /**
356 * Get card height
357 *
358 * @return integer
359 */
360 public static function getHeight()
361 {
362 return defined('GALETTE_CARD_HEIGHT') ? GALETTE_CARD_HEIGHT : self::HEIGHT;
363 }
364
365 /**
366 * Get number of columns
367 *
368 * @return integer
369 */
370 public static function getCols()
371 {
372 return defined('GALETTE_CARD_COLS') ? GALETTE_CARD_COLS : self::COLS;
373 }
374
375 /**
376 * Get number of rows
377 *
378 * @return integer
379 */
380 public static function getRows()
381 {
382 return defined('GALETTE_CARD_ROWS') ? GALETTE_CARD_ROWS : self::ROWS;
383 }
384 }