INFRA-132 - CRM/Upgrade - phpcbf (plus fixup)
[civicrm-core.git] / CRM / Upgrade / Snapshot / V4p2 / Price / BAO / FieldValue.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * Business objects for managing price fields values.
38 *
39 */
40 class CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue extends CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue {
41
42 /**
43 * Insert/update a new entry in the database.
44 *
45 * @param array $params
46 * (reference), array $ids.
47 *
48 * @param $ids
49 *
50 * @return CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue object
51 * @static
52 */
53 public static function &add(&$params, $ids) {
54
55 $fieldValueBAO = new CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue();
56 $fieldValueBAO->copyValues($params);
57
58 if ($id = CRM_Utils_Array::value('id', $ids)) {
59 $fieldValueBAO->id = $id;
60 }
61 if (!empty($params['is_default'])) {
62 $query = 'UPDATE civicrm_price_field_value SET is_default = 0 WHERE price_field_id = %1';
63 $p = array(1 => array($params['price_field_id'], 'Integer'));
64 CRM_Core_DAO::executeQuery($query, $p);
65 }
66
67 $fieldValueBAO->save();
68 return $fieldValueBAO;
69 }
70
71 /**
72 * Creates a new entry in the database.
73 *
74 * @param array $params
75 * (reference), array $ids.
76 *
77 * @param $ids
78 *
79 * @return CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue object
80 * @static
81 */
82 public static function create(&$params, $ids) {
83
84 if (!is_array($params) || empty($params)) {
85 return;
86 }
87
88 if ($id = CRM_Utils_Array::value('id', $ids)) {
89 if (isset($params['name'])) { unset($params['name']);
90 }
91
92 $oldWeight = NULL;
93 if ($id) {
94 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $id, 'weight', 'id');
95 }
96
97 $fieldValues = array('price_field_id' => CRM_Utils_Array::value('price_field_id', $params, 0));
98 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $oldWeight, $params['weight'], $fieldValues);
99 }
100 else {
101 if (empty($params['name'])) {
102 $params['name'] = CRM_Utils_String::munge(CRM_Utils_Array::value('label', $params), '_', 64);
103 }
104 if (empty($params['weight'])) {
105 $params['weight'] = 1;
106 }
107 }
108 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, 0);
109
110 return self::add($params, $ids);
111 }
112
113 /**
114 * Takes a bunch of params that are needed to match certain criteria and
115 * retrieves the relevant objects.
116 *
117 * @param array $params
118 * (reference ) an assoc array.
119 * @param array $defaults
120 * (reference ) an assoc array to hold the flattened values.
121 *
122 * @return CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue object
123 * @static
124 */
125 public static function retrieve(&$params, &$defaults) {
126 return CRM_Core_DAO::commonRetrieve('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $params, $defaults);
127 }
128
129 /**
130 * Retrive the all values for given field id
131 *
132 * @param int $fieldId
133 * Price_field_id.
134 * @param array $values
135 * (reference ) to hold the values.
136 * @param string $orderBy
137 * For order by, default weight.
138 * @param bool|int $isActive is_active, default false
139 *
140 * @return array $values
141 *
142 * @static
143 */
144 public static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE) {
145 $fieldValueDAO = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue();
146 $fieldValueDAO->price_field_id = $fieldId;
147 $fieldValueDAO->orderBy($orderBy, 'label');
148 if ($isActive) {
149 $fieldValueDAO->is_active = 1;
150 }
151 $fieldValueDAO->find();
152
153 while ($fieldValueDAO->fetch()) {
154 CRM_Core_DAO::storeValues($fieldValueDAO, $values[$fieldValueDAO->id]);
155 }
156
157 return $values;
158 }
159
160 /**
161 * Get the price field option label.
162 *
163 * @param int $id
164 * Id of field option.
165 *
166 * @return string name
167 *
168 * @static
169 *
170 */
171 public static function getOptionLabel($id) {
172 return CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $id, 'label');
173 }
174
175 /**
176 * Update the is_active flag in the db
177 *
178 * @param int $id
179 * Id of the database record.
180 * @param bool $is_active
181 * Value we want to set the is_active field.
182 *
183 * @return Object DAO object on sucess, null otherwise
184 *
185 * @static
186 */
187 public static function setIsActive($id, $is_active) {
188 return CRM_Core_DAO::setFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $id, 'is_active', $is_active);
189 }
190
191 /**
192 * Delete all values of the given field id
193 *
194 * @param int $fieldId
195 * Price field id.
196 *
197 * @return boolean
198 *
199 * @static
200 */
201 public static function deleteValues($fieldId) {
202 if (!$fieldId) {
203 return FALSE;
204 }
205
206 $fieldValueDAO = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue();
207 $fieldValueDAO->price_field_id = $fieldId;
208 $fieldValueDAO->delete();
209 }
210
211 /**
212 * Delete the value.
213 *
214 * @param int $id
215 * Id.
216 *
217 * @return boolean
218 *
219 * @static
220 */
221 public static function del($id) {
222 if (!$id) {
223 return FALSE;
224 }
225
226 $fieldValueDAO = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue();
227 $fieldValueDAO->id = $id;
228 return $fieldValueDAO->delete();
229 }
230 }