3 +--------------------------------------------------------------------+
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. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC https://civicrm.org/licensing
36 * This class contains functions for managing Label Formats.
38 class CRM_Core_BAO_LabelFormat
extends CRM_Core_DAO_OptionValue
{
41 * Static holder for the Label Formats Option Group ID.
44 private static $_gid = NULL;
47 * Label Format fields stored in the 'value' field of the Option Value table.
50 private static $optionValueFields = [
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',
58 // Paper orientation: 'portrait' or 'landscape'
59 'name' => 'orientation',
60 'type' => CRM_Utils_Type
::T_STRING
,
61 'default' => 'portrait',
64 // Font name: 'courier', 'helvetica', 'times'
65 'name' => 'font-name',
66 'type' => CRM_Utils_Type
::T_STRING
,
67 'default' => 'helvetica',
70 // Font size: always in points
71 'name' => 'font-size',
72 'type' => CRM_Utils_Type
::T_INT
,
76 // Font style: 'B' bold, 'I' italic, 'BI' bold+italic
77 'name' => 'font-style',
78 'type' => CRM_Utils_Type
::T_STRING
,
82 // Number of labels horizontally
84 'type' => CRM_Utils_Type
::T_INT
,
88 // Number of labels vertically
90 'type' => CRM_Utils_Type
::T_INT
,
94 // Unit of measurement for all of the following fields
96 'type' => CRM_Utils_Type
::T_STRING
,
102 'type' => CRM_Utils_Type
::T_FLOAT
,
109 'type' => CRM_Utils_Type
::T_FLOAT
,
114 // Horizontal space between two labels
116 'type' => CRM_Utils_Type
::T_FLOAT
,
118 'default' => 3.96875,
121 // Vertical space between two labels
123 'type' => CRM_Utils_Type
::T_FLOAT
,
130 'type' => CRM_Utils_Type
::T_FLOAT
,
137 'type' => CRM_Utils_Type
::T_FLOAT
,
142 // Space between text and left edge of label
143 'name' => 'lPadding',
144 'type' => CRM_Utils_Type
::T_FLOAT
,
149 // Space between text and top edge of label
150 'name' => 'tPadding',
151 'type' => CRM_Utils_Type
::T_FLOAT
,
158 * Get page orientations recognized by the DOMPDF package used to create PDF letters.
161 * array of page orientations
163 public static function getPageOrientations() {
165 'portrait' => ts('Portrait'),
166 'landscape' => ts('Landscape'),
171 * Get font names supported by the TCPDF package used to create PDF labels.
173 * @param string $name
177 * array of font names
179 public static function getFontNames($name = 'label_format') {
180 $label = new CRM_Utils_PDF_Label(self
::getDefaultValues($name));
181 return $label->getFontNames();
185 * Get font sizes supported by the TCPDF package used to create PDF labels.
188 * array of font sizes
190 public static function getFontSizes() {
192 for ($i = 6; $i <= 60; $i++
) {
193 $fontSizes[$i] = ts('%1 pt', [1 => $i]);
200 * Get measurement units recognized by the TCPDF package used to create PDF labels.
203 * array of measurement units
205 public static function getUnits() {
207 'in' => ts('Inches'),
208 'cm' => ts('Centimeters'),
209 'mm' => ts('Millimeters'),
210 'pt' => ts('Points'),
215 * Get text alignment recognized by the TCPDF package used to create PDF labels.
218 * array of alignments
220 public static function getTextAlignments() {
229 * Get text alignment recognized by the TCPDF package used to create PDF labels.
232 * array of alignments
234 public static function getFontStyles() {
243 * Get Option Group ID for Label Formats.
245 * @param string $name
248 * Group ID (null if Group ID doesn't exist)
249 * @throws \CRM_Core_Exception
251 private static function _getGid($name = 'label_format') {
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]) {
255 throw new CRM_Core_Exception(ts('Label Format Option Group not found in database.'));
258 return self
::$_gid[$name];
262 * Add ordering fields to Label Format list.
264 * @param array $list List of Label Formats
265 * @param string $returnURL
266 * URL of page calling this function.
269 * (reference) List of Label Formats
270 * @throws \CRM_Core_Exception
272 public static function addOrder(&$list, $returnURL) {
273 $filter = "option_group_id = " . self
::_getGid();
274 CRM_Utils_Weight
::addOrder($list, 'CRM_Core_DAO_OptionValue', 'id', $returnURL, $filter);
279 * Retrieve list of Label Formats.
281 * @param bool $namesOnly
282 * Return simple list of names.
283 * @param string $groupName
284 * Group name of the label format option group.
287 * (reference) label format list
288 * @throws \CRM_Core_Exception
290 public static function getList($namesOnly = FALSE, $groupName = 'label_format') {
292 if (self
::_getGid($groupName)) {
293 // get saved label formats from Option Value table
294 $dao = new CRM_Core_DAO_OptionValue();
295 $dao->option_group_id
= self
::_getGid($groupName);
297 $dao->orderBy('weight');
299 while ($dao->fetch()) {
301 $list[$groupName][$dao->name
] = $dao->label
;
304 CRM_Core_DAO
::storeValues($dao, $list[$groupName][$dao->id
]);
308 return $list[$groupName];
312 * Retrieve the default Label Format values.
314 * @param string $groupName
315 * Label format group name.
318 * Name/value pairs containing the default Label Format values.
319 * @throws \CRM_Core_Exception
321 public static function &getDefaultValues($groupName = 'label_format') {
322 $params = ['is_active' => 1, 'is_default' => 1];
324 if (!self
::retrieve($params, $defaults, $groupName)) {
325 foreach (self
::$optionValueFields as $name => $field) {
326 $defaults[$name] = $field['default'];
328 $filter = ['option_group_id' => self
::_getGid($groupName)];
329 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue', $filter);
335 * Get Label Format from the DB.
337 * @param string $field
338 * Field name to search by.
340 * Field value to search for.
342 * @param string $groupName
345 * (reference) associative array of name/value pairs
346 * @throws \CRM_Core_Exception
348 public static function &getLabelFormat($field, $val, $groupName = 'label_format') {
349 $params = ['is_active' => 1, $field => $val];
351 if (self
::retrieve($params, $labelFormat, $groupName)) {
355 return self
::getDefaultValues($groupName);
360 * Get Label Format by Name.
363 * Label format name. Empty = get default label format.
366 * (reference) associative array of name/value pairs
367 * @throws \CRM_Core_Exception
369 public static function &getByName($name) {
370 return self
::getLabelFormat('name', $name);
374 * Get Label Format by ID.
377 * Label format id. 0 = get default label format.
378 * @param string $groupName
382 * (reference) associative array of name/value pairs
383 * @throws \CRM_Core_Exception
385 public static function &getById($id, $groupName = 'label_format') {
386 return self
::getLabelFormat('id', $id, $groupName);
390 * Get Label Format field from associative array.
392 * @param string $field
393 * Name of a label format field.
394 * @param array $values associative array of name/value pairs containing
395 * label format field selections
397 * @param null $default
401 public static function getValue($field, &$values, $default = NULL) {
402 if (array_key_exists($field, self
::$optionValueFields)) {
403 switch (self
::$optionValueFields[$field]['type']) {
404 case CRM_Utils_Type
::T_INT
:
405 return (int) CRM_Utils_Array
::value($field, $values, $default);
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]);
413 return (float) (empty($f) ?
'0' : $f);
415 return CRM_Utils_Array
::value($field, $values, $default);
421 * Retrieve DB object based on input parameters.
423 * It also stores all the retrieved values in the default array.
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.
430 * @param string $groupName
432 * @return CRM_Core_DAO_OptionValue
433 * @throws \CRM_Core_Exception
435 public static function retrieve(&$params, &$values, $groupName = 'label_format') {
436 $optionValue = new CRM_Core_DAO_OptionValue();
437 $optionValue->copyValues($params);
438 $optionValue->option_group_id
= self
::_getGid($groupName);
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'],
454 // Add fields from the OptionValue base class
455 CRM_Core_DAO
::storeValues($optionValue, $values);
462 * Return the name of the group for customized labels.
464 public static function customGroupName() {
469 * Save the Label Format in the DB.
471 * @param array $values associative array of name/value pairs
473 * Id of the database record (null = new record).
474 * @param string $groupName
475 * Group name of the label format.
477 * @throws \CRM_Core_Exception
479 public function saveLabelFormat(&$values, $id = NULL, $groupName = 'label_format') {
480 // get the Option Group ID for Label Formats (create one if it doesn't exist)
481 $group_id = self
::_getGid($groupName);
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";
486 CRM_Core_DAO
::executeQuery($query);
489 // fetch existing record
497 $list = self
::getList(TRUE, $groupName);
499 while (array_key_exists("custom_$cnt", $list)) {
502 $values['name'] = "custom_$cnt";
503 $values['grouping'] = self
::customGroupName();
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';
513 $this->option_group_id
= $group_id;
514 $this->is_active
= 1;
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) {
519 if (!isset($v[$name])) {
522 $v[$name] = self
::getValue($name, $values, $v[$name]);
524 $this->value
= json_encode($v);
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.'));
533 // fix duplicate weights
534 $filter = ['option_group_id' => self
::_getGid()];
535 CRM_Utils_Weight
::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $filter);
539 * Delete a Label Format.
542 * ID of the label format to be deleted.
543 * @param string $groupName
546 * @throws \CRM_Core_Exception
548 public static function del($id, $groupName) {
550 $dao = new CRM_Core_DAO_OptionValue();
552 if ($dao->find(TRUE)) {
553 if ($dao->option_group_id
== self
::_getGid($groupName)) {
554 $filter = ['option_group_id' => self
::_getGid($groupName)];
555 CRM_Utils_Weight
::delWeight('CRM_Core_DAO_OptionValue', $id, $filter);
561 throw new CRM_Core_Exception(ts('Invalid value passed to delete function.'));