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