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