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 Paper Sizes | |
39 | */ | |
40 | class CRM_Core_BAO_PaperSize extends CRM_Core_DAO_OptionValue { | |
41 | ||
42 | /** | |
100fef9d | 43 | * Static holder for the Paper Size Option Group ID |
6a488035 TO |
44 | */ |
45 | private static $_gid = NULL; | |
46 | ||
47 | /** | |
48 | * Paper Size fields stored in the 'value' field of the Option Value table. | |
49 | */ | |
50 | private static $optionValueFields = array( | |
51 | 'metric' => array( | |
52 | 'name' => 'metric', | |
53 | 'type' => CRM_Utils_Type::T_STRING, | |
54 | 'default' => 'mm', | |
55 | ), | |
56 | 'width' => array( | |
57 | 'name' => 'width', | |
58 | 'type' => CRM_Utils_Type::T_FLOAT, | |
59 | 'metric' => TRUE, | |
60 | 'default' => 612, | |
61 | ), | |
62 | 'height' => array( | |
63 | 'name' => 'height', | |
64 | 'type' => CRM_Utils_Type::T_FLOAT, | |
65 | 'metric' => TRUE, | |
66 | 'default' => 792, | |
67 | ), | |
68 | ); | |
69 | ||
70 | /** | |
71 | * Get Option Group ID for Paper Sizes | |
72 | * | |
73 | * @param void | |
74 | * | |
a6c01b45 CW |
75 | * @return int |
76 | * Group ID (null if Group ID doesn't exist) | |
6a488035 | 77 | */ |
a5611c8e | 78 | private static function _getGid() { |
6a488035 TO |
79 | if (!self::$_gid) { |
80 | self::$_gid = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'paper_size', 'id', 'name'); | |
81 | if (!self::$_gid) { | |
82 | CRM_Core_Error::fatal(ts('Paper Size Option Group not found in database.')); | |
83 | } | |
84 | } | |
85 | return self::$_gid; | |
86 | } | |
87 | ||
88 | /** | |
89 | * Add ordering fields to Paper Size list | |
90 | * | |
6a0b768e TO |
91 | * @param array (reference) $list List of Paper Sizes |
92 | * @param string $returnURL | |
93 | * URL of page calling this function. | |
6a488035 | 94 | * |
a6c01b45 CW |
95 | * @return array |
96 | * (reference) List of Paper Sizes | |
6a488035 | 97 | * @static |
6a488035 | 98 | */ |
00be9182 | 99 | public static function &addOrder(&$list, $returnURL) { |
6a488035 TO |
100 | $filter = "option_group_id = " . self::_getGid(); |
101 | CRM_Utils_Weight::addOrder($list, 'CRM_Core_DAO_OptionValue', 'id', $returnURL, $filter); | |
102 | } | |
103 | ||
104 | /** | |
105 | * Retrieve list of Paper Sizes. | |
106 | * | |
6a0b768e TO |
107 | * @param bool $namesOnly |
108 | * Return simple list of names. | |
6a488035 | 109 | * |
a6c01b45 CW |
110 | * @return array |
111 | * (reference) Paper Size list | |
6a488035 | 112 | * @static |
6a488035 | 113 | */ |
00be9182 | 114 | public static function &getList($namesOnly = FALSE) { |
6a488035 TO |
115 | static $list = array(); |
116 | if (self::_getGid()) { | |
117 | // get saved Paper Sizes from Option Value table | |
118 | $dao = new CRM_Core_DAO_OptionValue(); | |
119 | $dao->option_group_id = self::_getGid(); | |
120 | $dao->is_active = 1; | |
121 | $dao->orderBy('weight'); | |
122 | $dao->find(); | |
123 | while ($dao->fetch()) { | |
124 | if ($namesOnly) { | |
125 | $list[$dao->name] = $dao->label; | |
126 | } | |
127 | else { | |
128 | CRM_Core_DAO::storeValues($dao, $list[$dao->id]); | |
129 | } | |
130 | } | |
131 | } | |
132 | return $list; | |
133 | } | |
134 | ||
135 | /** | |
100fef9d | 136 | * Retrieve the default Paper Size values |
6a488035 TO |
137 | * |
138 | * @param NULL | |
139 | * | |
a6c01b45 CW |
140 | * @return array |
141 | * Name/value pairs containing the default Paper Size values. | |
6a488035 | 142 | * @static |
6a488035 | 143 | */ |
00be9182 | 144 | public static function &getDefaultValues() { |
6a488035 TO |
145 | $params = array('is_active' => 1, 'is_default' => 1); |
146 | $defaults = array(); | |
147 | if (!self::retrieve($params, $defaults)) { | |
148 | foreach (self::$optionValueFields as $name => $field) { | |
149 | $defaults[$name] = $field['default']; | |
150 | } | |
151 | $filter = array('option_group_id' => self::_getGid()); | |
152 | $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', $filter); | |
153 | } | |
154 | return $defaults; | |
155 | } | |
156 | ||
157 | /** | |
158 | * Get Paper Size from the DB | |
159 | * | |
6a0b768e TO |
160 | * @param string $field |
161 | * Field name to search by. | |
162 | * @param int $val | |
163 | * Field value to search for. | |
6a488035 | 164 | * |
a6c01b45 CW |
165 | * @return array |
166 | * (reference) associative array of name/value pairs | |
6a488035 | 167 | */ |
00be9182 | 168 | public static function &getPaperFormat($field, $val) { |
6a488035 TO |
169 | $params = array('is_active' => 1, $field => $val); |
170 | $paperFormat = array(); | |
171 | if (self::retrieve($params, $paperFormat)) { | |
172 | return $paperFormat; | |
173 | } | |
174 | else { | |
175 | return self::getDefaultValues(); | |
176 | } | |
177 | } | |
178 | ||
179 | /** | |
180 | * Get Paper Size by Name | |
181 | * | |
6a0b768e TO |
182 | * @param int $name |
183 | * Paper Size name. Empty = get default Paper Size. | |
6a488035 | 184 | * |
a6c01b45 CW |
185 | * @return array |
186 | * (reference) associative array of name/value pairs | |
6a488035 | 187 | */ |
00be9182 | 188 | public static function &getByName($name) { |
6a488035 TO |
189 | return self::getPaperFormat('name', $name); |
190 | } | |
191 | ||
192 | /** | |
193 | * Get Paper Size by ID | |
194 | * | |
6a0b768e TO |
195 | * @param int $id |
196 | * Paper Size id. 0 = get default Paper Size. | |
6a488035 | 197 | * |
a6c01b45 CW |
198 | * @return array |
199 | * (reference) associative array of name/value pairs | |
6a488035 | 200 | */ |
00be9182 | 201 | public static function &getById($id) { |
6a488035 TO |
202 | return self::getPaperFormat('id', $id); |
203 | } | |
204 | ||
205 | /** | |
206 | * Get Paper Size field from associative array | |
207 | * | |
6a0b768e TO |
208 | * @param string $field |
209 | * Name of a Paper Size field. | |
da6b46f4 | 210 | * @param array (reference) $values associative array of name/value pairs containing |
6a488035 TO |
211 | * Paper Size field selections |
212 | * | |
da6b46f4 EM |
213 | * @param null $default |
214 | * | |
6a488035 | 215 | * @return value |
6a488035 TO |
216 | * @static |
217 | */ | |
00be9182 | 218 | public static function getValue($field, &$values, $default = NULL) { |
6a488035 TO |
219 | if (array_key_exists($field, self::$optionValueFields)) { |
220 | switch (self::$optionValueFields[$field]['type']) { | |
221 | case CRM_Utils_Type::T_INT: | |
222 | return (int)CRM_Utils_Array::value($field, $values, $default); | |
223 | ||
224 | case CRM_Utils_Type::T_FLOAT: | |
225 | // Round float values to three decimal places and trim trailing zeros. | |
226 | // Add a leading zero to values less than 1. | |
227 | $f = sprintf('%05.3f', $values[$field]); | |
228 | $f = rtrim($f, '0'); | |
229 | $f = rtrim($f, '.'); | |
230 | return (float)(empty($f) ? '0' : $f); | |
231 | } | |
232 | return CRM_Utils_Array::value($field, $values, $default); | |
233 | } | |
234 | return $default; | |
235 | } | |
236 | ||
237 | /** | |
238 | * Takes a bunch of params that are needed to match certain criteria and | |
239 | * retrieves the relevant objects. Typically the valid params are only | |
240 | * paper size id. It also stores all the retrieved values in the default array. | |
241 | * | |
6a0b768e TO |
242 | * @param array $params |
243 | * (reference ) an assoc array of name/value pairs. | |
244 | * @param array $values | |
245 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 246 | * |
c490a46a | 247 | * @return CRM_Core_DAO_OptionValue object |
6a488035 TO |
248 | * @static |
249 | */ | |
00be9182 | 250 | public static function retrieve(&$params, &$values) { |
6a488035 TO |
251 | $optionValue = new CRM_Core_DAO_OptionValue(); |
252 | $optionValue->copyValues($params); | |
253 | $optionValue->option_group_id = self::_getGid(); | |
254 | if ($optionValue->find(TRUE)) { | |
255 | // Extract fields that have been serialized in the 'value' column of the Option Value table. | |
256 | $values = json_decode($optionValue->value, TRUE); | |
257 | // Add any new fields that don't yet exist in the saved values. | |
258 | foreach (self::$optionValueFields as $name => $field) { | |
259 | if (!isset($values[$name])) { | |
260 | $values[$name] = $field['default']; | |
261 | if ($field['metric']) { | |
262 | $values[$name] = CRM_Utils_PDF_Utils::convertMetric($field['default'], | |
263 | self::$optionValueFields['metric']['default'], | |
264 | $values['metric'], 3 | |
265 | ); | |
266 | } | |
267 | } | |
268 | } | |
269 | // Add fields from the OptionValue base class | |
270 | CRM_Core_DAO::storeValues($optionValue, $values); | |
271 | return $optionValue; | |
272 | } | |
273 | return NULL; | |
274 | } | |
275 | ||
276 | /** | |
277 | * Save the Paper Size in the DB | |
278 | * | |
6a0b768e TO |
279 | * @param array (reference) $values associative array of name/value pairs |
280 | * @param int $id | |
281 | * Id of the database record (null = new record). | |
6a488035 TO |
282 | * |
283 | * @return void | |
6a488035 | 284 | */ |
00be9182 | 285 | public function savePaperSize(&$values, $id) { |
6a488035 TO |
286 | // get the Option Group ID for Paper Sizes (create one if it doesn't exist) |
287 | $group_id = self::_getGid(TRUE); | |
288 | ||
289 | // clear other default if this is the new default Paper Size | |
290 | if ($values['is_default']) { | |
291 | $query = "UPDATE civicrm_option_value SET is_default = 0 WHERE option_group_id = $group_id"; | |
292 | CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
293 | } | |
294 | if ($id) { | |
295 | // fetch existing record | |
296 | $this->id = $id; | |
297 | if ($this->find()) { | |
298 | $this->fetch(); | |
299 | } | |
300 | } | |
301 | else { | |
302 | // new record: set group = custom | |
303 | $values['grouping'] = self::customGroupName(); | |
304 | } | |
305 | // copy the supplied form values to the corresponding Option Value fields in the base class | |
306 | foreach ($this->fields() as $name => $field) { | |
307 | $this->$name = trim(CRM_Utils_Array::value($name, $values, $this->$name)); | |
308 | if (empty($this->$name)) { | |
309 | $this->$name = 'null'; | |
310 | } | |
311 | } | |
312 | $this->id = $id; | |
313 | $this->option_group_id = $group_id; | |
314 | $this->label = $this->name; | |
315 | $this->is_active = 1; | |
316 | ||
317 | // serialize Paper Size fields into a single string to store in the 'value' column of the Option Value table | |
318 | $v = json_decode($this->value, TRUE); | |
319 | foreach (self::$optionValueFields as $name => $field) { | |
320 | $v[$name] = self::getValue($name, $values, $v[$name]); | |
321 | } | |
322 | $this->value = json_encode($v); | |
323 | ||
324 | // make sure serialized array will fit in the 'value' column | |
325 | $attribute = CRM_Core_DAO::getAttribute('CRM_Core_BAO_PaperSize', 'value'); | |
326 | if (strlen($this->value) > $attribute['maxlength']) { | |
327 | CRM_Core_Error::fatal(ts('Paper Size does not fit in database.')); | |
328 | } | |
329 | $this->save(); | |
330 | ||
331 | // fix duplicate weights | |
332 | $filter = array('option_group_id' => self::_getGid()); | |
333 | CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $filter); | |
334 | } | |
335 | ||
336 | /** | |
100fef9d | 337 | * Delete a Paper Size |
6a488035 | 338 | * |
6a0b768e TO |
339 | * @param int $id |
340 | * ID of the Paper Size to be deleted. | |
6a488035 | 341 | * |
6a488035 TO |
342 | * @static |
343 | */ | |
00be9182 | 344 | public static function del($id) { |
6a488035 TO |
345 | if ($id) { |
346 | $dao = new CRM_Core_DAO_OptionValue(); | |
347 | $dao->id = $id; | |
348 | if ($dao->find(TRUE)) { | |
349 | if ($dao->option_group_id == self::_getGid()) { | |
350 | $filter = array('option_group_id' => self::_getGid()); | |
351 | CRM_Utils_Weight::delWeight('CRM_Core_DAO_OptionValue', $id, $filter); | |
352 | $dao->delete(); | |
353 | return; | |
354 | } | |
355 | } | |
356 | } | |
357 | CRM_Core_Error::fatal(ts('Invalid value passed to delete function.')); | |
358 | } | |
359 | } |