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