Merge pull request #1748 from kurund/CRM-13329
[civicrm-core.git] / CRM / Badge / BAO / Badge.php
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 */
39 class CRM_Badge_BAO_Badge {
40
41 public $debug = FALSE;
42
43 public $border = 0;
44
45 /**
46 * This function is called to create name label pdf
47 *
48 * @param array $participants associated array with participant info
49 * @param array $layoutInfo associated array which contains meta data about format/layout
50 *
51 * @return void
52 * @access public
53 */
54 public function createLabels(&$participants, &$layoutInfo) {
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();
60 $this->pdf->SetGenerator($this, "generateLabel");
61
62 // this is very useful for debugging, by default set to FALSE
63 if ($this->debug) {
64 $this->border = "LTRB";
65 }
66
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) {
85 $formattedRow = array('labelFormat' => $layout['label_format_name']);
86
87 if (CRM_Utils_Array::value('rowElements', $layout['data'])) {
88 foreach ($layout['data']['rowElements'] as $key => $element) {
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, "%B %E%f");
95 }
96 }
97
98 $formattedRow['token'][$key] = array(
99 'value' => $value,
100 'font_name' => $layout['data']['font_name'][$key],
101 'font_size' => $layout['data']['font_size'][$key],
102 'font_style' => $layout['data']['font_style'][$key],
103 'text_alignment' => $layout['data']['text_alignment'][$key],
104 'token' => $layout['data']['token'][$key],
105 );
106 }
107 }
108
109 if (CRM_Utils_Array::value('image_1', $layout['data'])) {
110 $formattedRow['image_1'] = $layout['data']['image_1'];
111 }
112 if (CRM_Utils_Array::value('width_image_1', $layout['data'])) {
113 $formattedRow['width_image_1'] = $layout['data']['width_image_1'];
114 }
115 if (CRM_Utils_Array::value('height_image_1', $layout['data'])) {
116 $formattedRow['height_image_1'] = $layout['data']['height_image_1'];
117 }
118
119 if (CRM_Utils_Array::value('image_2', $layout['data'])) {
120 $formattedRow['image_2'] = $layout['data']['image_2'];
121 }
122 if (CRM_Utils_Array::value('width_image_2', $layout['data'])) {
123 $formattedRow['width_image_2'] = $layout['data']['width_image_2'];
124 }
125 if (CRM_Utils_Array::value('height_image_2', $layout['data'])) {
126 $formattedRow['height_image_2'] = $layout['data']['height_image_2'];
127 }
128
129 if (CRM_Utils_Array::value('add_barcode', $layout['data'])) {
130 $formattedRow['barcode'] = array(
131 'alignment' => $layout['data']['barcode_alignment'],
132 'type' => $layout['data']['barcode_type'],
133 );
134 }
135
136 // finally assign all the row values, so that we can use it for barcode etc
137 $formattedRow['values'] = $row;
138
139 return $formattedRow;
140 }
141
142 public function generateLabel($formattedRow) {
143 switch ($formattedRow['labelFormat']) {
144 case 'A6 Badge Portrait 150x106':
145 case 'Hanging Badge 3-3/4" x 4-3"/4':
146 self::labelCreator($formattedRow, 5);
147 break;
148 case 'Avery 5395':
149 default:
150 self::labelCreator($formattedRow);
151 break;
152 }
153 }
154
155 public function labelCreator(&$formattedRow, $cellspacing = 0) {
156 $this->lMarginLogo = 18;
157 $this->tMarginName = 20;
158
159 $x = $this->pdf->GetAbsX();
160 $y = $this->pdf->getY();
161
162 $startOffset = 0;
163 if (CRM_Utils_Array::value('image_1', $formattedRow)) {
164 $this->printImage($formattedRow['image_1'], NULL, NULL, CRM_Utils_Array::value('width_image_1', $formattedRow),
165 CRM_Utils_Array::value('height_image_1', $formattedRow));
166 }
167
168 if (CRM_Utils_Array::value('image_2', $formattedRow)) {
169 $this->printImage($formattedRow['image_2'], $x + 68, NULL, CRM_Utils_Array::value('width_image_2', $formattedRow),
170 CRM_Utils_Array::value('height_image_2', $formattedRow));
171 }
172
173 if ((CRM_Utils_Array::value('height_image_1', $formattedRow) >
174 CRM_Utils_Array::value('height_image_2', $formattedRow)) &&
175 CRM_Utils_Array::value('height_image_1', $formattedRow)) {
176 $startOffset = CRM_Utils_Array::value('height_image_1', $formattedRow);
177 }
178 elseif (CRM_Utils_Array::value('height_image_2', $formattedRow)) {
179 $startOffset = CRM_Utils_Array::value('height_image_2', $formattedRow);
180 }
181
182 $this->pdf->SetLineStyle(array(
183 'width' => 0.1,
184 'cap' => 'round',
185 'join' => 'round',
186 'dash' => '2,2',
187 'color' => array(0, 0, 200)
188 ));
189
190 $rowCount = CRM_Badge_Form_Layout::FIELD_ROWCOUNT;
191 for ($i = 1; $i <= $rowCount; $i++) {
192 if (!empty($formattedRow['token'][$i]['token'])) {
193 $value = '';
194 if ($formattedRow['token'][$i]['token'] != 'spacer') {
195 $value = $formattedRow['token'][$i]['value'];
196 }
197
198 $offset = $this->pdf->getY() + $startOffset + $cellspacing;
199
200 $this->pdf->SetFont($formattedRow['token'][$i]['font_name'], $formattedRow['token'][$i]['font_style'],
201 $formattedRow['token'][$i]['font_size']);
202 $this->pdf->MultiCell($this->pdf->width, 0, $value,
203 $this->border, $formattedRow['token'][$i]['text_alignment'], 0, 1, $x, $offset);
204
205 // set this to zero so that it is added only for first element
206 $startOffset = 0;
207 }
208 }
209
210 if (CRM_Utils_Array::value('barcode', $formattedRow)) {
211 $data = $formattedRow['values'];
212
213 if ($formattedRow['barcode']['type'] == 'barcode') {
214 $data['current_value'] =
215 $formattedRow['values']['contact_id'] . '-' . $formattedRow['values']['participant_id'];
216 }
217 else {
218 // view participant url
219 $data['current_value'] = CRM_Utils_System::url('civicrm/contact/view/participant',
220 'action=view&reset=1&cid=' . $formattedRow['values']['contact_id'] . '&id='
221 . $formattedRow['values']['participant_id'],
222 TRUE,
223 NULL,
224 FALSE
225 );
226 }
227
228 // call hook alterBarcode
229 CRM_Utils_Hook::alterBarcode($data, $formattedRow['barcode']['type']);
230
231 if ($formattedRow['barcode']['type'] == 'barcode') {
232 // barcode position
233 $xAlign = $x;
234
235 switch ($formattedRow['barcode']['alignment']) {
236 case 'L':
237 $xAlign += -14;
238 break;
239 case 'R':
240 $xAlign += 32;
241 break;
242 case 'C':
243 $xAlign += 9;
244 break;
245 }
246
247 $style = array(
248 'position' => '',
249 'align' => '',
250 'stretch' => FALSE,
251 'fitwidth' => TRUE,
252 'cellfitalign' => '',
253 'border' => FALSE,
254 'hpadding' => 13.5,
255 'vpadding' => 'auto',
256 'fgcolor' => array(0, 0, 0),
257 'bgcolor' => FALSE,
258 'text' => FALSE,
259 'font' => 'helvetica',
260 'fontsize' => 8,
261 'stretchtext' => 0,
262 );
263
264 $this->pdf->write1DBarcode($data['current_value'], 'C128', $xAlign, $y + $this->pdf->height - 10, '70',
265 12, 0.4, $style, 'B');
266 }
267 else {
268 // qr code position
269 $xAlign = $x;
270
271 switch ($formattedRow['barcode']['alignment']) {
272 case 'L':
273 $xAlign += -5;
274 break;
275 case 'R':
276 $xAlign += 61;
277 break;
278 case 'C':
279 $xAlign += 29;
280 break;
281 }
282
283 $style = array(
284 'border' => false,
285 'hpadding' => 13.5,
286 'vpadding' => 'auto',
287 'fgcolor' => array(0,0,0),
288 'bgcolor' => false,
289 'position' => '',
290 );
291
292 $this->pdf->write2DBarcode($data['current_value'], 'QRCODE,H', $xAlign, $y + $this->pdf->height - 26, 30,
293 30, $style, 'B');
294 }
295 }
296 }
297
298 /**
299 * Helper function to print images
300 * @param string $img image url
301 *
302 * @return void
303 * @access public
304 */
305 function printImage($img, $x = '', $y = '', $w = NULL, $h = NULL) {
306 if (!$x) {
307 $x = $this->pdf->GetAbsX();
308 }
309
310 if (!$y) {
311 $y = $this->pdf->GetY();
312 }
313
314 $this->imgRes = 300;
315
316 if ($img) {
317 list($w, $h) = self::getImageProperties($img, $this->imgRes, $w, $h);
318 $this->pdf->Image($img, $x, $y, $w, $h, '', '', '', FALSE, 72, '', FALSE,
319 FALSE, $this->debug, FALSE, FALSE, FALSE);
320 }
321 $this->pdf->SetXY($x, $y);
322 }
323
324 static function getImageProperties($img, $imgRes = 300, $w = NULL, $h = NULL) {
325 $imgsize = getimagesize($img);
326 $f = $imgRes / 25.4;
327 $w = !empty($w) ? $w : $imgsize[0] / $f;
328 $h = !empty($h) ? $h : $imgsize[1] / $f;
329 return array($w, $h);
330 }
331
332 /**
333 * function to build badges parameters before actually creating badges.
334 *
335 * @param array $params associated array of submitted values
336 * @params object $form form/controller object
337 *
338 * @return void
339 * @access public
340 * @static
341 */
342 public static function buildBadges(&$params, &$form) {
343 // get name badge layout info
344 $layoutInfo = CRM_Badge_BAO_Layout::buildLayout($params);
345
346 // spit / get actual field names from token
347 $returnProperties = array();
348 if (!empty($layoutInfo['data']['token'])) {
349 foreach ($layoutInfo['data']['token'] as $index => $value) {
350 $element = '';
351 if ($value) {
352 $token = CRM_Utils_Token::getTokens($value);
353 if (key($token) == 'contact') {
354 $element = $token['contact'][0];
355 }
356 elseif (key($token) == 'event') {
357 $element = $token['event'][0];
358 //FIX ME - we need to standardize event token names
359 if (substr($element, 0, 6) != 'event_') {
360 $element = 'event_' . $element;
361 }
362 }
363 elseif (key($token) == 'participant') {
364 $element = $token['participant'][0];
365 }
366
367 // build returnproperties for query
368 $returnProperties[$element] = 1;
369 }
370
371 // add actual field name to row element
372 $layoutInfo['data']['rowElements'][$index] = $element;
373 }
374 }
375
376 // add additional required fields for query execution
377 $additionalFields = array('participant_register_date', 'participant_id', 'event_id', 'contact_id');
378 foreach ($additionalFields as $field) {
379 $returnProperties[$field] = 1;
380 }
381
382 if ($form->_single) {
383 $queryParams = NULL;
384 }
385 else {
386 $queryParams = $form->get('queryParams');
387 }
388
389 $query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, NULL, FALSE, FALSE,
390 CRM_Contact_BAO_Query::MODE_EVENT
391 );
392
393 list($select, $from, $where, $having) = $query->query();
394 if (empty($where)) {
395 $where = "WHERE {$form->_componentClause}";
396 }
397 else {
398 $where .= " AND {$form->_componentClause}";
399 }
400
401 $sortOrder = NULL;
402 if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
403 $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
404 if (!empty($sortOrder)) {
405 $sortOrder = " ORDER BY $sortOrder";
406 }
407 }
408 $queryString = "$select $from $where $having $sortOrder";
409
410 $dao = CRM_Core_DAO::executeQuery($queryString);
411 $rows = array();
412 while ($dao->fetch()) {
413 $rows[$dao->participant_id] = array();
414 foreach ($returnProperties as $key => $dontCare) {
415 $rows[$dao->participant_id][$key] = isset($dao->$key) ? $dao->$key : NULL;
416 }
417 }
418
419 $eventBadgeClass = new CRM_Badge_BAO_Badge();
420 $eventBadgeClass->createLabels($rows, $layoutInfo);
421 }
422 }
423