Merge pull request #4606 from johanv/CRM-15636-price_set_event_and_contribution
[civicrm-core.git] / CRM / Badge / BAO / Badge.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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-2014
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 */
53 public function createLabels(&$participants, &$layoutInfo) {
54 $this->pdf = new CRM_Utils_PDF_Label($layoutInfo['format'], 'mm');
55 $this->pdf->Open();
56 $this->pdf->setPrintHeader(FALSE);
57 $this->pdf->setPrintFooter(FALSE);
58 $this->pdf->AddPage();
59 $this->pdf->SetGenerator($this, "generateLabel");
60
61 // this is very useful for debugging, by default set to FALSE
62 if ($this->debug) {
63 $this->border = "LTRB";
64 }
65
66 foreach ($participants as $participant) {
67 $formattedRow = self::formatLabel($participant, $layoutInfo);
68 $this->pdf->AddPdfLabel($formattedRow);
69 }
70
71 $this->pdf->Output(CRM_Utils_String::munge($layoutInfo['title'], '_', 64) . '.pdf', 'D');
72 CRM_Utils_System::civiExit(1);
73 }
74
75 /**
76 * Funtion to create structure and add meta data according to layout
77 *
78 * @param array $row row element that needs to be formatted
79 * @param array $layout layout meta data
80 *
81 * @return array $formattedRow row with meta data
82 */
83 public static function formatLabel(&$row, &$layout) {
84 $formattedRow = array('labelFormat' => $layout['label_format_name']);
85 $formattedRow['labelTitle'] = $layout['title'];
86 $formattedRow['labelId'] = $layout['id'];
87
88
89 if (!empty($layout['data']['rowElements'])) {
90 foreach ($layout['data']['rowElements'] as $key => $element) {
91 $value = '';
92 if ($element) {
93 $value = $row[$element];
94 // hack to fix date field display format
95 if (strpos($element, '_date')) {
96 $value = CRM_Utils_Date::customFormat($value, "%B %E%f");
97 }
98 }
99
100 $formattedRow['token'][$key] = array(
101 'value' => $value,
102 'font_name' => $layout['data']['font_name'][$key],
103 'font_size' => $layout['data']['font_size'][$key],
104 'font_style' => $layout['data']['font_style'][$key],
105 'text_alignment' => $layout['data']['text_alignment'][$key],
106 'token' => $layout['data']['token'][$key],
107 );
108 }
109 }
110
111 if (!empty($layout['data']['image_1'])) {
112 $formattedRow['image_1'] = $layout['data']['image_1'];
113 }
114 if (!empty($layout['data']['width_image_1'])) {
115 $formattedRow['width_image_1'] = $layout['data']['width_image_1'];
116 }
117 if (!empty($layout['data']['height_image_1'])) {
118 $formattedRow['height_image_1'] = $layout['data']['height_image_1'];
119 }
120
121 if (!empty($layout['data']['image_2'])) {
122 $formattedRow['image_2'] = $layout['data']['image_2'];
123 }
124 if (!empty($layout['data']['width_image_2'])) {
125 $formattedRow['width_image_2'] = $layout['data']['width_image_2'];
126 }
127 if (!empty($layout['data']['height_image_2'])) {
128 $formattedRow['height_image_2'] = $layout['data']['height_image_2'];
129 }
130 if (!empty($row['image_URL']) && !empty($layout['data']['show_participant_image'])) {
131 $formattedRow['participant_image'] = $row['image_URL'];
132 }
133 if (!empty($layout['data']['width_participant_image'])) {
134 $formattedRow['width_participant_image'] = $layout['data']['width_participant_image'];
135 }
136 if (!empty($layout['data']['height_participant_image'])) {
137 $formattedRow['height_participant_image'] = $layout['data']['height_participant_image'];
138 }
139 if (!empty($layout['data']['alignment_participant_image'])) {
140 $formattedRow['alignment_participant_image'] = $layout['data']['alignment_participant_image'];
141 }
142
143 if (!empty($layout['data']['add_barcode'])) {
144 $formattedRow['barcode'] = array(
145 'alignment' => $layout['data']['barcode_alignment'],
146 'type' => $layout['data']['barcode_type'],
147 );
148 }
149
150 // finally assign all the row values, so that we can use it for barcode etc
151 $formattedRow['values'] = $row;
152
153 return $formattedRow;
154 }
155
156 /**
157 * @param $formattedRow
158 */
159 public function generateLabel($formattedRow) {
160 switch ($formattedRow['labelFormat']) {
161 case 'A6 Badge Portrait 150x106':
162 case 'Hanging Badge 3-3/4" x 4-3"/4':
163 self::labelCreator($formattedRow, 5);
164 break;
165 case 'Avery 5395':
166 default:
167 self::labelCreator($formattedRow);
168 break;
169 }
170 }
171
172 /**
173 * @param $formattedRow
174 * @param int $cellspacing
175 */
176 public function labelCreator(&$formattedRow, $cellspacing = 0) {
177 $this->lMarginLogo = 18;
178 $this->tMarginName = 20;
179
180 $x = $this->pdf->GetAbsX();
181 $y = $this->pdf->getY();
182
183 //call hook alterBadge
184 CRM_Utils_Hook::alterBadge($formattedRow['labelTitle'], $this, $formattedRow, $formattedRow['values']);
185
186 $startOffset = 0;
187 if (!empty($formattedRow['image_1'])) {
188 $this->printImage($formattedRow['image_1'], NULL, NULL, CRM_Utils_Array::value('width_image_1', $formattedRow),
189 CRM_Utils_Array::value('height_image_1', $formattedRow));
190 }
191
192 if (!empty($formattedRow['image_2'])) {
193 $this->printImage($formattedRow['image_2'], $x + 68, NULL, CRM_Utils_Array::value('width_image_2', $formattedRow),
194 CRM_Utils_Array::value('height_image_2', $formattedRow));
195 }
196
197 if ((CRM_Utils_Array::value('height_image_1', $formattedRow) >
198 CRM_Utils_Array::value('height_image_2', $formattedRow)) && !empty($formattedRow['height_image_1'])) {
199 $startOffset = CRM_Utils_Array::value('height_image_1', $formattedRow);
200 }
201 elseif (!empty($formattedRow['height_image_2'])) {
202 $startOffset = CRM_Utils_Array::value('height_image_2', $formattedRow);
203 }
204
205 if (CRM_Utils_Array::value('participant_image', $formattedRow)) {
206 $imageAlign = 0;
207 switch (CRM_Utils_Array::value('alignment_participant_image', $formattedRow)) {
208 case 'R':
209 $imageAlign = 68;
210 break;
211 case 'L':
212 $imageAlign = 0;
213 break;
214 default:
215 break;
216 }
217 $this->pdf->Image($formattedRow['participant_image'], $x + $imageAlign, $y + $startOffset, CRM_Utils_Array::value('width_participant_image', $formattedRow), CRM_Utils_Array::value('height_participant_image', $formattedRow));
218 if ($startOffset == NULL && CRM_Utils_Array::value('height_participant_image', $formattedRow)) {
219 $startOffset = CRM_Utils_Array::value('height_participant_image', $formattedRow);
220 }
221 }
222
223 $this->pdf->SetLineStyle(array(
224 'width' => 0.1,
225 'cap' => 'round',
226 'join' => 'round',
227 'dash' => '2,2',
228 'color' => array(0, 0, 200)
229 ));
230
231 $rowCount = CRM_Badge_Form_Layout::FIELD_ROWCOUNT;
232 for ($i = 1; $i <= $rowCount; $i++) {
233 if (!empty($formattedRow['token'][$i]['token'])) {
234 $value = '';
235 if ($formattedRow['token'][$i]['token'] != 'spacer') {
236 $value = $formattedRow['token'][$i]['value'];
237 }
238
239 $xAlign = $x;
240 $rowWidth = $this->pdf->width;
241 if (!empty($formattedRow['participant_image']) && !empty($formattedRow['width_participant_image'])) {
242 $rowWidth = $this->pdf->width - $formattedRow['width_participant_image'];
243 if ($formattedRow['alignment_participant_image'] == 'L') {
244 $xAlign = $x + $formattedRow['width_participant_image'] + $imageAlign;
245 }
246 }
247 $offset = $this->pdf->getY() + $startOffset + $cellspacing;
248
249 $this->pdf->SetFont($formattedRow['token'][$i]['font_name'], $formattedRow['token'][$i]['font_style'],
250 $formattedRow['token'][$i]['font_size']);
251 $this->pdf->MultiCell($rowWidth, 0, $value,
252 $this->border, $formattedRow['token'][$i]['text_alignment'], 0, 1, $xAlign, $offset);
253
254 // set this to zero so that it is added only for first element
255 $startOffset = 0;
256 }
257 }
258
259 if (!empty($formattedRow['barcode'])) {
260 $data = $formattedRow['values'];
261
262 if ($formattedRow['barcode']['type'] == 'barcode') {
263 $data['current_value'] =
264 $formattedRow['values']['contact_id'] . '-' . $formattedRow['values']['participant_id'];
265 }
266 else {
267 // view participant url
268 $data['current_value'] = CRM_Utils_System::url('civicrm/contact/view/participant',
269 'action=view&reset=1&cid=' . $formattedRow['values']['contact_id'] . '&id='
270 . $formattedRow['values']['participant_id'],
271 TRUE,
272 NULL,
273 FALSE
274 );
275 }
276
277 // call hook alterBarcode
278 CRM_Utils_Hook::alterBarcode($data, $formattedRow['barcode']['type']);
279
280 if ($formattedRow['barcode']['type'] == 'barcode') {
281 // barcode position
282 $xAlign = $x;
283
284 switch ($formattedRow['barcode']['alignment']) {
285 case 'L':
286 $xAlign += -14;
287 break;
288 case 'R':
289 $xAlign += 27;
290 break;
291 case 'C':
292 $xAlign += 9;
293 break;
294 }
295
296 $style = array(
297 'position' => '',
298 'align' => '',
299 'stretch' => FALSE,
300 'fitwidth' => TRUE,
301 'cellfitalign' => '',
302 'border' => FALSE,
303 'hpadding' => 13.5,
304 'vpadding' => 'auto',
305 'fgcolor' => array(0, 0, 0),
306 'bgcolor' => FALSE,
307 'text' => FALSE,
308 'font' => 'helvetica',
309 'fontsize' => 8,
310 'stretchtext' => 0,
311 );
312
313 $this->pdf->write1DBarcode($data['current_value'], 'C128', $xAlign, $y + $this->pdf->height - 10, '70',
314 12, 0.4, $style, 'B');
315 }
316 else {
317 // qr code position
318 $xAlign = $x;
319
320 switch ($formattedRow['barcode']['alignment']) {
321 case 'L':
322 $xAlign += -5;
323 break;
324 case 'R':
325 $xAlign += 56;
326 break;
327 case 'C':
328 $xAlign += 29;
329 break;
330 }
331
332 $style = array(
333 'border' => false,
334 'hpadding' => 13.5,
335 'vpadding' => 'auto',
336 'fgcolor' => array(0,0,0),
337 'bgcolor' => false,
338 'position' => '',
339 );
340
341 $this->pdf->write2DBarcode($data['current_value'], 'QRCODE,H', $xAlign, $y + $this->pdf->height - 26, 30,
342 30, $style, 'B');
343 }
344 }
345 }
346
347 /**
348 * Helper function to print images
349 *
350 * @param string $img image url
351 *
352 * @param string $x
353 * @param string $y
354 * @param null $w
355 * @param null $h
356 *
357 * @return void
358 */
359 public function printImage($img, $x = '', $y = '', $w = NULL, $h = NULL) {
360 if (!$x) {
361 $x = $this->pdf->GetAbsX();
362 }
363
364 if (!$y) {
365 $y = $this->pdf->GetY();
366 }
367
368 $this->imgRes = 300;
369
370 if ($img) {
371 list($w, $h) = self::getImageProperties($img, $this->imgRes, $w, $h);
372 $this->pdf->Image($img, $x, $y, $w, $h, '', '', '', FALSE, 72, '', FALSE,
373 FALSE, $this->debug, FALSE, FALSE, FALSE);
374 }
375 $this->pdf->SetXY($x, $y);
376 }
377
378 /**
379 * @param $img
380 * @param int $imgRes
381 * @param null $w
382 * @param null $h
383 *
384 * @return array
385 */
386 public static function getImageProperties($img, $imgRes = 300, $w = NULL, $h = NULL) {
387 $imgsize = getimagesize($img);
388 $f = $imgRes / 25.4;
389 $w = !empty($w) ? $w : $imgsize[0] / $f;
390 $h = !empty($h) ? $h : $imgsize[1] / $f;
391 return array($w, $h);
392 }
393
394 /**
395 * Build badges parameters before actually creating badges.
396 *
397 * @param array $params associated array of submitted values
398 * @param CRM_Core_Form $form
399 *
400 * @return void
401 * @static
402 */
403 public static function buildBadges(&$params, &$form) {
404 // get name badge layout info
405 $layoutInfo = CRM_Badge_BAO_Layout::buildLayout($params);
406
407 // split/get actual field names from token and individual contact image URLs
408 $returnProperties = array();
409 if (!empty($layoutInfo['data']['token'])) {
410 foreach ($layoutInfo['data']['token'] as $index => $value) {
411 $element = '';
412 if ($value) {
413 $token = CRM_Utils_Token::getTokens($value);
414 if (key($token) == 'contact') {
415 $element = $token['contact'][0];
416 }
417 elseif (key($token) == 'event') {
418 $element = $token['event'][0];
419 //FIX ME - we need to standardize event token names
420 if (substr($element, 0, 6) != 'event_') {
421 $element = 'event_' . $element;
422 }
423 }
424 elseif (key($token) == 'participant') {
425 $element = $token['participant'][0];
426 }
427
428 // build returnproperties for query
429 $returnProperties[$element] = 1;
430 }
431
432 // add actual field name to row element
433 $layoutInfo['data']['rowElements'][$index] = $element;
434 }
435 }
436
437 // add additional required fields for query execution
438 $additionalFields = array('participant_register_date', 'participant_id', 'event_id', 'contact_id', 'image_URL');
439 foreach ($additionalFields as $field) {
440 $returnProperties[$field] = 1;
441 }
442
443 if ($form->_single) {
444 $queryParams = NULL;
445 }
446 else {
447 $queryParams = $form->get('queryParams');
448 }
449
450 $query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, NULL, FALSE, FALSE,
451 CRM_Contact_BAO_Query::MODE_EVENT
452 );
453
454 list($select, $from, $where, $having) = $query->query();
455 if (empty($where)) {
456 $where = "WHERE {$form->_componentClause}";
457 }
458 else {
459 $where .= " AND {$form->_componentClause}";
460 }
461
462 $sortOrder = NULL;
463 if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
464 $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
465 if (!empty($sortOrder)) {
466 $sortOrder = " ORDER BY $sortOrder";
467 }
468 }
469 $queryString = "$select $from $where $having $sortOrder";
470
471 $dao = CRM_Core_DAO::executeQuery($queryString);
472 $rows = array();
473 while ($dao->fetch()) {
474 $query->convertToPseudoNames($dao);
475 $rows[$dao->participant_id] = array();
476 foreach ($returnProperties as $key => $dontCare) {
477 $rows[$dao->participant_id][$key] = isset($dao->$key) ? $dao->$key : NULL;
478 }
479 }
480
481 $eventBadgeClass = new CRM_Badge_BAO_Badge();
482 $eventBadgeClass->createLabels($rows, $layoutInfo);
483 }
484 }