Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
50bfb460 | 29 | * Class to print labels in Avery or custom formats |
6a488035 TO |
30 | * functionality and smarts to the base PDF_Label. |
31 | * | |
50bfb460 | 32 | * @package CRM |
8c9251b3 | 33 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
34 | */ |
35 | ||
5bc392e6 EM |
36 | /** |
37 | * Class CRM_Utils_PDF_Label | |
38 | */ | |
6a488035 TO |
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; | |
a5611c8e DL |
65 | // Line Height of label - used in event code |
66 | public $lineHeight = 0; | |
6a488035 TO |
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 | /** | |
fe482240 | 91 | * Constructor. |
6a488035 | 92 | * |
77855840 TO |
93 | * @param $format |
94 | * Either the name of a Label Format in the Option Value table. | |
6a488035 | 95 | * or an array of Label Format values. |
77b97be7 | 96 | * @param string|\Unit $unit Unit of measure for the PDF document |
6a488035 | 97 | */ |
00be9182 | 98 | public function __construct($format, $unit = 'mm') { |
6a488035 TO |
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 | ||
5bc392e6 EM |
119 | /** |
120 | * @param $objectinstance | |
121 | * @param string $methodname | |
122 | */ | |
00be9182 | 123 | public function SetGenerator($objectinstance, $methodname = 'generateLabel') { |
6a488035 TO |
124 | $this->generatorMethod = $methodname; |
125 | $this->generatorObject = $objectinstance; | |
126 | } | |
127 | ||
5bc392e6 | 128 | /** |
100fef9d | 129 | * @param string $name |
5bc392e6 EM |
130 | * @param bool $convert |
131 | * | |
132 | * @return float|int|mixed | |
133 | */ | |
00be9182 | 134 | public function getFormatValue($name, $convert = FALSE) { |
6a488035 TO |
135 | if (isset($this->format[$name])) { |
136 | $value = $this->format[$name]; | |
137 | $metric = $this->format['metric']; | |
138 | } | |
a5611c8e | 139 | else { |
6a488035 TO |
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 | ||
5bc392e6 | 149 | /** |
fe482240 | 150 | * initialize label format settings. |
d424ffde | 151 | * |
5bc392e6 EM |
152 | * @param $format |
153 | * @param $unit | |
154 | */ | |
00be9182 | 155 | public function LabelSetFormat(&$format, $unit) { |
6a488035 TO |
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 | ||
5bc392e6 | 181 | /** |
d424ffde CW |
182 | * Generate the pdf of one label (can be modified using SetGenerator) |
183 | * | |
184 | * @param string $text | |
5bc392e6 | 185 | */ |
00be9182 | 186 | public function generateLabel($text) { |
6a488035 TO |
187 | $args = array( |
188 | 'w' => $this->width, | |
189 | 'h' => 0, | |
190 | 'txt' => $text, | |
191 | 'border' => 0, | |
192 | 'align' => 'L', | |
193 | 'fill' => 0, | |
194 | 'ln' => 0, | |
195 | 'x' => '', | |
196 | 'y' => '', | |
197 | 'reseth' => TRUE, | |
198 | 'stretch' => 0, | |
199 | 'ishtml' => FALSE, | |
200 | 'autopadding' => FALSE, | |
201 | 'maxh' => $this->height, | |
202 | ); | |
203 | ||
204 | CRM_Utils_Hook::alterMailingLabelParams($args); | |
205 | ||
206 | if ($args['ishtml'] == TRUE) { | |
207 | $this->writeHTMLCell($args['w'], $args['h'], | |
208 | $args['x'], $args['y'], | |
209 | $args['txt'], $args['border'], | |
210 | $args['ln'], $args['fill'], | |
211 | $args['reseth'], $args['align'], | |
212 | $args['autopadding'] | |
213 | ); | |
214 | } | |
215 | else { | |
216 | $this->multiCell($args['w'], $args['h'], | |
217 | $args['txt'], $args['border'], | |
218 | $args['align'], $args['fill'], | |
219 | $args['ln'], $args['x'], | |
220 | $args['y'], $args['reseth'], | |
221 | $args['stretch'], $args['ishtml'], | |
222 | $args['autopadding'], $args['maxh'] | |
223 | ); | |
224 | } | |
225 | } | |
226 | ||
5bc392e6 | 227 | /** |
fe482240 | 228 | * Print a label. |
d424ffde | 229 | * |
5bc392e6 EM |
230 | * @param $texte |
231 | */ | |
00be9182 | 232 | public function AddPdfLabel($texte) { |
0318fed3 DD |
233 | if ($this->countX == $this->xNumber) { |
234 | // Page full, we start a new one | |
235 | $this->AddPage(); | |
236 | $this->countX = 0; | |
237 | $this->countY = 0; | |
238 | } | |
239 | ||
6a488035 TO |
240 | $posX = $this->marginLeft + ($this->countX * ($this->width + $this->xSpace)); |
241 | $posY = $this->marginTop + ($this->countY * ($this->height + $this->ySpace)); | |
242 | $this->SetXY($posX + $this->paddingLeft, $posY + $this->paddingTop); | |
243 | if ($this->generatorMethod) { | |
244 | call_user_func_array(array($this->generatorObject, $this->generatorMethod), array($texte)); | |
245 | } | |
246 | else { | |
247 | $this->generateLabel($texte); | |
248 | } | |
249 | $this->countY++; | |
250 | ||
251 | if ($this->countY == $this->yNumber) { | |
252 | // End of column reached, we start a new one | |
253 | $this->countX++; | |
254 | $this->countY = 0; | |
255 | } | |
6a488035 TO |
256 | } |
257 | ||
f2ac86d1 | 258 | /** |
259 | * Get the available font names. | |
260 | * | |
261 | * @return array | |
262 | */ | |
00be9182 | 263 | public function getFontNames() { |
6a488035 TO |
264 | // Define labels for TCPDF core fonts |
265 | $fontLabel = array( | |
266 | 'courier' => ts('Courier'), | |
267 | 'helvetica' => ts('Helvetica'), | |
268 | 'times' => ts('Times New Roman'), | |
269 | 'dejavusans' => ts('Deja Vu Sans (UTF-8)'), | |
270 | ); | |
d28132a1 | 271 | |
d28132a1 DL |
272 | // Check to see if we have any additional fonts to add. You can specify more fonts in |
273 | // civicrm.settings.php via: $config['CiviCRM Preferences']['additional_fonts'] | |
274 | // CRM-13307 | |
aaffa79f | 275 | $additionalFonts = Civi::settings()->get('additional_fonts'); |
d28132a1 DL |
276 | if (is_array($additionalFonts)) { |
277 | $fontLabel = array_merge($fontLabel, $additionalFonts); | |
278 | } | |
279 | ||
6a488035 TO |
280 | $tcpdfFonts = $this->fontlist; |
281 | foreach ($tcpdfFonts as $fontName) { | |
282 | if (array_key_exists($fontName, $fontLabel)) { | |
283 | $list[$fontName] = $fontLabel[$fontName]; | |
284 | } | |
285 | } | |
d28132a1 | 286 | |
6a488035 TO |
287 | return $list; |
288 | } | |
96025800 | 289 | |
6a488035 | 290 | } |