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