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