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