Merge pull request #1293 from kurund/CRM-13130
[civicrm-core.git] / CRM / Badge / BAO / Badge.php
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 */
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
87 if (CRM_Utils_Array::value('rowElements', $layout['data'])) {
88 foreach ($layout['data']['rowElements'] as $key => $element) {
89 $value = '';
90 if ($element) {
91 $value = $row[$element];
92 // hack to fix date field display format
93 if (strpos($element, '_date')) {
94 $value = CRM_Utils_Date::customFormat($value, "%B %E%f");
95 }
96 }
97
98 $formattedRow['token'][$key] = array(
99 'value' => $value,
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'])) {
116 $formattedRow['barcode'] = array(
117 'alignment' => $layout['data']['barcode_alignment'],
118 'type' => $layout['data']['barcode_type'],
119 );
120 }
121
122 // finally assign all the row values, so that we can use it for barcode etc
123 $formattedRow['values'] = $row;
124
125 return $formattedRow;
126 }
127
128 public function generateLabel($formattedRow) {
129 switch ($formattedRow['labelFormat']) {
130 case 'Avery 5395':
131 default:
132 self::labelAvery5395($formattedRow);
133 break;
134 }
135 }
136
137 public function labelAvery5395(&$formattedRow) {
138 $this->lMarginLogo = 18;
139 $this->tMarginName = 20;
140
141 $x = $this->pdf->GetAbsX();
142 $y = $this->pdf->GetY();
143
144 $titleWidth = $titleLeftMargin = 0;
145 if (CRM_Utils_Array::value('image_1', $formattedRow)) {
146 $this->printImage($formattedRow['image_1']);
147 $titleWidth = $titleLeftMargin = $this->lMarginLogo;
148 }
149
150 $titleRightMargin = 0;
151 if (CRM_Utils_Array::value('image_2', $formattedRow)) {
152 $this->printImage($formattedRow['image_2'], $x + 68);
153 $titleRightMargin = 36;
154 $titleWidth = $this->lMarginLogo;
155 }
156
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 ));
164
165 if ($titleLeftMargin && $titleRightMargin) {
166 $titleWidth = $titleRightMargin;
167 }
168
169 $this->pdf->SetFont($formattedRow['token'][1]['font_name'], '', $formattedRow['token'][1]['font_size']);
170 $this->pdf->MultiCell($this->pdf->width - $titleWidth, 0, $formattedRow['token'][1]['value'],
171 $this->border, $formattedRow['token'][1]['text_alignment'], 0, 1, $x + $titleLeftMargin, $y);
172
173 $this->pdf->SetFont($formattedRow['token'][2]['font_name'], '', $formattedRow['token'][2]['font_size']);
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);
176
177 $this->pdf->SetFont($formattedRow['token'][3]['font_name'], '', $formattedRow['token'][3]['font_size']);
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
181 $this->pdf->SetFont($formattedRow['token'][4]['font_name'], '', $formattedRow['token'][4]['font_size']);
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);
184
185 if (CRM_Utils_Array::value('barcode', $formattedRow)) {
186 $data = $formattedRow['values'];
187
188 if ($formattedRow['barcode']['type'] == 'barcode') {
189 $data['current_value'] =
190 $formattedRow['values']['contact_id'] . '-' . $formattedRow['values']['participant_id'];
191 }
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 );
201 }
202
203 // call hook alterBarcode
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':
212 $xAlign += -14;
213 break;
214 case 'R':
215 $xAlign += 32;
216 break;
217 case 'C':
218 $xAlign += 9;
219 break;
220 }
221
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
239 $this->pdf->write1DBarcode($data['current_value'], 'C128', $xAlign, $y + $this->pdf->height - 10, '70',
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':
248 $xAlign += -5;
249 break;
250 case 'R':
251 $xAlign += 61;
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
267 $this->pdf->write2DBarcode($data['current_value'], 'QRCODE,H', $xAlign, $y + $this->pdf->height - 26, 30,
268 30, $style, 'B');
269 }
270 }
271 }
272
273 /**
274 * Helper function to print images
275 * @param string $img image url
276 *
277 * @return void
278 * @access public
279 */
280 function printImage($img, $x = '', $y = '') {
281 if (!$x) {
282 $x = $this->pdf->GetAbsX();
283 }
284
285 if (!$y) {
286 $y = $this->pdf->GetY();
287 }
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;
297 $this->pdf->Image($img, $x, $y, $w, $h, '', '', '', FALSE, 72, '', FALSE,
298 FALSE, $this->debug, FALSE, FALSE, FALSE);
299 }
300 $this->pdf->SetXY($x, $y);
301 }
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 }
393 }
394