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