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