Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * Class to print labels in Avery or custom formats | |
30 | * functionality and smarts to the base PDF_Label. | |
31 | * | |
06b69b18 | 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
33 | * |
34 | * | |
35 | */ | |
36 | ||
37 | require_once 'tcpdf/tcpdf.php'; | |
5bc392e6 EM |
38 | |
39 | /** | |
40 | * Class CRM_Utils_PDF_Label | |
41 | */ | |
6a488035 TO |
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; | |
a5611c8e DL |
68 | // Line Height of label - used in event code |
69 | public $lineHeight = 0; | |
6a488035 TO |
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 Either the name of a Label Format in the Option Value table | |
97 | * or an array of Label Format values. | |
77b97be7 | 98 | * @param string|\Unit $unit Unit of measure for the PDF document |
6a488035 TO |
99 | * |
100 | * @access public | |
101 | */ | |
102 | 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 | ||
5bc392e6 EM |
123 | /** |
124 | * @param $objectinstance | |
125 | * @param string $methodname | |
126 | */ | |
6a488035 TO |
127 | function SetGenerator($objectinstance, $methodname = 'generateLabel') { |
128 | $this->generatorMethod = $methodname; | |
129 | $this->generatorObject = $objectinstance; | |
130 | } | |
131 | ||
5bc392e6 EM |
132 | /** |
133 | * @param $name | |
134 | * @param bool $convert | |
135 | * | |
136 | * @return float|int|mixed | |
137 | */ | |
6a488035 TO |
138 | function getFormatValue($name, $convert = FALSE) { |
139 | if (isset($this->format[$name])) { | |
140 | $value = $this->format[$name]; | |
141 | $metric = $this->format['metric']; | |
142 | } | |
a5611c8e | 143 | else { |
6a488035 TO |
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 | * Function to initialize label format settings | |
155 | */ | |
5bc392e6 EM |
156 | /** |
157 | * @param $format | |
158 | * @param $unit | |
159 | */ | |
6a488035 TO |
160 | 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 | * function to Generate the pdf of one label (can be modified using SetGenerator) | |
188 | */ | |
5bc392e6 EM |
189 | /** |
190 | * @param $text | |
191 | */ | |
6a488035 TO |
192 | 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 | * function to Print a label | |
235 | */ | |
5bc392e6 EM |
236 | /** |
237 | * @param $texte | |
238 | */ | |
6a488035 | 239 | function AddPdfLabel($texte) { |
0318fed3 DD |
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 | ||
6a488035 TO |
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 | } | |
6a488035 TO |
263 | } |
264 | ||
265 | 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 | ); | |
d28132a1 DL |
273 | |
274 | ||
275 | // Check to see if we have any additional fonts to add. You can specify more fonts in | |
276 | // civicrm.settings.php via: $config['CiviCRM Preferences']['additional_fonts'] | |
277 | // CRM-13307 | |
278 | $additionalFonts = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'additional_fonts'); | |
279 | if (is_array($additionalFonts)) { | |
280 | $fontLabel = array_merge($fontLabel, $additionalFonts); | |
281 | } | |
282 | ||
6a488035 TO |
283 | $tcpdfFonts = $this->fontlist; |
284 | foreach ($tcpdfFonts as $fontName) { | |
285 | if (array_key_exists($fontName, $fontLabel)) { | |
286 | $list[$fontName] = $fontLabel[$fontName]; | |
287 | } | |
288 | } | |
d28132a1 | 289 | |
6a488035 TO |
290 | return $list; |
291 | } | |
292 | } | |
293 |