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