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