Merge pull request #1157 from kurund/CRM-12967
[civicrm-core.git] / CRM / Badge / BAO / Badge.php
CommitLineData
42c62ec1
KJ
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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 *
28 * @package CRM
29 * @copyright CiviCRM LLC (c) 2004-2013
30 * $Id$
31 *
32 */
33
34/**
35 * Class CRM_Badge_Format_Badge
36 *
37 * parent class for building name badges
38 */
f4e94ab8 39class CRM_Badge_BAO_Badge {
382139c4
KJ
40
41 public $debug = FALSE;
42
43 public $border = 0;
44
42c62ec1
KJ
45 /**
46 * This function is called to create name label pdf
47 *
e135f2b5
KJ
48 * @param array $participants associated array with participant info
49 * @param array $layoutInfo associated array which contains meta data about format/layout
42c62ec1
KJ
50 *
51 * @return void
52 * @access public
53 */
1b1d29d8 54 public function createLabels(&$participants, &$layoutInfo) {
42c62ec1
KJ
55 $this->pdf = new CRM_Utils_PDF_Label($layoutInfo['format'], 'mm');
56 $this->pdf->Open();
57 $this->pdf->setPrintHeader(FALSE);
58 $this->pdf->setPrintFooter(FALSE);
59 $this->pdf->AddPage();
42c62ec1
KJ
60 $this->pdf->SetGenerator($this, "generateLabel");
61
1b1d29d8 62 // this is very useful for debugging, by default set to FALSE
1b1d29d8
KJ
63 if ($this->debug) {
64 $this->border = "LTRB";
65 }
66
42c62ec1
KJ
67 foreach ($participants as $participant) {
68 $formattedRow = self::formatLabel($participant, $layoutInfo);
69 $this->pdf->AddPdfLabel($formattedRow);
70 }
71
72 $this->pdf->Output(CRM_Utils_String::munge($layoutInfo['title'], '_', 64) . '.pdf', 'D');
73 CRM_Utils_System::civiExit(1);
74 }
75
76 /**
77 * Funtion to create structure and add meta data according to layout
78 *
79 * @param array $row row element that needs to be formatted
80 * @param array $layout layout meta data
81 *
82 * @return array $formattedRow row with meta data
83 */
84 static function formatLabel(&$row, &$layout) {
f4e94ab8
KJ
85 $formattedRow = array('labelFormat' => $layout['label_format_name']);
86
42c62ec1 87 if (CRM_Utils_Array::value('rowElements', $layout['data'])) {
e135f2b5 88 foreach ($layout['data']['rowElements'] as $key => $element) {
8d5579e2
KJ
89 $value = '';
90 if ($element) {
91 $value = $row[$element];
92 // hack to fix date field display format
93 if (strpos($element, '_date')) {
94 $value = CRM_Utils_Date::customFormat($value, "%e %b");
95 }
be09a99d
KJ
96 }
97
42c62ec1 98 $formattedRow['token'][$key] = array(
be09a99d 99 'value' => $value,
42c62ec1
KJ
100 'font_name' => $layout['data']['font_name'][$key],
101 'font_size' => $layout['data']['font_size'][$key],
102 'text_alignment' => $layout['data']['text_alignment'][$key],
103 );
104 }
105 }
106
107 if (CRM_Utils_Array::value('image_1', $layout['data'])) {
108 $formattedRow['image_1'] = $layout['data']['image_1'];
109 }
110
111 if (CRM_Utils_Array::value('image_2', $layout['data'])) {
112 $formattedRow['image_2'] = $layout['data']['image_2'];
113 }
114
115 if (CRM_Utils_Array::value('add_barcode', $layout['data'])) {
f38395f7
KJ
116 $formattedRow['barcode'] = array(
117 'alignment' => $layout['data']['barcode_alignment'],
118 'type' => $layout['data']['barcode_type'],
119 );
42c62ec1
KJ
120 }
121
7fd8017a
KJ
122 // finally assign all the row values, so that we can use it for barcode etc
123 $formattedRow['values'] = $row;
124
42c62ec1
KJ
125 return $formattedRow;
126 }
f4e94ab8
KJ
127
128 public function generateLabel($formattedRow) {
129 switch ($formattedRow['labelFormat']) {
1b1d29d8 130 case 'Avery 5395':
f4e94ab8
KJ
131 self::labelAvery5395($formattedRow);
132 break;
133 }
134 }
135
136 public function labelAvery5395(&$formattedRow) {
09651125 137 $this->lMarginLogo = 18;
f4e94ab8
KJ
138 $this->tMarginName = 20;
139
140 $x = $this->pdf->GetAbsX();
141 $y = $this->pdf->GetY();
142
09651125 143 $titleWidth = $titleLeftMargin = 0;
8d5579e2
KJ
144 if (CRM_Utils_Array::value('image_1', $formattedRow)) {
145 $this->printImage($formattedRow['image_1']);
09651125 146 $titleWidth = $titleLeftMargin = $this->lMarginLogo;
8d5579e2
KJ
147 }
148
09651125 149 $titleRightMargin = 0;
8d5579e2 150 if (CRM_Utils_Array::value('image_2', $formattedRow)) {
e135f2b5 151 $this->printImage($formattedRow['image_2'], $x + 68);
09651125
KJ
152 $titleRightMargin = 36;
153 $titleWidth = $this->lMarginLogo;
8d5579e2 154 }
f4e94ab8 155
e135f2b5
KJ
156 $this->pdf->SetLineStyle(array(
157 'width' => 0.1,
158 'cap' => 'round',
159 'join' => 'round',
160 'dash' => '2,2',
161 'color' => array(0, 0, 200)
162 ));
f4e94ab8 163
09651125
KJ
164 if ($titleLeftMargin && $titleRightMargin) {
165 $titleWidth = $titleRightMargin;
166 }
167
fc476655 168 $this->pdf->SetFont($formattedRow['token'][1]['font_name'], '', $formattedRow['token'][1]['font_size']);
e135f2b5 169 $this->pdf->MultiCell($this->pdf->width - $titleWidth, 0, $formattedRow['token'][1]['value'],
09651125 170 $this->border, $formattedRow['token'][1]['text_alignment'], 0, 1, $x + $titleLeftMargin, $y);
f4e94ab8 171
fc476655 172 $this->pdf->SetFont($formattedRow['token'][2]['font_name'], '', $formattedRow['token'][2]['font_size']);
1b1d29d8
KJ
173 $this->pdf->MultiCell($this->pdf->width, 10, $formattedRow['token'][2]['value'],
174 $this->border, $formattedRow['token'][2]['text_alignment'], 0, 1, $x, $y + $this->tMarginName);
f4e94ab8 175
fc476655 176 $this->pdf->SetFont($formattedRow['token'][3]['font_name'], '', $formattedRow['token'][3]['font_size']);
1b1d29d8
KJ
177 $this->pdf->MultiCell($this->pdf->width, 0, $formattedRow['token'][3]['value'],
178 $this->border, $formattedRow['token'][3]['text_alignment'], 0, 1, $x, $this->pdf->getY());
179
fc476655 180 $this->pdf->SetFont($formattedRow['token'][4]['font_name'], '', $formattedRow['token'][4]['font_size']);
ee82c9d7
KJ
181 $this->pdf->MultiCell($this->pdf->width, 0, $formattedRow['token'][4]['value'],
182 $this->border, $formattedRow['token'][4]['text_alignment'], 0, 1, $x, $y + $this->pdf->height - 5);
7fd8017a
KJ
183
184 if (CRM_Utils_Array::value('barcode', $formattedRow)) {
f38395f7 185 $data = $formattedRow['values'];
7fd8017a 186
f38395f7
KJ
187 if ($formattedRow['barcode']['type'] == 'barcode') {
188 $data['current_value'] =
189 $formattedRow['values']['contact_id'] . '-' . $formattedRow['values']['participant_id'];
8afbd311 190 }
f38395f7
KJ
191 else {
192 // view participant url
193 $data['current_value'] = CRM_Utils_System::url('civicrm/contact/view/participant',
194 'action=view&reset=1&cid=' . $formattedRow['values']['contact_id'] . '&id='
195 . $formattedRow['values']['participant_id'],
196 TRUE,
197 NULL,
198 FALSE
199 );
8afbd311
KJ
200 }
201
9399901d 202 // call hook alterBarcode
f38395f7
KJ
203 CRM_Utils_Hook::alterBarcode($data, $formattedRow['barcode']['type']);
204
205 if ($formattedRow['barcode']['type'] == 'barcode') {
206 // barcode position
207 $xAlign = $x;
208
209 switch ($formattedRow['barcode']['alignment']) {
210 case 'L':
211 $xAlign += -12;
212 break;
213 case 'R':
214 $xAlign += 30;
215 break;
216 case 'C':
217 $xAlign += 15;
218 break;
219 }
9399901d 220
f38395f7
KJ
221 $style = array(
222 'position' => '',
223 'align' => '',
224 'stretch' => FALSE,
225 'fitwidth' => TRUE,
226 'cellfitalign' => '',
227 'border' => FALSE,
228 'hpadding' => 13.5,
229 'vpadding' => 'auto',
230 'fgcolor' => array(0, 0, 0),
231 'bgcolor' => FALSE,
232 'text' => FALSE,
233 'font' => 'helvetica',
234 'fontsize' => 8,
235 'stretchtext' => 0,
236 );
237
238 $this->pdf->write1DBarcode($data['current_value'], 'C128', $xAlign, $y + $this->pdf->height - 10, '',
239 12, 0.4, $style, 'B');
240 }
241 else {
242 // qr code position
243 $xAlign = $x;
244
245 switch ($formattedRow['barcode']['alignment']) {
246 case 'L':
247 $xAlign += -8;
248 break;
249 case 'R':
250 $xAlign += 63;
251 break;
252 case 'C':
253 $xAlign += 29;
254 break;
255 }
256
257 $style = array(
258 'border' => false,
259 'hpadding' => 13.5,
260 'vpadding' => 'auto',
261 'fgcolor' => array(0,0,0),
262 'bgcolor' => false,
263 'position' => '',
264 );
265
266 $this->pdf->write2DBarcode($data['current_value'], 'QRCODE,Q', $xAlign, $y + $this->pdf->height - 23, 30,
267 30, $style, 'B');
268 }
7fd8017a 269 }
f4e94ab8
KJ
270 }
271
272 /**
273 * Helper function to print images
274 * @param string $img image url
275 *
276 * @return void
277 * @access public
278 */
e135f2b5 279 function printImage($img, $x = '', $y = '') {
1b1d29d8
KJ
280 if (!$x) {
281 $x = $this->pdf->GetAbsX();
282 }
283
284 if (!$y) {
285 $y = $this->pdf->GetY();
286 }
f4e94ab8
KJ
287
288 $this->imgRes = 300;
289
290 if ($img) {
291 $imgsize = getimagesize($img);
292 // mm
293 $f = $this->imgRes / 25.4;
294 $w = $imgsize[0] / $f;
295 $h = $imgsize[1] / $f;
1b1d29d8
KJ
296 $this->pdf->Image($img, $x, $y, $w, $h, '', '', '', FALSE, 72, '', FALSE,
297 FALSE, $this->debug, FALSE, FALSE, FALSE);
f4e94ab8
KJ
298 }
299 $this->pdf->SetXY($x, $y);
300 }
42c62ec1
KJ
301}
302