Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-08-19-00-06-22
[civicrm-core.git] / CRM / Core / BAO / LabelFormat.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.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 +--------------------------------------------------------------------+
27*/
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 /**
43 * static holder for the Label Formats Option Group ID
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 *
160 * @param void
161 *
162 * @return array array of page orientations
163 * @access public
01d7cdd8 164 * @static
6a488035 165 */
01d7cdd8 166 public static function getPageOrientations() {
6a488035
TO
167 return array(
168 'portrait' => ts('Portrait'),
169 'landscape' => ts('Landscape'),
170 );
171 }
172
173 /**
174 * Get font names supported by the TCPDF package used to create PDF labels.
175 *
eaf5045f 176 * @param string $name group name
6a488035
TO
177 *
178 * @return array array of font names
179 * @access public
01d7cdd8 180 * @static
6a488035 181 */
eaf5045f
KJ
182 public static function getFontNames($name='label_format') {
183 $label = new CRM_Utils_PDF_Label(self::getDefaultValues($name));
6a488035
TO
184 return $label->getFontNames();
185 }
186
187 /**
188 * Get font sizes supported by the TCPDF package used to create PDF labels.
189 *
190 * @param void
191 *
192 * @return array array of font sizes
193 * @access public
01d7cdd8 194 * @static
6a488035 195 */
01d7cdd8 196 public static function getFontSizes() {
b4a3ab6f 197 $fontSizes = array();
ccc0db8c 198 for ($i = 6; $i <= 60; $i++) {
199 $fontSizes[$i] = ts('%1 pt', array(1 => $i));
b4a3ab6f 200 }
201
202 return $fontSizes;
6a488035
TO
203 }
204
205 /**
206 * Get measurement units recognized by the TCPDF package used to create PDF labels.
207 *
208 * @param void
209 *
210 * @return array array of measurement units
211 * @access public
01d7cdd8 212 * @static
6a488035 213 */
01d7cdd8 214 public static function getUnits() {
6a488035
TO
215 return array(
216 'in' => ts('Inches'),
217 'cm' => ts('Centimeters'),
218 'mm' => ts('Millimeters'),
219 'pt' => ts('Points'),
220 );
221 }
222
636f1cbe
KJ
223 /**
224 * Get text alignment recognized by the TCPDF package used to create PDF labels.
225 *
226 * @param void
227 *
228 * @return array array of alignments
229 * @access public
230 * @static
231 */
232 public static function getTextAlignments() {
233 return array(
234 'R' => ts('Right'),
235 'L' => ts('Left'),
1b1d29d8 236 'C' => ts('Center'),
636f1cbe
KJ
237 );
238 }
239
0bc1ba89 240 /**
241 * Get text alignment recognized by the TCPDF package used to create PDF labels.
242 *
243 * @param void
244 *
245 * @return array array of alignments
246 * @access public
247 * @static
248 */
249 public static function getFontStyles() {
250 return array(
251 '' => ts('Normal'),
252 'B' => ts('Bold'),
253 'I' => ts('Italic'),
254 );
255 }
256
6a488035
TO
257 /**
258 * Get Option Group ID for Label Formats
259 *
77b97be7
EM
260 * @param string $name
261 *
262 * @internal param $void
6a488035
TO
263 *
264 * @return int Group ID (null if Group ID doesn't exist)
265 * @access private
266 */
eaf5045f
KJ
267 private static function _getGid($name='label_format') {
268 if (!isset(self::$_gid[$name]) || !self::$_gid[$name]) {
269 self::$_gid[$name] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $name, 'id', 'name');
270 if (!self::$_gid[$name]) {
6a488035
TO
271 CRM_Core_Error::fatal(ts('Label Format Option Group not found in database.'));
272 }
273 }
eaf5045f 274 return self::$_gid[$name];
6a488035
TO
275 }
276
277 /**
278 * Add ordering fields to Label Format list
279 *
280 * @param array (reference) $list List of Label Formats
281 * @param string $returnURL URL of page calling this function
282 *
283 * @return array (reference) List of Label Formats
284 * @static
285 * @access public
286 */
287 static function addOrder(&$list, $returnURL) {
288 $filter = "option_group_id = " . self::_getGid();
289 CRM_Utils_Weight::addOrder($list, 'CRM_Core_DAO_OptionValue', 'id', $returnURL, $filter);
290 }
291
292 /**
293 * Retrieve list of Label Formats.
294 *
295 * @param bool $namesOnly return simple list of names
4f1ddb25 296 * @param string $groupName group name of the label format option group
6a488035
TO
297 *
298 * @return array (reference) label format list
299 * @static
300 * @access public
301 */
4f1ddb25 302 static function &getList($namesOnly = FALSE, $groupName='label_format') {
6a488035 303 static $list = array();
4f1ddb25 304 if (self::_getGid($groupName)) {
6a488035
TO
305 // get saved label formats from Option Value table
306 $dao = new CRM_Core_DAO_OptionValue();
4f1ddb25 307 $dao->option_group_id = self::_getGid($groupName);
6a488035
TO
308 $dao->is_active = 1;
309 $dao->orderBy('weight');
310 $dao->find();
311 while ($dao->fetch()) {
312 if ($namesOnly) {
4f1ddb25 313 $list[$groupName][$dao->name] = $dao->label;
6a488035
TO
314 }
315 else {
4f1ddb25 316 CRM_Core_DAO::storeValues($dao, $list[$groupName][$dao->id]);
6a488035
TO
317 }
318 }
319 }
4f1ddb25 320 return $list[$groupName];
6a488035
TO
321 }
322
323 /**
324 * retrieve the default Label Format values
325 *
eaf5045f 326 * @param string $groupName label format group name
6a488035
TO
327 *
328 * @return array Name/value pairs containing the default Label Format values.
329 * @static
330 * @access public
331 */
eaf5045f 332 static function &getDefaultValues($groupName = 'label_format') {
6a488035
TO
333 $params = array('is_active' => 1, 'is_default' => 1);
334 $defaults = array();
eaf5045f 335 if (!self::retrieve($params, $defaults, $groupName)) {
6a488035
TO
336 foreach (self::$optionValueFields as $name => $field) {
337 $defaults[$name] = $field['default'];
338 }
eaf5045f 339 $filter = array('option_group_id' => self::_getGid($groupName));
6a488035
TO
340 $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', $filter);
341 }
342 return $defaults;
343 }
344
345 /**
346 * Get Label Format from the DB
347 *
77b97be7
EM
348 * @param string $field Field name to search by
349 * @param int $val Field value to search for
350 *
351 * @param string $groupName
6a488035
TO
352 *
353 * @return array $values (reference) associative array of name/value pairs
354 * @access public
355 */
eaf5045f 356 static function &getLabelFormat($field, $val, $groupName = 'label_format') {
6a488035
TO
357 $params = array('is_active' => 1, $field => $val);
358 $labelFormat = array();
eaf5045f 359 if (self::retrieve($params, $labelFormat, $groupName)) {
6a488035
TO
360 return $labelFormat;
361 }
362 else {
eaf5045f 363 return self::getDefaultValues($groupName);
6a488035
TO
364 }
365 }
366
367 /**
368 * Get Label Format by Name
369 *
370 * @param int $name Label format name. Empty = get default label format
371 *
372 * @return array $values (reference) associative array of name/value pairs
373 * @access public
374 */
375 static function &getByName($name) {
376 return self::getLabelFormat('name', $name);
377 }
378
379 /**
380 * Get Label Format by ID
381 *
382 * @param int $id label format id. 0 = get default label format
eaf5045f 383 * @param string $groupName group name
6a488035
TO
384 *
385 * @return array $values (reference) associative array of name/value pairs
386 * @access public
387 */
eaf5045f
KJ
388 static function &getById($id, $groupName = 'label_format') {
389 return self::getLabelFormat('id', $id, $groupName);
6a488035
TO
390 }
391
392 /**
393 * Get Label Format field from associative array
394 *
2a6da8d7
EM
395 * @param string $field name of a label format field
396 * @param array (reference) $values associative array of name/value pairs containing
6a488035
TO
397 * label format field selections
398 *
2a6da8d7
EM
399 * @param null $default
400 *
6a488035
TO
401 * @return value
402 * @access public
403 * @static
404 */
405 static function getValue($field, &$values, $default = NULL) {
406 if (array_key_exists($field, self::$optionValueFields)) {
407 switch (self::$optionValueFields[$field]['type']) {
408 case CRM_Utils_Type::T_INT:
409 return (int)CRM_Utils_Array::value($field, $values, $default);
410
411 case CRM_Utils_Type::T_FLOAT:
412 // Round float values to three decimal places and trim trailing zeros.
413 // Add a leading zero to values less than 1.
414 $f = sprintf('%05.3f', $values[$field]);
415 $f = rtrim($f, '0');
416 $f = rtrim($f, '.');
417 return (float)(empty($f) ? '0' : $f);
418 }
419 return CRM_Utils_Array::value($field, $values, $default);
420 }
421 return $default;
422 }
423
424 /**
425 * Takes a bunch of params that are needed to match certain criteria and
426 * retrieves the relevant objects. Typically the valid params are only
427 * label id. It also stores all the retrieved values in the default array.
428 *
77b97be7
EM
429 * @param array $params (reference ) an assoc array of name/value pairs
430 * @param array $values (reference ) an assoc array to hold the flattened values
431 *
432 * @param string $groupName
6a488035
TO
433 *
434 * @return object CRM_Core_DAO_OptionValue object
435 * @access public
436 * @static
437 */
eaf5045f 438 static function retrieve(&$params, &$values, $groupName='label_format') {
6a488035
TO
439 $optionValue = new CRM_Core_DAO_OptionValue();
440 $optionValue->copyValues($params);
eaf5045f 441 $optionValue->option_group_id = self::_getGid($groupName);
6a488035
TO
442 if ($optionValue->find(TRUE)) {
443 // Extract fields that have been serialized in the 'value' column of the Option Value table.
444 $values = json_decode($optionValue->value, TRUE);
445 // Add any new fields that don't yet exist in the saved values.
446 foreach (self::$optionValueFields as $name => $field) {
447 if (!isset($values[$name])) {
448 $values[$name] = $field['default'];
449 if ($field['metric']) {
450 $values[$name] = CRM_Utils_PDF_Utils::convertMetric($field['default'],
451 self::$optionValueFields['metric']['default'],
452 $values['metric'], 3
453 );
454 }
455 }
456 }
457 // Add fields from the OptionValue base class
458 CRM_Core_DAO::storeValues($optionValue, $values);
459 return $optionValue;
460 }
461 return NULL;
462 }
463
464 /**
465 * Return the name of the group for customized labels
466 *
467 * @param void
468 *
469 * @return void
470 * @access public
471 */
4f1ddb25 472 public static function customGroupName() {
6a488035
TO
473 return ts('Custom');
474 }
475
476 /**
477 * Save the Label Format in the DB
478 *
479 * @param array (reference) $values associative array of name/value pairs
480 * @param int $id id of the database record (null = new record)
eaf5045f 481 * @param string $groupName group name of the label format
6a488035
TO
482 *
483 * @return void
484 * @access public
485 */
eaf5045f 486 function saveLabelFormat(&$values, $id = NULL, $groupName = 'label_format') {
6a488035 487 // get the Option Group ID for Label Formats (create one if it doesn't exist)
eaf5045f 488 $group_id = self::_getGid($groupName);
6a488035
TO
489
490 // clear other default if this is the new default label format
491 if ($values['is_default']) {
492 $query = "UPDATE civicrm_option_value SET is_default = 0 WHERE option_group_id = $group_id";
493 CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
494 }
495 if ($id) {
496 // fetch existing record
497 $this->id = $id;
498 if ($this->find()) {
499 $this->fetch();
500 }
501 }
502 else {
503 // new record
eaf5045f 504 $list = self::getList(TRUE,$groupName);
6a488035
TO
505 $cnt = 1;
506 while (array_key_exists("custom_$cnt", $list)) $cnt++;
507 $values['name'] = "custom_$cnt";
508 $values['grouping'] = self::customGroupName();
509 }
510 // copy the supplied form values to the corresponding Option Value fields in the base class
511 foreach ($this->fields() as $name => $field) {
512 $this->$name = trim(CRM_Utils_Array::value($name, $values, $this->$name));
513 if (empty($this->$name)) {
514 $this->$name = 'null';
515 }
516 }
517 $this->id = $id;
518 $this->option_group_id = $group_id;
519 $this->is_active = 1;
520
521 // serialize label format fields into a single string to store in the 'value' column of the Option Value table
522 $v = json_decode($this->value, TRUE);
523 foreach (self::$optionValueFields as $name => $field) {
8e9fd9e4 524 if (!isset($v[$name])) {
525 $v[$name] = NULL;
526 }
6a488035
TO
527 $v[$name] = self::getValue($name, $values, $v[$name]);
528 }
529 $this->value = json_encode($v);
530
531 // make sure serialized array will fit in the 'value' column
532 $attribute = CRM_Core_DAO::getAttribute('CRM_Core_BAO_LabelFormat', 'value');
533 if (strlen($this->value) > $attribute['maxlength']) {
534 CRM_Core_Error::fatal(ts('Label Format does not fit in database.'));
535 }
536 $this->save();
537
538 // fix duplicate weights
539 $filter = array('option_group_id' => self::_getGid());
540 CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $filter);
541 }
542
543 /**
544 * Function to delete a Label Format
545 *
546 * @param int $id ID of the label format to be deleted.
eaf5045f 547 * @param string $groupName group name
6a488035
TO
548 * @access public
549 * @static
550 */
eaf5045f 551 static function del($id, $groupName) {
6a488035
TO
552 if ($id) {
553 $dao = new CRM_Core_DAO_OptionValue();
554 $dao->id = $id;
555 if ($dao->find(TRUE)) {
eaf5045f
KJ
556 if ($dao->option_group_id == self::_getGid($groupName)) {
557 $filter = array('option_group_id' => self::_getGid($groupName));
6a488035
TO
558 CRM_Utils_Weight::delWeight('CRM_Core_DAO_OptionValue', $id, $filter);
559 $dao->delete();
560 return;
561 }
562 }
563 }
564 CRM_Core_Error::fatal(ts('Invalid value passed to delete function.'));
565 }
566}
567