Merge pull request #4865 from eileenmcnaughton/my-first-factory
[civicrm-core.git] / CRM / Badge / BAO / Badge.php
CommitLineData
42c62ec1
KJ
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
42c62ec1
KJ
5 +--------------------------------------------------------------------+
6 | This file is a part of CiviCRM. |
7 | |
8 | CiviCRM is free software; you can copy, modify, and distribute it |
9 | under the terms of the GNU Affero General Public License |
10 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
11 | |
12 | CiviCRM is distributed in the hope that it will be useful, but |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
15 | See the GNU Affero General Public License for more details. |
16 | |
17 | You should have received a copy of the GNU Affero General Public |
18 | License and the CiviCRM Licensing Exception along |
19 | with this program; if not, contact CiviCRM LLC |
20 | at info[AT]civicrm[DOT]org. If you have questions about the |
21 | GNU Affero General Public License or the licensing of CiviCRM, |
22 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
23 +--------------------------------------------------------------------+
24*/
25
26/**
27 *
28 * @package CRM
06b69b18 29 * @copyright CiviCRM LLC (c) 2004-2014
42c62ec1
KJ
30 * $Id$
31 *
32 */
33
34/**
35 * Class CRM_Badge_Format_Badge
36 *
37 * parent class for building name badges
38 */
f4e94ab8 39class CRM_Badge_BAO_Badge {
382139c4
KJ
40
41 public $debug = FALSE;
42
43 public $border = 0;
44
42c62ec1
KJ
45 /**
46 * This function is called to create name label pdf
47 *
691efc59
TO
48 * @param array $participants
49 * Associated array with participant info.
50 * @param array $layoutInfo
51 * Associated array which contains meta data about format/layout.
42c62ec1
KJ
52 *
53 * @return void
42c62ec1 54 */
1b1d29d8 55 public function createLabels(&$participants, &$layoutInfo) {
42c62ec1
KJ
56 $this->pdf = new CRM_Utils_PDF_Label($layoutInfo['format'], 'mm');
57 $this->pdf->Open();
58 $this->pdf->setPrintHeader(FALSE);
59 $this->pdf->setPrintFooter(FALSE);
60 $this->pdf->AddPage();
42c62ec1
KJ
61 $this->pdf->SetGenerator($this, "generateLabel");
62
1b1d29d8 63 // this is very useful for debugging, by default set to FALSE
1b1d29d8
KJ
64 if ($this->debug) {
65 $this->border = "LTRB";
66 }
67
42c62ec1
KJ
68 foreach ($participants as $participant) {
69 $formattedRow = self::formatLabel($participant, $layoutInfo);
70 $this->pdf->AddPdfLabel($formattedRow);
71 }
72
73 $this->pdf->Output(CRM_Utils_String::munge($layoutInfo['title'], '_', 64) . '.pdf', 'D');
74 CRM_Utils_System::civiExit(1);
75 }
76
77 /**
78 * Funtion to create structure and add meta data according to layout
79 *
691efc59
TO
80 * @param array $row
81 * Row element that needs to be formatted.
82 * @param array $layout
83 * Layout meta data.
42c62ec1
KJ
84 *
85 * @return array $formattedRow row with meta data
86 */
00be9182 87 public static function formatLabel(&$row, &$layout) {
f4e94ab8 88 $formattedRow = array('labelFormat' => $layout['label_format_name']);
b62376e2
M
89 $formattedRow['labelTitle'] = $layout['title'];
90 $formattedRow['labelId'] = $layout['id'];
91
a7488080 92 if (!empty($layout['data']['rowElements'])) {
e135f2b5 93 foreach ($layout['data']['rowElements'] as $key => $element) {
8d5579e2
KJ
94 $value = '';
95 if ($element) {
96 $value = $row[$element];
97 // hack to fix date field display format
98 if (strpos($element, '_date')) {
1a502055 99 $value = CRM_Utils_Date::customFormat($value, "%B %E%f");
8d5579e2 100 }
be09a99d
KJ
101 }
102
42c62ec1 103 $formattedRow['token'][$key] = array(
be09a99d 104 'value' => $value,
42c62ec1
KJ
105 'font_name' => $layout['data']['font_name'][$key],
106 'font_size' => $layout['data']['font_size'][$key],
1c07f935 107 'font_style' => $layout['data']['font_style'][$key],
42c62ec1 108 'text_alignment' => $layout['data']['text_alignment'][$key],
50f52554 109 'token' => $layout['data']['token'][$key],
42c62ec1
KJ
110 );
111 }
112 }
113
a7488080 114 if (!empty($layout['data']['image_1'])) {
42c62ec1
KJ
115 $formattedRow['image_1'] = $layout['data']['image_1'];
116 }
a7488080 117 if (!empty($layout['data']['width_image_1'])) {
07d113b7
PJ
118 $formattedRow['width_image_1'] = $layout['data']['width_image_1'];
119 }
a7488080 120 if (!empty($layout['data']['height_image_1'])) {
07d113b7
PJ
121 $formattedRow['height_image_1'] = $layout['data']['height_image_1'];
122 }
42c62ec1 123
a7488080 124 if (!empty($layout['data']['image_2'])) {
42c62ec1
KJ
125 $formattedRow['image_2'] = $layout['data']['image_2'];
126 }
a7488080 127 if (!empty($layout['data']['width_image_2'])) {
07d113b7
PJ
128 $formattedRow['width_image_2'] = $layout['data']['width_image_2'];
129 }
a7488080 130 if (!empty($layout['data']['height_image_2'])) {
07d113b7
PJ
131 $formattedRow['height_image_2'] = $layout['data']['height_image_2'];
132 }
32d586ea 133 if (!empty($row['image_URL']) && !empty($layout['data']['show_participant_image'])) {
609df1ed 134 $formattedRow['participant_image'] = $row['image_URL'];
cf723d2e
M
135 }
136 if (!empty($layout['data']['width_participant_image'])) {
137 $formattedRow['width_participant_image'] = $layout['data']['width_participant_image'];
138 }
139 if (!empty($layout['data']['height_participant_image'])) {
140 $formattedRow['height_participant_image'] = $layout['data']['height_participant_image'];
141 }
142 if (!empty($layout['data']['alignment_participant_image'])) {
143 $formattedRow['alignment_participant_image'] = $layout['data']['alignment_participant_image'];
e0ef6999 144 }
42c62ec1 145
a7488080 146 if (!empty($layout['data']['add_barcode'])) {
f38395f7
KJ
147 $formattedRow['barcode'] = array(
148 'alignment' => $layout['data']['barcode_alignment'],
149 'type' => $layout['data']['barcode_type'],
150 );
42c62ec1
KJ
151 }
152
7fd8017a
KJ
153 // finally assign all the row values, so that we can use it for barcode etc
154 $formattedRow['values'] = $row;
155
42c62ec1
KJ
156 return $formattedRow;
157 }
f4e94ab8 158
e0ef6999
EM
159 /**
160 * @param $formattedRow
161 */
f4e94ab8
KJ
162 public function generateLabel($formattedRow) {
163 switch ($formattedRow['labelFormat']) {
8b5dac1e 164 case 'A6 Badge Portrait 150x106':
165 case 'Hanging Badge 3-3/4" x 4-3"/4':
166 self::labelCreator($formattedRow, 5);
167 break;
3a872e59 168
1b1d29d8 169 case 'Avery 5395':
8e9fd9e4 170 default:
8b5dac1e 171 self::labelCreator($formattedRow);
f4e94ab8
KJ
172 break;
173 }
174 }
175
e0ef6999
EM
176 /**
177 * @param $formattedRow
178 * @param int $cellspacing
179 */
8b5dac1e 180 public function labelCreator(&$formattedRow, $cellspacing = 0) {
09651125 181 $this->lMarginLogo = 18;
f4e94ab8
KJ
182 $this->tMarginName = 20;
183
184 $x = $this->pdf->GetAbsX();
b246735d 185 $y = $this->pdf->getY();
f4e94ab8 186
b09332e9 187 //call hook alterBadge
39798542 188 CRM_Utils_Hook::alterBadge($formattedRow['labelTitle'], $this, $formattedRow, $formattedRow['values']);
b09332e9 189
d67e7cf5 190 $startOffset = 0;
a7488080 191 if (!empty($formattedRow['image_1'])) {
07d113b7
PJ
192 $this->printImage($formattedRow['image_1'], NULL, NULL, CRM_Utils_Array::value('width_image_1', $formattedRow),
193 CRM_Utils_Array::value('height_image_1', $formattedRow));
8d5579e2
KJ
194 }
195
a7488080 196 if (!empty($formattedRow['image_2'])) {
07d113b7
PJ
197 $this->printImage($formattedRow['image_2'], $x + 68, NULL, CRM_Utils_Array::value('width_image_2', $formattedRow),
198 CRM_Utils_Array::value('height_image_2', $formattedRow));
d67e7cf5 199 }
200
201 if ((CRM_Utils_Array::value('height_image_1', $formattedRow) >
8cc574cf 202 CRM_Utils_Array::value('height_image_2', $formattedRow)) && !empty($formattedRow['height_image_1'])) {
d67e7cf5 203 $startOffset = CRM_Utils_Array::value('height_image_1', $formattedRow);
204 }
a7488080 205 elseif (!empty($formattedRow['height_image_2'])) {
d67e7cf5 206 $startOffset = CRM_Utils_Array::value('height_image_2', $formattedRow);
8d5579e2 207 }
f4e94ab8 208
39798542 209 if (CRM_Utils_Array::value('participant_image', $formattedRow)) {
5faddd8c 210 $imageAlign = 0;
39798542 211 switch (CRM_Utils_Array::value('alignment_participant_image', $formattedRow)) {
cf723d2e 212 case 'R':
5faddd8c 213 $imageAlign = 68;
cf723d2e 214 break;
3a872e59 215
cf723d2e 216 case 'L':
5faddd8c 217 $imageAlign = 0;
cf723d2e 218 break;
3a872e59 219
cf723d2e
M
220 default:
221 break;
222 }
39798542 223 $this->pdf->Image($formattedRow['participant_image'], $x + $imageAlign, $y + $startOffset, CRM_Utils_Array::value('width_participant_image', $formattedRow), CRM_Utils_Array::value('height_participant_image', $formattedRow));
224 if ($startOffset == NULL && CRM_Utils_Array::value('height_participant_image', $formattedRow)) {
225 $startOffset = CRM_Utils_Array::value('height_participant_image', $formattedRow);
cf723d2e
M
226 }
227 }
228
e135f2b5
KJ
229 $this->pdf->SetLineStyle(array(
230 'width' => 0.1,
231 'cap' => 'round',
232 'join' => 'round',
233 'dash' => '2,2',
21dfd5f5 234 'color' => array(0, 0, 200),
e135f2b5 235 ));
f4e94ab8 236
50f52554 237 $rowCount = CRM_Badge_Form_Layout::FIELD_ROWCOUNT;
d67e7cf5 238 for ($i = 1; $i <= $rowCount; $i++) {
50f52554 239 if (!empty($formattedRow['token'][$i]['token'])) {
240 $value = '';
241 if ($formattedRow['token'][$i]['token'] != 'spacer') {
242 $value = $formattedRow['token'][$i]['value'];
e0ef6999
EM
243 }
244
5faddd8c 245 $xAlign = $x;
609df1ed 246 $rowWidth = $this->pdf->width;
39798542 247 if (!empty($formattedRow['participant_image']) && !empty($formattedRow['width_participant_image'])) {
5faddd8c 248 $rowWidth = $this->pdf->width - $formattedRow['width_participant_image'];
39798542 249 if ($formattedRow['alignment_participant_image'] == 'L') {
5faddd8c 250 $xAlign = $x + $formattedRow['width_participant_image'] + $imageAlign;
39798542 251 }
50f52554 252 }
8b5dac1e 253 $offset = $this->pdf->getY() + $startOffset + $cellspacing;
d67e7cf5 254
50f52554 255 $this->pdf->SetFont($formattedRow['token'][$i]['font_name'], $formattedRow['token'][$i]['font_style'],
256 $formattedRow['token'][$i]['font_size']);
609df1ed 257 $this->pdf->MultiCell($rowWidth, 0, $value,
5faddd8c 258 $this->border, $formattedRow['token'][$i]['text_alignment'], 0, 1, $xAlign, $offset);
d67e7cf5 259
260 // set this to zero so that it is added only for first element
261 $startOffset = 0;
50f52554 262 }
263 }
7fd8017a 264
a7488080 265 if (!empty($formattedRow['barcode'])) {
f38395f7 266 $data = $formattedRow['values'];
7fd8017a 267
f38395f7
KJ
268 if ($formattedRow['barcode']['type'] == 'barcode') {
269 $data['current_value'] =
270 $formattedRow['values']['contact_id'] . '-' . $formattedRow['values']['participant_id'];
8afbd311 271 }
f38395f7
KJ
272 else {
273 // view participant url
274 $data['current_value'] = CRM_Utils_System::url('civicrm/contact/view/participant',
275 'action=view&reset=1&cid=' . $formattedRow['values']['contact_id'] . '&id='
276 . $formattedRow['values']['participant_id'],
277 TRUE,
278 NULL,
279 FALSE
280 );
8afbd311
KJ
281 }
282
9399901d 283 // call hook alterBarcode
f38395f7
KJ
284 CRM_Utils_Hook::alterBarcode($data, $formattedRow['barcode']['type']);
285
286 if ($formattedRow['barcode']['type'] == 'barcode') {
287 // barcode position
288 $xAlign = $x;
289
290 switch ($formattedRow['barcode']['alignment']) {
291 case 'L':
d3b0bbe1 292 $xAlign += -14;
f38395f7 293 break;
3a872e59 294
f38395f7 295 case 'R':
93c52de0 296 $xAlign += 27;
f38395f7 297 break;
3a872e59 298
f38395f7 299 case 'C':
d3b0bbe1 300 $xAlign += 9;
f38395f7
KJ
301 break;
302 }
9399901d 303
f38395f7
KJ
304 $style = array(
305 'position' => '',
306 'align' => '',
307 'stretch' => FALSE,
308 'fitwidth' => TRUE,
309 'cellfitalign' => '',
310 'border' => FALSE,
311 'hpadding' => 13.5,
312 'vpadding' => 'auto',
313 'fgcolor' => array(0, 0, 0),
314 'bgcolor' => FALSE,
315 'text' => FALSE,
316 'font' => 'helvetica',
317 'fontsize' => 8,
318 'stretchtext' => 0,
319 );
320
d3b0bbe1 321 $this->pdf->write1DBarcode($data['current_value'], 'C128', $xAlign, $y + $this->pdf->height - 10, '70',
f38395f7
KJ
322 12, 0.4, $style, 'B');
323 }
324 else {
325 // qr code position
326 $xAlign = $x;
327
328 switch ($formattedRow['barcode']['alignment']) {
329 case 'L':
d3b0bbe1 330 $xAlign += -5;
f38395f7 331 break;
3a872e59 332
f38395f7 333 case 'R':
0ae95ef2 334 $xAlign += 56;
f38395f7 335 break;
3a872e59 336
f38395f7
KJ
337 case 'C':
338 $xAlign += 29;
339 break;
340 }
341
342 $style = array(
3a872e59 343 'border' => FALSE,
f38395f7
KJ
344 'hpadding' => 13.5,
345 'vpadding' => 'auto',
3a872e59
TO
346 'fgcolor' => array(0, 0, 0),
347 'bgcolor' => FALSE,
f38395f7
KJ
348 'position' => '',
349 );
350
3a872e59 351 $this->pdf->write2DBarcode($data['current_value'], 'QRCODE,H', $xAlign, $y + $this->pdf->height - 26, 30,
d3b0bbe1 352 30, $style, 'B');
f38395f7 353 }
7fd8017a 354 }
f4e94ab8
KJ
355 }
356
357 /**
358 * Helper function to print images
fd31fa4c 359 *
691efc59
TO
360 * @param string $img
361 * Image url.
f4e94ab8 362 *
fd31fa4c
EM
363 * @param string $x
364 * @param string $y
365 * @param null $w
366 * @param null $h
367 *
f4e94ab8 368 * @return void
f4e94ab8 369 */
00be9182 370 public function printImage($img, $x = '', $y = '', $w = NULL, $h = NULL) {
1b1d29d8
KJ
371 if (!$x) {
372 $x = $this->pdf->GetAbsX();
373 }
374
375 if (!$y) {
376 $y = $this->pdf->GetY();
377 }
f4e94ab8
KJ
378
379 $this->imgRes = 300;
380
381 if ($img) {
07d113b7 382 list($w, $h) = self::getImageProperties($img, $this->imgRes, $w, $h);
1b1d29d8
KJ
383 $this->pdf->Image($img, $x, $y, $w, $h, '', '', '', FALSE, 72, '', FALSE,
384 FALSE, $this->debug, FALSE, FALSE, FALSE);
f4e94ab8
KJ
385 }
386 $this->pdf->SetXY($x, $y);
387 }
6d4a64c8 388
e0ef6999
EM
389 /**
390 * @param $img
391 * @param int $imgRes
392 * @param null $w
393 * @param null $h
394 *
395 * @return array
396 */
00be9182 397 public static function getImageProperties($img, $imgRes = 300, $w = NULL, $h = NULL) {
07d113b7
PJ
398 $imgsize = getimagesize($img);
399 $f = $imgRes / 25.4;
400 $w = !empty($w) ? $w : $imgsize[0] / $f;
401 $h = !empty($h) ? $h : $imgsize[1] / $f;
402 return array($w, $h);
403 }
404
6d4a64c8 405 /**
100fef9d 406 * Build badges parameters before actually creating badges.
6d4a64c8 407 *
691efc59
TO
408 * @param array $params
409 * Associated array of submitted values.
c490a46a 410 * @param CRM_Core_Form $form
6d4a64c8
KJ
411 *
412 * @return void
6d4a64c8
KJ
413 * @static
414 */
415 public static function buildBadges(&$params, &$form) {
416 // get name badge layout info
417 $layoutInfo = CRM_Badge_BAO_Layout::buildLayout($params);
418
c490a46a 419 // split/get actual field names from token and individual contact image URLs
6d4a64c8
KJ
420 $returnProperties = array();
421 if (!empty($layoutInfo['data']['token'])) {
422 foreach ($layoutInfo['data']['token'] as $index => $value) {
423 $element = '';
424 if ($value) {
425 $token = CRM_Utils_Token::getTokens($value);
426 if (key($token) == 'contact') {
427 $element = $token['contact'][0];
428 }
429 elseif (key($token) == 'event') {
430 $element = $token['event'][0];
431 //FIX ME - we need to standardize event token names
4cb8f020 432 if (substr($element, 0, 6) != 'event_') {
6d4a64c8
KJ
433 $element = 'event_' . $element;
434 }
435 }
436 elseif (key($token) == 'participant') {
437 $element = $token['participant'][0];
438 }
439
440 // build returnproperties for query
441 $returnProperties[$element] = 1;
442 }
443
444 // add actual field name to row element
445 $layoutInfo['data']['rowElements'][$index] = $element;
446 }
447 }
448
449 // add additional required fields for query execution
609df1ed 450 $additionalFields = array('participant_register_date', 'participant_id', 'event_id', 'contact_id', 'image_URL');
6d4a64c8
KJ
451 foreach ($additionalFields as $field) {
452 $returnProperties[$field] = 1;
453 }
454
455 if ($form->_single) {
456 $queryParams = NULL;
457 }
458 else {
459 $queryParams = $form->get('queryParams');
460 }
461
462 $query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, NULL, FALSE, FALSE,
463 CRM_Contact_BAO_Query::MODE_EVENT
464 );
465
466 list($select, $from, $where, $having) = $query->query();
467 if (empty($where)) {
468 $where = "WHERE {$form->_componentClause}";
469 }
470 else {
471 $where .= " AND {$form->_componentClause}";
472 }
473
474 $sortOrder = NULL;
475 if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
476 $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
477 if (!empty($sortOrder)) {
478 $sortOrder = " ORDER BY $sortOrder";
479 }
480 }
481 $queryString = "$select $from $where $having $sortOrder";
482
483 $dao = CRM_Core_DAO::executeQuery($queryString);
484 $rows = array();
485 while ($dao->fetch()) {
6f294e25 486 $query->convertToPseudoNames($dao);
6d4a64c8
KJ
487 $rows[$dao->participant_id] = array();
488 foreach ($returnProperties as $key => $dontCare) {
489 $rows[$dao->participant_id][$key] = isset($dao->$key) ? $dao->$key : NULL;
490 }
491 }
492
493 $eventBadgeClass = new CRM_Badge_BAO_Badge();
494 $eventBadgeClass->createLabels($rows, $layoutInfo);
495 }
42c62ec1 496}