Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Business objects for managing price fields values. | |
38 | * | |
39 | */ | |
9da8dc8c | 40 | class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue { |
6a488035 TO |
41 | |
42 | /** | |
100fef9d | 43 | * Insert/update a new entry in the database. |
6a488035 | 44 | * |
414c1420 TO |
45 | * @param array $params |
46 | * (reference), array $ids. | |
6a488035 | 47 | * |
77b97be7 EM |
48 | * @param $ids |
49 | * | |
16b10e64 | 50 | * @return CRM_Price_DAO_PriceFieldValue |
6a488035 | 51 | */ |
00be9182 | 52 | public static function add(&$params, $ids = array()) { |
6a488035 | 53 | |
9da8dc8c | 54 | $fieldValueBAO = new CRM_Price_BAO_PriceFieldValue(); |
6a488035 TO |
55 | $fieldValueBAO->copyValues($params); |
56 | ||
57 | if ($id = CRM_Utils_Array::value('id', $ids)) { | |
58 | $fieldValueBAO->id = $id; | |
564e4022 | 59 | $prevLabel = self::getOptionLabel($id); |
e276cab3 | 60 | if (!empty($params['label']) && $prevLabel != $params['label']) { |
564e4022 | 61 | self::updateAmountAndFeeLevel($id, $prevLabel, $params['label']); |
62 | } | |
6a488035 | 63 | } |
a7488080 | 64 | if (!empty($params['is_default'])) { |
6a488035 TO |
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(); | |
ba6920b3 | 71 | // Reset the cached values in this function. |
183a0610 | 72 | CRM_Price_BAO_PriceField::getOptions(CRM_Utils_Array::value('price_field_id', $params), FALSE, TRUE); |
6a488035 TO |
73 | return $fieldValueBAO; |
74 | } | |
75 | ||
76 | /** | |
77 | * Creates a new entry in the database. | |
78 | * | |
414c1420 TO |
79 | * @param array $params |
80 | * (reference), array $ids. | |
6a488035 | 81 | * |
77b97be7 EM |
82 | * @param $ids |
83 | * | |
16b10e64 | 84 | * @return CRM_Price_DAO_PriceFieldValue |
6a488035 | 85 | */ |
00be9182 | 86 | public static function create(&$params, $ids = array()) { |
db232378 | 87 | $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('id', $ids)); |
6a488035 | 88 | if (!is_array($params) || empty($params)) { |
408b79bf | 89 | return NULL; |
6a488035 | 90 | } |
22e263ad | 91 | if (!$id && empty($params['name'])) { |
b10b974d | 92 | $params['name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 242)); |
291ebece | 93 | } |
6a488035 | 94 | |
353ffa53 | 95 | if ($id && !empty($params['weight'])) { |
4f99ca55 TO |
96 | if (isset($params['name'])) { |
97 | unset($params['name']); | |
ba1dcfda | 98 | } |
6a488035 TO |
99 | |
100 | $oldWeight = NULL; | |
101 | if ($id) { | |
9da8dc8c | 102 | $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'weight', 'id'); |
6a488035 TO |
103 | } |
104 | ||
105 | $fieldValues = array('price_field_id' => CRM_Utils_Array::value('price_field_id', $params, 0)); | |
9da8dc8c | 106 | $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Price_DAO_PriceFieldValue', $oldWeight, $params['weight'], $fieldValues); |
6a488035 TO |
107 | } |
108 | else { | |
db232378 | 109 | if (!$id) { |
64d24a64 | 110 | CRM_Core_DAO::setCreateDefaults($params, self::getDefaults()); |
db232378 EM |
111 | if (empty($params['name'])) { |
112 | $params['name'] = CRM_Utils_String::munge(CRM_Utils_Array::value('label', $params), '_', 64); | |
113 | } | |
6a488035 TO |
114 | } |
115 | } | |
6a488035 TO |
116 | return self::add($params, $ids); |
117 | } | |
118 | ||
db232378 | 119 | /** |
fe482240 | 120 | * Get defaults for new entity. |
db232378 EM |
121 | * @return array |
122 | */ | |
00be9182 | 123 | public static function getDefaults() { |
db232378 EM |
124 | return array( |
125 | 'is_active' => 1, | |
126 | 'weight' => 1, | |
127 | ); | |
128 | ||
129 | } | |
130 | ||
6a488035 | 131 | /** |
fe482240 EM |
132 | * Retrieve DB object based on input parameters. |
133 | * | |
134 | * It also stores all the retrieved values in the default array. | |
6a488035 | 135 | * |
414c1420 TO |
136 | * @param array $params |
137 | * (reference ) an assoc array. | |
138 | * @param array $defaults | |
139 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 140 | * |
16b10e64 | 141 | * @return CRM_Price_DAO_PriceFieldValue |
6a488035 | 142 | */ |
00be9182 | 143 | public static function retrieve(&$params, &$defaults) { |
9da8dc8c | 144 | return CRM_Core_DAO::commonRetrieve('CRM_Price_DAO_PriceFieldValue', $params, $defaults); |
6a488035 TO |
145 | } |
146 | ||
147 | /** | |
fe482240 | 148 | * Retrive the all values for given field id. |
6a488035 | 149 | * |
414c1420 TO |
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. | |
dd244018 | 156 | * @param bool|int $isActive is_active, default false |
6a488035 | 157 | * |
a6c01b45 | 158 | * @return array |
6a488035 | 159 | * |
6a488035 | 160 | */ |
00be9182 | 161 | public static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE) { |
a17e67a1 PN |
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); | |
9da8dc8c | 165 | $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue(); |
6a488035 | 166 | $fieldValueDAO->price_field_id = $fieldId; |
a17e67a1 PN |
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 | } | |
6a488035 TO |
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 | * | |
414c1420 TO |
191 | * @param int $id |
192 | * Id of field option. | |
6a488035 | 193 | * |
a6c01b45 CW |
194 | * @return string |
195 | * name | |
6a488035 | 196 | * |
6a488035 TO |
197 | */ |
198 | public static function getOptionLabel($id) { | |
9da8dc8c | 199 | return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'label'); |
6a488035 TO |
200 | } |
201 | ||
202 | /** | |
fe482240 | 203 | * Update the is_active flag in the db. |
6a488035 | 204 | * |
414c1420 TO |
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. | |
6a488035 | 209 | * |
a6c01b45 | 210 | * @return Object |
b44e3f84 | 211 | * DAO object on success, null otherwise |
6a488035 | 212 | * |
6a488035 | 213 | */ |
00be9182 | 214 | public static function setIsActive($id, $is_active) { |
9da8dc8c | 215 | return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'is_active', $is_active); |
6a488035 TO |
216 | } |
217 | ||
218 | /** | |
fe482240 | 219 | * Delete all values of the given field id. |
6a488035 | 220 | * |
414c1420 TO |
221 | * @param int $fieldId |
222 | * Price field id. | |
6a488035 | 223 | * |
6a488035 | 224 | * |
6a488035 | 225 | */ |
00be9182 | 226 | public static function deleteValues($fieldId) { |
6a488035 | 227 | if (!$fieldId) { |
291ebece | 228 | return; |
6a488035 TO |
229 | } |
230 | ||
9da8dc8c | 231 | $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue(); |
6a488035 TO |
232 | $fieldValueDAO->price_field_id = $fieldId; |
233 | $fieldValueDAO->delete(); | |
234 | } | |
235 | ||
236 | /** | |
237 | * Delete the value. | |
238 | * | |
414c1420 TO |
239 | * @param int $id |
240 | * Id. | |
6a488035 | 241 | * |
408b79bf | 242 | * @return bool |
6a488035 | 243 | * |
6a488035 | 244 | */ |
00be9182 | 245 | public static function del($id) { |
6a488035 TO |
246 | if (!$id) { |
247 | return FALSE; | |
248 | } | |
249 | ||
9da8dc8c | 250 | $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue(); |
6a488035 TO |
251 | $fieldValueDAO->id = $id; |
252 | return $fieldValueDAO->delete(); | |
253 | } | |
77b97be7 | 254 | |
157b21d8 | 255 | /** |
77b97be7 | 256 | * Update civicrm_price_field_value.financial_type_id |
157b21d8 PN |
257 | * when financial_type_id of contribution_page or event is changed |
258 | * | |
414c1420 TO |
259 | * @param int $entityId |
260 | * Id. | |
408b79bf | 261 | * @param string $entityTable table. |
414c1420 | 262 | * Entity table. |
408b79bf | 263 | * @param string $financialTypeID type id. |
414c1420 | 264 | * Financial type id. |
157b21d8 | 265 | * |
157b21d8 | 266 | */ |
00be9182 | 267 | public static function updateFinancialType($entityId, $entityTable, $financialTypeID) { |
157b21d8 | 268 | if (!$entityId || !$entityTable || !$financialTypeID) { |
291ebece | 269 | return; |
157b21d8 | 270 | } |
157b21d8 PN |
271 | $params = array( |
272 | 1 => array($entityId, 'Integer'), | |
273 | 2 => array($entityTable, 'String'), | |
274 | 3 => array($financialTypeID, 'Integer'), | |
157b21d8 | 275 | ); |
cc9b58f3 PN |
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 '; | |
157b21d8 | 281 | } |
77b97be7 EM |
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 | |
cc9b58f3 PN |
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}"; | |
77b97be7 | 294 | |
157b21d8 PN |
295 | CRM_Core_DAO::executeQuery($sql, $params); |
296 | } | |
96025800 | 297 | |
564e4022 | 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 | $lineItem->label = $newLabel; | |
314 | $lineItem->save(); | |
5a4a15e5 | 315 | // update amount and fee level in civicrm_contribution and civicrm_participant |
316 | $params = array( | |
317 | 1 => array(CRM_Core_DAO::VALUE_SEPARATOR . $prevLabel . ' -', 'String'), | |
318 | 2 => array(CRM_Core_DAO::VALUE_SEPARATOR . $newLabel . ' -', 'String'), | |
319 | ); | |
07f49b81 | 320 | $participantIds = array(); |
5a4a15e5 | 321 | if (!empty($lineItem->contribution_id)) { |
322 | CRM_Core_DAO::executeQuery("UPDATE `civicrm_contribution` SET `amount_level` = REPLACE(amount_level, %1, %2) WHERE id = {$lineItem->contribution_id}", $params); | |
323 | $participantIds = CRM_Event_BAO_Participant::getParticipantIds($lineItem->contribution_id); | |
07f49b81 | 324 | } |
325 | // if contribution is not attached to the participant. | |
326 | if (empty($participantIds) && $lineItem->entity_table == 'civicrm_participant') { | |
327 | $participantIds[] = $lineItem->entity_id; | |
328 | } | |
329 | foreach ($participantIds as $key => $id) { | |
330 | if (!empty($id)) { | |
331 | CRM_Core_DAO::executeQuery("UPDATE `civicrm_participant` SET `fee_level` = REPLACE(fee_level, %1, %2) WHERE id = {$id}", $params); | |
564e4022 | 332 | } |
333 | } | |
334 | } | |
335 | } | |
336 | ||
6a488035 | 337 | } |