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