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 'text_alignment' => $layout['data']['text_alignment'][$key],
107 if (CRM_Utils_Array
::value('image_1', $layout['data'])) {
108 $formattedRow['image_1'] = $layout['data']['image_1'];
111 if (CRM_Utils_Array
::value('image_2', $layout['data'])) {
112 $formattedRow['image_2'] = $layout['data']['image_2'];
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'],
122 // finally assign all the row values, so that we can use it for barcode etc
123 $formattedRow['values'] = $row;
125 return $formattedRow;
128 public function generateLabel($formattedRow) {
129 switch ($formattedRow['labelFormat']) {
132 self
::labelAvery5395($formattedRow);
137 public function labelAvery5395(&$formattedRow) {
138 $this->lMarginLogo
= 18;
139 $this->tMarginName
= 20;
141 $x = $this->pdf
->GetAbsX();
142 $y = $this->pdf
->GetY();
144 $titleWidth = $titleLeftMargin = 0;
145 if (CRM_Utils_Array
::value('image_1', $formattedRow)) {
146 $this->printImage($formattedRow['image_1']);
147 $titleWidth = $titleLeftMargin = $this->lMarginLogo
;
150 $titleRightMargin = 0;
151 if (CRM_Utils_Array
::value('image_2', $formattedRow)) {
152 $this->printImage($formattedRow['image_2'], $x +
68);
153 $titleRightMargin = 36;
154 $titleWidth = $this->lMarginLogo
;
157 $this->pdf
->SetLineStyle(array(
162 'color' => array(0, 0, 200)
165 if ($titleLeftMargin && $titleRightMargin) {
166 $titleWidth = $titleRightMargin;
169 $this->pdf
->SetFont($formattedRow['token'][1]['font_name'], '', $formattedRow['token'][1]['font_size']);
170 $this->pdf
->MultiCell($this->pdf
->width
- $titleWidth, 0, $formattedRow['token'][1]['value'],
171 $this->border
, $formattedRow['token'][1]['text_alignment'], 0, 1, $x +
$titleLeftMargin, $y);
173 $this->pdf
->SetFont($formattedRow['token'][2]['font_name'], '', $formattedRow['token'][2]['font_size']);
174 $this->pdf
->MultiCell($this->pdf
->width
, 10, $formattedRow['token'][2]['value'],
175 $this->border
, $formattedRow['token'][2]['text_alignment'], 0, 1, $x, $y +
$this->tMarginName
);
177 $this->pdf
->SetFont($formattedRow['token'][3]['font_name'], '', $formattedRow['token'][3]['font_size']);
178 $this->pdf
->MultiCell($this->pdf
->width
, 0, $formattedRow['token'][3]['value'],
179 $this->border
, $formattedRow['token'][3]['text_alignment'], 0, 1, $x, $this->pdf
->getY());
181 $this->pdf
->SetFont($formattedRow['token'][4]['font_name'], '', $formattedRow['token'][4]['font_size']);
182 $this->pdf
->MultiCell($this->pdf
->width
, 0, $formattedRow['token'][4]['value'],
183 $this->border
, $formattedRow['token'][4]['text_alignment'], 0, 1, $x, $y +
$this->pdf
->height
- 5);
185 if (CRM_Utils_Array
::value('barcode', $formattedRow)) {
186 $data = $formattedRow['values'];
188 if ($formattedRow['barcode']['type'] == 'barcode') {
189 $data['current_value'] =
190 $formattedRow['values']['contact_id'] . '-' . $formattedRow['values']['participant_id'];
193 // view participant url
194 $data['current_value'] = CRM_Utils_System
::url('civicrm/contact/view/participant',
195 'action=view&reset=1&cid=' . $formattedRow['values']['contact_id'] . '&id='
196 . $formattedRow['values']['participant_id'],
203 // call hook alterBarcode
204 CRM_Utils_Hook
::alterBarcode($data, $formattedRow['barcode']['type']);
206 if ($formattedRow['barcode']['type'] == 'barcode') {
210 switch ($formattedRow['barcode']['alignment']) {
227 'cellfitalign' => '',
230 'vpadding' => 'auto',
231 'fgcolor' => array(0, 0, 0),
234 'font' => 'helvetica',
239 $this->pdf
->write1DBarcode($data['current_value'], 'C128', $xAlign, $y +
$this->pdf
->height
- 10, '70',
240 12, 0.4, $style, 'B');
246 switch ($formattedRow['barcode']['alignment']) {
261 'vpadding' => 'auto',
262 'fgcolor' => array(0,0,0),
267 $this->pdf
->write2DBarcode($data['current_value'], 'QRCODE,H', $xAlign, $y +
$this->pdf
->height
- 26, 30,
274 * Helper function to print images
275 * @param string $img image url
280 function printImage($img, $x = '', $y = '') {
282 $x = $this->pdf
->GetAbsX();
286 $y = $this->pdf
->GetY();
292 $imgsize = getimagesize($img);
294 $f = $this->imgRes
/ 25.4;
295 $w = $imgsize[0] / $f;
296 $h = $imgsize[1] / $f;
297 $this->pdf
->Image($img, $x, $y, $w, $h, '', '', '', FALSE, 72, '', FALSE,
298 FALSE, $this->debug
, FALSE, FALSE, FALSE);
300 $this->pdf
->SetXY($x, $y);
304 * function to build badges parameters before actually creating badges.
306 * @param array $params associated array of submitted values
307 * @params object $form form/controller object
313 public static function buildBadges(&$params, &$form) {
314 // get name badge layout info
315 $layoutInfo = CRM_Badge_BAO_Layout
::buildLayout($params);
317 // spit / get actual field names from token
318 $returnProperties = array();
319 if (!empty($layoutInfo['data']['token'])) {
320 foreach ($layoutInfo['data']['token'] as $index => $value) {
323 $token = CRM_Utils_Token
::getTokens($value);
324 if (key($token) == 'contact') {
325 $element = $token['contact'][0];
327 elseif (key($token) == 'event') {
328 $element = $token['event'][0];
329 //FIX ME - we need to standardize event token names
330 if (!strpos($element, 'event_')) {
331 $element = 'event_' . $element;
334 elseif (key($token) == 'participant') {
335 $element = $token['participant'][0];
338 // build returnproperties for query
339 $returnProperties[$element] = 1;
342 // add actual field name to row element
343 $layoutInfo['data']['rowElements'][$index] = $element;
347 // add additional required fields for query execution
348 $additionalFields = array('participant_register_date', 'participant_id', 'event_id', 'contact_id');
349 foreach ($additionalFields as $field) {
350 $returnProperties[$field] = 1;
353 if ($form->_single
) {
357 $queryParams = $form->get('queryParams');
360 $query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, NULL, FALSE, FALSE,
361 CRM_Contact_BAO_Query
::MODE_EVENT
364 list($select, $from, $where, $having) = $query->query();
366 $where = "WHERE {$form->_componentClause}";
369 $where .= " AND {$form->_componentClause}";
373 if ($form->get(CRM_Utils_Sort
::SORT_ORDER
)) {
374 $sortOrder = $form->get(CRM_Utils_Sort
::SORT_ORDER
);
375 if (!empty($sortOrder)) {
376 $sortOrder = " ORDER BY $sortOrder";
379 $queryString = "$select $from $where $having $sortOrder";
381 $dao = CRM_Core_DAO
::executeQuery($queryString);
383 while ($dao->fetch()) {
384 $rows[$dao->participant_id
] = array();
385 foreach ($returnProperties as $key => $dontCare) {
386 $rows[$dao->participant_id
][$key] = isset($dao->$key) ?
$dao->$key : NULL;
390 $eventBadgeClass = new CRM_Badge_BAO_Badge();
391 $eventBadgeClass->createLabels($rows, $layoutInfo);