Merge pull request #23956 from agh1/5.51.0-releasenotes-final
[civicrm-core.git] / CRM / Event / Badge.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
f452d72c 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
f452d72c
CW
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/*
e70a7fc0
TO
13 * Copyright (C) 2010 Tech To The People
14 * Licensed to CiviCRM under the Academic Free License version 3.0.
15 *
16 */
6a488035
TO
17
18/**
19 *
20 * @package CRM
ca5cec67 21 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
22 */
23
24/**
25 * This class print the name badges for the participants
26 * It isn't supposed to be called directly, but is the parent class of the classes in CRM/Event/Badges/XXX.php
27 *
28 */
29class CRM_Event_Badge {
90b461f1 30
0cf587a7 31 /**
0cf587a7 32 */
00be9182 33 public function __construct() {
be2fb01f 34 $this->style = [
353ffa53
TO
35 'width' => 0.1,
36 'cap' => 'round',
37 'join' => 'round',
38 'dash' => '2,2',
be2fb01f
CW
39 'color' => [0, 0, 200],
40 ];
353ffa53 41 $this->format = '5160';
6a488035 42 $this->imgExtension = 'png';
353ffa53
TO
43 $this->imgRes = 300;
44 $this->event = NULL;
6a488035
TO
45 $this->setDebug(FALSE);
46 }
47
0cf587a7
EM
48 /**
49 * @param bool $debug
50 */
00be9182 51 public function setDebug($debug = TRUE) {
6a488035
TO
52 if (!$debug) {
53 $this->debug = FALSE;
54 $this->border = 0;
55 }
56 else {
57 $this->debug = TRUE;
58 $this->border = "LTRB";
59 }
60 }
61
62 /**
54957108 63 * Create the labels (pdf).
64 *
6a488035
TO
65 * It assumes the participants are from the same event
66 *
d4dd1e85 67 * @param array $participants
6a488035
TO
68 */
69 public function run(&$participants) {
70 // fetch the 1st participant, and take her event to retrieve its attributes
71 $participant = reset($participants);
353ffa53 72 $eventID = $participant['event_id'];
6a488035
TO
73 $this->event = self::retrieveEvent($eventID);
74 //call function to create labels
75 self::createLabels($participants);
292c8687 76 CRM_Utils_System::civiExit();
6a488035
TO
77 }
78
0cf587a7 79 /**
100fef9d 80 * @param int $eventID
0cf587a7
EM
81 *
82 * @return CRM_Event_BAO_Event|null
83 */
6a488035
TO
84 protected function retrieveEvent($eventID) {
85 $bao = new CRM_Event_BAO_Event();
86 if ($bao->get('id', $eventID)) {
87 return $bao;
88 }
89 return NULL;
90 }
91
0cf587a7 92 /**
100fef9d 93 * @param int $eventID
0cf587a7
EM
94 * @param bool $img
95 *
96 * @return string
97 */
00be9182 98 public function getImageFileName($eventID, $img = FALSE) {
6a488035
TO
99 global $civicrm_root;
100 $path = "CRM/Event/Badge";
101 if ($img == FALSE) {
102 return FALSE;
103 }
104 if ($img == TRUE) {
105 $img = get_class($this) . "." . $this->imgExtension;
106 }
107
81733948
DG
108 // CRM-13235 - leverage the Smarty path to get all templates directories
109 $template = CRM_Core_Smarty::singleton();
110 if (isset($template->template_dir) && $template->template_dir) {
be2fb01f 111 $dirs = is_array($template->template_dir) ? $template->template_dir : [$template->template_dir];
81733948 112 foreach ($dirs as $dir) {
be2fb01f 113 foreach (["$dir/$path/$eventID/$img", "$dir/$path/$img"] as $imgFile) {
81733948
DG
114 if (file_exists($imgFile)) {
115 return $imgFile;
116 }
117 }
118 }
0db6c3e1
TO
119 }
120 else {
81733948 121 $imgFile = 'No template directories defined anywhere??';
6a488035
TO
122 }
123
124 // not sure it exists, but at least will display a meaniful fatal error in debug mode
125 return $imgFile;
126 }
127
0cf587a7
EM
128 /**
129 * @param bool $img
130 */
00be9182 131 public function printBackground($img = FALSE) {
6a488035
TO
132 $x = $this->pdf->GetAbsX();
133 $y = $this->pdf->GetY();
134 if ($this->debug) {
be2fb01f 135 $this->pdf->Rect($x, $y, $this->pdf->width, $this->pdf->height, 'D', [
90b461f1
SL
136 'all' => [
137 'width' => 1,
138 'cap' => 'round',
139 'join' => 'round',
140 'dash' => '2,10',
141 'color' => [255, 0, 0],
142 ],
143 ]);
6a488035
TO
144 }
145 $img = $this->getImageFileName($this->event->id, $img);
146 if ($img) {
147 $imgsize = getimagesize($img);
148 // mm
149 $f = $this->imgRes / 25.4;
150 $w = $imgsize[0] / $f;
151 $h = $imgsize[1] / $f;
152 $this->pdf->Image($img, $this->pdf->GetAbsX(), $this->pdf->GetY(), $w, $h, strtoupper($this->imgExtension), '', '', FALSE, 72, '', FALSE, FALSE, $this->debug, FALSE, FALSE, FALSE);
153 }
154 $this->pdf->SetXY($x, $y);
155 }
156
157 /**
ea3ddccf 158 * This is supposed to be overridden.
159 *
160 * @param array $participant
d5cc0fc2 161 */
6a488035
TO
162 public function generateLabel($participant) {
163 $txt = "{$this->event['title']}
164{$participant['display_name']}
165{$participant['current_employer']}";
166
167 $this->pdf->MultiCell($this->pdf->width, $this->pdf->lineHeight, $txt);
168 }
169
0479b4c8
TO
170 public function pdfExtraFormat() {
171 }
6a488035
TO
172
173 /**
ad37ac8e 174 * Create labels (pdf).
6a488035 175 *
c490a46a 176 * @param array $participants
6a488035 177 */
00be9182 178 public function createLabels(&$participants) {
6a488035
TO
179
180 $this->pdf = new CRM_Utils_PDF_Label($this->format, 'mm');
181 $this->pdfExtraFormat();
182 $this->pdf->Open();
183 $this->pdf->setPrintHeader(FALSE);
184 $this->pdf->setPrintFooter(FALSE);
185 $this->pdf->AddPage();
186 $this->pdf->AddFont('DejaVu Sans', '', 'DejaVuSans.php');
187 $this->pdf->SetFont('DejaVu Sans');
188 $this->pdf->SetGenerator($this, "generateLabel");
189
190 foreach ($participants as $participant) {
191 $this->pdf->AddPdfLabel($participant);
192 }
193 $this->pdf->Output($this->event->title . '.pdf', 'D');
194 }
96025800 195
6a488035 196}