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