Merge pull request #3207 from totten/master-alerts
[civicrm-core.git] / CRM / Badge / BAO / Badge.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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-2014
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 $formattedRow['labelTitle'] = $layout['title'];
87 $formattedRow['labelId'] = $layout['id'];
88
89
90 if (!empty($layout['data']['rowElements'])) {
91 foreach ($layout['data']['rowElements'] as $key => $element) {
92 $value = '';
93 if ($element) {
94 $value = $row[$element];
95 // hack to fix date field display format
96 if (strpos($element, '_date')) {
97 $value = CRM_Utils_Date::customFormat($value, "%B %E%f");
98 }
99 }
100
101 $formattedRow['token'][$key] = array(
102 'value' => $value,
103 'font_name' => $layout['data']['font_name'][$key],
104 'font_size' => $layout['data']['font_size'][$key],
105 'font_style' => $layout['data']['font_style'][$key],
106 'text_alignment' => $layout['data']['text_alignment'][$key],
107 'token' => $layout['data']['token'][$key],
108 );
109 }
110 }
111
112 if (!empty($layout['data']['image_1'])) {
113 $formattedRow['image_1'] = $layout['data']['image_1'];
114 }
115 if (!empty($layout['data']['width_image_1'])) {
116 $formattedRow['width_image_1'] = $layout['data']['width_image_1'];
117 }
118 if (!empty($layout['data']['height_image_1'])) {
119 $formattedRow['height_image_1'] = $layout['data']['height_image_1'];
120 }
121
122 if (!empty($layout['data']['image_2'])) {
123 $formattedRow['image_2'] = $layout['data']['image_2'];
124 }
125 if (!empty($layout['data']['width_image_2'])) {
126 $formattedRow['width_image_2'] = $layout['data']['width_image_2'];
127 }
128 if (!empty($layout['data']['height_image_2'])) {
129 $formattedRow['height_image_2'] = $layout['data']['height_image_2'];
130 }
131
132 if (!empty($layout['data']['add_barcode'])) {
133 $formattedRow['barcode'] = array(
134 'alignment' => $layout['data']['barcode_alignment'],
135 'type' => $layout['data']['barcode_type'],
136 );
137 }
138
139 // finally assign all the row values, so that we can use it for barcode etc
140 $formattedRow['values'] = $row;
141
142 return $formattedRow;
143 }
144
145 public function generateLabel($formattedRow) {
146 switch ($formattedRow['labelFormat']) {
147 case 'A6 Badge Portrait 150x106':
148 case 'Hanging Badge 3-3/4" x 4-3"/4':
149 self::labelCreator($formattedRow, 5);
150 break;
151 case 'Avery 5395':
152 default:
153 self::labelCreator($formattedRow);
154 break;
155 }
156 }
157
158 public function labelCreator(&$formattedRow, $cellspacing = 0) {
159
160 $this->lMarginLogo = 18;
161 $this->tMarginName = 20;
162
163 $x = $this->pdf->GetAbsX();
164 $y = $this->pdf->getY();
165
166 //call hook alterBadge
167 CRM_Utils_Hook::alterBadge($formattedRow['labelTitle'], $this, $formattedRow,$formattedRow['values']);
168
169 $startOffset = 0;
170 if (!empty($formattedRow['image_1'])) {
171 $this->printImage($formattedRow['image_1'], NULL, NULL, CRM_Utils_Array::value('width_image_1', $formattedRow),
172 CRM_Utils_Array::value('height_image_1', $formattedRow));
173 }
174
175 if (!empty($formattedRow['image_2'])) {
176 $this->printImage($formattedRow['image_2'], $x + 68, NULL, CRM_Utils_Array::value('width_image_2', $formattedRow),
177 CRM_Utils_Array::value('height_image_2', $formattedRow));
178 }
179
180 if ((CRM_Utils_Array::value('height_image_1', $formattedRow) >
181 CRM_Utils_Array::value('height_image_2', $formattedRow)) && !empty($formattedRow['height_image_1'])) {
182 $startOffset = CRM_Utils_Array::value('height_image_1', $formattedRow);
183 }
184 elseif (!empty($formattedRow['height_image_2'])) {
185 $startOffset = CRM_Utils_Array::value('height_image_2', $formattedRow);
186 }
187
188 $this->pdf->SetLineStyle(array(
189 'width' => 0.1,
190 'cap' => 'round',
191 'join' => 'round',
192 'dash' => '2,2',
193 'color' => array(0, 0, 200)
194 ));
195
196 $rowCount = CRM_Badge_Form_Layout::FIELD_ROWCOUNT;
197 for ($i = 1; $i <= $rowCount; $i++) {
198 if (!empty($formattedRow['token'][$i]['token'])) {
199 $value = '';
200 if ($formattedRow['token'][$i]['token'] != 'spacer') {
201 $value = $formattedRow['token'][$i]['value'];
202 }
203
204 $offset = $this->pdf->getY() + $startOffset + $cellspacing;
205
206 $this->pdf->SetFont($formattedRow['token'][$i]['font_name'], $formattedRow['token'][$i]['font_style'],
207 $formattedRow['token'][$i]['font_size']);
208 $this->pdf->MultiCell($this->pdf->width, 0, $value,
209 $this->border, $formattedRow['token'][$i]['text_alignment'], 0, 1, $x, $offset);
210
211 // set this to zero so that it is added only for first element
212 $startOffset = 0;
213 }
214 }
215
216 if (!empty($formattedRow['barcode'])) {
217 $data = $formattedRow['values'];
218
219 if ($formattedRow['barcode']['type'] == 'barcode') {
220 $data['current_value'] =
221 $formattedRow['values']['contact_id'] . '-' . $formattedRow['values']['participant_id'];
222 }
223 else {
224 // view participant url
225 $data['current_value'] = CRM_Utils_System::url('civicrm/contact/view/participant',
226 'action=view&reset=1&cid=' . $formattedRow['values']['contact_id'] . '&id='
227 . $formattedRow['values']['participant_id'],
228 TRUE,
229 NULL,
230 FALSE
231 );
232 }
233
234 // call hook alterBarcode
235 CRM_Utils_Hook::alterBarcode($data, $formattedRow['barcode']['type']);
236
237 if ($formattedRow['barcode']['type'] == 'barcode') {
238 // barcode position
239 $xAlign = $x;
240
241 switch ($formattedRow['barcode']['alignment']) {
242 case 'L':
243 $xAlign += -14;
244 break;
245 case 'R':
246 $xAlign += 27;
247 break;
248 case 'C':
249 $xAlign += 9;
250 break;
251 }
252
253 $style = array(
254 'position' => '',
255 'align' => '',
256 'stretch' => FALSE,
257 'fitwidth' => TRUE,
258 'cellfitalign' => '',
259 'border' => FALSE,
260 'hpadding' => 13.5,
261 'vpadding' => 'auto',
262 'fgcolor' => array(0, 0, 0),
263 'bgcolor' => FALSE,
264 'text' => FALSE,
265 'font' => 'helvetica',
266 'fontsize' => 8,
267 'stretchtext' => 0,
268 );
269
270 $this->pdf->write1DBarcode($data['current_value'], 'C128', $xAlign, $y + $this->pdf->height - 10, '70',
271 12, 0.4, $style, 'B');
272 }
273 else {
274 // qr code position
275 $xAlign = $x;
276
277 switch ($formattedRow['barcode']['alignment']) {
278 case 'L':
279 $xAlign += -5;
280 break;
281 case 'R':
282 $xAlign += 56;
283 break;
284 case 'C':
285 $xAlign += 29;
286 break;
287 }
288
289 $style = array(
290 'border' => false,
291 'hpadding' => 13.5,
292 'vpadding' => 'auto',
293 'fgcolor' => array(0,0,0),
294 'bgcolor' => false,
295 'position' => '',
296 );
297
298 $this->pdf->write2DBarcode($data['current_value'], 'QRCODE,H', $xAlign, $y + $this->pdf->height - 26, 30,
299 30, $style, 'B');
300 }
301 }
302 }
303
304 /**
305 * Helper function to print images
306 *
307 * @param string $img image url
308 *
309 * @param string $x
310 * @param string $y
311 * @param null $w
312 * @param null $h
313 *
314 * @return void
315 * @access public
316 */
317 function printImage($img, $x = '', $y = '', $w = NULL, $h = NULL) {
318 if (!$x) {
319 $x = $this->pdf->GetAbsX();
320 }
321
322 if (!$y) {
323 $y = $this->pdf->GetY();
324 }
325
326 $this->imgRes = 300;
327
328 if ($img) {
329 list($w, $h) = self::getImageProperties($img, $this->imgRes, $w, $h);
330 $this->pdf->Image($img, $x, $y, $w, $h, '', '', '', FALSE, 72, '', FALSE,
331 FALSE, $this->debug, FALSE, FALSE, FALSE);
332 }
333 $this->pdf->SetXY($x, $y);
334 }
335
336 static function getImageProperties($img, $imgRes = 300, $w = NULL, $h = NULL) {
337 $imgsize = getimagesize($img);
338 $f = $imgRes / 25.4;
339 $w = !empty($w) ? $w : $imgsize[0] / $f;
340 $h = !empty($h) ? $h : $imgsize[1] / $f;
341 return array($w, $h);
342 }
343
344 /**
345 * function to build badges parameters before actually creating badges.
346 *
347 * @param array $params associated array of submitted values
348 * @param $form
349 * @params object $form form/controller object
350 *
351 * @return void
352 * @access public
353 * @static
354 */
355 public static function buildBadges(&$params, &$form) {
356 // get name badge layout info
357 $layoutInfo = CRM_Badge_BAO_Layout::buildLayout($params);
358
359 // spit / get actual field names from token
360 $returnProperties = array();
361 if (!empty($layoutInfo['data']['token'])) {
362 foreach ($layoutInfo['data']['token'] as $index => $value) {
363 $element = '';
364 if ($value) {
365 $token = CRM_Utils_Token::getTokens($value);
366 if (key($token) == 'contact') {
367 $element = $token['contact'][0];
368 }
369 elseif (key($token) == 'event') {
370 $element = $token['event'][0];
371 //FIX ME - we need to standardize event token names
372 if (substr($element, 0, 6) != 'event_') {
373 $element = 'event_' . $element;
374 }
375 }
376 elseif (key($token) == 'participant') {
377 $element = $token['participant'][0];
378 }
379
380 // build returnproperties for query
381 $returnProperties[$element] = 1;
382 }
383
384 // add actual field name to row element
385 $layoutInfo['data']['rowElements'][$index] = $element;
386 }
387 }
388
389 // add additional required fields for query execution
390 $additionalFields = array('participant_register_date', 'participant_id', 'event_id', 'contact_id');
391 foreach ($additionalFields as $field) {
392 $returnProperties[$field] = 1;
393 }
394
395 if ($form->_single) {
396 $queryParams = NULL;
397 }
398 else {
399 $queryParams = $form->get('queryParams');
400 }
401
402 $query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, NULL, FALSE, FALSE,
403 CRM_Contact_BAO_Query::MODE_EVENT
404 );
405
406 list($select, $from, $where, $having) = $query->query();
407 if (empty($where)) {
408 $where = "WHERE {$form->_componentClause}";
409 }
410 else {
411 $where .= " AND {$form->_componentClause}";
412 }
413
414 $sortOrder = NULL;
415 if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
416 $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
417 if (!empty($sortOrder)) {
418 $sortOrder = " ORDER BY $sortOrder";
419 }
420 }
421 $queryString = "$select $from $where $having $sortOrder";
422
423 $dao = CRM_Core_DAO::executeQuery($queryString);
424 $rows = array();
425 while ($dao->fetch()) {
426 $rows[$dao->participant_id] = array();
427 foreach ($returnProperties as $key => $dontCare) {
428 $rows[$dao->participant_id][$key] = isset($dao->$key) ? $dao->$key : NULL;
429 }
430 }
431
432 $eventBadgeClass = new CRM_Badge_BAO_Badge();
433 $eventBadgeClass->createLabels($rows, $layoutInfo);
434 }
435 }
436