Commit | Line | Data |
---|---|---|
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 | 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 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')) { | |
1a502055 | 94 | $value = CRM_Utils_Date::customFormat($value, "%B %E%f"); |
8d5579e2 | 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], | |
1c07f935 | 102 | 'font_style' => $layout['data']['font_style'][$key], |
42c62ec1 | 103 | 'text_alignment' => $layout['data']['text_alignment'][$key], |
50f52554 | 104 | 'token' => $layout['data']['token'][$key], |
42c62ec1 KJ |
105 | ); |
106 | } | |
107 | } | |
108 | ||
109 | if (CRM_Utils_Array::value('image_1', $layout['data'])) { | |
110 | $formattedRow['image_1'] = $layout['data']['image_1']; | |
111 | } | |
07d113b7 PJ |
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 | } | |
42c62ec1 KJ |
118 | |
119 | if (CRM_Utils_Array::value('image_2', $layout['data'])) { | |
120 | $formattedRow['image_2'] = $layout['data']['image_2']; | |
121 | } | |
07d113b7 PJ |
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 | } | |
42c62ec1 KJ |
128 | |
129 | if (CRM_Utils_Array::value('add_barcode', $layout['data'])) { | |
f38395f7 KJ |
130 | $formattedRow['barcode'] = array( |
131 | 'alignment' => $layout['data']['barcode_alignment'], | |
132 | 'type' => $layout['data']['barcode_type'], | |
133 | ); | |
42c62ec1 KJ |
134 | } |
135 | ||
7fd8017a KJ |
136 | // finally assign all the row values, so that we can use it for barcode etc |
137 | $formattedRow['values'] = $row; | |
138 | ||
42c62ec1 KJ |
139 | return $formattedRow; |
140 | } | |
f4e94ab8 KJ |
141 | |
142 | public function generateLabel($formattedRow) { | |
143 | switch ($formattedRow['labelFormat']) { | |
8b5dac1e | 144 | case 'A6 Badge Portrait 150x106': |
145 | case 'Hanging Badge 3-3/4" x 4-3"/4': | |
146 | self::labelCreator($formattedRow, 5); | |
147 | break; | |
1b1d29d8 | 148 | case 'Avery 5395': |
8e9fd9e4 | 149 | default: |
8b5dac1e | 150 | self::labelCreator($formattedRow); |
f4e94ab8 KJ |
151 | break; |
152 | } | |
153 | } | |
154 | ||
8b5dac1e | 155 | public function labelCreator(&$formattedRow, $cellspacing = 0) { |
09651125 | 156 | $this->lMarginLogo = 18; |
f4e94ab8 KJ |
157 | $this->tMarginName = 20; |
158 | ||
159 | $x = $this->pdf->GetAbsX(); | |
b246735d | 160 | $y = $this->pdf->getY(); |
f4e94ab8 | 161 | |
d67e7cf5 | 162 | $startOffset = 0; |
8d5579e2 | 163 | if (CRM_Utils_Array::value('image_1', $formattedRow)) { |
07d113b7 PJ |
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)); | |
8d5579e2 KJ |
166 | } |
167 | ||
168 | if (CRM_Utils_Array::value('image_2', $formattedRow)) { | |
07d113b7 PJ |
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)); | |
d67e7cf5 | 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); | |
8d5579e2 | 180 | } |
f4e94ab8 | 181 | |
e135f2b5 KJ |
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 | )); | |
f4e94ab8 | 189 | |
50f52554 | 190 | $rowCount = CRM_Badge_Form_Layout::FIELD_ROWCOUNT; |
d67e7cf5 | 191 | for ($i = 1; $i <= $rowCount; $i++) { |
50f52554 | 192 | if (!empty($formattedRow['token'][$i]['token'])) { |
193 | $value = ''; | |
194 | if ($formattedRow['token'][$i]['token'] != 'spacer') { | |
195 | $value = $formattedRow['token'][$i]['value']; | |
196 | } | |
1b1d29d8 | 197 | |
8b5dac1e | 198 | $offset = $this->pdf->getY() + $startOffset + $cellspacing; |
d67e7cf5 | 199 | |
50f52554 | 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, | |
d67e7cf5 | 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; | |
50f52554 | 207 | } |
208 | } | |
7fd8017a KJ |
209 | |
210 | if (CRM_Utils_Array::value('barcode', $formattedRow)) { | |
f38395f7 | 211 | $data = $formattedRow['values']; |
7fd8017a | 212 | |
f38395f7 KJ |
213 | if ($formattedRow['barcode']['type'] == 'barcode') { |
214 | $data['current_value'] = | |
215 | $formattedRow['values']['contact_id'] . '-' . $formattedRow['values']['participant_id']; | |
8afbd311 | 216 | } |
f38395f7 KJ |
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 | ); | |
8afbd311 KJ |
226 | } |
227 | ||
9399901d | 228 | // call hook alterBarcode |
f38395f7 KJ |
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': | |
d3b0bbe1 | 237 | $xAlign += -14; |
f38395f7 KJ |
238 | break; |
239 | case 'R': | |
d3b0bbe1 | 240 | $xAlign += 32; |
f38395f7 KJ |
241 | break; |
242 | case 'C': | |
d3b0bbe1 | 243 | $xAlign += 9; |
f38395f7 KJ |
244 | break; |
245 | } | |
9399901d | 246 | |
f38395f7 KJ |
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 | ||
d3b0bbe1 | 264 | $this->pdf->write1DBarcode($data['current_value'], 'C128', $xAlign, $y + $this->pdf->height - 10, '70', |
f38395f7 KJ |
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': | |
d3b0bbe1 | 273 | $xAlign += -5; |
f38395f7 KJ |
274 | break; |
275 | case 'R': | |
d3b0bbe1 | 276 | $xAlign += 61; |
f38395f7 KJ |
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 | ||
d3b0bbe1 KJ |
292 | $this->pdf->write2DBarcode($data['current_value'], 'QRCODE,H', $xAlign, $y + $this->pdf->height - 26, 30, |
293 | 30, $style, 'B'); | |
f38395f7 | 294 | } |
7fd8017a | 295 | } |
f4e94ab8 KJ |
296 | } |
297 | ||
298 | /** | |
299 | * Helper function to print images | |
300 | * @param string $img image url | |
301 | * | |
302 | * @return void | |
303 | * @access public | |
304 | */ | |
07d113b7 | 305 | function printImage($img, $x = '', $y = '', $w = NULL, $h = NULL) { |
1b1d29d8 KJ |
306 | if (!$x) { |
307 | $x = $this->pdf->GetAbsX(); | |
308 | } | |
309 | ||
310 | if (!$y) { | |
311 | $y = $this->pdf->GetY(); | |
312 | } | |
f4e94ab8 KJ |
313 | |
314 | $this->imgRes = 300; | |
315 | ||
316 | if ($img) { | |
07d113b7 | 317 | list($w, $h) = self::getImageProperties($img, $this->imgRes, $w, $h); |
1b1d29d8 KJ |
318 | $this->pdf->Image($img, $x, $y, $w, $h, '', '', '', FALSE, 72, '', FALSE, |
319 | FALSE, $this->debug, FALSE, FALSE, FALSE); | |
f4e94ab8 KJ |
320 | } |
321 | $this->pdf->SetXY($x, $y); | |
322 | } | |
6d4a64c8 | 323 | |
07d113b7 PJ |
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 | ||
6d4a64c8 KJ |
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 | |
4cb8f020 | 359 | if (substr($element, 0, 6) != 'event_') { |
6d4a64c8 KJ |
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 | } | |
42c62ec1 KJ |
422 | } |
423 |