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 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);
39 $fieldValueBAO = self
::writeRecord($params);
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 and copy to defaults array.
110 * @param array $params
111 * Array of criteria values.
112 * @param array $defaults
113 * Array to be populated with found values.
116 * The DAO object, if found.
120 public static function retrieve($params, &$defaults) {
121 return self
::commonRetrieve(self
::class, $params, $defaults);
125 * Retrieve all values for given field id.
127 * @param int $fieldId
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.
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;
147 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes);
149 $addWhere = "financial_type_id IN (0)";
151 if (!empty($financialTypes) && !$admin) {
152 $addWhere = "financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ")";
154 if (!empty($addWhere)) {
155 $fieldValueDAO->whereAdd($addWhere);
158 $fieldValueDAO->orderBy($orderBy, 'label');
160 $fieldValueDAO->is_active
= 1;
162 $fieldValueDAO->find();
163 while ($fieldValueDAO->fetch()) {
164 CRM_Core_DAO
::storeValues($fieldValueDAO, $values[$fieldValueDAO->id
]);
171 * Get the price field option label.
174 * Id of field option.
180 public static function getOptionLabel($id) {
181 return CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'label');
185 * Update the is_active flag in the db.
188 * Id of the database record.
189 * @param bool $is_active
190 * Value we want to set the is_active field.
193 * true if we found and updated the object, else false
195 public static function setIsActive($id, $is_active) {
196 return CRM_Core_DAO
::setFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'is_active', $is_active);
200 * Delete all values of the given field id.
202 * @param int $fieldId
207 public static function deleteValues($fieldId) {
212 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
213 $fieldValueDAO->price_field_id
= $fieldId;
214 $fieldValueDAO->delete();
225 public static function del($id) {
226 return (bool) self
::deleteRecord(['id' => $id]);
230 * Update civicrm_price_field_value.financial_type_id
231 * when financial_type_id of contribution_page or event is changed
233 * @param int $entityId
235 * @param string $entityTable table.
237 * @param string $financialTypeID type id.
241 public static function updateFinancialType($entityId, $entityTable, $financialTypeID) {
242 if (!$entityId ||
!$entityTable ||
!$financialTypeID) {
246 1 => [$entityId, 'Integer'],
247 2 => [$entityTable, 'String'],
248 3 => [$financialTypeID, 'Integer'],
250 // for event discount
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 ';
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
261 SET cpfv.financial_type_id = CASE
262 WHEN cpfv.membership_type_id IS NOT NULL
263 THEN cpfv.financial_type_id
266 cps.financial_type_id = %3
267 WHERE cpse.id IS NOT NULL {$where}";
269 CRM_Core_DAO
::executeQuery($sql, $params);