3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * Business objects for managing price fields values.
38 class CRM_Price_BAO_PriceFieldValue
extends CRM_Price_DAO_PriceFieldValue
{
41 * Insert/update a new entry in the database.
43 * @param array $params
46 * Deprecated variable.
48 * @return CRM_Price_DAO_PriceFieldValue
50 public static function add(&$params, $ids = []) {
51 $hook = empty($params['id']) ?
'create' : 'edit';
52 CRM_Utils_Hook
::pre($hook, 'PriceFieldValue', CRM_Utils_Array
::value('id', $params), $params);
54 $fieldValueBAO = new CRM_Price_BAO_PriceFieldValue();
55 $fieldValueBAO->copyValues($params);
58 $priceFieldID = CRM_Utils_Array
::value('price_field_id', $params);
60 $id = CRM_Utils_Array
::value('id', $ids, CRM_Utils_Array
::value('id', $params));
63 $priceFieldID = CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceFieldValue', $id, 'price_field_id');
65 if (!empty($params['is_default'])) {
66 $query = 'UPDATE civicrm_price_field_value SET is_default = 0 WHERE price_field_id = %1';
67 $p = [1 => [$params['price_field_id'], 'Integer']];
68 CRM_Core_DAO
::executeQuery($query, $p);
71 $fieldValueBAO->save();
72 CRM_Utils_Hook
::post($hook, 'PriceFieldValue', $fieldValueBAO->id
, $fieldValueBAO);
74 // Reset the cached values in this function.
75 CRM_Price_BAO_PriceField
::getOptions(CRM_Utils_Array
::value('price_field_id', $params), FALSE, TRUE);
76 return $fieldValueBAO;
80 * Creates a new entry in the database.
82 * @param array $params
83 * (reference), array $ids.
87 * @return CRM_Price_DAO_PriceFieldValue
89 * @throws \CRM_Core_Exception
91 public static function create(&$params, $ids = []) {
92 $id = CRM_Utils_Array
::value('id', $params, CRM_Utils_Array
::value('id', $ids));
93 if (!is_array($params) ||
empty($params)) {
96 if (!$id && empty($params['name'])) {
97 $params['name'] = strtolower(CRM_Utils_String
::munge($params['label'], '_', 242));
100 if ($id && !empty($params['weight'])) {
101 if (isset($params['name'])) {
102 unset($params['name']);
107 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'weight', 'id');
109 $fieldValues = ['price_field_id' => CRM_Utils_Array
::value('price_field_id', $params, 0)];
110 $params['weight'] = CRM_Utils_Weight
::updateOtherWeights('CRM_Price_DAO_PriceFieldValue', $oldWeight, $params['weight'], $fieldValues);
114 CRM_Core_DAO
::setCreateDefaults($params, self
::getDefaults());
115 if (empty($params['name'])) {
116 $params['name'] = CRM_Utils_String
::munge(CRM_Utils_Array
::value('label', $params), '_', 64);
121 $financialType = CRM_Utils_Array
::value('financial_type_id', $params, NULL);
122 if (!$financialType && $id) {
123 $financialType = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'financial_type_id', 'id');
125 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes);
126 if (!empty($financialType) && !array_key_exists($financialType, $financialTypes) && $params['is_active']) {
127 throw new CRM_Core_Exception("Financial Type for Price Field Option is either disabled or does not exist");
129 return self
::add($params, $ids);
133 * Get defaults for new entity.
136 public static function getDefaults() {
145 * Retrieve DB object based on input parameters.
147 * It also stores all the retrieved values in the default array.
149 * @param array $params
150 * (reference ) an assoc array.
151 * @param array $defaults
152 * (reference ) an assoc array to hold the flattened values.
154 * @return CRM_Price_DAO_PriceFieldValue
156 public static function retrieve(&$params, &$defaults) {
157 return CRM_Core_DAO
::commonRetrieve('CRM_Price_DAO_PriceFieldValue', $params, $defaults);
161 * Retrieve all values for given field id.
163 * @param int $fieldId
165 * @param array $values
166 * (reference ) to hold the values.
167 * @param string $orderBy
168 * For order by, default weight.
169 * @param bool|int $isActive is_active, default false
170 * @param bool $admin is this loading it for use on an admin page.
175 public static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE, $admin = FALSE) {
176 $sql = "SELECT cs.id FROM civicrm_price_set cs INNER JOIN civicrm_price_field cp ON cp.price_set_id = cs.id
177 WHERE cs.name IN ('default_contribution_amount', 'default_membership_type_amount') AND cp.id = {$fieldId} ";
178 $setId = CRM_Core_DAO
::singleValueQuery($sql);
179 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
180 $fieldValueDAO->price_field_id
= $fieldId;
183 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes);
185 $addWhere = "financial_type_id IN (0)";
187 if (!empty($financialTypes) && !$admin) {
188 $addWhere = "financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ")";
190 if (!empty($addWhere)) {
191 $fieldValueDAO->whereAdd($addWhere);
194 $fieldValueDAO->orderBy($orderBy, 'label');
196 $fieldValueDAO->is_active
= 1;
198 $fieldValueDAO->find();
199 while ($fieldValueDAO->fetch()) {
200 CRM_Core_DAO
::storeValues($fieldValueDAO, $values[$fieldValueDAO->id
]);
207 * Get the price field option label.
210 * Id of field option.
216 public static function getOptionLabel($id) {
217 return CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'label');
221 * Update the is_active flag in the db.
224 * Id of the database record.
225 * @param bool $is_active
226 * Value we want to set the is_active field.
229 * true if we found and updated the object, else false
231 public static function setIsActive($id, $is_active) {
232 return CRM_Core_DAO
::setFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'is_active', $is_active);
236 * Delete all values of the given field id.
238 * @param int $fieldId
243 public static function deleteValues($fieldId) {
248 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
249 $fieldValueDAO->price_field_id
= $fieldId;
250 $fieldValueDAO->delete();
262 public static function del($id) {
267 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
268 $fieldValueDAO->id
= $id;
269 return $fieldValueDAO->delete();
273 * Update civicrm_price_field_value.financial_type_id
274 * when financial_type_id of contribution_page or event is changed
276 * @param int $entityId
278 * @param string $entityTable table.
280 * @param string $financialTypeID type id.
284 public static function updateFinancialType($entityId, $entityTable, $financialTypeID) {
285 if (!$entityId ||
!$entityTable ||
!$financialTypeID) {
289 1 => [$entityId, 'Integer'],
290 2 => [$entityTable, 'String'],
291 3 => [$financialTypeID, 'Integer'],
293 // for event discount
295 if ($entityTable == 'civicrm_event') {
296 $join = " LEFT JOIN civicrm_discount cd ON cd.price_set_id = cps.id AND cd.entity_id = %1 AND cd.entity_table = %2 ";
297 $where = ' OR cd.id IS NOT NULL ';
299 $sql = "UPDATE civicrm_price_set cps
300 LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = cps.id AND cpse.entity_id = %1 AND cpse.entity_table = %2
301 LEFT JOIN civicrm_price_field cpf ON cpf.price_set_id = cps.id
302 LEFT JOIN civicrm_price_field_value cpfv ON cpf.id = cpfv.price_field_id
304 SET cpfv.financial_type_id = CASE
305 WHEN cpfv.membership_type_id IS NOT NULL
306 THEN cpfv.financial_type_id
309 cps.financial_type_id = %3
310 WHERE cpse.id IS NOT NULL {$where}";
312 CRM_Core_DAO
::executeQuery($sql, $params);