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