Merge pull request #18997 from eileenmcnaughton/trans
[civicrm-core.git] / CRM / Core / BAO / PdfFormat.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 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 +--------------------------------------------------------------------+
d25dd0ee 27 */
6a488035
TO
28
29/**
30 *
31 * @package CRM
ca5cec67 32 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
33 */
34
35/**
8eedd10a 36 * This class contains functions for managing PDF Page Formats.
6a488035
TO
37 */
38class CRM_Core_BAO_PdfFormat extends CRM_Core_DAO_OptionValue {
39
40 /**
fe482240 41 * Static holder for the PDF Page Formats Option Group ID.
518fa0ee 42 * @var int
6a488035
TO
43 */
44 private static $_gid = NULL;
45
46 /**
47 * PDF Page Format fields stored in the 'value' field of the Option Value table.
518fa0ee 48 * @var array
6a488035 49 */
be2fb01f
CW
50 private static $optionValueFields = [
51 'paper_size' => [
6a488035
TO
52 'name' => 'paper_size',
53 'type' => CRM_Utils_Type::T_STRING,
54 'default' => 'letter',
be2fb01f
CW
55 ],
56 'stationery' => [
bdfa67c3 57 'name' => 'stationery',
d3e86119 58 'type' => CRM_Utils_Type::T_STRING,
bdfa67c3 59 'default' => '',
be2fb01f
CW
60 ],
61 'orientation' => [
6a488035
TO
62 'name' => 'orientation',
63 'type' => CRM_Utils_Type::T_STRING,
64 'default' => 'portrait',
be2fb01f
CW
65 ],
66 'metric' => [
6a488035
TO
67 'name' => 'metric',
68 'type' => CRM_Utils_Type::T_STRING,
69 'default' => 'in',
be2fb01f
CW
70 ],
71 'margin_top' => [
6a488035
TO
72 'name' => 'margin_top',
73 'type' => CRM_Utils_Type::T_FLOAT,
74 'metric' => TRUE,
75 'default' => 0.75,
be2fb01f
CW
76 ],
77 'margin_bottom' => [
6a488035
TO
78 'name' => 'margin_bottom',
79 'type' => CRM_Utils_Type::T_FLOAT,
80 'metric' => TRUE,
81 'default' => 0.75,
be2fb01f
CW
82 ],
83 'margin_left' => [
6a488035
TO
84 'name' => 'margin_left',
85 'type' => CRM_Utils_Type::T_FLOAT,
86 'metric' => TRUE,
87 'default' => 0.75,
be2fb01f
CW
88 ],
89 'margin_right' => [
6a488035
TO
90 'name' => 'margin_right',
91 'type' => CRM_Utils_Type::T_FLOAT,
92 'metric' => TRUE,
93 'default' => 0.75,
be2fb01f
CW
94 ],
95 ];
6a488035
TO
96
97 /**
98 * Get page orientations recognized by the DOMPDF package used to create PDF letters.
99 *
a6c01b45
CW
100 * @return array
101 * array of page orientations
6a488035 102 */
66d0e5f3 103 public static function getPageOrientations() {
be2fb01f 104 return [
6a488035
TO
105 'portrait' => ts('Portrait'),
106 'landscape' => ts('Landscape'),
be2fb01f 107 ];
6a488035
TO
108 }
109
110 /**
111 * Get measurement units recognized by the DOMPDF package used to create PDF letters.
112 *
a6c01b45
CW
113 * @return array
114 * array of measurement units
6a488035 115 */
66d0e5f3 116 public static function getUnits() {
be2fb01f 117 return [
6a488035
TO
118 'in' => ts('Inches'),
119 'cm' => ts('Centimeters'),
120 'mm' => ts('Millimeters'),
121 'pt' => ts('Points'),
be2fb01f 122 ];
6a488035
TO
123 }
124
125 /**
fe482240 126 * Get Option Group ID for PDF Page Formats.
6a488035 127 *
a6c01b45
CW
128 * @return int
129 * Group ID (null if Group ID doesn't exist)
ac15829d 130 * @throws CRM_Core_Exception
6a488035 131 */
ed106721 132 private static function _getGid() {
6a488035
TO
133 if (!self::$_gid) {
134 self::$_gid = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'pdf_format', 'id', 'name');
135 if (!self::$_gid) {
ac15829d 136 throw new CRM_Core_Exception(ts('PDF Format Option Group not found in database.'));
6a488035
TO
137 }
138 }
139 return self::$_gid;
140 }
141
142 /**
fe482240 143 * Add ordering fields to Page Format list.
6a488035 144 *
518fa0ee 145 * @param array $list List of PDF Page Formats
6a0b768e
TO
146 * @param string $returnURL
147 * URL of page calling this function.
6a488035 148 */
00be9182 149 public static function addOrder(&$list, $returnURL) {
6a488035
TO
150 $filter = "option_group_id = " . self::_getGid();
151 CRM_Utils_Weight::addOrder($list, 'CRM_Core_DAO_OptionValue', 'id', $returnURL, $filter);
152 }
153
154 /**
155 * Get list of PDF Page Formats.
156 *
6a0b768e
TO
157 * @param bool $namesOnly
158 * Return simple list of names.
6a488035 159 *
a6c01b45
CW
160 * @return array
161 * (reference) PDF Page Format list
6a488035 162 */
00be9182 163 public static function &getList($namesOnly = FALSE) {
be2fb01f 164 static $list = [];
6a488035
TO
165 if (self::_getGid()) {
166 // get saved PDF Page Formats from Option Value table
167 $dao = new CRM_Core_DAO_OptionValue();
168 $dao->option_group_id = self::_getGid();
169 $dao->is_active = 1;
170 $dao->orderBy('weight');
171 $dao->find();
172 while ($dao->fetch()) {
173 if ($namesOnly) {
174 $list[$dao->id] = $dao->name;
175 }
176 else {
177 CRM_Core_DAO::storeValues($dao, $list[$dao->id]);
178 }
179 }
180 }
181 return $list;
182 }
183
184 /**
fe482240 185 * Get the default PDF Page Format values.
6a488035 186 *
a6c01b45
CW
187 * @return array
188 * Name/value pairs containing the default PDF Page Format values.
6a488035 189 */
00be9182 190 public static function &getDefaultValues() {
be2fb01f
CW
191 $params = ['is_active' => 1, 'is_default' => 1];
192 $defaults = [];
6a488035
TO
193 if (!self::retrieve($params, $defaults)) {
194 foreach (self::$optionValueFields as $name => $field) {
195 $defaults[$name] = $field['default'];
196 }
be2fb01f 197 $filter = ['option_group_id' => self::_getGid()];
6a488035
TO
198 $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', $filter);
199
200 // also set the id to avoid NOTICES, CRM-8454
201 $defaults['id'] = NULL;
202 }
203 return $defaults;
204 }
205
206 /**
fe482240 207 * Get PDF Page Format from the DB.
6a488035 208 *
6a0b768e
TO
209 * @param string $field
210 * Field name to search by.
211 * @param int $val
212 * Field value to search for.
6a488035 213 *
a6c01b45
CW
214 * @return array
215 * (reference) associative array of name/value pairs
6a488035 216 */
00be9182 217 public static function &getPdfFormat($field, $val) {
be2fb01f
CW
218 $params = ['is_active' => 1, $field => $val];
219 $pdfFormat = [];
6a488035
TO
220 if (self::retrieve($params, $pdfFormat)) {
221 return $pdfFormat;
222 }
223 else {
224 return self::getDefaultValues();
225 }
226 }
227
228 /**
fe482240 229 * Get PDF Page Format by Name.
6a488035 230 *
6a0b768e
TO
231 * @param int $name
232 * PDF Page Format name. Empty = get default PDF Page Format.
6a488035 233 *
a6c01b45
CW
234 * @return array
235 * (reference) associative array of name/value pairs
6a488035 236 */
00be9182 237 public static function &getByName($name) {
6a488035
TO
238 return self::getPdfFormat('name', $name);
239 }
240
241 /**
fe482240 242 * Get PDF Page Format by ID.
6a488035 243 *
6a0b768e
TO
244 * @param int $id
245 * PDF Page Format id. 0 = get default PDF Page Format.
6a488035 246 *
a6c01b45
CW
247 * @return array
248 * (reference) associative array of name/value pairs
6a488035 249 */
00be9182 250 public static function &getById($id) {
6a488035
TO
251 return self::getPdfFormat('id', $id);
252 }
253
254 /**
fe482240 255 * Get PDF Page Format field from associative array.
6a488035 256 *
6a0b768e
TO
257 * @param string $field
258 * Name of a PDF Page Format field.
518fa0ee 259 * @param array $values associative array of name/value pairs containing
6a488035
TO
260 * PDF Page Format field selections
261 *
fd31fa4c
EM
262 * @param null $default
263 *
6a488035 264 * @return value
6a488035 265 */
00be9182 266 public static function getValue($field, &$values, $default = NULL) {
6a488035
TO
267 if (array_key_exists($field, self::$optionValueFields)) {
268 switch (self::$optionValueFields[$field]['type']) {
269 case CRM_Utils_Type::T_INT:
353ffa53 270 return (int) CRM_Utils_Array::value($field, $values, $default);
6a488035
TO
271
272 case CRM_Utils_Type::T_FLOAT:
273 // Round float values to three decimal places and trim trailing zeros.
274 // Add a leading zero to values less than 1.
275 $f = sprintf('%05.3f', $values[$field]);
276 $f = rtrim($f, '0');
277 $f = rtrim($f, '.');
353ffa53 278 return (float) (empty($f) ? '0' : $f);
6a488035
TO
279 }
280 return CRM_Utils_Array::value($field, $values, $default);
281 }
282 return $default;
283 }
284
285 /**
fe482240
EM
286 * Retrieve DB object based on input parameters.
287 *
288 * It also stores all the retrieved values in the default array.
6a488035 289 *
6a0b768e
TO
290 * @param array $params
291 * (reference ) an assoc array of name/value pairs.
292 * @param array $values
293 * (reference ) an assoc array to hold the flattened values.
6a488035 294 *
16b10e64 295 * @return CRM_Core_DAO_OptionValue
6a488035 296 */
00be9182 297 public static function retrieve(&$params, &$values) {
6a488035
TO
298 $optionValue = new CRM_Core_DAO_OptionValue();
299 $optionValue->copyValues($params);
300 $optionValue->option_group_id = self::_getGid();
301 if ($optionValue->find(TRUE)) {
302 // Extract fields that have been serialized in the 'value' column of the Option Value table.
303 $values = json_decode($optionValue->value, TRUE);
304 // Add any new fields that don't yet exist in the saved values.
305 foreach (self::$optionValueFields as $name => $field) {
306 if (!isset($values[$name])) {
307 $values[$name] = $field['default'];
fa5ddecd 308 if (!empty($field['metric'])) {
6a488035
TO
309 $values[$name] = CRM_Utils_PDF_Utils::convertMetric($field['default'],
310 self::$optionValueFields['metric']['default'],
311 $values['metric'], 3
312 );
313 }
314 }
315 }
316 // Add fields from the OptionValue base class
317 CRM_Core_DAO::storeValues($optionValue, $values);
318 return $optionValue;
319 }
320 return NULL;
321 }
322
323 /**
fe482240 324 * Save the PDF Page Format in the DB.
6a488035 325 *
8eedd10a 326 * @param array $values associative array of name/value pairs
6a0b768e
TO
327 * @param int $id
328 * Id of the database record (null = new record).
ac15829d 329 * @throws CRM_Core_Exception
6a488035 330 */
00be9182 331 public function savePdfFormat(&$values, $id = NULL) {
6a488035
TO
332 // get the Option Group ID for PDF Page Formats (create one if it doesn't exist)
333 $group_id = self::_getGid();
334
335 // clear other default if this is the new default PDF Page Format
336 if ($values['is_default']) {
337 $query = "UPDATE civicrm_option_value SET is_default = 0 WHERE option_group_id = $group_id";
33621c4f 338 CRM_Core_DAO::executeQuery($query);
6a488035
TO
339 }
340 if ($id) {
341 // fetch existing record
342 $this->id = $id;
343 if ($this->find()) {
344 $this->fetch();
345 }
346 }
347 // copy the supplied form values to the corresponding Option Value fields in the base class
348 foreach ($this->fields() as $name => $field) {
349 $this->$name = trim(CRM_Utils_Array::value($name, $values, $this->$name));
350 if (empty($this->$name)) {
351 $this->$name = 'null';
352 }
353 }
354 $this->id = $id;
355 $this->option_group_id = $group_id;
356 $this->label = $this->name;
357 $this->is_active = 1;
358
359 // serialize PDF Page Format fields into a single string to store in the 'value' column of the Option Value table
360 $v = json_decode($this->value, TRUE);
361 foreach (self::$optionValueFields as $name => $field) {
dfad0283 362 $v[$name] = self::getValue($name, $values, CRM_Utils_Array::value($name, $v));
6a488035
TO
363 }
364 $this->value = json_encode($v);
365
366 // make sure serialized array will fit in the 'value' column
367 $attribute = CRM_Core_DAO::getAttribute('CRM_Core_BAO_PdfFormat', 'value');
368 if (strlen($this->value) > $attribute['maxlength']) {
ac15829d 369 throw new CRM_Core_Exception(ts('PDF Page Format does not fit in database.'));
6a488035
TO
370 }
371 $this->save();
372
373 // fix duplicate weights
be2fb01f 374 $filter = ['option_group_id' => self::_getGid()];
6a488035
TO
375 CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $filter);
376 }
377
378 /**
fe482240 379 * Delete a PDF Page Format.
6a488035 380 *
6a0b768e
TO
381 * @param int $id
382 * ID of the PDF Page Format to be deleted.
ac15829d 383 * @throws CRM_Core_Exception
6a488035 384 */
00be9182 385 public static function del($id) {
6a488035
TO
386 if ($id) {
387 $dao = new CRM_Core_DAO_OptionValue();
388 $dao->id = $id;
389 if ($dao->find(TRUE)) {
390 if ($dao->option_group_id == self::_getGid()) {
be2fb01f 391 $filter = ['option_group_id' => self::_getGid()];
6a488035
TO
392 CRM_Utils_Weight::delWeight('CRM_Core_DAO_OptionValue', $id, $filter);
393 $dao->delete();
394 return;
395 }
396 }
397 }
ac15829d 398 throw new CRM_Core_Exception(ts('Invalid value passed to delete function.'));
6a488035 399 }
96025800 400
6a488035 401}