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