Merge pull request #17622 from seamuslee001/smart_group_error
[civicrm-core.git] / CRM / Price / BAO / PriceFieldValue.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Business objects for managing price fields values.
20 *
21 */
22 class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue {
23
24 /**
25 * Insert/update a new entry in the database.
26 *
27 * @param array $params
28 *
29 * @return CRM_Price_DAO_PriceFieldValue
30 */
31 public static function add($params) {
32 $fieldValueBAO = self::writeRecord($params);
33
34 if (!empty($params['is_default'])) {
35 $priceFieldID = $params['price_field_id'] ?? CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceFieldValue', $fieldValueBAO->id, 'price_field_id');
36 $query = 'UPDATE civicrm_price_field_value SET is_default = 0 WHERE price_field_id = %1';
37 $p = [1 => [$priceFieldID, 'Integer']];
38 CRM_Core_DAO::executeQuery($query, $p);
39 }
40
41 // Reset the cached values in this function.
42 CRM_Price_BAO_PriceField::getOptions(CRM_Utils_Array::value('price_field_id', $params), FALSE, TRUE);
43 return $fieldValueBAO;
44 }
45
46 /**
47 * Creates a new entry in the database.
48 *
49 * @param array $params
50 * (reference), array $ids.
51 *
52 * @param $ids
53 *
54 * @return CRM_Price_DAO_PriceFieldValue
55 *
56 * @throws \CRM_Core_Exception
57 */
58 public static function create(&$params, $ids = []) {
59 $id = $params['id'] ?? $ids['id'] ?? NULL;
60 if (!is_array($params) || empty($params)) {
61 return NULL;
62 }
63 if (!$id && empty($params['name'])) {
64 $params['name'] = strtolower(CRM_Utils_String::munge(($params['label'] ?? '_'), '_', 242));
65 }
66
67 if ($id && !empty($params['weight'])) {
68 if (isset($params['name'])) {
69 unset($params['name']);
70 }
71
72 $oldWeight = NULL;
73 if ($id) {
74 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'weight', 'id');
75 }
76 $fieldValues = ['price_field_id' => CRM_Utils_Array::value('price_field_id', $params, 0)];
77 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Price_DAO_PriceFieldValue', $oldWeight, $params['weight'], $fieldValues);
78 }
79 elseif (!$id) {
80 CRM_Core_DAO::setCreateDefaults($params, self::getDefaults());
81 }
82
83 $financialType = $params['financial_type_id'] ?? NULL;
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 }
91 $params['id'] = $id;
92 return self::add($params);
93 }
94
95 /**
96 * Get defaults for new entity.
97 * @return array
98 */
99 public static function getDefaults() {
100 return [
101 'is_active' => 1,
102 'weight' => 1,
103 ];
104
105 }
106
107 /**
108 * Retrieve DB object based on input parameters.
109 *
110 * It also stores all the retrieved values in the default array.
111 *
112 * @param array $params
113 * (reference ) an assoc array.
114 * @param array $defaults
115 * (reference ) an assoc array to hold the flattened values.
116 *
117 * @return CRM_Price_DAO_PriceFieldValue
118 */
119 public static function retrieve(&$params, &$defaults) {
120 return CRM_Core_DAO::commonRetrieve('CRM_Price_DAO_PriceFieldValue', $params, $defaults);
121 }
122
123 /**
124 * Retrieve all values for given field id.
125 *
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.
132 * @param bool|int $isActive is_active, default false
133 * @param bool $admin is this loading it for use on an admin page.
134 *
135 * @return array
136 *
137 */
138 public static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE, $admin = FALSE) {
139 $sql = "SELECT cs.id FROM civicrm_price_set cs INNER JOIN civicrm_price_field cp ON cp.price_set_id = cs.id
140 WHERE cs.name IN ('default_contribution_amount', 'default_membership_type_amount') AND cp.id = {$fieldId} ";
141 $setId = CRM_Core_DAO::singleValueQuery($sql);
142 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
143 $fieldValueDAO->price_field_id = $fieldId;
144 $addWhere = '';
145 if (!$setId) {
146 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes);
147 if (!$admin) {
148 $addWhere = "financial_type_id IN (0)";
149 }
150 if (!empty($financialTypes) && !$admin) {
151 $addWhere = "financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ")";
152 }
153 if (!empty($addWhere)) {
154 $fieldValueDAO->whereAdd($addWhere);
155 }
156 }
157 $fieldValueDAO->orderBy($orderBy, 'label');
158 if ($isActive) {
159 $fieldValueDAO->is_active = 1;
160 }
161 $fieldValueDAO->find();
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 *
172 * @param int $id
173 * Id of field option.
174 *
175 * @return string
176 * name
177 *
178 */
179 public static function getOptionLabel($id) {
180 return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'label');
181 }
182
183 /**
184 * Update the is_active flag in the db.
185 *
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.
190 *
191 * @return bool
192 * true if we found and updated the object, else false
193 */
194 public static function setIsActive($id, $is_active) {
195 return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'is_active', $is_active);
196 }
197
198 /**
199 * Delete all values of the given field id.
200 *
201 * @param int $fieldId
202 * Price field id.
203 *
204 *
205 */
206 public static function deleteValues($fieldId) {
207 if (!$fieldId) {
208 return;
209 }
210
211 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
212 $fieldValueDAO->price_field_id = $fieldId;
213 $fieldValueDAO->delete();
214 }
215
216 /**
217 * Delete the value.
218 *
219 * @param int $id
220 * Id.
221 *
222 * @return bool
223 *
224 */
225 public static function del($id) {
226 if (!$id) {
227 return FALSE;
228 }
229
230 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
231 $fieldValueDAO->id = $id;
232 return $fieldValueDAO->delete();
233 }
234
235 /**
236 * Update civicrm_price_field_value.financial_type_id
237 * when financial_type_id of contribution_page or event is changed
238 *
239 * @param int $entityId
240 * Id.
241 * @param string $entityTable table.
242 * Entity table.
243 * @param string $financialTypeID type id.
244 * Financial type id.
245 *
246 */
247 public static function updateFinancialType($entityId, $entityTable, $financialTypeID) {
248 if (!$entityId || !$entityTable || !$financialTypeID) {
249 return;
250 }
251 $params = [
252 1 => [$entityId, 'Integer'],
253 2 => [$entityTable, 'String'],
254 3 => [$financialTypeID, 'Integer'],
255 ];
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 ';
261 }
262 $sql = "UPDATE civicrm_price_set cps
263 LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = cps.id AND cpse.entity_id = %1 AND cpse.entity_table = %2
264 LEFT JOIN civicrm_price_field cpf ON cpf.price_set_id = cps.id
265 LEFT JOIN civicrm_price_field_value cpfv ON cpf.id = cpfv.price_field_id
266 {$join}
267 SET cpfv.financial_type_id = CASE
268 WHEN cpfv.membership_type_id IS NOT NULL
269 THEN cpfv.financial_type_id
270 ELSE %3
271 END,
272 cps.financial_type_id = %3
273 WHERE cpse.id IS NOT NULL {$where}";
274
275 CRM_Core_DAO::executeQuery($sql, $params);
276 }
277
278 }