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