519122e18fcaa27d338335923e7bd4da47638abd
[civicrm-core.git] / CRM / Price / BAO / PriceFieldValue.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 * $Id$
33 *
34 */
35
36 /**
37 * Business objects for managing price fields values.
38 *
39 */
40 class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue {
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_Price_DAO_PriceFieldValue
51 */
52 public static function add(&$params, $ids = array()) {
53
54 $fieldValueBAO = new CRM_Price_BAO_PriceFieldValue();
55 $fieldValueBAO->copyValues($params);
56
57 if ($id = CRM_Utils_Array::value('id', $ids)) {
58 $fieldValueBAO->id = $id;
59 $prevLabel = self::getOptionLabel($id);
60 if (!empty($params['label']) && $prevLabel != $params['label']) {
61 self::updateAmountAndFeeLevel($id, $prevLabel, $params['label']);
62 }
63 }
64 if (!empty($params['is_default'])) {
65 $query = 'UPDATE civicrm_price_field_value SET is_default = 0 WHERE price_field_id = %1';
66 $p = array(1 => array($params['price_field_id'], 'Integer'));
67 CRM_Core_DAO::executeQuery($query, $p);
68 }
69
70 $fieldValueBAO->save();
71 // Reset the cached values in this function.
72 CRM_Price_BAO_PriceField::getOptions(CRM_Utils_Array::value('price_field_id', $params), FALSE, TRUE);
73 return $fieldValueBAO;
74 }
75
76 /**
77 * Creates a new entry in the database.
78 *
79 * @param array $params
80 * (reference), array $ids.
81 *
82 * @param $ids
83 *
84 * @return CRM_Price_DAO_PriceFieldValue
85 */
86 public static function create(&$params, $ids = array()) {
87 $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('id', $ids));
88 if (!is_array($params) || empty($params)) {
89 return NULL;
90 }
91 if (!$id && empty($params['name'])) {
92 $params['name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 242));
93 }
94
95 if ($id && !empty($params['weight'])) {
96 if (isset($params['name'])) {
97 unset($params['name']);
98 }
99
100 $oldWeight = NULL;
101 if ($id) {
102 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'weight', 'id');
103 }
104
105 $fieldValues = array('price_field_id' => CRM_Utils_Array::value('price_field_id', $params, 0));
106 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Price_DAO_PriceFieldValue', $oldWeight, $params['weight'], $fieldValues);
107 }
108 else {
109 if (!$id) {
110 CRM_Core_DAO::setCreateDefaults($params, self::getDefaults());
111 if (empty($params['name'])) {
112 $params['name'] = CRM_Utils_String::munge(CRM_Utils_Array::value('label', $params), '_', 64);
113 }
114 }
115 }
116 return self::add($params, $ids);
117 }
118
119 /**
120 * Get defaults for new entity.
121 * @return array
122 */
123 public static function getDefaults() {
124 return array(
125 'is_active' => 1,
126 'weight' => 1,
127 );
128
129 }
130
131 /**
132 * Retrieve DB object based on input parameters.
133 *
134 * It also stores all the retrieved values in the default array.
135 *
136 * @param array $params
137 * (reference ) an assoc array.
138 * @param array $defaults
139 * (reference ) an assoc array to hold the flattened values.
140 *
141 * @return CRM_Price_DAO_PriceFieldValue
142 */
143 public static function retrieve(&$params, &$defaults) {
144 return CRM_Core_DAO::commonRetrieve('CRM_Price_DAO_PriceFieldValue', $params, $defaults);
145 }
146
147 /**
148 * Retrive the all values for given field id.
149 *
150 * @param int $fieldId
151 * Price_field_id.
152 * @param array $values
153 * (reference ) to hold the values.
154 * @param string $orderBy
155 * For order by, default weight.
156 * @param bool|int $isActive is_active, default false
157 *
158 * @return array
159 *
160 */
161 public static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE) {
162 $sql = "SELECT cs.id FROM civicrm_price_set cs INNER JOIN civicrm_price_field cp ON cp.price_set_id = cs.id
163 WHERE cs.name IN ('default_contribution_amount', 'default_membership_type_amount') AND cp.id = {$fieldId} ";
164 $setId = CRM_Core_DAO::singleValueQuery($sql);
165 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
166 $fieldValueDAO->price_field_id = $fieldId;
167 if (!$setId) {
168 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes);
169 $addWhere = "financial_type_id IN (0)";
170 if (!empty($financialTypes)) {
171 $addWhere = "financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ")";
172 }
173 $fieldValueDAO->whereAdd($addWhere);
174 }
175 $fieldValueDAO->orderBy($orderBy, 'label');
176 if ($isActive) {
177 $fieldValueDAO->is_active = 1;
178 }
179 $fieldValueDAO->find();
180
181 while ($fieldValueDAO->fetch()) {
182 CRM_Core_DAO::storeValues($fieldValueDAO, $values[$fieldValueDAO->id]);
183 }
184
185 return $values;
186 }
187
188 /**
189 * Get the price field option label.
190 *
191 * @param int $id
192 * Id of field option.
193 *
194 * @return string
195 * name
196 *
197 */
198 public static function getOptionLabel($id) {
199 return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'label');
200 }
201
202 /**
203 * Update the is_active flag in the db.
204 *
205 * @param int $id
206 * Id of the database record.
207 * @param bool $is_active
208 * Value we want to set the is_active field.
209 *
210 * @return Object
211 * DAO object on success, null otherwise
212 *
213 */
214 public static function setIsActive($id, $is_active) {
215 return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'is_active', $is_active);
216 }
217
218 /**
219 * Delete all values of the given field id.
220 *
221 * @param int $fieldId
222 * Price field id.
223 *
224 *
225 */
226 public static function deleteValues($fieldId) {
227 if (!$fieldId) {
228 return;
229 }
230
231 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
232 $fieldValueDAO->price_field_id = $fieldId;
233 $fieldValueDAO->delete();
234 }
235
236 /**
237 * Delete the value.
238 *
239 * @param int $id
240 * Id.
241 *
242 * @return bool
243 *
244 */
245 public static function del($id) {
246 if (!$id) {
247 return FALSE;
248 }
249
250 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
251 $fieldValueDAO->id = $id;
252 return $fieldValueDAO->delete();
253 }
254
255 /**
256 * Update civicrm_price_field_value.financial_type_id
257 * when financial_type_id of contribution_page or event is changed
258 *
259 * @param int $entityId
260 * Id.
261 * @param string $entityTable table.
262 * Entity table.
263 * @param string $financialTypeID type id.
264 * Financial type id.
265 *
266 */
267 public static function updateFinancialType($entityId, $entityTable, $financialTypeID) {
268 if (!$entityId || !$entityTable || !$financialTypeID) {
269 return;
270 }
271 $params = array(
272 1 => array($entityId, 'Integer'),
273 2 => array($entityTable, 'String'),
274 3 => array($financialTypeID, 'Integer'),
275 );
276 // for event discount
277 $join = $where = '';
278 if ($entityTable == 'civicrm_event') {
279 $join = " LEFT JOIN civicrm_discount cd ON cd.price_set_id = cps.id AND cd.entity_id = %1 AND cd.entity_table = %2 ";
280 $where = ' OR cd.id IS NOT NULL ';
281 }
282 $sql = "UPDATE civicrm_price_set cps
283 LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = cps.id AND cpse.entity_id = %1 AND cpse.entity_table = %2
284 LEFT JOIN civicrm_price_field cpf ON cpf.price_set_id = cps.id
285 LEFT JOIN civicrm_price_field_value cpfv ON cpf.id = cpfv.price_field_id
286 {$join}
287 SET cpfv.financial_type_id = CASE
288 WHEN cpfv.membership_type_id IS NOT NULL
289 THEN cpfv.financial_type_id
290 ELSE %3
291 END,
292 cps.financial_type_id = %3
293 WHERE cpse.id IS NOT NULL {$where}";
294
295 CRM_Core_DAO::executeQuery($sql, $params);
296 }
297
298 /**
299 * Update price option label in line_item, civicrm_contribution and civicrm_participant.
300 *
301 * @param int $id - id of the price_field_value
302 * @param string $prevLabel
303 * @param string $newLabel
304 *
305 */
306 public static function updateAmountAndFeeLevel($id, $prevLabel, $newLabel) {
307 // update price field label in line item.
308 $lineItem = new CRM_Price_DAO_LineItem();
309 $lineItem->price_field_value_id = $id;
310 $lineItem->label = $prevLabel;
311 $lineItem->find();
312 while ($lineItem->fetch()) {
313 $lineItemParams['id'] = $lineItem->id;
314 $lineItemParams['label'] = $newLabel;
315 CRM_Price_BAO_LineItem::create($lineItemParams);
316
317 // update amount and fee level in civicrm_contribution and civicrm_participant
318 $params = array(
319 1 => array(CRM_Core_DAO::VALUE_SEPARATOR . $prevLabel . ' -', 'String'),
320 2 => array(CRM_Core_DAO::VALUE_SEPARATOR . $newLabel . ' -', 'String'),
321 );
322 $participantIds = array();
323 // Update contribution
324 if (!empty($lineItem->contribution_id)) {
325 CRM_Core_DAO::executeQuery("UPDATE `civicrm_contribution` SET `amount_level` = REPLACE(amount_level, %1, %2) WHERE id = {$lineItem->contribution_id}", $params);
326 }
327 // Update participant
328 if ($lineItem->entity_table == 'civicrm_participant') {
329 CRM_Core_DAO::executeQuery("UPDATE `civicrm_participant` SET `fee_level` = REPLACE(fee_level, %1, %2) WHERE id = {$lineItem->entity_id}", $params);
330 }
331 }
332 }
333
334 }