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