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'; | |
38 | class CRM_Utils_PDF_Label extends TCPDF { | |
39 | ||
40 | // make these properties public due to | |
41 | // CRM-5880 | |
42 | // Default label format values | |
43 | public $defaults; | |
44 | // Current label format values | |
45 | public $format; | |
46 | // Name of format | |
47 | public $formatName; | |
48 | // Left margin of labels | |
49 | public $marginLeft; | |
50 | // Top margin of labels | |
51 | public $marginTop; | |
52 | // Horizontal space between 2 labels | |
53 | public $xSpace; | |
54 | // Vertical space between 2 labels | |
55 | public $ySpace; | |
56 | // Number of labels horizontally | |
57 | public $xNumber; | |
58 | // Number of labels vertically | |
59 | public $yNumber; | |
60 | // Width of label | |
61 | public $width; | |
62 | // Height of label | |
63 | public $height; | |
a5611c8e DL |
64 | // Line Height of label - used in event code |
65 | public $lineHeight = 0; | |
6a488035 TO |
66 | // Space between text and left edge of label |
67 | public $paddingLeft; | |
68 | // Space between text and top edge of label | |
69 | public $paddingTop; | |
70 | // Character size (in points) | |
71 | public $charSize; | |
72 | // Metric used for all PDF doc measurements | |
73 | public $metricDoc; | |
74 | // Name of the font | |
75 | public $fontName; | |
76 | // 'B' bold, 'I' italic, 'BI' bold+italic | |
77 | public $fontStyle; | |
78 | // Paper size name | |
79 | public $paperSize; | |
80 | // Paper orientation | |
81 | public $orientation; | |
82 | // Paper dimensions array (w, h) | |
83 | public $paper_dimensions; | |
84 | // Counter for positioning labels | |
85 | public $countX = 0; | |
86 | // Counter for positioning labels | |
87 | public $countY = 0; | |
88 | ||
89 | /** | |
90 | * Constructor | |
91 | * | |
92 | * @param $format Either the name of a Label Format in the Option Value table | |
93 | * or an array of Label Format values. | |
94 | * @param $unit Unit of measure for the PDF document | |
95 | * | |
96 | * @access public | |
97 | */ | |
98 | 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 | function SetGenerator($objectinstance, $methodname = 'generateLabel') { | |
120 | $this->generatorMethod = $methodname; | |
121 | $this->generatorObject = $objectinstance; | |
122 | } | |
123 | ||
124 | function getFormatValue($name, $convert = FALSE) { | |
125 | if (isset($this->format[$name])) { | |
126 | $value = $this->format[$name]; | |
127 | $metric = $this->format['metric']; | |
128 | } | |
a5611c8e | 129 | else { |
6a488035 TO |
130 | $value = CRM_Utils_Array::value($name, $this->defaults); |
131 | $metric = $this->defaults['metric']; | |
132 | } | |
133 | if ($convert) { | |
134 | $value = CRM_Utils_PDF_Utils::convertMetric($value, $metric, $this->metricDoc); | |
135 | } | |
136 | return $value; | |
137 | } | |
138 | ||
139 | /* | |
140 | * Function to initialize label format settings | |
141 | */ | |
142 | function LabelSetFormat(&$format, $unit) { | |
143 | $this->defaults = CRM_Core_BAO_LabelFormat::getDefaultValues(); | |
144 | $this->format = &$format; | |
145 | $this->formatName = $this->getFormatValue('name'); | |
146 | $this->paperSize = $this->getFormatValue('paper-size'); | |
147 | $this->orientation = $this->getFormatValue('orientation'); | |
148 | $this->fontName = $this->getFormatValue('font-name'); | |
149 | $this->charSize = $this->getFormatValue('font-size'); | |
150 | $this->fontStyle = $this->getFormatValue('font-style'); | |
151 | $this->xNumber = $this->getFormatValue('NX'); | |
152 | $this->yNumber = $this->getFormatValue('NY'); | |
153 | $this->metricDoc = $unit; | |
154 | $this->marginLeft = $this->getFormatValue('lMargin', TRUE); | |
155 | $this->marginTop = $this->getFormatValue('tMargin', TRUE); | |
156 | $this->xSpace = $this->getFormatValue('SpaceX', TRUE); | |
157 | $this->ySpace = $this->getFormatValue('SpaceY', TRUE); | |
158 | $this->width = $this->getFormatValue('width', TRUE); | |
159 | $this->height = $this->getFormatValue('height', TRUE); | |
160 | $this->paddingLeft = $this->getFormatValue('lPadding', TRUE); | |
161 | $this->paddingTop = $this->getFormatValue('tPadding', TRUE); | |
162 | $paperSize = CRM_Core_BAO_PaperSize::getByName($this->paperSize); | |
163 | $w = CRM_Utils_PDF_Utils::convertMetric($paperSize['width'], $paperSize['metric'], $this->metricDoc); | |
164 | $h = CRM_Utils_PDF_Utils::convertMetric($paperSize['height'], $paperSize['metric'], $this->metricDoc); | |
165 | $this->paper_dimensions = array($w, $h); | |
166 | } | |
167 | ||
168 | /* | |
169 | * function to Generate the pdf of one label (can be modified using SetGenerator) | |
170 | */ | |
171 | function generateLabel($text) { | |
172 | $args = array( | |
173 | 'w' => $this->width, | |
174 | 'h' => 0, | |
175 | 'txt' => $text, | |
176 | 'border' => 0, | |
177 | 'align' => 'L', | |
178 | 'fill' => 0, | |
179 | 'ln' => 0, | |
180 | 'x' => '', | |
181 | 'y' => '', | |
182 | 'reseth' => TRUE, | |
183 | 'stretch' => 0, | |
184 | 'ishtml' => FALSE, | |
185 | 'autopadding' => FALSE, | |
186 | 'maxh' => $this->height, | |
187 | ); | |
188 | ||
189 | CRM_Utils_Hook::alterMailingLabelParams($args); | |
190 | ||
191 | if ($args['ishtml'] == TRUE) { | |
192 | $this->writeHTMLCell($args['w'], $args['h'], | |
193 | $args['x'], $args['y'], | |
194 | $args['txt'], $args['border'], | |
195 | $args['ln'], $args['fill'], | |
196 | $args['reseth'], $args['align'], | |
197 | $args['autopadding'] | |
198 | ); | |
199 | } | |
200 | else { | |
201 | $this->multiCell($args['w'], $args['h'], | |
202 | $args['txt'], $args['border'], | |
203 | $args['align'], $args['fill'], | |
204 | $args['ln'], $args['x'], | |
205 | $args['y'], $args['reseth'], | |
206 | $args['stretch'], $args['ishtml'], | |
207 | $args['autopadding'], $args['maxh'] | |
208 | ); | |
209 | } | |
210 | } | |
211 | ||
212 | /* | |
213 | * function to Print a label | |
214 | */ | |
215 | function AddPdfLabel($texte) { | |
0318fed3 DD |
216 | if ($this->countX == $this->xNumber) { |
217 | // Page full, we start a new one | |
218 | $this->AddPage(); | |
219 | $this->countX = 0; | |
220 | $this->countY = 0; | |
221 | } | |
222 | ||
6a488035 TO |
223 | $posX = $this->marginLeft + ($this->countX * ($this->width + $this->xSpace)); |
224 | $posY = $this->marginTop + ($this->countY * ($this->height + $this->ySpace)); | |
225 | $this->SetXY($posX + $this->paddingLeft, $posY + $this->paddingTop); | |
226 | if ($this->generatorMethod) { | |
227 | call_user_func_array(array($this->generatorObject, $this->generatorMethod), array($texte)); | |
228 | } | |
229 | else { | |
230 | $this->generateLabel($texte); | |
231 | } | |
232 | $this->countY++; | |
233 | ||
234 | if ($this->countY == $this->yNumber) { | |
235 | // End of column reached, we start a new one | |
236 | $this->countX++; | |
237 | $this->countY = 0; | |
238 | } | |
6a488035 TO |
239 | } |
240 | ||
241 | function getFontNames() { | |
242 | // Define labels for TCPDF core fonts | |
243 | $fontLabel = array( | |
244 | 'courier' => ts('Courier'), | |
245 | 'helvetica' => ts('Helvetica'), | |
246 | 'times' => ts('Times New Roman'), | |
247 | 'dejavusans' => ts('Deja Vu Sans (UTF-8)'), | |
248 | ); | |
d28132a1 DL |
249 | |
250 | ||
251 | // Check to see if we have any additional fonts to add. You can specify more fonts in | |
252 | // civicrm.settings.php via: $config['CiviCRM Preferences']['additional_fonts'] | |
253 | // CRM-13307 | |
254 | $additionalFonts = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'additional_fonts'); | |
255 | if (is_array($additionalFonts)) { | |
256 | $fontLabel = array_merge($fontLabel, $additionalFonts); | |
257 | } | |
258 | ||
6a488035 TO |
259 | $tcpdfFonts = $this->fontlist; |
260 | foreach ($tcpdfFonts as $fontName) { | |
261 | if (array_key_exists($fontName, $fontLabel)) { | |
262 | $list[$fontName] = $fontLabel[$fontName]; | |
263 | } | |
264 | } | |
d28132a1 | 265 | |
6a488035 TO |
266 | return $list; |
267 | } | |
268 | } | |
269 |