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