3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Business objects for managing price fields values.
22 class CRM_Price_BAO_PriceFieldValue
extends CRM_Price_DAO_PriceFieldValue
{
25 * Insert/update a new entry in the database.
27 * @param array $params
29 * @return CRM_Price_DAO_PriceFieldValue
31 public static function add($params) {
32 $fieldValueBAO = self
::writeRecord($params);
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);
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;
47 * Creates a new entry in the database.
49 * @param array $params
50 * (reference), array $ids.
54 * @return CRM_Price_DAO_PriceFieldValue
56 * @throws \CRM_Core_Exception
58 public static function create(&$params, $ids = []) {
59 $id = $params['id'] ??
$ids['id'] ??
NULL;
60 if (!is_array($params) ||
empty($params)) {
63 if (!$id && empty($params['name'])) {
64 $params['name'] = strtolower(CRM_Utils_String
::munge(($params['label'] ??
'_'), '_', 242));
67 if ($id && !empty($params['weight'])) {
68 if (isset($params['name'])) {
69 unset($params['name']);
74 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'weight', 'id');
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);
80 CRM_Core_DAO
::setCreateDefaults($params, self
::getDefaults());
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');
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");
92 return self
::add($params);
96 * Get defaults for new entity.
99 public static function getDefaults() {
108 * Retrieve DB object based on input parameters.
110 * It also stores all the retrieved values in the default array.
112 * @param array $params
113 * (reference ) an assoc array.
114 * @param array $defaults
115 * (reference ) an assoc array to hold the flattened values.
117 * @return CRM_Price_DAO_PriceFieldValue
119 public static function retrieve(&$params, &$defaults) {
120 return CRM_Core_DAO
::commonRetrieve('CRM_Price_DAO_PriceFieldValue', $params, $defaults);
124 * Retrieve all values for given field id.
126 * @param int $fieldId
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.
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;
146 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes);
148 $addWhere = "financial_type_id IN (0)";
150 if (!empty($financialTypes) && !$admin) {
151 $addWhere = "financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ")";
153 if (!empty($addWhere)) {
154 $fieldValueDAO->whereAdd($addWhere);
157 $fieldValueDAO->orderBy($orderBy, 'label');
159 $fieldValueDAO->is_active
= 1;
161 $fieldValueDAO->find();
162 while ($fieldValueDAO->fetch()) {
163 CRM_Core_DAO
::storeValues($fieldValueDAO, $values[$fieldValueDAO->id
]);
170 * Get the price field option label.
173 * Id of field option.
179 public static function getOptionLabel($id) {
180 return CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'label');
184 * Update the is_active flag in the db.
187 * Id of the database record.
188 * @param bool $is_active
189 * Value we want to set the is_active field.
192 * true if we found and updated the object, else false
194 public static function setIsActive($id, $is_active) {
195 return CRM_Core_DAO
::setFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'is_active', $is_active);
199 * Delete all values of the given field id.
201 * @param int $fieldId
206 public static function deleteValues($fieldId) {
211 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
212 $fieldValueDAO->price_field_id
= $fieldId;
213 $fieldValueDAO->delete();
225 public static function del($id) {
230 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
231 $fieldValueDAO->id
= $id;
232 return $fieldValueDAO->delete();
236 * Update civicrm_price_field_value.financial_type_id
237 * when financial_type_id of contribution_page or event is changed
239 * @param int $entityId
241 * @param string $entityTable table.
243 * @param string $financialTypeID type id.
247 public static function updateFinancialType($entityId, $entityTable, $financialTypeID) {
248 if (!$entityId ||
!$entityTable ||
!$financialTypeID) {
252 1 => [$entityId, 'Integer'],
253 2 => [$entityTable, 'String'],
254 3 => [$financialTypeID, 'Integer'],
256 // for event discount
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 ';
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
267 SET cpfv.financial_type_id = CASE
268 WHEN cpfv.membership_type_id IS NOT NULL
269 THEN cpfv.financial_type_id
272 cps.financial_type_id = %3
273 WHERE cpse.id IS NOT NULL {$where}";
275 CRM_Core_DAO
::executeQuery($sql, $params);