e501e95ff7515f35933e7b46b94769b61760276c
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Class CRM_Badge_Format_Badge.
21 * parent class for building name badges
23 class CRM_Badge_BAO_Badge
{
28 public $debug = FALSE;
36 * This function is called to create name label pdf.
38 * @param array $participants
39 * Associated array with participant info.
40 * @param array $layoutInfo
41 * Associated array which contains meta data about format/layout.
43 public function createLabels($participants, &$layoutInfo) {
44 $this->pdf
= new CRM_Utils_PDF_Label($layoutInfo['format'], 'mm');
46 $this->pdf
->setPrintHeader(FALSE);
47 $this->pdf
->setPrintFooter(FALSE);
48 $this->pdf
->AddPage();
49 $this->pdf
->SetGenerator($this, "generateLabel");
51 // this is very useful for debugging, by default set to FALSE
53 $this->border
= "LTRB";
56 foreach ($participants as $participant) {
57 $formattedRow = self
::formatLabel($participant, $layoutInfo);
58 $this->pdf
->AddPdfLabel($formattedRow);
61 if (CIVICRM_UF
=== 'UnitTests') {
62 throw new CRM_Core_Exception_PrematureExitException('pdf output called', ['formattedRow' => $formattedRow]);
64 $this->pdf
->Output(CRM_Utils_String
::munge($layoutInfo['title'], '_', 64) . '.pdf', 'D');
65 CRM_Utils_System
::civiExit();
69 * Function to create structure and add meta data according to layout.
72 * Row element that needs to be formatted.
73 * @param array $layout
79 public static function formatLabel(&$row, &$layout) {
80 $formattedRow = ['labelFormat' => $layout['label_format_name']];
81 $formattedRow['labelTitle'] = $layout['title'];
82 $formattedRow['labelId'] = $layout['id'];
84 if (!empty($layout['data']['rowElements'])) {
85 foreach ($layout['data']['rowElements'] as $key => $element) {
88 $value = $row[$element];
89 // hack to fix date field display format
90 if (strpos($element, '_date')) {
91 $value = CRM_Utils_Date
::customFormat($value, "%B %E%f");
95 $formattedRow['token'][$key] = [
97 'font_name' => $layout['data']['font_name'][$key],
98 'font_size' => $layout['data']['font_size'][$key],
99 'font_style' => $layout['data']['font_style'][$key],
100 'text_alignment' => $layout['data']['text_alignment'][$key],
101 'token' => $layout['data']['token'][$key],
106 if (!empty($layout['data']['image_1'])) {
107 $formattedRow['image_1'] = $layout['data']['image_1'];
109 if (!empty($layout['data']['width_image_1'])) {
110 $formattedRow['width_image_1'] = $layout['data']['width_image_1'];
112 if (!empty($layout['data']['height_image_1'])) {
113 $formattedRow['height_image_1'] = $layout['data']['height_image_1'];
116 if (!empty($layout['data']['image_2'])) {
117 $formattedRow['image_2'] = $layout['data']['image_2'];
119 if (!empty($layout['data']['width_image_2'])) {
120 $formattedRow['width_image_2'] = $layout['data']['width_image_2'];
122 if (!empty($layout['data']['height_image_2'])) {
123 $formattedRow['height_image_2'] = $layout['data']['height_image_2'];
125 if (!empty($row['image_URL']) && !empty($layout['data']['show_participant_image'])) {
126 $formattedRow['participant_image'] = $row['image_URL'];
128 if (!empty($layout['data']['width_participant_image'])) {
129 $formattedRow['width_participant_image'] = $layout['data']['width_participant_image'];
131 if (!empty($layout['data']['height_participant_image'])) {
132 $formattedRow['height_participant_image'] = $layout['data']['height_participant_image'];
134 if (!empty($layout['data']['alignment_participant_image'])) {
135 $formattedRow['alignment_participant_image'] = $layout['data']['alignment_participant_image'];
138 if (!empty($layout['data']['add_barcode'])) {
139 $formattedRow['barcode'] = [
140 'alignment' => $layout['data']['barcode_alignment'],
141 'type' => $layout['data']['barcode_type'],
145 // finally assign all the row values, so that we can use it for barcode etc
146 $formattedRow['values'] = $row;
148 return $formattedRow;
152 * @param $formattedRow
154 public function generateLabel($formattedRow) {
155 switch ($formattedRow['labelFormat']) {
156 case 'A6 Badge Portrait 150x106':
157 case 'Hanging Badge 3-3/4" x 4-3"/4':
158 self
::labelCreator($formattedRow, 5);
163 self
::labelCreator($formattedRow);
169 * @param $formattedRow
170 * @param int $cellspacing
172 public function labelCreator(&$formattedRow, $cellspacing = 0) {
173 $this->lMarginLogo
= 18;
174 $this->tMarginName
= 20;
176 $x = $this->pdf
->GetAbsX();
177 $y = $this->pdf
->getY();
179 //call hook alterBadge
180 CRM_Utils_Hook
::alterBadge($formattedRow['labelTitle'], $this, $formattedRow, $formattedRow['values']);
183 if (!empty($formattedRow['image_1'])) {
184 $this->printImage($formattedRow['image_1'], NULL, NULL, CRM_Utils_Array
::value('width_image_1', $formattedRow),
185 CRM_Utils_Array
::value('height_image_1', $formattedRow));
188 if (!empty($formattedRow['image_2'])) {
189 $this->printImage($formattedRow['image_2'], $x +
68, NULL, CRM_Utils_Array
::value('width_image_2', $formattedRow),
190 CRM_Utils_Array
::value('height_image_2', $formattedRow));
193 if ((CRM_Utils_Array
::value('height_image_1', $formattedRow) >
194 CRM_Utils_Array
::value('height_image_2', $formattedRow)) && !empty($formattedRow['height_image_1'])
196 $startOffset = $formattedRow['height_image_1'] ??
NULL;
198 elseif (!empty($formattedRow['height_image_2'])) {
199 $startOffset = $formattedRow['height_image_2'] ??
NULL;
202 if (!empty($formattedRow['participant_image'])) {
204 switch (CRM_Utils_Array
::value('alignment_participant_image', $formattedRow)) {
216 $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));
217 if ($startOffset == NULL && !empty($formattedRow['height_participant_image'])) {
218 $startOffset = $formattedRow['height_participant_image'];
222 $this->pdf
->SetLineStyle([
227 'color' => [0, 0, 200],
230 $rowCount = CRM_Badge_Form_Layout
::FIELD_ROWCOUNT
;
231 for ($i = 1; $i <= $rowCount; $i++
) {
232 if (!empty($formattedRow['token'][$i]['token'])) {
234 if ($formattedRow['token'][$i]['token'] != 'spacer') {
235 $value = $formattedRow['token'][$i]['value'];
239 $rowWidth = $this->pdf
->width
;
240 if (!empty($formattedRow['participant_image']) && !empty($formattedRow['width_participant_image'])) {
241 $rowWidth = $this->pdf
->width
- $formattedRow['width_participant_image'];
242 if ($formattedRow['alignment_participant_image'] == 'L') {
243 $xAlign = $x +
$formattedRow['width_participant_image'] +
$imageAlign;
246 $offset = $this->pdf
->getY() +
$startOffset +
$cellspacing;
248 $this->pdf
->SetFont($formattedRow['token'][$i]['font_name'], $formattedRow['token'][$i]['font_style'],
249 $formattedRow['token'][$i]['font_size']);
250 $this->pdf
->MultiCell($rowWidth, 0, $value,
251 $this->border
, $formattedRow['token'][$i]['text_alignment'], 0, 1, $xAlign, $offset);
253 // set this to zero so that it is added only for first element
258 if (!empty($formattedRow['barcode'])) {
259 $data = $formattedRow['values'];
261 if ($formattedRow['barcode']['type'] == 'barcode') {
262 $data['current_value'] = $formattedRow['values']['contact_id'] . '-' . $formattedRow['values']['participant_id'];
265 // view participant url
266 $data['current_value'] = CRM_Utils_System
::url('civicrm/contact/view/participant',
267 'action=view&reset=1&cid=' . $formattedRow['values']['contact_id'] . '&id='
268 . $formattedRow['values']['participant_id'],
275 // call hook alterBarcode
276 CRM_Utils_Hook
::alterBarcode($data, $formattedRow['barcode']['type']);
278 if ($formattedRow['barcode']['type'] == 'barcode') {
282 switch ($formattedRow['barcode']['alignment']) {
301 'cellfitalign' => '',
304 'vpadding' => 'auto',
305 'fgcolor' => [0, 0, 0],
308 'font' => 'helvetica',
313 $this->pdf
->write1DBarcode($data['current_value'], 'C128', $xAlign, $y +
$this->pdf
->height
- 10, '70',
314 12, 0.4, $style, 'B');
320 switch ($formattedRow['barcode']['alignment']) {
337 'vpadding' => 'auto',
338 'fgcolor' => [0, 0, 0],
343 $this->pdf
->write2DBarcode($data['current_value'], 'QRCODE,H', $xAlign, $y +
$this->pdf
->height
- 26, 30,
350 * Helper function to print images.
360 public function printImage($img, $x = '', $y = '', $w = NULL, $h = NULL) {
362 $x = $this->pdf
->GetAbsX();
366 $y = $this->pdf
->GetY();
372 list($w, $h) = self
::getImageProperties($img, $this->imgRes
, $w, $h);
373 $this->pdf
->Image($img, $x, $y, $w, $h, '', '', '', FALSE, 72, '', FALSE,
374 FALSE, $this->debug
, FALSE, FALSE, FALSE);
376 $this->pdf
->SetXY($x, $y);
387 public static function getImageProperties($img, $imgRes = 300, $w = NULL, $h = NULL) {
388 $imgsize = getimagesize($img);
390 $w = !empty($w) ?
$w : $imgsize[0] / $f;
391 $h = !empty($h) ?
$h : $imgsize[1] / $f;
396 * Build badges parameters before actually creating badges.
398 * @param array $params
399 * Associated array of submitted values.
400 * @param CRM_Core_Form $form
402 public static function buildBadges(&$params, &$form) {
403 // get name badge layout info
404 $layoutInfo = CRM_Badge_BAO_Layout
::buildLayout($params);
406 // split/get actual field names from token and individual contact image URLs
407 $returnProperties = [];
408 if (!empty($layoutInfo['data']['token'])) {
409 foreach ($layoutInfo['data']['token'] as $index => $value) {
412 $token = CRM_Utils_Token
::getTokens($value);
413 if (key($token) == 'contact') {
414 $element = $token['contact'][0];
416 elseif (key($token) == 'event') {
417 $element = $token['event'][0];
418 //FIX ME - we need to standardize event token names
419 if (substr($element, 0, 6) != 'event_') {
420 $element = 'event_' . $element;
423 elseif (key($token) == 'participant') {
424 $element = $token['participant'][0];
427 // build returnproperties for query
428 $returnProperties[$element] = 1;
431 // add actual field name to row element
432 $layoutInfo['data']['rowElements'][$index] = $element;
436 // add additional required fields for query execution
437 $additionalFields = ['participant_register_date', 'participant_id', 'event_id', 'contact_id', 'image_URL'];
438 foreach ($additionalFields as $field) {
439 $returnProperties[$field] = 1;
442 if ($form->_single
) {
446 $queryParams = $form->get('queryParams');
449 $query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, NULL, FALSE, FALSE,
450 CRM_Contact_BAO_Query
::MODE_EVENT
453 list($select, $from, $where, $having) = $query->query();
455 $where = "WHERE {$form->_componentClause}";
458 $where .= " AND {$form->_componentClause}";
462 if ($form->get(CRM_Utils_Sort
::SORT_ORDER
)) {
463 $sortOrder = $form->get(CRM_Utils_Sort
::SORT_ORDER
);
464 if (!empty($sortOrder)) {
465 $sortOrder = " ORDER BY $sortOrder";
468 $queryString = "$select $from $where $having $sortOrder";
470 $dao = CRM_Core_DAO
::executeQuery($queryString);
472 while ($dao->fetch()) {
473 $query->convertToPseudoNames($dao);
474 $rows[$dao->participant_id
] = [];
475 foreach ($returnProperties as $key => $dontCare) {
476 $value = $dao->$key ??
NULL;
477 // Format custom fields
478 if (strstr($key, 'custom_') && isset($value)) {
479 $value = CRM_Core_BAO_CustomField
::displayValue($value, substr($key, 7), $dao->contact_id
);
481 $rows[$dao->participant_id
][$key] = $value;
485 $eventBadgeClass = new CRM_Badge_BAO_Badge();
486 $eventBadgeClass->createLabels($rows, $layoutInfo);