Merge pull request #5680 from elinw/restauthenticationjoomla
[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 +--------------------------------------------------------------------+
d25dd0ee 24 */
42c62ec1
KJ
25
26/**
27 *
28 * @package CRM
e7112fa7 29 * @copyright CiviCRM LLC (c) 2004-2015
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 45 /**
eceb18cc 46 * This function is called to create name label pdf.
42c62ec1 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 52 *
2b37475d 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 /**
b44e3f84 78 * Function to create structure and add meta data according to layout.
42c62ec1 79 *
691efc59
TO
80 * @param array $row
81 * Row element that needs to be formatted.
82 * @param array $layout
83 * Layout meta data.
42c62ec1 84 *
a6c01b45
CW
85 * @return array
86 * row with meta data
42c62ec1 87 */
00be9182 88 public static function formatLabel(&$row, &$layout) {
f4e94ab8 89 $formattedRow = array('labelFormat' => $layout['label_format_name']);
b62376e2
M
90 $formattedRow['labelTitle'] = $layout['title'];
91 $formattedRow['labelId'] = $layout['id'];
92
a7488080 93 if (!empty($layout['data']['rowElements'])) {
e135f2b5 94 foreach ($layout['data']['rowElements'] as $key => $element) {
8d5579e2
KJ
95 $value = '';
96 if ($element) {
97 $value = $row[$element];
98 // hack to fix date field display format
99 if (strpos($element, '_date')) {
1a502055 100 $value = CRM_Utils_Date::customFormat($value, "%B %E%f");
8d5579e2 101 }
be09a99d
KJ
102 }
103
42c62ec1 104 $formattedRow['token'][$key] = array(
be09a99d 105 'value' => $value,
42c62ec1
KJ
106 'font_name' => $layout['data']['font_name'][$key],
107 'font_size' => $layout['data']['font_size'][$key],
1c07f935 108 'font_style' => $layout['data']['font_style'][$key],
42c62ec1 109 'text_alignment' => $layout['data']['text_alignment'][$key],
50f52554 110 'token' => $layout['data']['token'][$key],
42c62ec1
KJ
111 );
112 }
113 }
114
a7488080 115 if (!empty($layout['data']['image_1'])) {
42c62ec1
KJ
116 $formattedRow['image_1'] = $layout['data']['image_1'];
117 }
a7488080 118 if (!empty($layout['data']['width_image_1'])) {
07d113b7
PJ
119 $formattedRow['width_image_1'] = $layout['data']['width_image_1'];
120 }
a7488080 121 if (!empty($layout['data']['height_image_1'])) {
07d113b7
PJ
122 $formattedRow['height_image_1'] = $layout['data']['height_image_1'];
123 }
42c62ec1 124
a7488080 125 if (!empty($layout['data']['image_2'])) {
42c62ec1
KJ
126 $formattedRow['image_2'] = $layout['data']['image_2'];
127 }
a7488080 128 if (!empty($layout['data']['width_image_2'])) {
07d113b7
PJ
129 $formattedRow['width_image_2'] = $layout['data']['width_image_2'];
130 }
a7488080 131 if (!empty($layout['data']['height_image_2'])) {
07d113b7
PJ
132 $formattedRow['height_image_2'] = $layout['data']['height_image_2'];
133 }
32d586ea 134 if (!empty($row['image_URL']) && !empty($layout['data']['show_participant_image'])) {
609df1ed 135 $formattedRow['participant_image'] = $row['image_URL'];
cf723d2e
M
136 }
137 if (!empty($layout['data']['width_participant_image'])) {
138 $formattedRow['width_participant_image'] = $layout['data']['width_participant_image'];
139 }
140 if (!empty($layout['data']['height_participant_image'])) {
141 $formattedRow['height_participant_image'] = $layout['data']['height_participant_image'];
142 }
143 if (!empty($layout['data']['alignment_participant_image'])) {
144 $formattedRow['alignment_participant_image'] = $layout['data']['alignment_participant_image'];
e0ef6999 145 }
42c62ec1 146
a7488080 147 if (!empty($layout['data']['add_barcode'])) {
f38395f7
KJ
148 $formattedRow['barcode'] = array(
149 'alignment' => $layout['data']['barcode_alignment'],
150 'type' => $layout['data']['barcode_type'],
151 );
42c62ec1
KJ
152 }
153
7fd8017a
KJ
154 // finally assign all the row values, so that we can use it for barcode etc
155 $formattedRow['values'] = $row;
156
42c62ec1
KJ
157 return $formattedRow;
158 }
f4e94ab8 159
e0ef6999
EM
160 /**
161 * @param $formattedRow
162 */
f4e94ab8
KJ
163 public function generateLabel($formattedRow) {
164 switch ($formattedRow['labelFormat']) {
8b5dac1e 165 case 'A6 Badge Portrait 150x106':
166 case 'Hanging Badge 3-3/4" x 4-3"/4':
167 self::labelCreator($formattedRow, 5);
168 break;
3a872e59 169
1b1d29d8 170 case 'Avery 5395':
8e9fd9e4 171 default:
8b5dac1e 172 self::labelCreator($formattedRow);
f4e94ab8
KJ
173 break;
174 }
175 }
176
e0ef6999
EM
177 /**
178 * @param $formattedRow
179 * @param int $cellspacing
180 */
8b5dac1e 181 public function labelCreator(&$formattedRow, $cellspacing = 0) {
09651125 182 $this->lMarginLogo = 18;
f4e94ab8
KJ
183 $this->tMarginName = 20;
184
185 $x = $this->pdf->GetAbsX();
b246735d 186 $y = $this->pdf->getY();
f4e94ab8 187
b09332e9 188 //call hook alterBadge
39798542 189 CRM_Utils_Hook::alterBadge($formattedRow['labelTitle'], $this, $formattedRow, $formattedRow['values']);
b09332e9 190
d67e7cf5 191 $startOffset = 0;
a7488080 192 if (!empty($formattedRow['image_1'])) {
07d113b7
PJ
193 $this->printImage($formattedRow['image_1'], NULL, NULL, CRM_Utils_Array::value('width_image_1', $formattedRow),
194 CRM_Utils_Array::value('height_image_1', $formattedRow));
8d5579e2
KJ
195 }
196
a7488080 197 if (!empty($formattedRow['image_2'])) {
07d113b7
PJ
198 $this->printImage($formattedRow['image_2'], $x + 68, NULL, CRM_Utils_Array::value('width_image_2', $formattedRow),
199 CRM_Utils_Array::value('height_image_2', $formattedRow));
d67e7cf5 200 }
201
202 if ((CRM_Utils_Array::value('height_image_1', $formattedRow) >
353ffa53
TO
203 CRM_Utils_Array::value('height_image_2', $formattedRow)) && !empty($formattedRow['height_image_1'])
204 ) {
d67e7cf5 205 $startOffset = CRM_Utils_Array::value('height_image_1', $formattedRow);
206 }
a7488080 207 elseif (!empty($formattedRow['height_image_2'])) {
d67e7cf5 208 $startOffset = CRM_Utils_Array::value('height_image_2', $formattedRow);
8d5579e2 209 }
f4e94ab8 210
b53cbfbc 211 if (!empty($formattedRow['participant_image'])) {
5faddd8c 212 $imageAlign = 0;
39798542 213 switch (CRM_Utils_Array::value('alignment_participant_image', $formattedRow)) {
cf723d2e 214 case 'R':
5faddd8c 215 $imageAlign = 68;
cf723d2e 216 break;
3a872e59 217
cf723d2e 218 case 'L':
5faddd8c 219 $imageAlign = 0;
cf723d2e 220 break;
3a872e59 221
cf723d2e
M
222 default:
223 break;
224 }
39798542 225 $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));
226 if ($startOffset == NULL && CRM_Utils_Array::value('height_participant_image', $formattedRow)) {
227 $startOffset = CRM_Utils_Array::value('height_participant_image', $formattedRow);
cf723d2e
M
228 }
229 }
230
e135f2b5
KJ
231 $this->pdf->SetLineStyle(array(
232 'width' => 0.1,
233 'cap' => 'round',
234 'join' => 'round',
235 'dash' => '2,2',
21dfd5f5 236 'color' => array(0, 0, 200),
e135f2b5 237 ));
f4e94ab8 238
50f52554 239 $rowCount = CRM_Badge_Form_Layout::FIELD_ROWCOUNT;
d67e7cf5 240 for ($i = 1; $i <= $rowCount; $i++) {
50f52554 241 if (!empty($formattedRow['token'][$i]['token'])) {
242 $value = '';
243 if ($formattedRow['token'][$i]['token'] != 'spacer') {
244 $value = $formattedRow['token'][$i]['value'];
e0ef6999
EM
245 }
246
5faddd8c 247 $xAlign = $x;
609df1ed 248 $rowWidth = $this->pdf->width;
39798542 249 if (!empty($formattedRow['participant_image']) && !empty($formattedRow['width_participant_image'])) {
5faddd8c 250 $rowWidth = $this->pdf->width - $formattedRow['width_participant_image'];
39798542 251 if ($formattedRow['alignment_participant_image'] == 'L') {
5faddd8c 252 $xAlign = $x + $formattedRow['width_participant_image'] + $imageAlign;
39798542 253 }
50f52554 254 }
8b5dac1e 255 $offset = $this->pdf->getY() + $startOffset + $cellspacing;
d67e7cf5 256
50f52554 257 $this->pdf->SetFont($formattedRow['token'][$i]['font_name'], $formattedRow['token'][$i]['font_style'],
258 $formattedRow['token'][$i]['font_size']);
609df1ed 259 $this->pdf->MultiCell($rowWidth, 0, $value,
5faddd8c 260 $this->border, $formattedRow['token'][$i]['text_alignment'], 0, 1, $xAlign, $offset);
d67e7cf5 261
262 // set this to zero so that it is added only for first element
263 $startOffset = 0;
50f52554 264 }
265 }
7fd8017a 266
a7488080 267 if (!empty($formattedRow['barcode'])) {
f38395f7 268 $data = $formattedRow['values'];
7fd8017a 269
f38395f7 270 if ($formattedRow['barcode']['type'] == 'barcode') {
408b79bf 271 $data['current_value'] = $formattedRow['values']['contact_id'] . '-' . $formattedRow['values']['participant_id'];
8afbd311 272 }
f38395f7
KJ
273 else {
274 // view participant url
275 $data['current_value'] = CRM_Utils_System::url('civicrm/contact/view/participant',
276 'action=view&reset=1&cid=' . $formattedRow['values']['contact_id'] . '&id='
277 . $formattedRow['values']['participant_id'],
278 TRUE,
279 NULL,
280 FALSE
281 );
8afbd311
KJ
282 }
283
9399901d 284 // call hook alterBarcode
f38395f7
KJ
285 CRM_Utils_Hook::alterBarcode($data, $formattedRow['barcode']['type']);
286
287 if ($formattedRow['barcode']['type'] == 'barcode') {
288 // barcode position
289 $xAlign = $x;
290
291 switch ($formattedRow['barcode']['alignment']) {
292 case 'L':
d3b0bbe1 293 $xAlign += -14;
f38395f7 294 break;
3a872e59 295
f38395f7 296 case 'R':
93c52de0 297 $xAlign += 27;
f38395f7 298 break;
3a872e59 299
f38395f7 300 case 'C':
d3b0bbe1 301 $xAlign += 9;
f38395f7
KJ
302 break;
303 }
9399901d 304
f38395f7
KJ
305 $style = array(
306 'position' => '',
307 'align' => '',
308 'stretch' => FALSE,
309 'fitwidth' => TRUE,
310 'cellfitalign' => '',
311 'border' => FALSE,
312 'hpadding' => 13.5,
313 'vpadding' => 'auto',
314 'fgcolor' => array(0, 0, 0),
315 'bgcolor' => FALSE,
316 'text' => FALSE,
317 'font' => 'helvetica',
318 'fontsize' => 8,
319 'stretchtext' => 0,
320 );
321
d3b0bbe1 322 $this->pdf->write1DBarcode($data['current_value'], 'C128', $xAlign, $y + $this->pdf->height - 10, '70',
f38395f7
KJ
323 12, 0.4, $style, 'B');
324 }
325 else {
326 // qr code position
327 $xAlign = $x;
328
329 switch ($formattedRow['barcode']['alignment']) {
330 case 'L':
d3b0bbe1 331 $xAlign += -5;
f38395f7 332 break;
3a872e59 333
f38395f7 334 case 'R':
0ae95ef2 335 $xAlign += 56;
f38395f7 336 break;
3a872e59 337
f38395f7
KJ
338 case 'C':
339 $xAlign += 29;
340 break;
341 }
342
343 $style = array(
3a872e59 344 'border' => FALSE,
f38395f7
KJ
345 'hpadding' => 13.5,
346 'vpadding' => 'auto',
3a872e59
TO
347 'fgcolor' => array(0, 0, 0),
348 'bgcolor' => FALSE,
f38395f7
KJ
349 'position' => '',
350 );
351
3a872e59 352 $this->pdf->write2DBarcode($data['current_value'], 'QRCODE,H', $xAlign, $y + $this->pdf->height - 26, 30,
d3b0bbe1 353 30, $style, 'B');
f38395f7 354 }
7fd8017a 355 }
f4e94ab8
KJ
356 }
357
358 /**
eceb18cc 359 * Helper function to print images.
fd31fa4c 360 *
691efc59
TO
361 * @param string $img
362 * Image url.
f4e94ab8 363 *
fd31fa4c
EM
364 * @param string $x
365 * @param string $y
366 * @param null $w
367 * @param null $h
368 *
f4e94ab8 369 * @return void
f4e94ab8 370 */
00be9182 371 public function printImage($img, $x = '', $y = '', $w = NULL, $h = NULL) {
1b1d29d8
KJ
372 if (!$x) {
373 $x = $this->pdf->GetAbsX();
374 }
375
376 if (!$y) {
377 $y = $this->pdf->GetY();
378 }
f4e94ab8
KJ
379
380 $this->imgRes = 300;
381
382 if ($img) {
07d113b7 383 list($w, $h) = self::getImageProperties($img, $this->imgRes, $w, $h);
1b1d29d8
KJ
384 $this->pdf->Image($img, $x, $y, $w, $h, '', '', '', FALSE, 72, '', FALSE,
385 FALSE, $this->debug, FALSE, FALSE, FALSE);
f4e94ab8
KJ
386 }
387 $this->pdf->SetXY($x, $y);
388 }
6d4a64c8 389
e0ef6999
EM
390 /**
391 * @param $img
392 * @param int $imgRes
393 * @param null $w
394 * @param null $h
395 *
396 * @return array
397 */
00be9182 398 public static function getImageProperties($img, $imgRes = 300, $w = NULL, $h = NULL) {
07d113b7
PJ
399 $imgsize = getimagesize($img);
400 $f = $imgRes / 25.4;
401 $w = !empty($w) ? $w : $imgsize[0] / $f;
402 $h = !empty($h) ? $h : $imgsize[1] / $f;
403 return array($w, $h);
404 }
405
6d4a64c8 406 /**
100fef9d 407 * Build badges parameters before actually creating badges.
6d4a64c8 408 *
691efc59
TO
409 * @param array $params
410 * Associated array of submitted values.
c490a46a 411 * @param CRM_Core_Form $form
6d4a64c8
KJ
412 *
413 * @return void
6d4a64c8
KJ
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) {
cffc5e59
CW
489 $value = isset($dao->$key) ? $dao->$key : NULL;
490 // Format custom fields
491 if (strstr($key, 'custom_') && isset($value)) {
492 $value = CRM_Core_BAO_CustomField::getDisplayValue($value, substr($key, 7), $query->_options, $dao->contact_id);
493 }
494 $rows[$dao->participant_id][$key] = $value;
6d4a64c8
KJ
495 }
496 }
497
498 $eventBadgeClass = new CRM_Badge_BAO_Badge();
499 $eventBadgeClass->createLabels($rows, $layoutInfo);
500 }
96025800 501
42c62ec1 502}