3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
37 * Business objects for managing price fields values.
40 class CRM_Price_BAO_PriceFieldValue
extends CRM_Price_DAO_PriceFieldValue
{
43 * Insert/update a new entry in the database.
45 * @param array $params
46 * (reference), array $ids.
50 * @return CRM_Price_DAO_PriceFieldValue
52 public static function add(&$params, $ids = array()) {
54 $fieldValueBAO = new CRM_Price_BAO_PriceFieldValue();
55 $fieldValueBAO->copyValues($params);
57 if ($id = CRM_Utils_Array
::value('id', $ids)) {
58 $fieldValueBAO->id
= $id;
59 $prevLabel = self
::getOptionLabel($id);
60 if (!empty($params['label']) && $prevLabel != $params['label']) {
61 self
::updateAmountAndFeeLevel($id, $prevLabel, $params['label']);
65 $priceFieldID = CRM_Utils_Array
::value('price_field_id', $params);
67 $priceFieldID = CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceFieldValue', $id, 'price_field_id');
69 $isError = CRM_Financial_BAO_FinancialAccount
::validateFinancialType(
70 CRM_Utils_Array
::value('financial_type_id', $params),
75 throw new CRM_Core_Exception(ts('Deferred revenue account is not configured for selected financial type. Please have an administrator set up the deferred revenue account at Administer > CiviContribute > Financial Accounts, then configure it for financial types at Administer > CiviContribution > Financial Types, Accounts'));
77 if (!empty($params['is_default'])) {
78 $query = 'UPDATE civicrm_price_field_value SET is_default = 0 WHERE price_field_id = %1';
79 $p = array(1 => array($params['price_field_id'], 'Integer'));
80 CRM_Core_DAO
::executeQuery($query, $p);
83 $fieldValueBAO->save();
84 // Reset the cached values in this function.
85 CRM_Price_BAO_PriceField
::getOptions(CRM_Utils_Array
::value('price_field_id', $params), FALSE, TRUE);
86 return $fieldValueBAO;
90 * Creates a new entry in the database.
92 * @param array $params
93 * (reference), array $ids.
97 * @return CRM_Price_DAO_PriceFieldValue
99 public static function create(&$params, $ids = array()) {
100 $id = CRM_Utils_Array
::value('id', $params, CRM_Utils_Array
::value('id', $ids));
101 if (!is_array($params) ||
empty($params)) {
104 if (!$id && empty($params['name'])) {
105 $params['name'] = strtolower(CRM_Utils_String
::munge($params['label'], '_', 242));
108 if ($id && !empty($params['weight'])) {
109 if (isset($params['name'])) {
110 unset($params['name']);
115 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'weight', 'id');
118 $fieldValues = array('price_field_id' => CRM_Utils_Array
::value('price_field_id', $params, 0));
119 $params['weight'] = CRM_Utils_Weight
::updateOtherWeights('CRM_Price_DAO_PriceFieldValue', $oldWeight, $params['weight'], $fieldValues);
123 CRM_Core_DAO
::setCreateDefaults($params, self
::getDefaults());
124 if (empty($params['name'])) {
125 $params['name'] = CRM_Utils_String
::munge(CRM_Utils_Array
::value('label', $params), '_', 64);
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 * Retrive the 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
174 public static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE) {
175 $sql = "SELECT cs.id FROM civicrm_price_set cs INNER JOIN civicrm_price_field cp ON cp.price_set_id = cs.id
176 WHERE cs.name IN ('default_contribution_amount', 'default_membership_type_amount') AND cp.id = {$fieldId} ";
177 $setId = CRM_Core_DAO
::singleValueQuery($sql);
178 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
179 $fieldValueDAO->price_field_id
= $fieldId;
181 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes);
182 $addWhere = "financial_type_id IN (0)";
183 if (!empty($financialTypes)) {
184 $addWhere = "financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ")";
186 $fieldValueDAO->whereAdd($addWhere);
188 $fieldValueDAO->orderBy($orderBy, 'label');
190 $fieldValueDAO->is_active
= 1;
192 $fieldValueDAO->find();
194 while ($fieldValueDAO->fetch()) {
195 CRM_Core_DAO
::storeValues($fieldValueDAO, $values[$fieldValueDAO->id
]);
202 * Get the price field option label.
205 * Id of field option.
211 public static function getOptionLabel($id) {
212 return CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'label');
216 * Update the is_active flag in the db.
219 * Id of the database record.
220 * @param bool $is_active
221 * Value we want to set the is_active field.
224 * DAO object on success, null otherwise
227 public static function setIsActive($id, $is_active) {
228 return CRM_Core_DAO
::setFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'is_active', $is_active);
232 * Delete all values of the given field id.
234 * @param int $fieldId
239 public static function deleteValues($fieldId) {
244 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
245 $fieldValueDAO->price_field_id
= $fieldId;
246 $fieldValueDAO->delete();
258 public static function del($id) {
263 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
264 $fieldValueDAO->id
= $id;
265 return $fieldValueDAO->delete();
269 * Update civicrm_price_field_value.financial_type_id
270 * when financial_type_id of contribution_page or event is changed
272 * @param int $entityId
274 * @param string $entityTable table.
276 * @param string $financialTypeID type id.
280 public static function updateFinancialType($entityId, $entityTable, $financialTypeID) {
281 if (!$entityId ||
!$entityTable ||
!$financialTypeID) {
285 1 => array($entityId, 'Integer'),
286 2 => array($entityTable, 'String'),
287 3 => array($financialTypeID, 'Integer'),
289 // for event discount
291 if ($entityTable == 'civicrm_event') {
292 $join = " LEFT JOIN civicrm_discount cd ON cd.price_set_id = cps.id AND cd.entity_id = %1 AND cd.entity_table = %2 ";
293 $where = ' OR cd.id IS NOT NULL ';
295 $sql = "UPDATE civicrm_price_set cps
296 LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = cps.id AND cpse.entity_id = %1 AND cpse.entity_table = %2
297 LEFT JOIN civicrm_price_field cpf ON cpf.price_set_id = cps.id
298 LEFT JOIN civicrm_price_field_value cpfv ON cpf.id = cpfv.price_field_id
300 SET cpfv.financial_type_id = CASE
301 WHEN cpfv.membership_type_id IS NOT NULL
302 THEN cpfv.financial_type_id
305 cps.financial_type_id = %3
306 WHERE cpse.id IS NOT NULL {$where}";
308 CRM_Core_DAO
::executeQuery($sql, $params);
312 * Update price option label in line_item, civicrm_contribution and civicrm_participant.
314 * @param int $id - id of the price_field_value
315 * @param string $prevLabel
316 * @param string $newLabel
319 public static function updateAmountAndFeeLevel($id, $prevLabel, $newLabel) {
320 // update price field label in line item.
321 $lineItem = new CRM_Price_DAO_LineItem();
322 $lineItem->price_field_value_id
= $id;
323 $lineItem->label
= $prevLabel;
325 while ($lineItem->fetch()) {
326 $lineItemParams['id'] = $lineItem->id
;
327 $lineItemParams['label'] = $newLabel;
328 CRM_Price_BAO_LineItem
::create($lineItemParams);
330 // update amount and fee level in civicrm_contribution and civicrm_participant
332 1 => array(CRM_Core_DAO
::VALUE_SEPARATOR
. $prevLabel . ' -', 'String'),
333 2 => array(CRM_Core_DAO
::VALUE_SEPARATOR
. $newLabel . ' -', 'String'),
335 // Update contribution
336 if (!empty($lineItem->contribution_id
)) {
337 CRM_Core_DAO
::executeQuery("UPDATE `civicrm_contribution` SET `amount_level` = REPLACE(amount_level, %1, %2) WHERE id = {$lineItem->contribution_id}", $params);
339 // Update participant
340 if ($lineItem->entity_table
== 'civicrm_participant') {
341 CRM_Core_DAO
::executeQuery("UPDATE `civicrm_participant` SET `fee_level` = REPLACE(fee_level, %1, %2) WHERE id = {$lineItem->entity_id}", $params);