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