3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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-2014
35 * Class CRM_Badge_Format_Badge
37 * parent class for building name badges
39 class CRM_Badge_BAO_Badge
{
41 public $debug = FALSE;
46 * This function is called to create name label pdf
48 * @param array $participants associated array with participant info
49 * @param array $layoutInfo associated array which contains meta data about format/layout
54 public function createLabels(&$participants, &$layoutInfo) {
55 $this->pdf
= new CRM_Utils_PDF_Label($layoutInfo['format'], 'mm');
57 $this->pdf
->setPrintHeader(FALSE);
58 $this->pdf
->setPrintFooter(FALSE);
59 $this->pdf
->AddPage();
60 $this->pdf
->SetGenerator($this, "generateLabel");
62 // this is very useful for debugging, by default set to FALSE
64 $this->border
= "LTRB";
67 foreach ($participants as $participant) {
68 $formattedRow = self
::formatLabel($participant, $layoutInfo);
69 $this->pdf
->AddPdfLabel($formattedRow);
72 $this->pdf
->Output(CRM_Utils_String
::munge($layoutInfo['title'], '_', 64) . '.pdf', 'D');
73 CRM_Utils_System
::civiExit(1);
77 * Funtion to create structure and add meta data according to layout
79 * @param array $row row element that needs to be formatted
80 * @param array $layout layout meta data
82 * @return array $formattedRow row with meta data
84 static function formatLabel(&$row, &$layout) {
85 $formattedRow = array('labelFormat' => $layout['label_format_name']);
86 $formattedRow['labelTitle'] = $layout['title'];
87 $formattedRow['labelId'] = $layout['id'];
90 if (!empty($layout['data']['rowElements'])) {
91 foreach ($layout['data']['rowElements'] as $key => $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");
101 $formattedRow['token'][$key] = array(
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],
112 if (!empty($layout['data']['image_1'])) {
113 $formattedRow['image_1'] = $layout['data']['image_1'];
115 if (!empty($layout['data']['width_image_1'])) {
116 $formattedRow['width_image_1'] = $layout['data']['width_image_1'];
118 if (!empty($layout['data']['height_image_1'])) {
119 $formattedRow['height_image_1'] = $layout['data']['height_image_1'];
122 if (!empty($layout['data']['image_2'])) {
123 $formattedRow['image_2'] = $layout['data']['image_2'];
125 if (!empty($layout['data']['width_image_2'])) {
126 $formattedRow['width_image_2'] = $layout['data']['width_image_2'];
128 if (!empty($layout['data']['height_image_2'])) {
129 $formattedRow['height_image_2'] = $layout['data']['height_image_2'];
131 if (!empty($row['image_URL']) && !empty($layout['data']['show_participant_image'])) {
132 $formattedRow['participant_image'] = $row['image_URL'];
134 if (!empty($layout['data']['width_participant_image'])) {
135 $formattedRow['width_participant_image'] = $layout['data']['width_participant_image'];
137 if (!empty($layout['data']['height_participant_image'])) {
138 $formattedRow['height_participant_image'] = $layout['data']['height_participant_image'];
140 if (!empty($layout['data']['alignment_participant_image'])) {
141 $formattedRow['alignment_participant_image'] = $layout['data']['alignment_participant_image'];
144 if (!empty($layout['data']['add_barcode'])) {
145 $formattedRow['barcode'] = array(
146 'alignment' => $layout['data']['barcode_alignment'],
147 'type' => $layout['data']['barcode_type'],
151 // finally assign all the row values, so that we can use it for barcode etc
152 $formattedRow['values'] = $row;
154 return $formattedRow;
158 * @param $formattedRow
160 public function generateLabel($formattedRow) {
161 switch ($formattedRow['labelFormat']) {
162 case 'A6 Badge Portrait 150x106':
163 case 'Hanging Badge 3-3/4" x 4-3"/4':
164 self
::labelCreator($formattedRow, 5);
168 self
::labelCreator($formattedRow);
174 * @param $formattedRow
175 * @param int $cellspacing
177 public function labelCreator(&$formattedRow, $cellspacing = 0) {
178 $this->lMarginLogo
= 18;
179 $this->tMarginName
= 20;
181 $x = $this->pdf
->GetAbsX();
182 $y = $this->pdf
->getY();
184 //call hook alterBadge
185 CRM_Utils_Hook
::alterBadge($formattedRow['labelTitle'], $this, $formattedRow, $formattedRow['values']);
188 if (!empty($formattedRow['image_1'])) {
189 $this->printImage($formattedRow['image_1'], NULL, NULL, CRM_Utils_Array
::value('width_image_1', $formattedRow),
190 CRM_Utils_Array
::value('height_image_1', $formattedRow));
193 if (!empty($formattedRow['image_2'])) {
194 $this->printImage($formattedRow['image_2'], $x +
68, NULL, CRM_Utils_Array
::value('width_image_2', $formattedRow),
195 CRM_Utils_Array
::value('height_image_2', $formattedRow));
198 if ((CRM_Utils_Array
::value('height_image_1', $formattedRow) >
199 CRM_Utils_Array
::value('height_image_2', $formattedRow)) && !empty($formattedRow['height_image_1'])) {
200 $startOffset = CRM_Utils_Array
::value('height_image_1', $formattedRow);
202 elseif (!empty($formattedRow['height_image_2'])) {
203 $startOffset = CRM_Utils_Array
::value('height_image_2', $formattedRow);
206 if (CRM_Utils_Array
::value('participant_image', $formattedRow)) {
208 switch (CRM_Utils_Array
::value('alignment_participant_image', $formattedRow)) {
218 $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));
219 if ($startOffset == NULL && CRM_Utils_Array
::value('height_participant_image', $formattedRow)) {
220 $startOffset = CRM_Utils_Array
::value('height_participant_image', $formattedRow);
224 $this->pdf
->SetLineStyle(array(
229 'color' => array(0, 0, 200)
232 $rowCount = CRM_Badge_Form_Layout
::FIELD_ROWCOUNT
;
233 for ($i = 1; $i <= $rowCount; $i++
) {
234 if (!empty($formattedRow['token'][$i]['token'])) {
236 if ($formattedRow['token'][$i]['token'] != 'spacer') {
237 $value = $formattedRow['token'][$i]['value'];
241 $rowWidth = $this->pdf
->width
;
242 if (!empty($formattedRow['participant_image']) && !empty($formattedRow['width_participant_image'])) {
243 $rowWidth = $this->pdf
->width
- $formattedRow['width_participant_image'];
244 if ($formattedRow['alignment_participant_image'] == 'L') {
245 $xAlign = $x +
$formattedRow['width_participant_image'] +
$imageAlign;
248 $offset = $this->pdf
->getY() +
$startOffset +
$cellspacing;
250 $this->pdf
->SetFont($formattedRow['token'][$i]['font_name'], $formattedRow['token'][$i]['font_style'],
251 $formattedRow['token'][$i]['font_size']);
252 $this->pdf
->MultiCell($rowWidth, 0, $value,
253 $this->border
, $formattedRow['token'][$i]['text_alignment'], 0, 1, $xAlign, $offset);
255 // set this to zero so that it is added only for first element
260 if (!empty($formattedRow['barcode'])) {
261 $data = $formattedRow['values'];
263 if ($formattedRow['barcode']['type'] == 'barcode') {
264 $data['current_value'] =
265 $formattedRow['values']['contact_id'] . '-' . $formattedRow['values']['participant_id'];
268 // view participant url
269 $data['current_value'] = CRM_Utils_System
::url('civicrm/contact/view/participant',
270 'action=view&reset=1&cid=' . $formattedRow['values']['contact_id'] . '&id='
271 . $formattedRow['values']['participant_id'],
278 // call hook alterBarcode
279 CRM_Utils_Hook
::alterBarcode($data, $formattedRow['barcode']['type']);
281 if ($formattedRow['barcode']['type'] == 'barcode') {
285 switch ($formattedRow['barcode']['alignment']) {
302 'cellfitalign' => '',
305 'vpadding' => 'auto',
306 'fgcolor' => array(0, 0, 0),
309 'font' => 'helvetica',
314 $this->pdf
->write1DBarcode($data['current_value'], 'C128', $xAlign, $y +
$this->pdf
->height
- 10, '70',
315 12, 0.4, $style, 'B');
321 switch ($formattedRow['barcode']['alignment']) {
336 'vpadding' => 'auto',
337 'fgcolor' => array(0,0,0),
342 $this->pdf
->write2DBarcode($data['current_value'], 'QRCODE,H', $xAlign, $y +
$this->pdf
->height
- 26, 30,
349 * Helper function to print images
351 * @param string $img image url
361 function printImage($img, $x = '', $y = '', $w = NULL, $h = NULL) {
363 $x = $this->pdf
->GetAbsX();
367 $y = $this->pdf
->GetY();
373 list($w, $h) = self
::getImageProperties($img, $this->imgRes
, $w, $h);
374 $this->pdf
->Image($img, $x, $y, $w, $h, '', '', '', FALSE, 72, '', FALSE,
375 FALSE, $this->debug
, FALSE, FALSE, FALSE);
377 $this->pdf
->SetXY($x, $y);
388 static function getImageProperties($img, $imgRes = 300, $w = NULL, $h = NULL) {
389 $imgsize = getimagesize($img);
391 $w = !empty($w) ?
$w : $imgsize[0] / $f;
392 $h = !empty($h) ?
$h : $imgsize[1] / $f;
393 return array($w, $h);
397 * function to build badges parameters before actually creating badges.
399 * @param array $params associated array of submitted values
401 * @params object $form form/controller object
407 public static function buildBadges(&$params, &$form) {
408 // get name badge layout info
409 $layoutInfo = CRM_Badge_BAO_Layout
::buildLayout($params);
411 // spit / get actual field names from tokeni and individual contact image URLs
412 $returnProperties = array();
413 if (!empty($layoutInfo['data']['token'])) {
414 foreach ($layoutInfo['data']['token'] as $index => $value) {
417 $token = CRM_Utils_Token
::getTokens($value);
418 if (key($token) == 'contact') {
419 $element = $token['contact'][0];
421 elseif (key($token) == 'event') {
422 $element = $token['event'][0];
423 //FIX ME - we need to standardize event token names
424 if (substr($element, 0, 6) != 'event_') {
425 $element = 'event_' . $element;
428 elseif (key($token) == 'participant') {
429 $element = $token['participant'][0];
432 // build returnproperties for query
433 $returnProperties[$element] = 1;
436 // add actual field name to row element
437 $layoutInfo['data']['rowElements'][$index] = $element;
441 // add additional required fields for query execution
442 $additionalFields = array('participant_register_date', 'participant_id', 'event_id', 'contact_id', 'image_URL');
443 foreach ($additionalFields as $field) {
444 $returnProperties[$field] = 1;
447 if ($form->_single
) {
451 $queryParams = $form->get('queryParams');
454 $query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, NULL, FALSE, FALSE,
455 CRM_Contact_BAO_Query
::MODE_EVENT
458 list($select, $from, $where, $having) = $query->query();
460 $where = "WHERE {$form->_componentClause}";
463 $where .= " AND {$form->_componentClause}";
467 if ($form->get(CRM_Utils_Sort
::SORT_ORDER
)) {
468 $sortOrder = $form->get(CRM_Utils_Sort
::SORT_ORDER
);
469 if (!empty($sortOrder)) {
470 $sortOrder = " ORDER BY $sortOrder";
473 $queryString = "$select $from $where $having $sortOrder";
475 $dao = CRM_Core_DAO
::executeQuery($queryString);
477 while ($dao->fetch()) {
478 $rows[$dao->participant_id
] = array();
479 foreach ($returnProperties as $key => $dontCare) {
480 $rows[$dao->participant_id
][$key] = isset($dao->$key) ?
$dao->$key : NULL;
484 $eventBadgeClass = new CRM_Badge_BAO_Badge();
485 $eventBadgeClass->createLabels($rows, $layoutInfo);