Merge pull request #13596 from jackrabbithanna/726-address-api-states
[civicrm-core.git] / CRM / Utils / PDF / Label.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 * @package CRM
33 * @copyright CiviCRM LLC (c) 2004-2019
34 */
35
36 /**
37 * Class CRM_Utils_PDF_Label
38 */
39 class CRM_Utils_PDF_Label extends TCPDF {
40
41 // make these properties public due to
42 // CRM-5880
43 // Default label format values
44 public $defaults;
45 // Current label format values
46 public $format;
47 // Name of format
48 public $formatName;
49 // Left margin of labels
50 public $marginLeft;
51 // Top margin of labels
52 public $marginTop;
53 // Horizontal space between 2 labels
54 public $xSpace;
55 // Vertical space between 2 labels
56 public $ySpace;
57 // Number of labels horizontally
58 public $xNumber;
59 // Number of labels vertically
60 public $yNumber;
61 // Width of label
62 public $width;
63 // Height of label
64 public $height;
65 // Line Height of label - used in event code
66 public $lineHeight = 0;
67 // Space between text and left edge of label
68 public $paddingLeft;
69 // Space between text and top edge of label
70 public $paddingTop;
71 // Character size (in points)
72 public $charSize;
73 // Metric used for all PDF doc measurements
74 public $metricDoc;
75 // Name of the font
76 public $fontName;
77 // 'B' bold, 'I' italic, 'BI' bold+italic
78 public $fontStyle;
79 // Paper size name
80 public $paperSize;
81 // Paper orientation
82 public $orientation;
83 // Paper dimensions array (w, h)
84 public $paper_dimensions;
85 // Counter for positioning labels
86 public $countX = 0;
87 // Counter for positioning labels
88 public $countY = 0;
89
90 /**
91 * Constructor.
92 *
93 * @param $format
94 * Either the name of a Label Format in the Option Value table.
95 * or an array of Label Format values.
96 * @param string|\Unit $unit Unit of measure for the PDF document
97 */
98 public function __construct($format, $unit = 'mm') {
99 if (is_array($format)) {
100 // Custom format
101 $tFormat = $format;
102 }
103 else {
104 // Saved format
105 $tFormat = CRM_Core_BAO_LabelFormat::getByName($format);
106 }
107
108 $this->LabelSetFormat($tFormat, $unit);
109 parent::__construct($this->orientation, $this->metricDoc, $this->paper_dimensions);
110 $this->generatorMethod = NULL;
111 $this->SetFont($this->fontName, $this->fontStyle);
112 $this->SetFontSize($this->charSize);
113 $this->SetMargins(0, 0);
114 $this->SetAutoPageBreak(FALSE);
115 $this->setPrintHeader(FALSE);
116 $this->setPrintFooter(FALSE);
117 }
118
119 /**
120 * @param $objectinstance
121 * @param string $methodname
122 */
123 public function SetGenerator($objectinstance, $methodname = 'generateLabel') {
124 $this->generatorMethod = $methodname;
125 $this->generatorObject = $objectinstance;
126 }
127
128 /**
129 * @param string $name
130 * @param bool $convert
131 *
132 * @return float|int|mixed
133 */
134 public function getFormatValue($name, $convert = FALSE) {
135 if (isset($this->format[$name])) {
136 $value = $this->format[$name];
137 $metric = $this->format['metric'];
138 }
139 else {
140 $value = CRM_Utils_Array::value($name, $this->defaults);
141 $metric = $this->defaults['metric'];
142 }
143 if ($convert) {
144 $value = CRM_Utils_PDF_Utils::convertMetric($value, $metric, $this->metricDoc);
145 }
146 return $value;
147 }
148
149 /**
150 * initialize label format settings.
151 *
152 * @param $format
153 * @param $unit
154 */
155 public function LabelSetFormat(&$format, $unit) {
156 $this->defaults = CRM_Core_BAO_LabelFormat::getDefaultValues();
157 $this->format = &$format;
158 $this->formatName = $this->getFormatValue('name');
159 $this->paperSize = $this->getFormatValue('paper-size');
160 $this->orientation = $this->getFormatValue('orientation');
161 $this->fontName = $this->getFormatValue('font-name');
162 $this->charSize = $this->getFormatValue('font-size');
163 $this->fontStyle = $this->getFormatValue('font-style');
164 $this->xNumber = $this->getFormatValue('NX');
165 $this->yNumber = $this->getFormatValue('NY');
166 $this->metricDoc = $unit;
167 $this->marginLeft = $this->getFormatValue('lMargin', TRUE);
168 $this->marginTop = $this->getFormatValue('tMargin', TRUE);
169 $this->xSpace = $this->getFormatValue('SpaceX', TRUE);
170 $this->ySpace = $this->getFormatValue('SpaceY', TRUE);
171 $this->width = $this->getFormatValue('width', TRUE);
172 $this->height = $this->getFormatValue('height', TRUE);
173 $this->paddingLeft = $this->getFormatValue('lPadding', TRUE);
174 $this->paddingTop = $this->getFormatValue('tPadding', TRUE);
175 $paperSize = CRM_Core_BAO_PaperSize::getByName($this->paperSize);
176 $w = CRM_Utils_PDF_Utils::convertMetric($paperSize['width'], $paperSize['metric'], $this->metricDoc);
177 $h = CRM_Utils_PDF_Utils::convertMetric($paperSize['height'], $paperSize['metric'], $this->metricDoc);
178 $this->paper_dimensions = array($w, $h);
179 }
180
181 /**
182 * Generate the pdf of one label (can be modified using SetGenerator)
183 *
184 * @param string $text
185 */
186 public function generateLabel($text) {
187 // paddingLeft is used for both left & right padding so needs to be
188 // subtracted twice from width to get the width that is available for text
189 $args = array(
190 'w' => $this->width - 2 * $this->paddingLeft,
191 'h' => 0,
192 'txt' => $text,
193 'border' => 0,
194 'align' => 'L',
195 'fill' => 0,
196 'ln' => 0,
197 'x' => '',
198 'y' => '',
199 'reseth' => TRUE,
200 'stretch' => 0,
201 'ishtml' => FALSE,
202 'autopadding' => FALSE,
203 'maxh' => $this->height,
204 );
205
206 CRM_Utils_Hook::alterMailingLabelParams($args);
207
208 if ($args['ishtml'] == TRUE) {
209 $this->writeHTMLCell($args['w'], $args['h'],
210 $args['x'], $args['y'],
211 $args['txt'], $args['border'],
212 $args['ln'], $args['fill'],
213 $args['reseth'], $args['align'],
214 $args['autopadding']
215 );
216 }
217 else {
218 $this->multiCell($args['w'], $args['h'],
219 $args['txt'], $args['border'],
220 $args['align'], $args['fill'],
221 $args['ln'], $args['x'],
222 $args['y'], $args['reseth'],
223 $args['stretch'], $args['ishtml'],
224 $args['autopadding'], $args['maxh']
225 );
226 }
227 }
228
229 /**
230 * Print a label.
231 *
232 * @param $texte
233 */
234 public function AddPdfLabel($texte) {
235 if ($this->countX == $this->xNumber) {
236 // Page full, we start a new one
237 $this->AddPage();
238 $this->countX = 0;
239 $this->countY = 0;
240 }
241
242 $posX = $this->marginLeft + ($this->countX * ($this->width + $this->xSpace));
243 $posY = $this->marginTop + ($this->countY * ($this->height + $this->ySpace));
244 $this->SetXY($posX + $this->paddingLeft, $posY + $this->paddingTop);
245 if ($this->generatorMethod) {
246 call_user_func_array(array($this->generatorObject, $this->generatorMethod), array($texte));
247 }
248 else {
249 $this->generateLabel($texte);
250 }
251 $this->countY++;
252
253 if ($this->countY == $this->yNumber) {
254 // End of column reached, we start a new one
255 $this->countX++;
256 $this->countY = 0;
257 }
258 }
259
260 /**
261 * Get the available font names.
262 *
263 * @return array
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 = Civi::settings()->get('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
292 }