3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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-2013
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']);
87 if (CRM_Utils_Array
::value('rowElements', $layout['data'])) {
88 foreach ($layout['data']['rowElements'] as $key => $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");
98 $formattedRow['token'][$key] = array(
100 'font_name' => $layout['data']['font_name'][$key],
101 'font_size' => $layout['data']['font_size'][$key],
102 'font_style' => $layout['data']['font_style'][$key],
103 'text_alignment' => $layout['data']['text_alignment'][$key],
104 'token' => $layout['data']['token'][$key],
109 if (CRM_Utils_Array
::value('image_1', $layout['data'])) {
110 $formattedRow['image_1'] = $layout['data']['image_1'];
112 if (CRM_Utils_Array
::value('width_image_1', $layout['data'])) {
113 $formattedRow['width_image_1'] = $layout['data']['width_image_1'];
115 if (CRM_Utils_Array
::value('height_image_1', $layout['data'])) {
116 $formattedRow['height_image_1'] = $layout['data']['height_image_1'];
119 if (CRM_Utils_Array
::value('image_2', $layout['data'])) {
120 $formattedRow['image_2'] = $layout['data']['image_2'];
122 if (CRM_Utils_Array
::value('width_image_2', $layout['data'])) {
123 $formattedRow['width_image_2'] = $layout['data']['width_image_2'];
125 if (CRM_Utils_Array
::value('height_image_2', $layout['data'])) {
126 $formattedRow['height_image_2'] = $layout['data']['height_image_2'];
129 if (CRM_Utils_Array
::value('add_barcode', $layout['data'])) {
130 $formattedRow['barcode'] = array(
131 'alignment' => $layout['data']['barcode_alignment'],
132 'type' => $layout['data']['barcode_type'],
136 // finally assign all the row values, so that we can use it for barcode etc
137 $formattedRow['values'] = $row;
139 return $formattedRow;
142 public function generateLabel($formattedRow) {
143 switch ($formattedRow['labelFormat']) {
144 case 'A6 Badge Portrait 150x106':
145 case 'Hanging Badge 3-3/4" x 4-3"/4':
146 self
::labelCreator($formattedRow, 5);
150 self
::labelCreator($formattedRow);
155 public function labelCreator(&$formattedRow, $cellspacing = 0) {
156 $this->lMarginLogo
= 18;
157 $this->tMarginName
= 20;
159 $x = $this->pdf
->GetAbsX();
160 $y = $this->pdf
->getY();
163 if (CRM_Utils_Array
::value('image_1', $formattedRow)) {
164 $this->printImage($formattedRow['image_1'], NULL, NULL, CRM_Utils_Array
::value('width_image_1', $formattedRow),
165 CRM_Utils_Array
::value('height_image_1', $formattedRow));
168 if (CRM_Utils_Array
::value('image_2', $formattedRow)) {
169 $this->printImage($formattedRow['image_2'], $x +
68, NULL, CRM_Utils_Array
::value('width_image_2', $formattedRow),
170 CRM_Utils_Array
::value('height_image_2', $formattedRow));
173 if ((CRM_Utils_Array
::value('height_image_1', $formattedRow) >
174 CRM_Utils_Array
::value('height_image_2', $formattedRow)) &&
175 CRM_Utils_Array
::value('height_image_1', $formattedRow)) {
176 $startOffset = CRM_Utils_Array
::value('height_image_1', $formattedRow);
178 elseif (CRM_Utils_Array
::value('height_image_2', $formattedRow)) {
179 $startOffset = CRM_Utils_Array
::value('height_image_2', $formattedRow);
182 $this->pdf
->SetLineStyle(array(
187 'color' => array(0, 0, 200)
190 $rowCount = CRM_Badge_Form_Layout
::FIELD_ROWCOUNT
;
191 for ($i = 1; $i <= $rowCount; $i++
) {
192 if (!empty($formattedRow['token'][$i]['token'])) {
194 if ($formattedRow['token'][$i]['token'] != 'spacer') {
195 $value = $formattedRow['token'][$i]['value'];
198 $offset = $this->pdf
->getY() +
$startOffset +
$cellspacing;
200 $this->pdf
->SetFont($formattedRow['token'][$i]['font_name'], $formattedRow['token'][$i]['font_style'],
201 $formattedRow['token'][$i]['font_size']);
202 $this->pdf
->MultiCell($this->pdf
->width
, 0, $value,
203 $this->border
, $formattedRow['token'][$i]['text_alignment'], 0, 1, $x, $offset);
205 // set this to zero so that it is added only for first element
210 if (CRM_Utils_Array
::value('barcode', $formattedRow)) {
211 $data = $formattedRow['values'];
213 if ($formattedRow['barcode']['type'] == 'barcode') {
214 $data['current_value'] =
215 $formattedRow['values']['contact_id'] . '-' . $formattedRow['values']['participant_id'];
218 // view participant url
219 $data['current_value'] = CRM_Utils_System
::url('civicrm/contact/view/participant',
220 'action=view&reset=1&cid=' . $formattedRow['values']['contact_id'] . '&id='
221 . $formattedRow['values']['participant_id'],
228 // call hook alterBarcode
229 CRM_Utils_Hook
::alterBarcode($data, $formattedRow['barcode']['type']);
231 if ($formattedRow['barcode']['type'] == 'barcode') {
235 switch ($formattedRow['barcode']['alignment']) {
252 'cellfitalign' => '',
255 'vpadding' => 'auto',
256 'fgcolor' => array(0, 0, 0),
259 'font' => 'helvetica',
264 $this->pdf
->write1DBarcode($data['current_value'], 'C128', $xAlign, $y +
$this->pdf
->height
- 10, '70',
265 12, 0.4, $style, 'B');
271 switch ($formattedRow['barcode']['alignment']) {
286 'vpadding' => 'auto',
287 'fgcolor' => array(0,0,0),
292 $this->pdf
->write2DBarcode($data['current_value'], 'QRCODE,H', $xAlign, $y +
$this->pdf
->height
- 26, 30,
299 * Helper function to print images
300 * @param string $img image url
305 function printImage($img, $x = '', $y = '', $w = NULL, $h = NULL) {
307 $x = $this->pdf
->GetAbsX();
311 $y = $this->pdf
->GetY();
317 list($w, $h) = self
::getImageProperties($img, $this->imgRes
, $w, $h);
318 $this->pdf
->Image($img, $x, $y, $w, $h, '', '', '', FALSE, 72, '', FALSE,
319 FALSE, $this->debug
, FALSE, FALSE, FALSE);
321 $this->pdf
->SetXY($x, $y);
324 static function getImageProperties($img, $imgRes = 300, $w = NULL, $h = NULL) {
325 $imgsize = getimagesize($img);
327 $w = !empty($w) ?
$w : $imgsize[0] / $f;
328 $h = !empty($h) ?
$h : $imgsize[1] / $f;
329 return array($w, $h);
333 * function to build badges parameters before actually creating badges.
335 * @param array $params associated array of submitted values
336 * @params object $form form/controller object
342 public static function buildBadges(&$params, &$form) {
343 // get name badge layout info
344 $layoutInfo = CRM_Badge_BAO_Layout
::buildLayout($params);
346 // spit / get actual field names from token
347 $returnProperties = array();
348 if (!empty($layoutInfo['data']['token'])) {
349 foreach ($layoutInfo['data']['token'] as $index => $value) {
352 $token = CRM_Utils_Token
::getTokens($value);
353 if (key($token) == 'contact') {
354 $element = $token['contact'][0];
356 elseif (key($token) == 'event') {
357 $element = $token['event'][0];
358 //FIX ME - we need to standardize event token names
359 if (substr($element, 0, 6) != 'event_') {
360 $element = 'event_' . $element;
363 elseif (key($token) == 'participant') {
364 $element = $token['participant'][0];
367 // build returnproperties for query
368 $returnProperties[$element] = 1;
371 // add actual field name to row element
372 $layoutInfo['data']['rowElements'][$index] = $element;
376 // add additional required fields for query execution
377 $additionalFields = array('participant_register_date', 'participant_id', 'event_id', 'contact_id');
378 foreach ($additionalFields as $field) {
379 $returnProperties[$field] = 1;
382 if ($form->_single
) {
386 $queryParams = $form->get('queryParams');
389 $query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, NULL, FALSE, FALSE,
390 CRM_Contact_BAO_Query
::MODE_EVENT
393 list($select, $from, $where, $having) = $query->query();
395 $where = "WHERE {$form->_componentClause}";
398 $where .= " AND {$form->_componentClause}";
402 if ($form->get(CRM_Utils_Sort
::SORT_ORDER
)) {
403 $sortOrder = $form->get(CRM_Utils_Sort
::SORT_ORDER
);
404 if (!empty($sortOrder)) {
405 $sortOrder = " ORDER BY $sortOrder";
408 $queryString = "$select $from $where $having $sortOrder";
410 $dao = CRM_Core_DAO
::executeQuery($queryString);
412 while ($dao->fetch()) {
413 $rows[$dao->participant_id
] = array();
414 foreach ($returnProperties as $key => $dontCare) {
415 $rows[$dao->participant_id
][$key] = isset($dao->$key) ?
$dao->$key : NULL;
419 $eventBadgeClass = new CRM_Badge_BAO_Badge();
420 $eventBadgeClass->createLabels($rows, $layoutInfo);