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 (c) 2004-2019
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.
43 private static $_gid = NULL;
46 * Label Format fields stored in the 'value' field of the Option Value table.
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',
55 'orientation' => array(
56 // Paper orientation: 'portrait' or 'landscape'
57 'name' => 'orientation',
58 'type' => CRM_Utils_Type
::T_STRING
,
59 'default' => 'portrait',
62 // Font name: 'courier', 'helvetica', 'times'
63 'name' => 'font-name',
64 'type' => CRM_Utils_Type
::T_STRING
,
65 'default' => 'helvetica',
68 // Font size: always in points
69 'name' => 'font-size',
70 'type' => CRM_Utils_Type
::T_INT
,
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
,
80 // Number of labels horizontally
82 'type' => CRM_Utils_Type
::T_INT
,
86 // Number of labels vertically
88 'type' => CRM_Utils_Type
::T_INT
,
92 // Unit of measurement for all of the following fields
94 'type' => CRM_Utils_Type
::T_STRING
,
100 'type' => CRM_Utils_Type
::T_FLOAT
,
107 'type' => CRM_Utils_Type
::T_FLOAT
,
112 // Horizontal space between two labels
114 'type' => CRM_Utils_Type
::T_FLOAT
,
116 'default' => 3.96875,
119 // Vertical space between two labels
121 'type' => CRM_Utils_Type
::T_FLOAT
,
128 'type' => CRM_Utils_Type
::T_FLOAT
,
135 'type' => CRM_Utils_Type
::T_FLOAT
,
140 // Space between text and left edge of label
141 'name' => 'lPadding',
142 'type' => CRM_Utils_Type
::T_FLOAT
,
147 // Space between text and top edge of label
148 'name' => 'tPadding',
149 'type' => CRM_Utils_Type
::T_FLOAT
,
156 * Get page orientations recognized by the DOMPDF package used to create PDF letters.
159 * array of page orientations
161 public static function getPageOrientations() {
163 'portrait' => ts('Portrait'),
164 'landscape' => ts('Landscape'),
169 * Get font names supported by the TCPDF package used to create PDF labels.
171 * @param string $name
175 * array of font names
177 public static function getFontNames($name = 'label_format') {
178 $label = new CRM_Utils_PDF_Label(self
::getDefaultValues($name));
179 return $label->getFontNames();
183 * Get font sizes supported by the TCPDF package used to create PDF labels.
186 * array of font sizes
188 public static function getFontSizes() {
189 $fontSizes = array();
190 for ($i = 6; $i <= 60; $i++
) {
191 $fontSizes[$i] = ts('%1 pt', array(1 => $i));
198 * Get measurement units recognized by the TCPDF package used to create PDF labels.
201 * array of measurement units
203 public static function getUnits() {
205 'in' => ts('Inches'),
206 'cm' => ts('Centimeters'),
207 'mm' => ts('Millimeters'),
208 'pt' => ts('Points'),
213 * Get text alignment recognized by the TCPDF package used to create PDF labels.
216 * array of alignments
218 public static function getTextAlignments() {
227 * Get text alignment recognized by the TCPDF package used to create PDF labels.
230 * array of alignments
232 public static function getFontStyles() {
241 * Get Option Group ID for Label Formats.
243 * @param string $name
246 * Group ID (null if Group ID doesn't exist)
248 private static function _getGid($name = 'label_format') {
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]) {
252 CRM_Core_Error
::fatal(ts('Label Format Option Group not found in database.'));
255 return self
::$_gid[$name];
259 * Add ordering fields to Label Format list.
261 * @param array (reference) $list List of Label Formats
262 * @param string $returnURL
263 * URL of page calling this function.
266 * (reference) List of Label Formats
268 public static function addOrder(&$list, $returnURL) {
269 $filter = "option_group_id = " . self
::_getGid();
270 CRM_Utils_Weight
::addOrder($list, 'CRM_Core_DAO_OptionValue', 'id', $returnURL, $filter);
275 * Retrieve list of Label Formats.
277 * @param bool $namesOnly
278 * Return simple list of names.
279 * @param string $groupName
280 * Group name of the label format option group.
283 * (reference) label format list
285 public static function &getList($namesOnly = FALSE, $groupName = 'label_format') {
286 static $list = array();
287 if (self
::_getGid($groupName)) {
288 // get saved label formats from Option Value table
289 $dao = new CRM_Core_DAO_OptionValue();
290 $dao->option_group_id
= self
::_getGid($groupName);
292 $dao->orderBy('weight');
294 while ($dao->fetch()) {
296 $list[$groupName][$dao->name
] = $dao->label
;
299 CRM_Core_DAO
::storeValues($dao, $list[$groupName][$dao->id
]);
303 return $list[$groupName];
307 * Retrieve the default Label Format values.
309 * @param string $groupName
310 * Label format group name.
313 * Name/value pairs containing the default Label Format values.
315 public static function &getDefaultValues($groupName = 'label_format') {
316 $params = array('is_active' => 1, 'is_default' => 1);
318 if (!self
::retrieve($params, $defaults, $groupName)) {
319 foreach (self
::$optionValueFields as $name => $field) {
320 $defaults[$name] = $field['default'];
322 $filter = array('option_group_id' => self
::_getGid($groupName));
323 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue', $filter);
329 * Get Label Format from the DB.
331 * @param string $field
332 * Field name to search by.
334 * Field value to search for.
336 * @param string $groupName
339 * (reference) associative array of name/value pairs
341 public static function &getLabelFormat($field, $val, $groupName = 'label_format') {
342 $params = array('is_active' => 1, $field => $val);
343 $labelFormat = array();
344 if (self
::retrieve($params, $labelFormat, $groupName)) {
348 return self
::getDefaultValues($groupName);
353 * Get Label Format by Name.
356 * Label format name. Empty = get default label format.
359 * (reference) associative array of name/value pairs
361 public static function &getByName($name) {
362 return self
::getLabelFormat('name', $name);
366 * Get Label Format by ID.
369 * Label format id. 0 = get default label format.
370 * @param string $groupName
374 * (reference) associative array of name/value pairs
376 public static function &getById($id, $groupName = 'label_format') {
377 return self
::getLabelFormat('id', $id, $groupName);
381 * Get Label Format field from associative array.
383 * @param string $field
384 * Name of a label format field.
385 * @param array (reference) $values associative array of name/value pairs containing
386 * label format field selections
388 * @param null $default
392 public static function getValue($field, &$values, $default = NULL) {
393 if (array_key_exists($field, self
::$optionValueFields)) {
394 switch (self
::$optionValueFields[$field]['type']) {
395 case CRM_Utils_Type
::T_INT
:
396 return (int) CRM_Utils_Array
::value($field, $values, $default);
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]);
404 return (float) (empty($f) ?
'0' : $f);
406 return CRM_Utils_Array
::value($field, $values, $default);
412 * Retrieve DB object based on input parameters.
414 * It also stores all the retrieved values in the default array.
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.
421 * @param string $groupName
423 * @return CRM_Core_DAO_OptionValue
425 public static function retrieve(&$params, &$values, $groupName = 'label_format') {
426 $optionValue = new CRM_Core_DAO_OptionValue();
427 $optionValue->copyValues($params);
428 $optionValue->option_group_id
= self
::_getGid($groupName);
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'],
444 // Add fields from the OptionValue base class
445 CRM_Core_DAO
::storeValues($optionValue, $values);
452 * Return the name of the group for customized labels.
454 public static function customGroupName() {
459 * Save the Label Format in the DB.
461 * @param array (reference) $values associative array of name/value pairs
463 * Id of the database record (null = new record).
464 * @param string $groupName
465 * Group name of the label format.
467 public function saveLabelFormat(&$values, $id = NULL, $groupName = 'label_format') {
468 // get the Option Group ID for Label Formats (create one if it doesn't exist)
469 $group_id = self
::_getGid($groupName);
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";
474 CRM_Core_DAO
::executeQuery($query);
477 // fetch existing record
485 $list = self
::getList(TRUE, $groupName);
487 while (array_key_exists("custom_$cnt", $list)) {
490 $values['name'] = "custom_$cnt";
491 $values['grouping'] = self
::customGroupName();
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';
501 $this->option_group_id
= $group_id;
502 $this->is_active
= 1;
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) {
507 if (!isset($v[$name])) {
510 $v[$name] = self
::getValue($name, $values, $v[$name]);
512 $this->value
= json_encode($v);
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.'));
521 // fix duplicate weights
522 $filter = array('option_group_id' => self
::_getGid());
523 CRM_Utils_Weight
::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $filter);
527 * Delete a Label Format.
530 * ID of the label format to be deleted.
531 * @param string $groupName
534 public static function del($id, $groupName) {
536 $dao = new CRM_Core_DAO_OptionValue();
538 if ($dao->find(TRUE)) {
539 if ($dao->option_group_id
== self
::_getGid($groupName)) {
540 $filter = array('option_group_id' => self
::_getGid($groupName));
541 CRM_Utils_Weight
::delWeight('CRM_Core_DAO_OptionValue', $id, $filter);
547 CRM_Core_Error
::fatal(ts('Invalid value passed to delete function.'));