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