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