Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | /** | |
29 | * Class to print labels in Avery or custom formats | |
30 | * functionality and smarts to the base PDF_Label. | |
31 | * | |
e7112fa7 | 32 | * @copyright CiviCRM LLC (c) 2004-2015 |
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 | /** | |
fe482240 | 94 | * Constructor. |
6a488035 | 95 | * |
77855840 TO |
96 | * @param $format |
97 | * Either the name of a Label Format in the Option Value table. | |
6a488035 | 98 | * or an array of Label Format values. |
77b97be7 | 99 | * @param string|\Unit $unit Unit of measure for the PDF document |
6a488035 | 100 | */ |
00be9182 | 101 | public function __construct($format, $unit = 'mm') { |
6a488035 TO |
102 | if (is_array($format)) { |
103 | // Custom format | |
104 | $tFormat = $format; | |
105 | } | |
106 | else { | |
107 | // Saved format | |
108 | $tFormat = CRM_Core_BAO_LabelFormat::getByName($format); | |
109 | } | |
110 | ||
111 | $this->LabelSetFormat($tFormat, $unit); | |
112 | parent::__construct($this->orientation, $this->metricDoc, $this->paper_dimensions); | |
113 | $this->generatorMethod = NULL; | |
114 | $this->SetFont($this->fontName, $this->fontStyle); | |
115 | $this->SetFontSize($this->charSize); | |
116 | $this->SetMargins(0, 0); | |
117 | $this->SetAutoPageBreak(FALSE); | |
118 | $this->setPrintHeader(FALSE); | |
119 | $this->setPrintFooter(FALSE); | |
120 | } | |
121 | ||
5bc392e6 EM |
122 | /** |
123 | * @param $objectinstance | |
124 | * @param string $methodname | |
125 | */ | |
00be9182 | 126 | public function SetGenerator($objectinstance, $methodname = 'generateLabel') { |
6a488035 TO |
127 | $this->generatorMethod = $methodname; |
128 | $this->generatorObject = $objectinstance; | |
129 | } | |
130 | ||
5bc392e6 | 131 | /** |
100fef9d | 132 | * @param string $name |
5bc392e6 EM |
133 | * @param bool $convert |
134 | * | |
135 | * @return float|int|mixed | |
136 | */ | |
00be9182 | 137 | public function getFormatValue($name, $convert = FALSE) { |
6a488035 TO |
138 | if (isset($this->format[$name])) { |
139 | $value = $this->format[$name]; | |
140 | $metric = $this->format['metric']; | |
141 | } | |
a5611c8e | 142 | else { |
6a488035 TO |
143 | $value = CRM_Utils_Array::value($name, $this->defaults); |
144 | $metric = $this->defaults['metric']; | |
145 | } | |
146 | if ($convert) { | |
147 | $value = CRM_Utils_PDF_Utils::convertMetric($value, $metric, $this->metricDoc); | |
148 | } | |
149 | return $value; | |
150 | } | |
151 | ||
5bc392e6 | 152 | /** |
fe482240 | 153 | * initialize label format settings. |
d424ffde | 154 | * |
5bc392e6 EM |
155 | * @param $format |
156 | * @param $unit | |
157 | */ | |
00be9182 | 158 | public function LabelSetFormat(&$format, $unit) { |
6a488035 TO |
159 | $this->defaults = CRM_Core_BAO_LabelFormat::getDefaultValues(); |
160 | $this->format = &$format; | |
161 | $this->formatName = $this->getFormatValue('name'); | |
162 | $this->paperSize = $this->getFormatValue('paper-size'); | |
163 | $this->orientation = $this->getFormatValue('orientation'); | |
164 | $this->fontName = $this->getFormatValue('font-name'); | |
165 | $this->charSize = $this->getFormatValue('font-size'); | |
166 | $this->fontStyle = $this->getFormatValue('font-style'); | |
167 | $this->xNumber = $this->getFormatValue('NX'); | |
168 | $this->yNumber = $this->getFormatValue('NY'); | |
169 | $this->metricDoc = $unit; | |
170 | $this->marginLeft = $this->getFormatValue('lMargin', TRUE); | |
171 | $this->marginTop = $this->getFormatValue('tMargin', TRUE); | |
172 | $this->xSpace = $this->getFormatValue('SpaceX', TRUE); | |
173 | $this->ySpace = $this->getFormatValue('SpaceY', TRUE); | |
174 | $this->width = $this->getFormatValue('width', TRUE); | |
175 | $this->height = $this->getFormatValue('height', TRUE); | |
176 | $this->paddingLeft = $this->getFormatValue('lPadding', TRUE); | |
177 | $this->paddingTop = $this->getFormatValue('tPadding', TRUE); | |
178 | $paperSize = CRM_Core_BAO_PaperSize::getByName($this->paperSize); | |
179 | $w = CRM_Utils_PDF_Utils::convertMetric($paperSize['width'], $paperSize['metric'], $this->metricDoc); | |
180 | $h = CRM_Utils_PDF_Utils::convertMetric($paperSize['height'], $paperSize['metric'], $this->metricDoc); | |
181 | $this->paper_dimensions = array($w, $h); | |
182 | } | |
183 | ||
5bc392e6 | 184 | /** |
d424ffde CW |
185 | * Generate the pdf of one label (can be modified using SetGenerator) |
186 | * | |
187 | * @param string $text | |
5bc392e6 | 188 | */ |
00be9182 | 189 | public function generateLabel($text) { |
6a488035 TO |
190 | $args = array( |
191 | 'w' => $this->width, | |
192 | 'h' => 0, | |
193 | 'txt' => $text, | |
194 | 'border' => 0, | |
195 | 'align' => 'L', | |
196 | 'fill' => 0, | |
197 | 'ln' => 0, | |
198 | 'x' => '', | |
199 | 'y' => '', | |
200 | 'reseth' => TRUE, | |
201 | 'stretch' => 0, | |
202 | 'ishtml' => FALSE, | |
203 | 'autopadding' => FALSE, | |
204 | 'maxh' => $this->height, | |
205 | ); | |
206 | ||
207 | CRM_Utils_Hook::alterMailingLabelParams($args); | |
208 | ||
209 | if ($args['ishtml'] == TRUE) { | |
210 | $this->writeHTMLCell($args['w'], $args['h'], | |
211 | $args['x'], $args['y'], | |
212 | $args['txt'], $args['border'], | |
213 | $args['ln'], $args['fill'], | |
214 | $args['reseth'], $args['align'], | |
215 | $args['autopadding'] | |
216 | ); | |
217 | } | |
218 | else { | |
219 | $this->multiCell($args['w'], $args['h'], | |
220 | $args['txt'], $args['border'], | |
221 | $args['align'], $args['fill'], | |
222 | $args['ln'], $args['x'], | |
223 | $args['y'], $args['reseth'], | |
224 | $args['stretch'], $args['ishtml'], | |
225 | $args['autopadding'], $args['maxh'] | |
226 | ); | |
227 | } | |
228 | } | |
229 | ||
5bc392e6 | 230 | /** |
fe482240 | 231 | * Print a label. |
d424ffde | 232 | * |
5bc392e6 EM |
233 | * @param $texte |
234 | */ | |
00be9182 | 235 | public function AddPdfLabel($texte) { |
0318fed3 DD |
236 | if ($this->countX == $this->xNumber) { |
237 | // Page full, we start a new one | |
238 | $this->AddPage(); | |
239 | $this->countX = 0; | |
240 | $this->countY = 0; | |
241 | } | |
242 | ||
6a488035 TO |
243 | $posX = $this->marginLeft + ($this->countX * ($this->width + $this->xSpace)); |
244 | $posY = $this->marginTop + ($this->countY * ($this->height + $this->ySpace)); | |
245 | $this->SetXY($posX + $this->paddingLeft, $posY + $this->paddingTop); | |
246 | if ($this->generatorMethod) { | |
247 | call_user_func_array(array($this->generatorObject, $this->generatorMethod), array($texte)); | |
248 | } | |
249 | else { | |
250 | $this->generateLabel($texte); | |
251 | } | |
252 | $this->countY++; | |
253 | ||
254 | if ($this->countY == $this->yNumber) { | |
255 | // End of column reached, we start a new one | |
256 | $this->countX++; | |
257 | $this->countY = 0; | |
258 | } | |
6a488035 TO |
259 | } |
260 | ||
00be9182 | 261 | public function getFontNames() { |
6a488035 TO |
262 | // Define labels for TCPDF core fonts |
263 | $fontLabel = array( | |
264 | 'courier' => ts('Courier'), | |
265 | 'helvetica' => ts('Helvetica'), | |
266 | 'times' => ts('Times New Roman'), | |
267 | 'dejavusans' => ts('Deja Vu Sans (UTF-8)'), | |
268 | ); | |
d28132a1 | 269 | |
d28132a1 DL |
270 | // Check to see if we have any additional fonts to add. You can specify more fonts in |
271 | // civicrm.settings.php via: $config['CiviCRM Preferences']['additional_fonts'] | |
272 | // CRM-13307 | |
273 | $additionalFonts = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'additional_fonts'); | |
274 | if (is_array($additionalFonts)) { | |
275 | $fontLabel = array_merge($fontLabel, $additionalFonts); | |
276 | } | |
277 | ||
6a488035 TO |
278 | $tcpdfFonts = $this->fontlist; |
279 | foreach ($tcpdfFonts as $fontName) { | |
280 | if (array_key_exists($fontName, $fontLabel)) { | |
281 | $list[$fontName] = $fontLabel[$fontName]; | |
282 | } | |
283 | } | |
d28132a1 | 284 | |
6a488035 TO |
285 | return $list; |
286 | } | |
96025800 | 287 | |
6a488035 | 288 | } |