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
18 use Civi\Token\TokenProcessor
;
21 * Class CRM_Badge_Format_Badge.
23 * parent class for building name badges
25 class CRM_Badge_BAO_Badge
{
30 public $debug = FALSE;
38 * This function is called to create name label pdf.
40 * @param array $participants
41 * Associated array with participant info.
42 * @param array $layoutInfo
43 * Associated array which contains meta data about format/layout.
45 public function createLabels($participants, &$layoutInfo) {
46 $this->pdf
= new CRM_Utils_PDF_Label($layoutInfo['format'], 'mm');
48 $this->pdf
->setPrintHeader(FALSE);
49 $this->pdf
->setPrintFooter(FALSE);
50 $this->pdf
->AddPage();
51 $this->pdf
->SetGenerator($this, "generateLabel");
53 // this is very useful for debugging, by default set to FALSE
55 $this->border
= "LTRB";
58 foreach ($participants as $participant) {
59 $formattedRow = self
::formatLabel($participant, $layoutInfo);
60 $this->pdf
->AddPdfLabel($formattedRow);
63 if (CIVICRM_UF
=== 'UnitTests') {
64 throw new CRM_Core_Exception_PrematureExitException('pdf output called', ['formattedRow' => $formattedRow]);
66 $this->pdf
->Output(CRM_Utils_String
::munge($layoutInfo['title'], '_', 64) . '.pdf', 'D');
67 CRM_Utils_System
::civiExit();
71 * Function to create structure and add meta data according to layout.
74 * Row element that needs to be formatted.
75 * @param array $layout
81 public static function formatLabel(&$row, &$layout) {
82 $formattedRow = ['labelFormat' => $layout['label_format_name']];
83 $formattedRow['labelTitle'] = $layout['title'];
84 $formattedRow['labelId'] = $layout['id'];
86 if (!empty($layout['data']['rowElements'])) {
87 foreach ($layout['data']['rowElements'] as $key => $element) {
90 $value = $row[$element];
93 $formattedRow['token'][$key] = [
95 'font_name' => $layout['data']['font_name'][$key],
96 'font_size' => $layout['data']['font_size'][$key],
97 'font_style' => $layout['data']['font_style'][$key],
98 'text_alignment' => $layout['data']['text_alignment'][$key],
99 'token' => $layout['data']['token'][$key],
104 if (!empty($layout['data']['image_1'])) {
105 $formattedRow['image_1'] = $layout['data']['image_1'];
107 if (!empty($layout['data']['width_image_1'])) {
108 $formattedRow['width_image_1'] = $layout['data']['width_image_1'];
110 if (!empty($layout['data']['height_image_1'])) {
111 $formattedRow['height_image_1'] = $layout['data']['height_image_1'];
114 if (!empty($layout['data']['image_2'])) {
115 $formattedRow['image_2'] = $layout['data']['image_2'];
117 if (!empty($layout['data']['width_image_2'])) {
118 $formattedRow['width_image_2'] = $layout['data']['width_image_2'];
120 if (!empty($layout['data']['height_image_2'])) {
121 $formattedRow['height_image_2'] = $layout['data']['height_image_2'];
123 if (!empty($row['image_URL']) && !empty($layout['data']['show_participant_image'])) {
124 $formattedRow['participant_image'] = $row['image_URL'];
126 if (!empty($layout['data']['width_participant_image'])) {
127 $formattedRow['width_participant_image'] = $layout['data']['width_participant_image'];
129 if (!empty($layout['data']['height_participant_image'])) {
130 $formattedRow['height_participant_image'] = $layout['data']['height_participant_image'];
132 if (!empty($layout['data']['alignment_participant_image'])) {
133 $formattedRow['alignment_participant_image'] = $layout['data']['alignment_participant_image'];
136 if (!empty($layout['data']['add_barcode'])) {
137 $formattedRow['barcode'] = [
138 'alignment' => $layout['data']['barcode_alignment'],
139 'type' => $layout['data']['barcode_type'],
143 // finally assign all the row values, so that we can use it for barcode etc
144 $formattedRow['values'] = $row;
146 return $formattedRow;
150 * @param array $formattedRow
152 public function generateLabel($formattedRow) {
153 switch ($formattedRow['labelFormat']) {
154 case 'A6 Badge Portrait 150x106':
155 case 'Hanging Badge 3-3/4" x 4-3"/4':
156 self
::labelCreator($formattedRow, 5);
161 self
::labelCreator($formattedRow);
167 * @param array $formattedRow
168 * @param int $cellspacing
170 public function labelCreator(&$formattedRow, $cellspacing = 0) {
171 $this->lMarginLogo
= 18;
172 $this->tMarginName
= 20;
174 $x = $this->pdf
->GetAbsX();
175 $y = $this->pdf
->getY();
177 //call hook alterBadge
178 CRM_Utils_Hook
::alterBadge($formattedRow['labelTitle'], $this, $formattedRow, $formattedRow['values']);
181 if (!empty($formattedRow['image_1'])) {
182 $this->printImage($formattedRow['image_1'], NULL, NULL, CRM_Utils_Array
::value('width_image_1', $formattedRow),
183 CRM_Utils_Array
::value('height_image_1', $formattedRow));
186 if (!empty($formattedRow['image_2'])) {
187 $this->printImage($formattedRow['image_2'], $x +
68, NULL, CRM_Utils_Array
::value('width_image_2', $formattedRow),
188 CRM_Utils_Array
::value('height_image_2', $formattedRow));
191 if ((CRM_Utils_Array
::value('height_image_1', $formattedRow) >
192 CRM_Utils_Array
::value('height_image_2', $formattedRow)) && !empty($formattedRow['height_image_1'])
194 $startOffset = $formattedRow['height_image_1'] ??
NULL;
196 elseif (!empty($formattedRow['height_image_2'])) {
197 $startOffset = $formattedRow['height_image_2'] ??
NULL;
200 if (!empty($formattedRow['participant_image'])) {
202 switch (CRM_Utils_Array
::value('alignment_participant_image', $formattedRow)) {
214 $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));
215 if ($startOffset == NULL && !empty($formattedRow['height_participant_image'])) {
216 $startOffset = $formattedRow['height_participant_image'];
220 $this->pdf
->SetLineStyle([
225 'color' => [0, 0, 200],
228 $rowCount = CRM_Badge_Form_Layout
::FIELD_ROWCOUNT
;
229 for ($i = 1; $i <= $rowCount; $i++
) {
230 if (!empty($formattedRow['token'][$i]['token'])) {
232 if ($formattedRow['token'][$i]['token'] != 'spacer') {
233 $value = $formattedRow['token'][$i]['value'];
237 $rowWidth = $this->pdf
->width
;
238 if (!empty($formattedRow['participant_image']) && !empty($formattedRow['width_participant_image'])) {
239 $rowWidth = $this->pdf
->width
- $formattedRow['width_participant_image'];
240 if ($formattedRow['alignment_participant_image'] == 'L') {
241 $xAlign = $x +
$formattedRow['width_participant_image'] +
$imageAlign;
244 $offset = $this->pdf
->getY() +
$startOffset +
$cellspacing;
246 $this->pdf
->SetFont($formattedRow['token'][$i]['font_name'], $formattedRow['token'][$i]['font_style'],
247 $formattedRow['token'][$i]['font_size']);
248 $this->pdf
->MultiCell($rowWidth, 0, $value,
249 $this->border
, $formattedRow['token'][$i]['text_alignment'], 0, 1, $xAlign, $offset);
251 // set this to zero so that it is added only for first element
256 if (!empty($formattedRow['barcode'])) {
257 $data = $formattedRow['values'];
259 if ($formattedRow['barcode']['type'] == 'barcode') {
260 $data['current_value'] = $formattedRow['values']['contact_id'] . '-' . $formattedRow['values']['participant_id'];
263 // view participant url
264 $data['current_value'] = CRM_Utils_System
::url('civicrm/contact/view/participant',
265 'action=view&reset=1&cid=' . $formattedRow['values']['contact_id'] . '&id='
266 . $formattedRow['values']['participant_id'],
273 // call hook alterBarcode
274 CRM_Utils_Hook
::alterBarcode($data, $formattedRow['barcode']['type']);
276 if ($formattedRow['barcode']['type'] == 'barcode') {
280 switch ($formattedRow['barcode']['alignment']) {
299 'cellfitalign' => '',
302 'vpadding' => 'auto',
303 'fgcolor' => [0, 0, 0],
306 'font' => 'helvetica',
311 $this->pdf
->write1DBarcode($data['current_value'], 'C128', $xAlign, $y +
$this->pdf
->height
- 10, '70',
312 12, 0.4, $style, 'B');
318 switch ($formattedRow['barcode']['alignment']) {
335 'vpadding' => 'auto',
336 'fgcolor' => [0, 0, 0],
341 $this->pdf
->write2DBarcode($data['current_value'], 'QRCODE,H', $xAlign, $y +
$this->pdf
->height
- 26, 30,
348 * Helper function to print images.
352 * @param string|null $x
353 * @param string|null $y
357 public function printImage($img, $x = NULL, $y = NULL, $w = NULL, $h = NULL) {
359 $x = $this->pdf
->GetAbsX();
363 $y = $this->pdf
->GetY();
369 [$w, $h] = self
::getImageProperties($img, $this->imgRes
, $w, $h);
370 $this->pdf
->Image($img, $x, $y, $w, $h, '', '', '', FALSE, 72, '', FALSE,
371 FALSE, $this->debug
, FALSE, FALSE, FALSE);
373 $this->pdf
->SetXY($x, $y);
386 public static function getImageProperties($img, $imgRes = 300, $w = NULL, $h = NULL) {
387 $imgsize = getimagesize($img);
389 $w = !empty($w) ?
$w : $imgsize[0] / $f;
390 $h = !empty($h) ?
$h : $imgsize[1] / $f;
395 * Build badges parameters before actually creating badges.
397 * @param array $params
398 * Associated array of submitted values.
399 * @param CRM_Core_Form $form
401 public static function buildBadges(&$params, &$form) {
402 // get name badge layout info
403 $layoutInfo = CRM_Badge_BAO_Layout
::buildLayout($params);
404 $tokenProcessor = new TokenProcessor(\Civi
::dispatcher(), ['schema' => ['participantId', 'eventId'], 'smarty' => FALSE]);
405 // split/get actual field names from token and individual contact image URLs
406 $processorTokens = [];
407 if (!empty($layoutInfo['data']['token'])) {
408 foreach ($layoutInfo['data']['token'] as $index => $value) {
410 $tokenName = str_replace(['}', '{contact.', '{participant.', '{event.'], '', $value);
411 $tokenProcessor->addMessage($tokenName, $value, 'text/plain');
412 $processorTokens[] = $tokenName;
413 $layoutInfo['data']['rowElements'][$index] = $tokenName;
418 $returnProperties = [
419 'participant_id' => 1,
423 $sortOrder = $form->get(CRM_Utils_Sort
::SORT_ORDER
);
426 $sortField = explode(' ', $sortOrder)[0];
427 // Add to select so aliaising is handled.
428 $returnProperties[trim(str_replace('`', ' ', $sortField))] = 1;
429 $sortOrder = " ORDER BY $sortOrder";
431 if ($form->_single
) {
435 $queryParams = $form->get('queryParams');
438 $query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, NULL, FALSE, FALSE,
439 CRM_Contact_BAO_Query
::MODE_EVENT
442 [$select, $from, $where, $having] = $query->query();
444 $where = "WHERE {$form->_componentClause}";
447 $where .= " AND {$form->_componentClause}";
450 $queryString = "$select $from $where $having $sortOrder";
452 $dao = CRM_Core_DAO
::executeQuery($queryString);
455 while ($dao->fetch()) {
456 $tokenProcessor->addRow(['contactId' => $dao->contact_id
, 'participantId' => $dao->participant_id
, 'eventId' => $dao->event_id
]);
458 $tokenProcessor->evaluate();
459 foreach ($tokenProcessor->getRows() as $row) {
460 foreach ($processorTokens as $processorToken) {
461 $rows[$row->context
['participantId']][$processorToken] = $row->render($processorToken);
465 $eventBadgeClass = new CRM_Badge_BAO_Badge();
466 $eventBadgeClass->createLabels($rows, $layoutInfo);