[REF] - Deprecate & delegate BAO::retrieve
[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 if (!empty($params['is_default'])) {
33 $priceFieldID = $params['price_field_id'] ?? CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceFieldValue', $fieldValueBAO->id, 'price_field_id');
34 $query = 'UPDATE civicrm_price_field_value SET is_default = 0 WHERE price_field_id = %1';
35 $p = [1 => [$priceFieldID, 'Integer']];
36 CRM_Core_DAO::executeQuery($query, $p);
37 }
38
39 $fieldValueBAO = self::writeRecord($params);
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 and copy to defaults array.
109 *
110 * @param array $params
111 * Array of criteria values.
112 * @param array $defaults
113 * Array to be populated with found values.
114 *
115 * @return self|null
116 * The DAO object, if found.
117 *
118 * @deprecated
119 */
120 public static function retrieve($params, &$defaults) {
121 return self::commonRetrieve(self::class, $params, $defaults);
122 }
123
124 /**
125 * Retrieve all values for given field id.
126 *
127 * @param int $fieldId
128 * Price_field_id.
129 * @param array $values
130 * (reference ) to hold the values.
131 * @param string $orderBy
132 * For order by, default weight.
133 * @param bool|int $isActive is_active, default false
134 * @param bool $admin is this loading it for use on an admin page.
135 *
136 * @return array
137 *
138 */
139 public static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE, $admin = FALSE) {
140 $sql = "SELECT cs.id FROM civicrm_price_set cs INNER JOIN civicrm_price_field cp ON cp.price_set_id = cs.id
141 WHERE cs.name IN ('default_contribution_amount', 'default_membership_type_amount') AND cp.id = {$fieldId} ";
142 $setId = CRM_Core_DAO::singleValueQuery($sql);
143 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
144 $fieldValueDAO->price_field_id = $fieldId;
145 $addWhere = '';
146 if (!$setId) {
147 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes);
148 if (!$admin) {
149 $addWhere = "financial_type_id IN (0)";
150 }
151 if (!empty($financialTypes) && !$admin) {
152 $addWhere = "financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ")";
153 }
154 if (!empty($addWhere)) {
155 $fieldValueDAO->whereAdd($addWhere);
156 }
157 }
158 $fieldValueDAO->orderBy($orderBy, 'label');
159 if ($isActive) {
160 $fieldValueDAO->is_active = 1;
161 }
162 $fieldValueDAO->find();
163 while ($fieldValueDAO->fetch()) {
164 CRM_Core_DAO::storeValues($fieldValueDAO, $values[$fieldValueDAO->id]);
165 }
166
167 return $values;
168 }
169
170 /**
171 * Get the price field option label.
172 *
173 * @param int $id
174 * Id of field option.
175 *
176 * @return string
177 * name
178 *
179 */
180 public static function getOptionLabel($id) {
181 return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'label');
182 }
183
184 /**
185 * Update the is_active flag in the db.
186 *
187 * @param int $id
188 * Id of the database record.
189 * @param bool $is_active
190 * Value we want to set the is_active field.
191 *
192 * @return bool
193 * true if we found and updated the object, else false
194 */
195 public static function setIsActive($id, $is_active) {
196 return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'is_active', $is_active);
197 }
198
199 /**
200 * Delete all values of the given field id.
201 *
202 * @param int $fieldId
203 * Price field id.
204 *
205 *
206 */
207 public static function deleteValues($fieldId) {
208 if (!$fieldId) {
209 return;
210 }
211
212 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
213 $fieldValueDAO->price_field_id = $fieldId;
214 $fieldValueDAO->delete();
215 }
216
217 /**
218 * Delete the value.
219 *
220 * @param int $id
221 *
222 * @deprecated
223 * @return bool
224 */
225 public static function del($id) {
226 return (bool) self::deleteRecord(['id' => $id]);
227 }
228
229 /**
230 * Update civicrm_price_field_value.financial_type_id
231 * when financial_type_id of contribution_page or event is changed
232 *
233 * @param int $entityId
234 * Id.
235 * @param string $entityTable table.
236 * Entity table.
237 * @param string $financialTypeID type id.
238 * Financial type id.
239 *
240 */
241 public static function updateFinancialType($entityId, $entityTable, $financialTypeID) {
242 if (!$entityId || !$entityTable || !$financialTypeID) {
243 return;
244 }
245 $params = [
246 1 => [$entityId, 'Integer'],
247 2 => [$entityTable, 'String'],
248 3 => [$financialTypeID, 'Integer'],
249 ];
250 // for event discount
251 $join = $where = '';
252 if ($entityTable == 'civicrm_event') {
253 $join = " LEFT JOIN civicrm_discount cd ON cd.price_set_id = cps.id AND cd.entity_id = %1 AND cd.entity_table = %2 ";
254 $where = ' OR cd.id IS NOT NULL ';
255 }
256 $sql = "UPDATE civicrm_price_set cps
257 LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = cps.id AND cpse.entity_id = %1 AND cpse.entity_table = %2
258 LEFT JOIN civicrm_price_field cpf ON cpf.price_set_id = cps.id
259 LEFT JOIN civicrm_price_field_value cpfv ON cpf.id = cpfv.price_field_id
260 {$join}
261 SET cpfv.financial_type_id = CASE
262 WHEN cpfv.membership_type_id IS NOT NULL
263 THEN cpfv.financial_type_id
264 ELSE %3
265 END,
266 cps.financial_type_id = %3
267 WHERE cpse.id IS NOT NULL {$where}";
268
269 CRM_Core_DAO::executeQuery($sql, $params);
270 }
271
272 }