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