INFRA-132 - Change "else if" to "elseif"
[civicrm-core.git] / CRM / Utils / PDF / Label.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class to print labels in Avery or custom formats
30 * functionality and smarts to the base PDF_Label.
31 *
32 * @copyright CiviCRM LLC (c) 2004-2014
33 *
34 *
35 */
36
37 require_once 'tcpdf/tcpdf.php';
38
39 /**
40 * Class CRM_Utils_PDF_Label
41 */
42 class CRM_Utils_PDF_Label extends TCPDF {
43
44 // make these properties public due to
45 // CRM-5880
46 // Default label format values
47 public $defaults;
48 // Current label format values
49 public $format;
50 // Name of format
51 public $formatName;
52 // Left margin of labels
53 public $marginLeft;
54 // Top margin of labels
55 public $marginTop;
56 // Horizontal space between 2 labels
57 public $xSpace;
58 // Vertical space between 2 labels
59 public $ySpace;
60 // Number of labels horizontally
61 public $xNumber;
62 // Number of labels vertically
63 public $yNumber;
64 // Width of label
65 public $width;
66 // Height of label
67 public $height;
68 // Line Height of label - used in event code
69 public $lineHeight = 0;
70 // Space between text and left edge of label
71 public $paddingLeft;
72 // Space between text and top edge of label
73 public $paddingTop;
74 // Character size (in points)
75 public $charSize;
76 // Metric used for all PDF doc measurements
77 public $metricDoc;
78 // Name of the font
79 public $fontName;
80 // 'B' bold, 'I' italic, 'BI' bold+italic
81 public $fontStyle;
82 // Paper size name
83 public $paperSize;
84 // Paper orientation
85 public $orientation;
86 // Paper dimensions array (w, h)
87 public $paper_dimensions;
88 // Counter for positioning labels
89 public $countX = 0;
90 // Counter for positioning labels
91 public $countY = 0;
92
93 /**
94 * Constructor
95 *
96 * @param $format
97 * Either the name of a Label Format in the Option Value table.
98 * or an array of Label Format values.
99 * @param string|\Unit $unit Unit of measure for the PDF document
100 *
101 */
102 public function __construct($format, $unit = 'mm') {
103 if (is_array($format)) {
104 // Custom format
105 $tFormat = $format;
106 }
107 else {
108 // Saved format
109 $tFormat = CRM_Core_BAO_LabelFormat::getByName($format);
110 }
111
112 $this->LabelSetFormat($tFormat, $unit);
113 parent::__construct($this->orientation, $this->metricDoc, $this->paper_dimensions);
114 $this->generatorMethod = NULL;
115 $this->SetFont($this->fontName, $this->fontStyle);
116 $this->SetFontSize($this->charSize);
117 $this->SetMargins(0, 0);
118 $this->SetAutoPageBreak(FALSE);
119 $this->setPrintHeader(FALSE);
120 $this->setPrintFooter(FALSE);
121 }
122
123 /**
124 * @param $objectinstance
125 * @param string $methodname
126 */
127 public function SetGenerator($objectinstance, $methodname = 'generateLabel') {
128 $this->generatorMethod = $methodname;
129 $this->generatorObject = $objectinstance;
130 }
131
132 /**
133 * @param string $name
134 * @param bool $convert
135 *
136 * @return float|int|mixed
137 */
138 public function getFormatValue($name, $convert = FALSE) {
139 if (isset($this->format[$name])) {
140 $value = $this->format[$name];
141 $metric = $this->format['metric'];
142 }
143 else {
144 $value = CRM_Utils_Array::value($name, $this->defaults);
145 $metric = $this->defaults['metric'];
146 }
147 if ($convert) {
148 $value = CRM_Utils_PDF_Utils::convertMetric($value, $metric, $this->metricDoc);
149 }
150 return $value;
151 }
152
153 /*
154 * initialize label format settings
155 */
156 /**
157 * @param $format
158 * @param $unit
159 */
160 public function LabelSetFormat(&$format, $unit) {
161 $this->defaults = CRM_Core_BAO_LabelFormat::getDefaultValues();
162 $this->format = &$format;
163 $this->formatName = $this->getFormatValue('name');
164 $this->paperSize = $this->getFormatValue('paper-size');
165 $this->orientation = $this->getFormatValue('orientation');
166 $this->fontName = $this->getFormatValue('font-name');
167 $this->charSize = $this->getFormatValue('font-size');
168 $this->fontStyle = $this->getFormatValue('font-style');
169 $this->xNumber = $this->getFormatValue('NX');
170 $this->yNumber = $this->getFormatValue('NY');
171 $this->metricDoc = $unit;
172 $this->marginLeft = $this->getFormatValue('lMargin', TRUE);
173 $this->marginTop = $this->getFormatValue('tMargin', TRUE);
174 $this->xSpace = $this->getFormatValue('SpaceX', TRUE);
175 $this->ySpace = $this->getFormatValue('SpaceY', TRUE);
176 $this->width = $this->getFormatValue('width', TRUE);
177 $this->height = $this->getFormatValue('height', TRUE);
178 $this->paddingLeft = $this->getFormatValue('lPadding', TRUE);
179 $this->paddingTop = $this->getFormatValue('tPadding', TRUE);
180 $paperSize = CRM_Core_BAO_PaperSize::getByName($this->paperSize);
181 $w = CRM_Utils_PDF_Utils::convertMetric($paperSize['width'], $paperSize['metric'], $this->metricDoc);
182 $h = CRM_Utils_PDF_Utils::convertMetric($paperSize['height'], $paperSize['metric'], $this->metricDoc);
183 $this->paper_dimensions = array($w, $h);
184 }
185
186 /*
187 * Generate the pdf of one label (can be modified using SetGenerator)
188 */
189 /**
190 * @param $text
191 */
192 public function generateLabel($text) {
193 $args = array(
194 'w' => $this->width,
195 'h' => 0,
196 'txt' => $text,
197 'border' => 0,
198 'align' => 'L',
199 'fill' => 0,
200 'ln' => 0,
201 'x' => '',
202 'y' => '',
203 'reseth' => TRUE,
204 'stretch' => 0,
205 'ishtml' => FALSE,
206 'autopadding' => FALSE,
207 'maxh' => $this->height,
208 );
209
210 CRM_Utils_Hook::alterMailingLabelParams($args);
211
212 if ($args['ishtml'] == TRUE) {
213 $this->writeHTMLCell($args['w'], $args['h'],
214 $args['x'], $args['y'],
215 $args['txt'], $args['border'],
216 $args['ln'], $args['fill'],
217 $args['reseth'], $args['align'],
218 $args['autopadding']
219 );
220 }
221 else {
222 $this->multiCell($args['w'], $args['h'],
223 $args['txt'], $args['border'],
224 $args['align'], $args['fill'],
225 $args['ln'], $args['x'],
226 $args['y'], $args['reseth'],
227 $args['stretch'], $args['ishtml'],
228 $args['autopadding'], $args['maxh']
229 );
230 }
231 }
232
233 /*
234 * Print a label
235 */
236 /**
237 * @param $texte
238 */
239 public function AddPdfLabel($texte) {
240 if ($this->countX == $this->xNumber) {
241 // Page full, we start a new one
242 $this->AddPage();
243 $this->countX = 0;
244 $this->countY = 0;
245 }
246
247 $posX = $this->marginLeft + ($this->countX * ($this->width + $this->xSpace));
248 $posY = $this->marginTop + ($this->countY * ($this->height + $this->ySpace));
249 $this->SetXY($posX + $this->paddingLeft, $posY + $this->paddingTop);
250 if ($this->generatorMethod) {
251 call_user_func_array(array($this->generatorObject, $this->generatorMethod), array($texte));
252 }
253 else {
254 $this->generateLabel($texte);
255 }
256 $this->countY++;
257
258 if ($this->countY == $this->yNumber) {
259 // End of column reached, we start a new one
260 $this->countX++;
261 $this->countY = 0;
262 }
263 }
264
265 public function getFontNames() {
266 // Define labels for TCPDF core fonts
267 $fontLabel = array(
268 'courier' => ts('Courier'),
269 'helvetica' => ts('Helvetica'),
270 'times' => ts('Times New Roman'),
271 'dejavusans' => ts('Deja Vu Sans (UTF-8)'),
272 );
273
274 // Check to see if we have any additional fonts to add. You can specify more fonts in
275 // civicrm.settings.php via: $config['CiviCRM Preferences']['additional_fonts']
276 // CRM-13307
277 $additionalFonts = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'additional_fonts');
278 if (is_array($additionalFonts)) {
279 $fontLabel = array_merge($fontLabel, $additionalFonts);
280 }
281
282 $tcpdfFonts = $this->fontlist;
283 foreach ($tcpdfFonts as $fontName) {
284 if (array_key_exists($fontName, $fontLabel)) {
285 $list[$fontName] = $fontLabel[$fontName];
286 }
287 }
288
289 return $list;
290 }
291 }