Merge pull request #20626 from akosgarai/issue/2645
[civicrm-core.git] / CRM / Price / BAO / PriceFieldValue.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
19 * Business objects for managing price fields values.
20 *
21 */
9da8dc8c 22class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue {
6a488035
TO
23
24 /**
100fef9d 25 * Insert/update a new entry in the database.
6a488035 26 *
414c1420 27 * @param array $params
6a488035 28 *
16b10e64 29 * @return CRM_Price_DAO_PriceFieldValue
6a488035 30 */
f3601685 31 public static function add($params) {
a7488080 32 if (!empty($params['is_default'])) {
f3601685 33 $priceFieldID = $params['price_field_id'] ?? CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceFieldValue', $fieldValueBAO->id, 'price_field_id');
6a488035 34 $query = 'UPDATE civicrm_price_field_value SET is_default = 0 WHERE price_field_id = %1';
f3601685 35 $p = [1 => [$priceFieldID, 'Integer']];
6a488035
TO
36 CRM_Core_DAO::executeQuery($query, $p);
37 }
38
50a890fd 39 $fieldValueBAO = self::writeRecord($params);
40
ba6920b3 41 // Reset the cached values in this function.
183a0610 42 CRM_Price_BAO_PriceField::getOptions(CRM_Utils_Array::value('price_field_id', $params), FALSE, TRUE);
6a488035
TO
43 return $fieldValueBAO;
44 }
45
46 /**
47 * Creates a new entry in the database.
48 *
414c1420
TO
49 * @param array $params
50 * (reference), array $ids.
6a488035 51 *
77b97be7
EM
52 * @param $ids
53 *
16b10e64 54 * @return CRM_Price_DAO_PriceFieldValue
74531938 55 *
56 * @throws \CRM_Core_Exception
6a488035 57 */
be2fb01f 58 public static function create(&$params, $ids = []) {
8df1a020 59 $id = $params['id'] ?? $ids['id'] ?? NULL;
6a488035 60 if (!is_array($params) || empty($params)) {
408b79bf 61 return NULL;
6a488035 62 }
22e263ad 63 if (!$id && empty($params['name'])) {
7fb27138 64 $params['name'] = strtolower(CRM_Utils_String::munge(($params['label'] ?? '_'), '_', 242));
291ebece 65 }
6a488035 66
353ffa53 67 if ($id && !empty($params['weight'])) {
4f99ca55
TO
68 if (isset($params['name'])) {
69 unset($params['name']);
ba1dcfda 70 }
6a488035
TO
71
72 $oldWeight = NULL;
73 if ($id) {
9da8dc8c 74 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'weight', 'id');
6a488035 75 }
be2fb01f 76 $fieldValues = ['price_field_id' => CRM_Utils_Array::value('price_field_id', $params, 0)];
9da8dc8c 77 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Price_DAO_PriceFieldValue', $oldWeight, $params['weight'], $fieldValues);
6a488035 78 }
7fb27138
CW
79 elseif (!$id) {
80 CRM_Core_DAO::setCreateDefaults($params, self::getDefaults());
6a488035 81 }
1019b2fe 82
9c1bc317 83 $financialType = $params['financial_type_id'] ?? NULL;
1019b2fe
SL
84 if (!$financialType && $id) {
85 $financialType = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'financial_type_id', 'id');
86 }
87 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes);
88 if (!empty($financialType) && !array_key_exists($financialType, $financialTypes) && $params['is_active']) {
89 throw new CRM_Core_Exception("Financial Type for Price Field Option is either disabled or does not exist");
90 }
f3601685
CW
91 $params['id'] = $id;
92 return self::add($params);
6a488035
TO
93 }
94
db232378 95 /**
fe482240 96 * Get defaults for new entity.
db232378
EM
97 * @return array
98 */
00be9182 99 public static function getDefaults() {
be2fb01f 100 return [
db232378
EM
101 'is_active' => 1,
102 'weight' => 1,
be2fb01f 103 ];
db232378
EM
104
105 }
106
6a488035 107 /**
fe482240
EM
108 * Retrieve DB object based on input parameters.
109 *
110 * It also stores all the retrieved values in the default array.
6a488035 111 *
414c1420
TO
112 * @param array $params
113 * (reference ) an assoc array.
114 * @param array $defaults
115 * (reference ) an assoc array to hold the flattened values.
6a488035 116 *
16b10e64 117 * @return CRM_Price_DAO_PriceFieldValue
6a488035 118 */
00be9182 119 public static function retrieve(&$params, &$defaults) {
9da8dc8c 120 return CRM_Core_DAO::commonRetrieve('CRM_Price_DAO_PriceFieldValue', $params, $defaults);
6a488035
TO
121 }
122
123 /**
23dba589 124 * Retrieve all values for given field id.
6a488035 125 *
414c1420
TO
126 * @param int $fieldId
127 * Price_field_id.
128 * @param array $values
129 * (reference ) to hold the values.
130 * @param string $orderBy
131 * For order by, default weight.
dd244018 132 * @param bool|int $isActive is_active, default false
1019b2fe 133 * @param bool $admin is this loading it for use on an admin page.
6a488035 134 *
a6c01b45 135 * @return array
6a488035 136 *
6a488035 137 */
1019b2fe 138 public static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE, $admin = FALSE) {
8df1a020 139 $sql = "SELECT cs.id FROM civicrm_price_set cs INNER JOIN civicrm_price_field cp ON cp.price_set_id = cs.id
a17e67a1
PN
140 WHERE cs.name IN ('default_contribution_amount', 'default_membership_type_amount') AND cp.id = {$fieldId} ";
141 $setId = CRM_Core_DAO::singleValueQuery($sql);
9da8dc8c 142 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
6a488035 143 $fieldValueDAO->price_field_id = $fieldId;
1019b2fe 144 $addWhere = '';
a17e67a1
PN
145 if (!$setId) {
146 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes);
1019b2fe
SL
147 if (!$admin) {
148 $addWhere = "financial_type_id IN (0)";
149 }
150 if (!empty($financialTypes) && !$admin) {
a17e67a1
PN
151 $addWhere = "financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ")";
152 }
1019b2fe
SL
153 if (!empty($addWhere)) {
154 $fieldValueDAO->whereAdd($addWhere);
155 }
a17e67a1 156 }
6a488035
TO
157 $fieldValueDAO->orderBy($orderBy, 'label');
158 if ($isActive) {
159 $fieldValueDAO->is_active = 1;
160 }
161 $fieldValueDAO->find();
6a488035
TO
162 while ($fieldValueDAO->fetch()) {
163 CRM_Core_DAO::storeValues($fieldValueDAO, $values[$fieldValueDAO->id]);
164 }
165
166 return $values;
167 }
168
169 /**
170 * Get the price field option label.
171 *
414c1420
TO
172 * @param int $id
173 * Id of field option.
6a488035 174 *
a6c01b45
CW
175 * @return string
176 * name
6a488035 177 *
6a488035
TO
178 */
179 public static function getOptionLabel($id) {
9da8dc8c 180 return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'label');
6a488035
TO
181 }
182
183 /**
fe482240 184 * Update the is_active flag in the db.
6a488035 185 *
414c1420
TO
186 * @param int $id
187 * Id of the database record.
188 * @param bool $is_active
189 * Value we want to set the is_active field.
6a488035 190 *
8a4fede3 191 * @return bool
192 * true if we found and updated the object, else false
6a488035 193 */
00be9182 194 public static function setIsActive($id, $is_active) {
9da8dc8c 195 return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'is_active', $is_active);
6a488035
TO
196 }
197
198 /**
fe482240 199 * Delete all values of the given field id.
6a488035 200 *
414c1420
TO
201 * @param int $fieldId
202 * Price field id.
6a488035 203 *
6a488035 204 *
6a488035 205 */
00be9182 206 public static function deleteValues($fieldId) {
6a488035 207 if (!$fieldId) {
291ebece 208 return;
6a488035
TO
209 }
210
9da8dc8c 211 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
6a488035
TO
212 $fieldValueDAO->price_field_id = $fieldId;
213 $fieldValueDAO->delete();
214 }
215
216 /**
217 * Delete the value.
218 *
414c1420
TO
219 * @param int $id
220 * Id.
6a488035 221 *
408b79bf 222 * @return bool
6a488035 223 *
6a488035 224 */
00be9182 225 public static function del($id) {
6a488035
TO
226 if (!$id) {
227 return FALSE;
228 }
229
9da8dc8c 230 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
6a488035
TO
231 $fieldValueDAO->id = $id;
232 return $fieldValueDAO->delete();
233 }
77b97be7 234
157b21d8 235 /**
77b97be7 236 * Update civicrm_price_field_value.financial_type_id
157b21d8
PN
237 * when financial_type_id of contribution_page or event is changed
238 *
414c1420
TO
239 * @param int $entityId
240 * Id.
408b79bf 241 * @param string $entityTable table.
414c1420 242 * Entity table.
408b79bf 243 * @param string $financialTypeID type id.
414c1420 244 * Financial type id.
157b21d8 245 *
157b21d8 246 */
00be9182 247 public static function updateFinancialType($entityId, $entityTable, $financialTypeID) {
157b21d8 248 if (!$entityId || !$entityTable || !$financialTypeID) {
291ebece 249 return;
157b21d8 250 }
be2fb01f
CW
251 $params = [
252 1 => [$entityId, 'Integer'],
253 2 => [$entityTable, 'String'],
254 3 => [$financialTypeID, 'Integer'],
255 ];
cc9b58f3
PN
256 // for event discount
257 $join = $where = '';
258 if ($entityTable == 'civicrm_event') {
259 $join = " LEFT JOIN civicrm_discount cd ON cd.price_set_id = cps.id AND cd.entity_id = %1 AND cd.entity_table = %2 ";
260 $where = ' OR cd.id IS NOT NULL ';
157b21d8 261 }
77b97be7
EM
262 $sql = "UPDATE civicrm_price_set cps
263LEFT 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
264LEFT JOIN civicrm_price_field cpf ON cpf.price_set_id = cps.id
265LEFT JOIN civicrm_price_field_value cpfv ON cpf.id = cpfv.price_field_id
266{$join}
267SET cpfv.financial_type_id = CASE
268 WHEN cpfv.membership_type_id IS NOT NULL
269 THEN cpfv.financial_type_id
270 ELSE %3
271END,
272cps.financial_type_id = %3
273WHERE cpse.id IS NOT NULL {$where}";
77b97be7 274
157b21d8
PN
275 CRM_Core_DAO::executeQuery($sql, $params);
276 }
96025800 277
6a488035 278}