3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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-2013
38 * This class contains functions for managing Label Formats
40 class CRM_Core_BAO_LabelFormat
extends CRM_Core_DAO_OptionValue
{
43 * static holder for the Label Formats Option Group ID
45 private static $_gid = NULL;
48 * Label Format fields stored in the 'value' field of the Option Value table.
50 private static $optionValueFields = array(
51 'paper-size' => array(
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',
57 'orientation' => array(
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
,
75 'font-style' => array(
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.
162 * @return array array of page orientations
166 public static function getPageOrientations() {
168 'portrait' => ts('Portrait'),
169 'landscape' => ts('Landscape'),
174 * Get font names supported by the TCPDF package used to create PDF labels.
176 * @param string $name group name
178 * @return array array of font names
182 public static function getFontNames($name='label_format') {
183 $label = new CRM_Utils_PDF_Label(self
::getDefaultValues($name));
184 return $label->getFontNames();
188 * Get font sizes supported by the TCPDF package used to create PDF labels.
192 * @return array array of font sizes
196 public static function getFontSizes() {
197 $fontSizes = array();
198 for ($i = 6; $i <= 60; $i++
) {
199 $fontSizes[$i] = ts('%1 pt', array(1 => $i));
206 * Get measurement units recognized by the TCPDF package used to create PDF labels.
210 * @return array array of measurement units
214 public static function getUnits() {
216 'in' => ts('Inches'),
217 'cm' => ts('Centimeters'),
218 'mm' => ts('Millimeters'),
219 'pt' => ts('Points'),
224 * Get text alignment recognized by the TCPDF package used to create PDF labels.
228 * @return array array of alignments
232 public static function getTextAlignments() {
241 * Get text alignment recognized by the TCPDF package used to create PDF labels.
245 * @return array array of alignments
249 public static function getFontStyles() {
258 * Get Option Group ID for Label Formats
262 * @return int Group ID (null if Group ID doesn't exist)
265 private static function _getGid($name='label_format') {
266 if (!isset(self
::$_gid[$name]) ||
!self
::$_gid[$name]) {
267 self
::$_gid[$name] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $name, 'id', 'name');
268 if (!self
::$_gid[$name]) {
269 CRM_Core_Error
::fatal(ts('Label Format Option Group not found in database.'));
272 return self
::$_gid[$name];
276 * Add ordering fields to Label Format list
278 * @param array (reference) $list List of Label Formats
279 * @param string $returnURL URL of page calling this function
281 * @return array (reference) List of Label Formats
285 static function addOrder(&$list, $returnURL) {
286 $filter = "option_group_id = " . self
::_getGid();
287 CRM_Utils_Weight
::addOrder($list, 'CRM_Core_DAO_OptionValue', 'id', $returnURL, $filter);
291 * Retrieve list of Label Formats.
293 * @param bool $namesOnly return simple list of names
294 * @param string $groupName group name of the label format option group
296 * @return array (reference) label format list
300 static function &getList($namesOnly = FALSE, $groupName='label_format') {
301 static $list = array();
302 if (self
::_getGid($groupName)) {
303 // get saved label formats from Option Value table
304 $dao = new CRM_Core_DAO_OptionValue();
305 $dao->option_group_id
= self
::_getGid($groupName);
307 $dao->orderBy('weight');
309 while ($dao->fetch()) {
311 $list[$groupName][$dao->name
] = $dao->label
;
314 CRM_Core_DAO
::storeValues($dao, $list[$groupName][$dao->id
]);
318 return $list[$groupName];
322 * retrieve the default Label Format values
324 * @param string $groupName label format group name
326 * @return array Name/value pairs containing the default Label Format values.
330 static function &getDefaultValues($groupName = 'label_format') {
331 $params = array('is_active' => 1, 'is_default' => 1);
333 if (!self
::retrieve($params, $defaults, $groupName)) {
334 foreach (self
::$optionValueFields as $name => $field) {
335 $defaults[$name] = $field['default'];
337 $filter = array('option_group_id' => self
::_getGid($groupName));
338 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue', $filter);
344 * Get Label Format from the DB
346 * @param string $field Field name to search by
347 * @param int $val Field value to search for
349 * @return array $values (reference) associative array of name/value pairs
352 static function &getLabelFormat($field, $val, $groupName = 'label_format') {
353 $params = array('is_active' => 1, $field => $val);
354 $labelFormat = array();
355 if (self
::retrieve($params, $labelFormat, $groupName)) {
359 return self
::getDefaultValues($groupName);
364 * Get Label Format by Name
366 * @param int $name Label format name. Empty = get default label format
368 * @return array $values (reference) associative array of name/value pairs
371 static function &getByName($name) {
372 return self
::getLabelFormat('name', $name);
376 * Get Label Format by ID
378 * @param int $id label format id. 0 = get default label format
379 * @param string $groupName group name
381 * @return array $values (reference) associative array of name/value pairs
384 static function &getById($id, $groupName = 'label_format') {
385 return self
::getLabelFormat('id', $id, $groupName);
389 * Get Label Format field from associative array
391 * @param string $field name of a label format field
392 * @param array (reference) $values associative array of name/value pairs containing
393 * label format field selections
399 static function getValue($field, &$values, $default = NULL) {
400 if (array_key_exists($field, self
::$optionValueFields)) {
401 switch (self
::$optionValueFields[$field]['type']) {
402 case CRM_Utils_Type
::T_INT
:
403 return (int)CRM_Utils_Array
::value($field, $values, $default);
405 case CRM_Utils_Type
::T_FLOAT
:
406 // Round float values to three decimal places and trim trailing zeros.
407 // Add a leading zero to values less than 1.
408 $f = sprintf('%05.3f', $values[$field]);
411 return (float)(empty($f) ?
'0' : $f);
413 return CRM_Utils_Array
::value($field, $values, $default);
419 * Takes a bunch of params that are needed to match certain criteria and
420 * retrieves the relevant objects. Typically the valid params are only
421 * label id. It also stores all the retrieved values in the default array.
423 * @param array $params (reference ) an assoc array of name/value pairs
424 * @param array $values (reference ) an assoc array to hold the flattened values
426 * @return object CRM_Core_DAO_OptionValue object
430 static function retrieve(&$params, &$values, $groupName='label_format') {
431 $optionValue = new CRM_Core_DAO_OptionValue();
432 $optionValue->copyValues($params);
433 $optionValue->option_group_id
= self
::_getGid($groupName);
434 if ($optionValue->find(TRUE)) {
435 // Extract fields that have been serialized in the 'value' column of the Option Value table.
436 $values = json_decode($optionValue->value
, TRUE);
437 // Add any new fields that don't yet exist in the saved values.
438 foreach (self
::$optionValueFields as $name => $field) {
439 if (!isset($values[$name])) {
440 $values[$name] = $field['default'];
441 if ($field['metric']) {
442 $values[$name] = CRM_Utils_PDF_Utils
::convertMetric($field['default'],
443 self
::$optionValueFields['metric']['default'],
449 // Add fields from the OptionValue base class
450 CRM_Core_DAO
::storeValues($optionValue, $values);
457 * 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 (reference) $values associative array of name/value pairs
472 * @param int $id id of the database record (null = new record)
473 * @param string $groupName group name of the label format
478 function saveLabelFormat(&$values, $id = NULL, $groupName = 'label_format') {
479 // get the Option Group ID for Label Formats (create one if it doesn't exist)
480 $group_id = self
::_getGid($groupName);
482 // clear other default if this is the new default label format
483 if ($values['is_default']) {
484 $query = "UPDATE civicrm_option_value SET is_default = 0 WHERE option_group_id = $group_id";
485 CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
488 // fetch existing record
496 $list = self
::getList(TRUE,$groupName);
498 while (array_key_exists("custom_$cnt", $list)) $cnt++
;
499 $values['name'] = "custom_$cnt";
500 $values['grouping'] = self
::customGroupName();
502 // copy the supplied form values to the corresponding Option Value fields in the base class
503 foreach ($this->fields() as $name => $field) {
504 $this->$name = trim(CRM_Utils_Array
::value($name, $values, $this->$name));
505 if (empty($this->$name)) {
506 $this->$name = 'null';
510 $this->option_group_id
= $group_id;
511 $this->is_active
= 1;
513 // serialize label format fields into a single string to store in the 'value' column of the Option Value table
514 $v = json_decode($this->value
, TRUE);
515 foreach (self
::$optionValueFields as $name => $field) {
516 if (!isset($v[$name])) {
519 $v[$name] = self
::getValue($name, $values, $v[$name]);
521 $this->value
= json_encode($v);
523 // make sure serialized array will fit in the 'value' column
524 $attribute = CRM_Core_DAO
::getAttribute('CRM_Core_BAO_LabelFormat', 'value');
525 if (strlen($this->value
) > $attribute['maxlength']) {
526 CRM_Core_Error
::fatal(ts('Label Format does not fit in database.'));
530 // fix duplicate weights
531 $filter = array('option_group_id' => self
::_getGid());
532 CRM_Utils_Weight
::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $filter);
536 * Function to delete a Label Format
538 * @param int $id ID of the label format to be deleted.
539 * @param string $groupName group name
543 static function del($id, $groupName) {
545 $dao = new CRM_Core_DAO_OptionValue();
547 if ($dao->find(TRUE)) {
548 if ($dao->option_group_id
== self
::_getGid($groupName)) {
549 $filter = array('option_group_id' => self
::_getGid($groupName));
550 CRM_Utils_Weight
::delWeight('CRM_Core_DAO_OptionValue', $id, $filter);
556 CRM_Core_Error
::fatal(ts('Invalid value passed to delete function.'));