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