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