Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright (C) 2011 Marty Wright | | |
7 | | Licensed to CiviCRM under the Academic Free License version 3.0. | | |
8 | +--------------------------------------------------------------------+ | |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * | |
31 | * @package CRM | |
32 | * @copyright CiviCRM LLC (c) 2004-2013 | |
33 | * $Id$ | |
34 | * | |
35 | */ | |
36 | ||
37 | /** | |
38 | * This class contains functions for managing Label Formats | |
39 | */ | |
40 | class CRM_Core_BAO_LabelFormat extends CRM_Core_DAO_OptionValue { | |
41 | ||
42 | /** | |
43 | * static holder for the Label Formats Option Group ID | |
44 | */ | |
45 | private static $_gid = NULL; | |
46 | ||
47 | /** | |
48 | * Label Format fields stored in the 'value' field of the Option Value table. | |
49 | */ | |
50 | private static $optionValueFields = array( | |
51 | 'paper-size' => array( | |
52 | // Paper size: names defined in option_value table (option_group = 'paper_size') | |
53 | 'name' => 'paper-size', | |
54 | 'type' => CRM_Utils_Type::T_STRING, | |
55 | 'default' => 'letter', | |
56 | ), | |
57 | 'orientation' => array( | |
58 | // Paper orientation: 'portrait' or 'landscape' | |
59 | 'name' => 'orientation', | |
60 | 'type' => CRM_Utils_Type::T_STRING, | |
61 | 'default' => 'portrait', | |
62 | ), | |
63 | 'font-name' => array( | |
64 | // Font name: 'courier', 'helvetica', 'times' | |
65 | 'name' => 'font-name', | |
66 | 'type' => CRM_Utils_Type::T_STRING, | |
67 | 'default' => 'helvetica', | |
68 | ), | |
69 | 'font-size' => array( | |
70 | // Font size: always in points | |
71 | 'name' => 'font-size', | |
72 | 'type' => CRM_Utils_Type::T_INT, | |
73 | 'default' => 8, | |
74 | ), | |
75 | 'font-style' => array( | |
76 | // Font style: 'B' bold, 'I' italic, 'BI' bold+italic | |
77 | 'name' => 'font-style', | |
78 | 'type' => CRM_Utils_Type::T_STRING, | |
79 | 'default' => '', | |
80 | ), | |
81 | 'NX' => array( | |
82 | // Number of labels horizontally | |
83 | 'name' => 'NX', | |
84 | 'type' => CRM_Utils_Type::T_INT, | |
85 | 'default' => 3, | |
86 | ), | |
87 | 'NY' => array( | |
88 | // Number of labels vertically | |
89 | 'name' => 'NY', | |
90 | 'type' => CRM_Utils_Type::T_INT, | |
91 | 'default' => 10, | |
92 | ), | |
93 | 'metric' => array( | |
94 | // Unit of measurement for all of the following fields | |
95 | 'name' => 'metric', | |
96 | 'type' => CRM_Utils_Type::T_STRING, | |
97 | 'default' => 'mm', | |
98 | ), | |
99 | 'lMargin' => array( | |
100 | // Left margin | |
101 | 'name' => 'lMargin', | |
102 | 'type' => CRM_Utils_Type::T_FLOAT, | |
103 | 'metric' => TRUE, | |
104 | 'default' => 4.7625, | |
105 | ), | |
106 | 'tMargin' => array( | |
107 | // Right margin | |
108 | 'name' => 'tMargin', | |
109 | 'type' => CRM_Utils_Type::T_FLOAT, | |
110 | 'metric' => TRUE, | |
111 | 'default' => 12.7, | |
112 | ), | |
113 | 'SpaceX' => array( | |
114 | // Horizontal space between two labels | |
115 | 'name' => 'SpaceX', | |
116 | 'type' => CRM_Utils_Type::T_FLOAT, | |
117 | 'metric' => TRUE, | |
118 | 'default' => 3.96875, | |
119 | ), | |
120 | 'SpaceY' => array( | |
121 | // Vertical space between two labels | |
122 | 'name' => 'SpaceY', | |
123 | 'type' => CRM_Utils_Type::T_FLOAT, | |
124 | 'metric' => TRUE, | |
125 | 'default' => 0, | |
126 | ), | |
127 | 'width' => array( | |
128 | // Width of label | |
129 | 'name' => 'width', | |
130 | 'type' => CRM_Utils_Type::T_FLOAT, | |
131 | 'metric' => TRUE, | |
132 | 'default' => 65.875, | |
133 | ), | |
134 | 'height' => array( | |
135 | // Height of label | |
136 | 'name' => 'height', | |
137 | 'type' => CRM_Utils_Type::T_FLOAT, | |
138 | 'metric' => TRUE, | |
139 | 'default' => 25.4, | |
140 | ), | |
141 | 'lPadding' => array( | |
142 | // Space between text and left edge of label | |
143 | 'name' => 'lPadding', | |
144 | 'type' => CRM_Utils_Type::T_FLOAT, | |
145 | 'metric' => TRUE, | |
146 | 'default' => 5.08, | |
147 | ), | |
148 | 'tPadding' => array( | |
149 | // Space between text and top edge of label | |
150 | 'name' => 'tPadding', | |
151 | 'type' => CRM_Utils_Type::T_FLOAT, | |
152 | 'metric' => TRUE, | |
153 | 'default' => 5.08, | |
154 | ), | |
155 | ); | |
156 | ||
157 | /** | |
158 | * Get page orientations recognized by the DOMPDF package used to create PDF letters. | |
159 | * | |
160 | * @param void | |
161 | * | |
162 | * @return array array of page orientations | |
163 | * @access public | |
01d7cdd8 | 164 | * @static |
6a488035 | 165 | */ |
01d7cdd8 | 166 | public static function getPageOrientations() { |
6a488035 TO |
167 | return array( |
168 | 'portrait' => ts('Portrait'), | |
169 | 'landscape' => ts('Landscape'), | |
170 | ); | |
171 | } | |
172 | ||
173 | /** | |
174 | * Get font names supported by the TCPDF package used to create PDF labels. | |
175 | * | |
eaf5045f | 176 | * @param string $name group name |
6a488035 TO |
177 | * |
178 | * @return array array of font names | |
179 | * @access public | |
01d7cdd8 | 180 | * @static |
6a488035 | 181 | */ |
eaf5045f KJ |
182 | public static function getFontNames($name='label_format') { |
183 | $label = new CRM_Utils_PDF_Label(self::getDefaultValues($name)); | |
6a488035 TO |
184 | return $label->getFontNames(); |
185 | } | |
186 | ||
187 | /** | |
188 | * Get font sizes supported by the TCPDF package used to create PDF labels. | |
189 | * | |
190 | * @param void | |
191 | * | |
192 | * @return array array of font sizes | |
193 | * @access public | |
01d7cdd8 | 194 | * @static |
6a488035 | 195 | */ |
01d7cdd8 | 196 | public static function getFontSizes() { |
6a488035 TO |
197 | return array( |
198 | 6 => ts('6 pt'), | |
199 | 7 => ts('7 pt'), | |
200 | 8 => ts('8 pt'), | |
201 | 9 => ts('9 pt'), | |
202 | 10 => ts('10 pt'), | |
203 | 11 => ts('11 pt'), | |
204 | 12 => ts('12 pt'), | |
205 | 13 => ts('13 pt'), | |
206 | 14 => ts('14 pt'), | |
207 | 15 => ts('15 pt'), | |
1b1d29d8 KJ |
208 | 16 => ts('16 pt'), |
209 | 17 => ts('17 pt'), | |
210 | 18 => ts('18 pt'), | |
211 | 19 => ts('19 pt'), | |
212 | 20 => ts('20 pt'), | |
213 | 21 => ts('21 pt'), | |
214 | 22 => ts('22 pt'), | |
215 | 23 => ts('23 pt'), | |
216 | 24 => ts('24 pt'), | |
6a488035 TO |
217 | ); |
218 | } | |
219 | ||
220 | /** | |
221 | * Get measurement units recognized by the TCPDF package used to create PDF labels. | |
222 | * | |
223 | * @param void | |
224 | * | |
225 | * @return array array of measurement units | |
226 | * @access public | |
01d7cdd8 | 227 | * @static |
6a488035 | 228 | */ |
01d7cdd8 | 229 | public static function getUnits() { |
6a488035 TO |
230 | return array( |
231 | 'in' => ts('Inches'), | |
232 | 'cm' => ts('Centimeters'), | |
233 | 'mm' => ts('Millimeters'), | |
234 | 'pt' => ts('Points'), | |
235 | ); | |
236 | } | |
237 | ||
636f1cbe KJ |
238 | /** |
239 | * Get text alignment recognized by the TCPDF package used to create PDF labels. | |
240 | * | |
241 | * @param void | |
242 | * | |
243 | * @return array array of alignments | |
244 | * @access public | |
245 | * @static | |
246 | */ | |
247 | public static function getTextAlignments() { | |
248 | return array( | |
249 | 'R' => ts('Right'), | |
250 | 'L' => ts('Left'), | |
1b1d29d8 | 251 | 'C' => ts('Center'), |
636f1cbe KJ |
252 | ); |
253 | } | |
254 | ||
0bc1ba89 | 255 | /** |
256 | * Get text alignment recognized by the TCPDF package used to create PDF labels. | |
257 | * | |
258 | * @param void | |
259 | * | |
260 | * @return array array of alignments | |
261 | * @access public | |
262 | * @static | |
263 | */ | |
264 | public static function getFontStyles() { | |
265 | return array( | |
266 | '' => ts('Normal'), | |
267 | 'B' => ts('Bold'), | |
268 | 'I' => ts('Italic'), | |
269 | ); | |
270 | } | |
271 | ||
6a488035 TO |
272 | /** |
273 | * Get Option Group ID for Label Formats | |
274 | * | |
275 | * @param void | |
276 | * | |
277 | * @return int Group ID (null if Group ID doesn't exist) | |
278 | * @access private | |
279 | */ | |
eaf5045f KJ |
280 | private static function _getGid($name='label_format') { |
281 | if (!isset(self::$_gid[$name]) || !self::$_gid[$name]) { | |
282 | self::$_gid[$name] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $name, 'id', 'name'); | |
283 | if (!self::$_gid[$name]) { | |
6a488035 TO |
284 | CRM_Core_Error::fatal(ts('Label Format Option Group not found in database.')); |
285 | } | |
286 | } | |
eaf5045f | 287 | return self::$_gid[$name]; |
6a488035 TO |
288 | } |
289 | ||
290 | /** | |
291 | * Add ordering fields to Label Format list | |
292 | * | |
293 | * @param array (reference) $list List of Label Formats | |
294 | * @param string $returnURL URL of page calling this function | |
295 | * | |
296 | * @return array (reference) List of Label Formats | |
297 | * @static | |
298 | * @access public | |
299 | */ | |
300 | static function addOrder(&$list, $returnURL) { | |
301 | $filter = "option_group_id = " . self::_getGid(); | |
302 | CRM_Utils_Weight::addOrder($list, 'CRM_Core_DAO_OptionValue', 'id', $returnURL, $filter); | |
303 | } | |
304 | ||
305 | /** | |
306 | * Retrieve list of Label Formats. | |
307 | * | |
308 | * @param bool $namesOnly return simple list of names | |
4f1ddb25 | 309 | * @param string $groupName group name of the label format option group |
6a488035 TO |
310 | * |
311 | * @return array (reference) label format list | |
312 | * @static | |
313 | * @access public | |
314 | */ | |
4f1ddb25 | 315 | static function &getList($namesOnly = FALSE, $groupName='label_format') { |
6a488035 | 316 | static $list = array(); |
4f1ddb25 | 317 | if (self::_getGid($groupName)) { |
6a488035 TO |
318 | // get saved label formats from Option Value table |
319 | $dao = new CRM_Core_DAO_OptionValue(); | |
4f1ddb25 | 320 | $dao->option_group_id = self::_getGid($groupName); |
6a488035 TO |
321 | $dao->is_active = 1; |
322 | $dao->orderBy('weight'); | |
323 | $dao->find(); | |
324 | while ($dao->fetch()) { | |
325 | if ($namesOnly) { | |
4f1ddb25 | 326 | $list[$groupName][$dao->name] = $dao->label; |
6a488035 TO |
327 | } |
328 | else { | |
4f1ddb25 | 329 | CRM_Core_DAO::storeValues($dao, $list[$groupName][$dao->id]); |
6a488035 TO |
330 | } |
331 | } | |
332 | } | |
4f1ddb25 | 333 | return $list[$groupName]; |
6a488035 TO |
334 | } |
335 | ||
336 | /** | |
337 | * retrieve the default Label Format values | |
338 | * | |
eaf5045f | 339 | * @param string $groupName label format group name |
6a488035 TO |
340 | * |
341 | * @return array Name/value pairs containing the default Label Format values. | |
342 | * @static | |
343 | * @access public | |
344 | */ | |
eaf5045f | 345 | static function &getDefaultValues($groupName = 'label_format') { |
6a488035 TO |
346 | $params = array('is_active' => 1, 'is_default' => 1); |
347 | $defaults = array(); | |
eaf5045f | 348 | if (!self::retrieve($params, $defaults, $groupName)) { |
6a488035 TO |
349 | foreach (self::$optionValueFields as $name => $field) { |
350 | $defaults[$name] = $field['default']; | |
351 | } | |
eaf5045f | 352 | $filter = array('option_group_id' => self::_getGid($groupName)); |
6a488035 TO |
353 | $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', $filter); |
354 | } | |
355 | return $defaults; | |
356 | } | |
357 | ||
358 | /** | |
359 | * Get Label Format from the DB | |
360 | * | |
361 | * @param string $field Field name to search by | |
362 | * @param int $val Field value to search for | |
363 | * | |
364 | * @return array $values (reference) associative array of name/value pairs | |
365 | * @access public | |
366 | */ | |
eaf5045f | 367 | static function &getLabelFormat($field, $val, $groupName = 'label_format') { |
6a488035 TO |
368 | $params = array('is_active' => 1, $field => $val); |
369 | $labelFormat = array(); | |
eaf5045f | 370 | if (self::retrieve($params, $labelFormat, $groupName)) { |
6a488035 TO |
371 | return $labelFormat; |
372 | } | |
373 | else { | |
eaf5045f | 374 | return self::getDefaultValues($groupName); |
6a488035 TO |
375 | } |
376 | } | |
377 | ||
378 | /** | |
379 | * Get Label Format by Name | |
380 | * | |
381 | * @param int $name Label format name. Empty = get default label format | |
382 | * | |
383 | * @return array $values (reference) associative array of name/value pairs | |
384 | * @access public | |
385 | */ | |
386 | static function &getByName($name) { | |
387 | return self::getLabelFormat('name', $name); | |
388 | } | |
389 | ||
390 | /** | |
391 | * Get Label Format by ID | |
392 | * | |
393 | * @param int $id label format id. 0 = get default label format | |
eaf5045f | 394 | * @param string $groupName group name |
6a488035 TO |
395 | * |
396 | * @return array $values (reference) associative array of name/value pairs | |
397 | * @access public | |
398 | */ | |
eaf5045f KJ |
399 | static function &getById($id, $groupName = 'label_format') { |
400 | return self::getLabelFormat('id', $id, $groupName); | |
6a488035 TO |
401 | } |
402 | ||
403 | /** | |
404 | * Get Label Format field from associative array | |
405 | * | |
406 | * @param string $field name of a label format field | |
407 | * @param array (reference) $values associative array of name/value pairs containing | |
408 | * label format field selections | |
409 | * | |
410 | * @return value | |
411 | * @access public | |
412 | * @static | |
413 | */ | |
414 | static function getValue($field, &$values, $default = NULL) { | |
415 | if (array_key_exists($field, self::$optionValueFields)) { | |
416 | switch (self::$optionValueFields[$field]['type']) { | |
417 | case CRM_Utils_Type::T_INT: | |
418 | return (int)CRM_Utils_Array::value($field, $values, $default); | |
419 | ||
420 | case CRM_Utils_Type::T_FLOAT: | |
421 | // Round float values to three decimal places and trim trailing zeros. | |
422 | // Add a leading zero to values less than 1. | |
423 | $f = sprintf('%05.3f', $values[$field]); | |
424 | $f = rtrim($f, '0'); | |
425 | $f = rtrim($f, '.'); | |
426 | return (float)(empty($f) ? '0' : $f); | |
427 | } | |
428 | return CRM_Utils_Array::value($field, $values, $default); | |
429 | } | |
430 | return $default; | |
431 | } | |
432 | ||
433 | /** | |
434 | * Takes a bunch of params that are needed to match certain criteria and | |
435 | * retrieves the relevant objects. Typically the valid params are only | |
436 | * label id. It also stores all the retrieved values in the default array. | |
437 | * | |
438 | * @param array $params (reference ) an assoc array of name/value pairs | |
439 | * @param array $values (reference ) an assoc array to hold the flattened values | |
440 | * | |
441 | * @return object CRM_Core_DAO_OptionValue object | |
442 | * @access public | |
443 | * @static | |
444 | */ | |
eaf5045f | 445 | static function retrieve(&$params, &$values, $groupName='label_format') { |
6a488035 TO |
446 | $optionValue = new CRM_Core_DAO_OptionValue(); |
447 | $optionValue->copyValues($params); | |
eaf5045f | 448 | $optionValue->option_group_id = self::_getGid($groupName); |
6a488035 TO |
449 | if ($optionValue->find(TRUE)) { |
450 | // Extract fields that have been serialized in the 'value' column of the Option Value table. | |
451 | $values = json_decode($optionValue->value, TRUE); | |
452 | // Add any new fields that don't yet exist in the saved values. | |
453 | foreach (self::$optionValueFields as $name => $field) { | |
454 | if (!isset($values[$name])) { | |
455 | $values[$name] = $field['default']; | |
456 | if ($field['metric']) { | |
457 | $values[$name] = CRM_Utils_PDF_Utils::convertMetric($field['default'], | |
458 | self::$optionValueFields['metric']['default'], | |
459 | $values['metric'], 3 | |
460 | ); | |
461 | } | |
462 | } | |
463 | } | |
464 | // Add fields from the OptionValue base class | |
465 | CRM_Core_DAO::storeValues($optionValue, $values); | |
466 | return $optionValue; | |
467 | } | |
468 | return NULL; | |
469 | } | |
470 | ||
471 | /** | |
472 | * Return the name of the group for customized labels | |
473 | * | |
474 | * @param void | |
475 | * | |
476 | * @return void | |
477 | * @access public | |
478 | */ | |
4f1ddb25 | 479 | public static function customGroupName() { |
6a488035 TO |
480 | return ts('Custom'); |
481 | } | |
482 | ||
483 | /** | |
484 | * Save the Label Format in the DB | |
485 | * | |
486 | * @param array (reference) $values associative array of name/value pairs | |
487 | * @param int $id id of the database record (null = new record) | |
eaf5045f | 488 | * @param string $groupName group name of the label format |
6a488035 TO |
489 | * |
490 | * @return void | |
491 | * @access public | |
492 | */ | |
eaf5045f | 493 | function saveLabelFormat(&$values, $id = NULL, $groupName = 'label_format') { |
6a488035 | 494 | // get the Option Group ID for Label Formats (create one if it doesn't exist) |
eaf5045f | 495 | $group_id = self::_getGid($groupName); |
6a488035 TO |
496 | |
497 | // clear other default if this is the new default label format | |
498 | if ($values['is_default']) { | |
499 | $query = "UPDATE civicrm_option_value SET is_default = 0 WHERE option_group_id = $group_id"; | |
500 | CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
501 | } | |
502 | if ($id) { | |
503 | // fetch existing record | |
504 | $this->id = $id; | |
505 | if ($this->find()) { | |
506 | $this->fetch(); | |
507 | } | |
508 | } | |
509 | else { | |
510 | // new record | |
eaf5045f | 511 | $list = self::getList(TRUE,$groupName); |
6a488035 TO |
512 | $cnt = 1; |
513 | while (array_key_exists("custom_$cnt", $list)) $cnt++; | |
514 | $values['name'] = "custom_$cnt"; | |
515 | $values['grouping'] = self::customGroupName(); | |
516 | } | |
517 | // copy the supplied form values to the corresponding Option Value fields in the base class | |
518 | foreach ($this->fields() as $name => $field) { | |
519 | $this->$name = trim(CRM_Utils_Array::value($name, $values, $this->$name)); | |
520 | if (empty($this->$name)) { | |
521 | $this->$name = 'null'; | |
522 | } | |
523 | } | |
524 | $this->id = $id; | |
525 | $this->option_group_id = $group_id; | |
526 | $this->is_active = 1; | |
527 | ||
528 | // serialize label format fields into a single string to store in the 'value' column of the Option Value table | |
529 | $v = json_decode($this->value, TRUE); | |
530 | foreach (self::$optionValueFields as $name => $field) { | |
8e9fd9e4 | 531 | if (!isset($v[$name])) { |
532 | $v[$name] = NULL; | |
533 | } | |
6a488035 TO |
534 | $v[$name] = self::getValue($name, $values, $v[$name]); |
535 | } | |
536 | $this->value = json_encode($v); | |
537 | ||
538 | // make sure serialized array will fit in the 'value' column | |
539 | $attribute = CRM_Core_DAO::getAttribute('CRM_Core_BAO_LabelFormat', 'value'); | |
540 | if (strlen($this->value) > $attribute['maxlength']) { | |
541 | CRM_Core_Error::fatal(ts('Label Format does not fit in database.')); | |
542 | } | |
543 | $this->save(); | |
544 | ||
545 | // fix duplicate weights | |
546 | $filter = array('option_group_id' => self::_getGid()); | |
547 | CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $filter); | |
548 | } | |
549 | ||
550 | /** | |
551 | * Function to delete a Label Format | |
552 | * | |
553 | * @param int $id ID of the label format to be deleted. | |
eaf5045f | 554 | * @param string $groupName group name |
6a488035 TO |
555 | * @access public |
556 | * @static | |
557 | */ | |
eaf5045f | 558 | static function del($id, $groupName) { |
6a488035 TO |
559 | if ($id) { |
560 | $dao = new CRM_Core_DAO_OptionValue(); | |
561 | $dao->id = $id; | |
562 | if ($dao->find(TRUE)) { | |
eaf5045f KJ |
563 | if ($dao->option_group_id == self::_getGid($groupName)) { |
564 | $filter = array('option_group_id' => self::_getGid($groupName)); | |
6a488035 TO |
565 | CRM_Utils_Weight::delWeight('CRM_Core_DAO_OptionValue', $id, $filter); |
566 | $dao->delete(); | |
567 | return; | |
568 | } | |
569 | } | |
570 | } | |
571 | CRM_Core_Error::fatal(ts('Invalid value passed to delete function.')); | |
572 | } | |
573 | } | |
574 |