Merge pull request #23902 from civicrm/5.51
[civicrm-core.git] / CRM / Event / Badge.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /*
13 * Copyright (C) 2010 Tech To The People
14 * Licensed to CiviCRM under the Academic Free License version 3.0.
15 *
16 */
17
18 /**
19 *
20 * @package CRM
21 * @copyright CiviCRM LLC https://civicrm.org/licensing
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 */
29 class CRM_Event_Badge {
30
31 /**
32 */
33 public function __construct() {
34 $this->style = [
35 'width' => 0.1,
36 'cap' => 'round',
37 'join' => 'round',
38 'dash' => '2,2',
39 'color' => [0, 0, 200],
40 ];
41 $this->format = '5160';
42 $this->imgExtension = 'png';
43 $this->imgRes = 300;
44 $this->event = NULL;
45 $this->setDebug(FALSE);
46 }
47
48 /**
49 * @param bool $debug
50 */
51 public function setDebug($debug = TRUE) {
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 /**
63 * Create the labels (pdf).
64 *
65 * It assumes the participants are from the same event
66 *
67 * @param array $participants
68 */
69 public function run(&$participants) {
70 // fetch the 1st participant, and take her event to retrieve its attributes
71 $participant = reset($participants);
72 $eventID = $participant['event_id'];
73 $this->event = self::retrieveEvent($eventID);
74 //call function to create labels
75 self::createLabels($participants);
76 CRM_Utils_System::civiExit();
77 }
78
79 /**
80 * @param int $eventID
81 *
82 * @return CRM_Event_BAO_Event|null
83 */
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
92 /**
93 * @param int $eventID
94 * @param bool $img
95 *
96 * @return string
97 */
98 public function getImageFileName($eventID, $img = FALSE) {
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
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) {
111 $dirs = is_array($template->template_dir) ? $template->template_dir : [$template->template_dir];
112 foreach ($dirs as $dir) {
113 foreach (["$dir/$path/$eventID/$img", "$dir/$path/$img"] as $imgFile) {
114 if (file_exists($imgFile)) {
115 return $imgFile;
116 }
117 }
118 }
119 }
120 else {
121 $imgFile = 'No template directories defined anywhere??';
122 }
123
124 // not sure it exists, but at least will display a meaniful fatal error in debug mode
125 return $imgFile;
126 }
127
128 /**
129 * @param bool $img
130 */
131 public function printBackground($img = FALSE) {
132 $x = $this->pdf->GetAbsX();
133 $y = $this->pdf->GetY();
134 if ($this->debug) {
135 $this->pdf->Rect($x, $y, $this->pdf->width, $this->pdf->height, 'D', [
136 'all' => [
137 'width' => 1,
138 'cap' => 'round',
139 'join' => 'round',
140 'dash' => '2,10',
141 'color' => [255, 0, 0],
142 ],
143 ]);
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 /**
158 * This is supposed to be overridden.
159 *
160 * @param array $participant
161 */
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
170 public function pdfExtraFormat() {
171 }
172
173 /**
174 * Create labels (pdf).
175 *
176 * @param array $participants
177 */
178 public function createLabels(&$participants) {
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 }
195
196 }