Merge pull request #1151 from yashodha/4.4
[civicrm-core.git] / CRM / Badge / BAO / Badge.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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-2013
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
87 if (CRM_Utils_Array::value('rowElements', $layout['data'])) {
88 foreach ($layout['data']['rowElements'] as $key => $element) {
89 $value = '';
90 if ($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, "%e %b");
95 }
96 }
97
98 $formattedRow['token'][$key] = array(
99 'value' => $value,
100 'font_name' => $layout['data']['font_name'][$key],
101 'font_size' => $layout['data']['font_size'][$key],
102 'text_alignment' => $layout['data']['text_alignment'][$key],
103 );
104 }
105 }
106
107 if (CRM_Utils_Array::value('image_1', $layout['data'])) {
108 $formattedRow['image_1'] = $layout['data']['image_1'];
109 }
110
111 if (CRM_Utils_Array::value('image_2', $layout['data'])) {
112 $formattedRow['image_2'] = $layout['data']['image_2'];
113 }
114
115 if (CRM_Utils_Array::value('add_barcode', $layout['data'])) {
116 $formattedRow['barcode'] = $layout['data']['barcode_alignment'];
117 }
118
119 // finally assign all the row values, so that we can use it for barcode etc
120 $formattedRow['values'] = $row;
121
122 return $formattedRow;
123 }
124
125 public function generateLabel($formattedRow) {
126 switch ($formattedRow['labelFormat']) {
127 case 'Avery 5395':
128 self::labelAvery5395($formattedRow);
129 break;
130 }
131 }
132
133 public function labelAvery5395(&$formattedRow) {
134 $this->lMarginLogo = 18;
135 $this->tMarginName = 20;
136
137 $x = $this->pdf->GetAbsX();
138 $y = $this->pdf->GetY();
139
140 $titleWidth = $titleLeftMargin = 0;
141 if (CRM_Utils_Array::value('image_1', $formattedRow)) {
142 $this->printImage($formattedRow['image_1']);
143 $titleWidth = $titleLeftMargin = $this->lMarginLogo;
144 }
145
146 $titleRightMargin = 0;
147 if (CRM_Utils_Array::value('image_2', $formattedRow)) {
148 $this->printImage($formattedRow['image_2'], $x + 68);
149 $titleRightMargin = 36;
150 $titleWidth = $this->lMarginLogo;
151 }
152
153 $this->pdf->SetLineStyle(array(
154 'width' => 0.1,
155 'cap' => 'round',
156 'join' => 'round',
157 'dash' => '2,2',
158 'color' => array(0, 0, 200)
159 ));
160
161 if ($titleLeftMargin && $titleRightMargin) {
162 $titleWidth = $titleRightMargin;
163 }
164
165 $this->pdf->SetFont($formattedRow['token'][1]['font_name'], '', $formattedRow['token'][1]['font_size']);
166 $this->pdf->MultiCell($this->pdf->width - $titleWidth, 0, $formattedRow['token'][1]['value'],
167 $this->border, $formattedRow['token'][1]['text_alignment'], 0, 1, $x + $titleLeftMargin, $y);
168
169 $this->pdf->SetFont($formattedRow['token'][2]['font_name'], '', $formattedRow['token'][2]['font_size']);
170 $this->pdf->MultiCell($this->pdf->width, 10, $formattedRow['token'][2]['value'],
171 $this->border, $formattedRow['token'][2]['text_alignment'], 0, 1, $x, $y + $this->tMarginName);
172
173 $this->pdf->SetFont($formattedRow['token'][3]['font_name'], '', $formattedRow['token'][3]['font_size']);
174 $this->pdf->MultiCell($this->pdf->width, 0, $formattedRow['token'][3]['value'],
175 $this->border, $formattedRow['token'][3]['text_alignment'], 0, 1, $x, $this->pdf->getY());
176
177 $this->pdf->SetFont($formattedRow['token'][4]['font_name'], '', $formattedRow['token'][4]['font_size']);
178 $this->pdf->MultiCell($this->pdf->width, 0, $formattedRow['token'][4]['value'],
179 $this->border, $formattedRow['token'][4]['text_alignment'], 0, 1, $x, $y + $this->pdf->height - 5);
180
181 if (CRM_Utils_Array::value('barcode', $formattedRow)) {
182 $style = array(
183 'position' => '',
184 'align' => '',
185 'stretch' => FALSE,
186 'fitwidth' => TRUE,
187 'cellfitalign' => '',
188 'border' => FALSE,
189 'hpadding' => 13.5,
190 'vpadding' => 'auto',
191 'fgcolor' => array(0, 0, 0),
192 'bgcolor' => FALSE,
193 'text' => FALSE,
194 'font' => 'helvetica',
195 'fontsize' => 8,
196 'stretchtext' => 0,
197 );
198
199 // barcode position
200 $xAlign = $x;
201 if ($formattedRow['barcode'] == 'L') {
202 $xAlign += -12;
203 }
204 elseif ($formattedRow['barcode'] == 'R') {
205 $xAlign += 30;
206 }
207 elseif ($formattedRow['barcode'] == 'C') {
208 $xAlign += 15;
209 }
210
211 $data = $formattedRow['values'];
212 $data['current_value'] = $formattedRow['values']['contact_id'] . '-' . $formattedRow['values']['participant_id'];
213
214 // call hook alterBarcode
215 CRM_Utils_Hook::alterBarcode($data);
216
217 $this->pdf->write1DBarcode($data['current_value'], 'C128', $xAlign, $y + $this->pdf->height - 10, '',
218 12, 0.4, $style, 'B');
219 }
220 }
221
222 /**
223 * Helper function to print images
224 * @param string $img image url
225 *
226 * @return void
227 * @access public
228 */
229 function printImage($img, $x = '', $y = '') {
230 if (!$x) {
231 $x = $this->pdf->GetAbsX();
232 }
233
234 if (!$y) {
235 $y = $this->pdf->GetY();
236 }
237
238 $this->imgRes = 300;
239
240 if ($img) {
241 $imgsize = getimagesize($img);
242 // mm
243 $f = $this->imgRes / 25.4;
244 $w = $imgsize[0] / $f;
245 $h = $imgsize[1] / $f;
246 $this->pdf->Image($img, $x, $y, $w, $h, '', '', '', FALSE, 72, '', FALSE,
247 FALSE, $this->debug, FALSE, FALSE, FALSE);
248 }
249 $this->pdf->SetXY($x, $y);
250 }
251 }
252