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], | |
102 | 'text_alignment' => $layout['data']['text_alignment'][$key], | |
103 | ); | |
104 | } | |
105 | } | |
106 | ||
107 | if (CRM_Utils_Array::value('image_1', $layout['data'])) { | |
108 | $formattedRow['image_1'] = $layout['data']['image_1']; | |
109 | } | |
110 | ||
111 | if (CRM_Utils_Array::value('image_2', $layout['data'])) { | |
112 | $formattedRow['image_2'] = $layout['data']['image_2']; | |
113 | } | |
114 | ||
115 | if (CRM_Utils_Array::value('add_barcode', $layout['data'])) { | |
f38395f7 KJ |
116 | $formattedRow['barcode'] = array( |
117 | 'alignment' => $layout['data']['barcode_alignment'], | |
118 | 'type' => $layout['data']['barcode_type'], | |
119 | ); | |
42c62ec1 KJ |
120 | } |
121 | ||
7fd8017a KJ |
122 | // finally assign all the row values, so that we can use it for barcode etc |
123 | $formattedRow['values'] = $row; | |
124 | ||
42c62ec1 KJ |
125 | return $formattedRow; |
126 | } | |
f4e94ab8 KJ |
127 | |
128 | public function generateLabel($formattedRow) { | |
129 | switch ($formattedRow['labelFormat']) { | |
1b1d29d8 | 130 | case 'Avery 5395': |
8e9fd9e4 | 131 | default: |
f4e94ab8 KJ |
132 | self::labelAvery5395($formattedRow); |
133 | break; | |
134 | } | |
135 | } | |
136 | ||
137 | public function labelAvery5395(&$formattedRow) { | |
09651125 | 138 | $this->lMarginLogo = 18; |
f4e94ab8 KJ |
139 | $this->tMarginName = 20; |
140 | ||
141 | $x = $this->pdf->GetAbsX(); | |
142 | $y = $this->pdf->GetY(); | |
143 | ||
09651125 | 144 | $titleWidth = $titleLeftMargin = 0; |
8d5579e2 KJ |
145 | if (CRM_Utils_Array::value('image_1', $formattedRow)) { |
146 | $this->printImage($formattedRow['image_1']); | |
09651125 | 147 | $titleWidth = $titleLeftMargin = $this->lMarginLogo; |
8d5579e2 KJ |
148 | } |
149 | ||
09651125 | 150 | $titleRightMargin = 0; |
8d5579e2 | 151 | if (CRM_Utils_Array::value('image_2', $formattedRow)) { |
e135f2b5 | 152 | $this->printImage($formattedRow['image_2'], $x + 68); |
09651125 KJ |
153 | $titleRightMargin = 36; |
154 | $titleWidth = $this->lMarginLogo; | |
8d5579e2 | 155 | } |
f4e94ab8 | 156 | |
e135f2b5 KJ |
157 | $this->pdf->SetLineStyle(array( |
158 | 'width' => 0.1, | |
159 | 'cap' => 'round', | |
160 | 'join' => 'round', | |
161 | 'dash' => '2,2', | |
162 | 'color' => array(0, 0, 200) | |
163 | )); | |
f4e94ab8 | 164 | |
09651125 KJ |
165 | if ($titleLeftMargin && $titleRightMargin) { |
166 | $titleWidth = $titleRightMargin; | |
167 | } | |
168 | ||
fc476655 | 169 | $this->pdf->SetFont($formattedRow['token'][1]['font_name'], '', $formattedRow['token'][1]['font_size']); |
e135f2b5 | 170 | $this->pdf->MultiCell($this->pdf->width - $titleWidth, 0, $formattedRow['token'][1]['value'], |
09651125 | 171 | $this->border, $formattedRow['token'][1]['text_alignment'], 0, 1, $x + $titleLeftMargin, $y); |
f4e94ab8 | 172 | |
fc476655 | 173 | $this->pdf->SetFont($formattedRow['token'][2]['font_name'], '', $formattedRow['token'][2]['font_size']); |
1b1d29d8 KJ |
174 | $this->pdf->MultiCell($this->pdf->width, 10, $formattedRow['token'][2]['value'], |
175 | $this->border, $formattedRow['token'][2]['text_alignment'], 0, 1, $x, $y + $this->tMarginName); | |
f4e94ab8 | 176 | |
fc476655 | 177 | $this->pdf->SetFont($formattedRow['token'][3]['font_name'], '', $formattedRow['token'][3]['font_size']); |
1b1d29d8 KJ |
178 | $this->pdf->MultiCell($this->pdf->width, 0, $formattedRow['token'][3]['value'], |
179 | $this->border, $formattedRow['token'][3]['text_alignment'], 0, 1, $x, $this->pdf->getY()); | |
180 | ||
fc476655 | 181 | $this->pdf->SetFont($formattedRow['token'][4]['font_name'], '', $formattedRow['token'][4]['font_size']); |
ee82c9d7 KJ |
182 | $this->pdf->MultiCell($this->pdf->width, 0, $formattedRow['token'][4]['value'], |
183 | $this->border, $formattedRow['token'][4]['text_alignment'], 0, 1, $x, $y + $this->pdf->height - 5); | |
7fd8017a KJ |
184 | |
185 | if (CRM_Utils_Array::value('barcode', $formattedRow)) { | |
f38395f7 | 186 | $data = $formattedRow['values']; |
7fd8017a | 187 | |
f38395f7 KJ |
188 | if ($formattedRow['barcode']['type'] == 'barcode') { |
189 | $data['current_value'] = | |
190 | $formattedRow['values']['contact_id'] . '-' . $formattedRow['values']['participant_id']; | |
8afbd311 | 191 | } |
f38395f7 KJ |
192 | else { |
193 | // view participant url | |
194 | $data['current_value'] = CRM_Utils_System::url('civicrm/contact/view/participant', | |
195 | 'action=view&reset=1&cid=' . $formattedRow['values']['contact_id'] . '&id=' | |
196 | . $formattedRow['values']['participant_id'], | |
197 | TRUE, | |
198 | NULL, | |
199 | FALSE | |
200 | ); | |
8afbd311 KJ |
201 | } |
202 | ||
9399901d | 203 | // call hook alterBarcode |
f38395f7 KJ |
204 | CRM_Utils_Hook::alterBarcode($data, $formattedRow['barcode']['type']); |
205 | ||
206 | if ($formattedRow['barcode']['type'] == 'barcode') { | |
207 | // barcode position | |
208 | $xAlign = $x; | |
209 | ||
210 | switch ($formattedRow['barcode']['alignment']) { | |
211 | case 'L': | |
d3b0bbe1 | 212 | $xAlign += -14; |
f38395f7 KJ |
213 | break; |
214 | case 'R': | |
d3b0bbe1 | 215 | $xAlign += 32; |
f38395f7 KJ |
216 | break; |
217 | case 'C': | |
d3b0bbe1 | 218 | $xAlign += 9; |
f38395f7 KJ |
219 | break; |
220 | } | |
9399901d | 221 | |
f38395f7 KJ |
222 | $style = array( |
223 | 'position' => '', | |
224 | 'align' => '', | |
225 | 'stretch' => FALSE, | |
226 | 'fitwidth' => TRUE, | |
227 | 'cellfitalign' => '', | |
228 | 'border' => FALSE, | |
229 | 'hpadding' => 13.5, | |
230 | 'vpadding' => 'auto', | |
231 | 'fgcolor' => array(0, 0, 0), | |
232 | 'bgcolor' => FALSE, | |
233 | 'text' => FALSE, | |
234 | 'font' => 'helvetica', | |
235 | 'fontsize' => 8, | |
236 | 'stretchtext' => 0, | |
237 | ); | |
238 | ||
d3b0bbe1 | 239 | $this->pdf->write1DBarcode($data['current_value'], 'C128', $xAlign, $y + $this->pdf->height - 10, '70', |
f38395f7 KJ |
240 | 12, 0.4, $style, 'B'); |
241 | } | |
242 | else { | |
243 | // qr code position | |
244 | $xAlign = $x; | |
245 | ||
246 | switch ($formattedRow['barcode']['alignment']) { | |
247 | case 'L': | |
d3b0bbe1 | 248 | $xAlign += -5; |
f38395f7 KJ |
249 | break; |
250 | case 'R': | |
d3b0bbe1 | 251 | $xAlign += 61; |
f38395f7 KJ |
252 | break; |
253 | case 'C': | |
254 | $xAlign += 29; | |
255 | break; | |
256 | } | |
257 | ||
258 | $style = array( | |
259 | 'border' => false, | |
260 | 'hpadding' => 13.5, | |
261 | 'vpadding' => 'auto', | |
262 | 'fgcolor' => array(0,0,0), | |
263 | 'bgcolor' => false, | |
264 | 'position' => '', | |
265 | ); | |
266 | ||
d3b0bbe1 KJ |
267 | $this->pdf->write2DBarcode($data['current_value'], 'QRCODE,H', $xAlign, $y + $this->pdf->height - 26, 30, |
268 | 30, $style, 'B'); | |
f38395f7 | 269 | } |
7fd8017a | 270 | } |
f4e94ab8 KJ |
271 | } |
272 | ||
273 | /** | |
274 | * Helper function to print images | |
275 | * @param string $img image url | |
276 | * | |
277 | * @return void | |
278 | * @access public | |
279 | */ | |
e135f2b5 | 280 | function printImage($img, $x = '', $y = '') { |
1b1d29d8 KJ |
281 | if (!$x) { |
282 | $x = $this->pdf->GetAbsX(); | |
283 | } | |
284 | ||
285 | if (!$y) { | |
286 | $y = $this->pdf->GetY(); | |
287 | } | |
f4e94ab8 KJ |
288 | |
289 | $this->imgRes = 300; | |
290 | ||
291 | if ($img) { | |
292 | $imgsize = getimagesize($img); | |
293 | // mm | |
294 | $f = $this->imgRes / 25.4; | |
295 | $w = $imgsize[0] / $f; | |
296 | $h = $imgsize[1] / $f; | |
1b1d29d8 KJ |
297 | $this->pdf->Image($img, $x, $y, $w, $h, '', '', '', FALSE, 72, '', FALSE, |
298 | FALSE, $this->debug, FALSE, FALSE, FALSE); | |
f4e94ab8 KJ |
299 | } |
300 | $this->pdf->SetXY($x, $y); | |
301 | } | |
6d4a64c8 KJ |
302 | |
303 | /** | |
304 | * function to build badges parameters before actually creating badges. | |
305 | * | |
306 | * @param array $params associated array of submitted values | |
307 | * @params object $form form/controller object | |
308 | * | |
309 | * @return void | |
310 | * @access public | |
311 | * @static | |
312 | */ | |
313 | public static function buildBadges(&$params, &$form) { | |
314 | // get name badge layout info | |
315 | $layoutInfo = CRM_Badge_BAO_Layout::buildLayout($params); | |
316 | ||
317 | // spit / get actual field names from token | |
318 | $returnProperties = array(); | |
319 | if (!empty($layoutInfo['data']['token'])) { | |
320 | foreach ($layoutInfo['data']['token'] as $index => $value) { | |
321 | $element = ''; | |
322 | if ($value) { | |
323 | $token = CRM_Utils_Token::getTokens($value); | |
324 | if (key($token) == 'contact') { | |
325 | $element = $token['contact'][0]; | |
326 | } | |
327 | elseif (key($token) == 'event') { | |
328 | $element = $token['event'][0]; | |
329 | //FIX ME - we need to standardize event token names | |
330 | if (!strpos($element, 'event_')) { | |
331 | $element = 'event_' . $element; | |
332 | } | |
333 | } | |
334 | elseif (key($token) == 'participant') { | |
335 | $element = $token['participant'][0]; | |
336 | } | |
337 | ||
338 | // build returnproperties for query | |
339 | $returnProperties[$element] = 1; | |
340 | } | |
341 | ||
342 | // add actual field name to row element | |
343 | $layoutInfo['data']['rowElements'][$index] = $element; | |
344 | } | |
345 | } | |
346 | ||
347 | // add additional required fields for query execution | |
348 | $additionalFields = array('participant_register_date', 'participant_id', 'event_id', 'contact_id'); | |
349 | foreach ($additionalFields as $field) { | |
350 | $returnProperties[$field] = 1; | |
351 | } | |
352 | ||
353 | if ($form->_single) { | |
354 | $queryParams = NULL; | |
355 | } | |
356 | else { | |
357 | $queryParams = $form->get('queryParams'); | |
358 | } | |
359 | ||
360 | $query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, NULL, FALSE, FALSE, | |
361 | CRM_Contact_BAO_Query::MODE_EVENT | |
362 | ); | |
363 | ||
364 | list($select, $from, $where, $having) = $query->query(); | |
365 | if (empty($where)) { | |
366 | $where = "WHERE {$form->_componentClause}"; | |
367 | } | |
368 | else { | |
369 | $where .= " AND {$form->_componentClause}"; | |
370 | } | |
371 | ||
372 | $sortOrder = NULL; | |
373 | if ($form->get(CRM_Utils_Sort::SORT_ORDER)) { | |
374 | $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER); | |
375 | if (!empty($sortOrder)) { | |
376 | $sortOrder = " ORDER BY $sortOrder"; | |
377 | } | |
378 | } | |
379 | $queryString = "$select $from $where $having $sortOrder"; | |
380 | ||
381 | $dao = CRM_Core_DAO::executeQuery($queryString); | |
382 | $rows = array(); | |
383 | while ($dao->fetch()) { | |
384 | $rows[$dao->participant_id] = array(); | |
385 | foreach ($returnProperties as $key => $dontCare) { | |
386 | $rows[$dao->participant_id][$key] = isset($dao->$key) ? $dao->$key : NULL; | |
387 | } | |
388 | } | |
389 | ||
390 | $eventBadgeClass = new CRM_Badge_BAO_Badge(); | |
391 | $eventBadgeClass->createLabels($rows, $layoutInfo); | |
392 | } | |
42c62ec1 KJ |
393 | } |
394 |