3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * @copyright CiviCRM LLC (c) 2004-2016
33 * Class CRM_Badge_Format_Badge.
35 * parent class for building name badges
37 class CRM_Badge_BAO_Badge
{
42 public $debug = FALSE;
50 * This function is called to create name label pdf.
52 * @param array $participants
53 * Associated array with participant info.
54 * @param array $layoutInfo
55 * Associated array which contains meta data about format/layout.
57 public function createLabels(&$participants, &$layoutInfo) {
58 $this->pdf
= new CRM_Utils_PDF_Label($layoutInfo['format'], 'mm');
60 $this->pdf
->setPrintHeader(FALSE);
61 $this->pdf
->setPrintFooter(FALSE);
62 $this->pdf
->AddPage();
63 $this->pdf
->SetGenerator($this, "generateLabel");
65 // this is very useful for debugging, by default set to FALSE
67 $this->border
= "LTRB";
70 foreach ($participants as $participant) {
71 $formattedRow = self
::formatLabel($participant, $layoutInfo);
72 $this->pdf
->AddPdfLabel($formattedRow);
75 $this->pdf
->Output(CRM_Utils_String
::munge($layoutInfo['title'], '_', 64) . '.pdf', 'D');
76 CRM_Utils_System
::civiExit(1);
80 * Function to create structure and add meta data according to layout.
83 * Row element that needs to be formatted.
84 * @param array $layout
90 public static function formatLabel(&$row, &$layout) {
91 $formattedRow = array('labelFormat' => $layout['label_format_name']);
92 $formattedRow['labelTitle'] = $layout['title'];
93 $formattedRow['labelId'] = $layout['id'];
95 if (!empty($layout['data']['rowElements'])) {
96 foreach ($layout['data']['rowElements'] as $key => $element) {
99 $value = $row[$element];
100 // hack to fix date field display format
101 if (strpos($element, '_date')) {
102 $value = CRM_Utils_Date
::customFormat($value, "%B %E%f");
106 $formattedRow['token'][$key] = array(
108 'font_name' => $layout['data']['font_name'][$key],
109 'font_size' => $layout['data']['font_size'][$key],
110 'font_style' => $layout['data']['font_style'][$key],
111 'text_alignment' => $layout['data']['text_alignment'][$key],
112 'token' => $layout['data']['token'][$key],
117 if (!empty($layout['data']['image_1'])) {
118 $formattedRow['image_1'] = $layout['data']['image_1'];
120 if (!empty($layout['data']['width_image_1'])) {
121 $formattedRow['width_image_1'] = $layout['data']['width_image_1'];
123 if (!empty($layout['data']['height_image_1'])) {
124 $formattedRow['height_image_1'] = $layout['data']['height_image_1'];
127 if (!empty($layout['data']['image_2'])) {
128 $formattedRow['image_2'] = $layout['data']['image_2'];
130 if (!empty($layout['data']['width_image_2'])) {
131 $formattedRow['width_image_2'] = $layout['data']['width_image_2'];
133 if (!empty($layout['data']['height_image_2'])) {
134 $formattedRow['height_image_2'] = $layout['data']['height_image_2'];
136 if (!empty($row['image_URL']) && !empty($layout['data']['show_participant_image'])) {
137 $formattedRow['participant_image'] = $row['image_URL'];
139 if (!empty($layout['data']['width_participant_image'])) {
140 $formattedRow['width_participant_image'] = $layout['data']['width_participant_image'];
142 if (!empty($layout['data']['height_participant_image'])) {
143 $formattedRow['height_participant_image'] = $layout['data']['height_participant_image'];
145 if (!empty($layout['data']['alignment_participant_image'])) {
146 $formattedRow['alignment_participant_image'] = $layout['data']['alignment_participant_image'];
149 if (!empty($layout['data']['add_barcode'])) {
150 $formattedRow['barcode'] = array(
151 'alignment' => $layout['data']['barcode_alignment'],
152 'type' => $layout['data']['barcode_type'],
156 // finally assign all the row values, so that we can use it for barcode etc
157 $formattedRow['values'] = $row;
159 return $formattedRow;
163 * @param $formattedRow
165 public function generateLabel($formattedRow) {
166 switch ($formattedRow['labelFormat']) {
167 case 'A6 Badge Portrait 150x106':
168 case 'Hanging Badge 3-3/4" x 4-3"/4':
169 self
::labelCreator($formattedRow, 5);
174 self
::labelCreator($formattedRow);
180 * @param $formattedRow
181 * @param int $cellspacing
183 public function labelCreator(&$formattedRow, $cellspacing = 0) {
184 $this->lMarginLogo
= 18;
185 $this->tMarginName
= 20;
187 $x = $this->pdf
->GetAbsX();
188 $y = $this->pdf
->getY();
190 //call hook alterBadge
191 CRM_Utils_Hook
::alterBadge($formattedRow['labelTitle'], $this, $formattedRow, $formattedRow['values']);
194 if (!empty($formattedRow['image_1'])) {
195 $this->printImage($formattedRow['image_1'], NULL, NULL, CRM_Utils_Array
::value('width_image_1', $formattedRow),
196 CRM_Utils_Array
::value('height_image_1', $formattedRow));
199 if (!empty($formattedRow['image_2'])) {
200 $this->printImage($formattedRow['image_2'], $x +
68, NULL, CRM_Utils_Array
::value('width_image_2', $formattedRow),
201 CRM_Utils_Array
::value('height_image_2', $formattedRow));
204 if ((CRM_Utils_Array
::value('height_image_1', $formattedRow) >
205 CRM_Utils_Array
::value('height_image_2', $formattedRow)) && !empty($formattedRow['height_image_1'])
207 $startOffset = CRM_Utils_Array
::value('height_image_1', $formattedRow);
209 elseif (!empty($formattedRow['height_image_2'])) {
210 $startOffset = CRM_Utils_Array
::value('height_image_2', $formattedRow);
213 if (!empty($formattedRow['participant_image'])) {
215 switch (CRM_Utils_Array
::value('alignment_participant_image', $formattedRow)) {
227 $this->pdf
->Image($formattedRow['participant_image'], $x +
$imageAlign, $y +
$startOffset, CRM_Utils_Array
::value('width_participant_image', $formattedRow), CRM_Utils_Array
::value('height_participant_image', $formattedRow));
228 if ($startOffset == NULL && CRM_Utils_Array
::value('height_participant_image', $formattedRow)) {
229 $startOffset = CRM_Utils_Array
::value('height_participant_image', $formattedRow);
233 $this->pdf
->SetLineStyle(array(
238 'color' => array(0, 0, 200),
241 $rowCount = CRM_Badge_Form_Layout
::FIELD_ROWCOUNT
;
242 for ($i = 1; $i <= $rowCount; $i++
) {
243 if (!empty($formattedRow['token'][$i]['token'])) {
245 if ($formattedRow['token'][$i]['token'] != 'spacer') {
246 $value = $formattedRow['token'][$i]['value'];
250 $rowWidth = $this->pdf
->width
;
251 if (!empty($formattedRow['participant_image']) && !empty($formattedRow['width_participant_image'])) {
252 $rowWidth = $this->pdf
->width
- $formattedRow['width_participant_image'];
253 if ($formattedRow['alignment_participant_image'] == 'L') {
254 $xAlign = $x +
$formattedRow['width_participant_image'] +
$imageAlign;
257 $offset = $this->pdf
->getY() +
$startOffset +
$cellspacing;
259 $this->pdf
->SetFont($formattedRow['token'][$i]['font_name'], $formattedRow['token'][$i]['font_style'],
260 $formattedRow['token'][$i]['font_size']);
261 $this->pdf
->MultiCell($rowWidth, 0, $value,
262 $this->border
, $formattedRow['token'][$i]['text_alignment'], 0, 1, $xAlign, $offset);
264 // set this to zero so that it is added only for first element
269 if (!empty($formattedRow['barcode'])) {
270 $data = $formattedRow['values'];
272 if ($formattedRow['barcode']['type'] == 'barcode') {
273 $data['current_value'] = $formattedRow['values']['contact_id'] . '-' . $formattedRow['values']['participant_id'];
276 // view participant url
277 $data['current_value'] = CRM_Utils_System
::url('civicrm/contact/view/participant',
278 'action=view&reset=1&cid=' . $formattedRow['values']['contact_id'] . '&id='
279 . $formattedRow['values']['participant_id'],
286 // call hook alterBarcode
287 CRM_Utils_Hook
::alterBarcode($data, $formattedRow['barcode']['type']);
289 if ($formattedRow['barcode']['type'] == 'barcode') {
293 switch ($formattedRow['barcode']['alignment']) {
312 'cellfitalign' => '',
315 'vpadding' => 'auto',
316 'fgcolor' => array(0, 0, 0),
319 'font' => 'helvetica',
324 $this->pdf
->write1DBarcode($data['current_value'], 'C128', $xAlign, $y +
$this->pdf
->height
- 10, '70',
325 12, 0.4, $style, 'B');
331 switch ($formattedRow['barcode']['alignment']) {
348 'vpadding' => 'auto',
349 'fgcolor' => array(0, 0, 0),
354 $this->pdf
->write2DBarcode($data['current_value'], 'QRCODE,H', $xAlign, $y +
$this->pdf
->height
- 26, 30,
361 * Helper function to print images.
371 public function printImage($img, $x = '', $y = '', $w = NULL, $h = NULL) {
373 $x = $this->pdf
->GetAbsX();
377 $y = $this->pdf
->GetY();
383 list($w, $h) = self
::getImageProperties($img, $this->imgRes
, $w, $h);
384 $this->pdf
->Image($img, $x, $y, $w, $h, '', '', '', FALSE, 72, '', FALSE,
385 FALSE, $this->debug
, FALSE, FALSE, FALSE);
387 $this->pdf
->SetXY($x, $y);
398 public static function getImageProperties($img, $imgRes = 300, $w = NULL, $h = NULL) {
399 $imgsize = getimagesize($img);
401 $w = !empty($w) ?
$w : $imgsize[0] / $f;
402 $h = !empty($h) ?
$h : $imgsize[1] / $f;
403 return array($w, $h);
407 * Build badges parameters before actually creating badges.
409 * @param array $params
410 * Associated array of submitted values.
411 * @param CRM_Core_Form $form
413 public static function buildBadges(&$params, &$form) {
414 // get name badge layout info
415 $layoutInfo = CRM_Badge_BAO_Layout
::buildLayout($params);
417 // split/get actual field names from token and individual contact image URLs
418 $returnProperties = array();
419 if (!empty($layoutInfo['data']['token'])) {
420 foreach ($layoutInfo['data']['token'] as $index => $value) {
423 $token = CRM_Utils_Token
::getTokens($value);
424 if (key($token) == 'contact') {
425 $element = $token['contact'][0];
427 elseif (key($token) == 'event') {
428 $element = $token['event'][0];
429 //FIX ME - we need to standardize event token names
430 if (substr($element, 0, 6) != 'event_') {
431 $element = 'event_' . $element;
434 elseif (key($token) == 'participant') {
435 $element = $token['participant'][0];
438 // build returnproperties for query
439 $returnProperties[$element] = 1;
442 // add actual field name to row element
443 $layoutInfo['data']['rowElements'][$index] = $element;
447 // add additional required fields for query execution
448 $additionalFields = array('participant_register_date', 'participant_id', 'event_id', 'contact_id', 'image_URL');
449 foreach ($additionalFields as $field) {
450 $returnProperties[$field] = 1;
453 if ($form->_single
) {
457 $queryParams = $form->get('queryParams');
460 $query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, NULL, FALSE, FALSE,
461 CRM_Contact_BAO_Query
::MODE_EVENT
464 list($select, $from, $where, $having) = $query->query();
466 $where = "WHERE {$form->_componentClause}";
469 $where .= " AND {$form->_componentClause}";
473 if ($form->get(CRM_Utils_Sort
::SORT_ORDER
)) {
474 $sortOrder = $form->get(CRM_Utils_Sort
::SORT_ORDER
);
475 if (!empty($sortOrder)) {
476 $sortOrder = " ORDER BY $sortOrder";
479 $queryString = "$select $from $where $having $sortOrder";
481 $dao = CRM_Core_DAO
::executeQuery($queryString);
483 while ($dao->fetch()) {
484 $query->convertToPseudoNames($dao);
485 $rows[$dao->participant_id
] = array();
486 foreach ($returnProperties as $key => $dontCare) {
487 $value = isset($dao->$key) ?
$dao->$key : NULL;
488 // Format custom fields
489 if (strstr($key, 'custom_') && isset($value)) {
490 $value = CRM_Core_BAO_CustomField
::displayValue($value, substr($key, 7), $dao->contact_id
);
492 $rows[$dao->participant_id
][$key] = $value;
496 $eventBadgeClass = new CRM_Badge_BAO_Badge();
497 $eventBadgeClass->createLabels($rows, $layoutInfo);