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']);
64 if (!empty($params['is_default'])) {
65 $query = 'UPDATE civicrm_price_field_value SET is_default = 0 WHERE price_field_id = %1';
66 $p = array(1 => array($params['price_field_id'], 'Integer'));
67 CRM_Core_DAO
::executeQuery($query, $p);
70 $fieldValueBAO->save();
71 // Reset the cached values in this function.
72 CRM_Price_BAO_PriceField
::getOptions(CRM_Utils_Array
::value('price_field_id', $params), FALSE, TRUE);
73 return $fieldValueBAO;
77 * Creates a new entry in the database.
79 * @param array $params
80 * (reference), array $ids.
84 * @return CRM_Price_DAO_PriceFieldValue
86 public static function create(&$params, $ids = array()) {
87 $id = CRM_Utils_Array
::value('id', $params, CRM_Utils_Array
::value('id', $ids));
88 if (!is_array($params) ||
empty($params)) {
91 if (!$id && empty($params['name'])) {
92 $params['name'] = strtolower(CRM_Utils_String
::munge($params['label'], '_', 242));
95 if ($id && !empty($params['weight'])) {
96 if (isset($params['name'])) {
97 unset($params['name']);
102 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'weight', 'id');
105 $fieldValues = array('price_field_id' => CRM_Utils_Array
::value('price_field_id', $params, 0));
106 $params['weight'] = CRM_Utils_Weight
::updateOtherWeights('CRM_Price_DAO_PriceFieldValue', $oldWeight, $params['weight'], $fieldValues);
110 CRM_Core_DAO
::setCreateDefaults($params, self
::getDefaults());
111 if (empty($params['name'])) {
112 $params['name'] = CRM_Utils_String
::munge(CRM_Utils_Array
::value('label', $params), '_', 64);
116 return self
::add($params, $ids);
120 * Get defaults for new entity.
123 public static function getDefaults() {
132 * Retrieve DB object based on input parameters.
134 * It also stores all the retrieved values in the default array.
136 * @param array $params
137 * (reference ) an assoc array.
138 * @param array $defaults
139 * (reference ) an assoc array to hold the flattened values.
141 * @return CRM_Price_DAO_PriceFieldValue
143 public static function retrieve(&$params, &$defaults) {
144 return CRM_Core_DAO
::commonRetrieve('CRM_Price_DAO_PriceFieldValue', $params, $defaults);
148 * Retrive the all values for given field id.
150 * @param int $fieldId
152 * @param array $values
153 * (reference ) to hold the values.
154 * @param string $orderBy
155 * For order by, default weight.
156 * @param bool|int $isActive is_active, default false
161 public static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE) {
162 $sql = "SELECT cs.id FROM civicrm_price_set cs INNER JOIN civicrm_price_field cp ON cp.price_set_id = cs.id
163 WHERE cs.name IN ('default_contribution_amount', 'default_membership_type_amount') AND cp.id = {$fieldId} ";
164 $setId = CRM_Core_DAO
::singleValueQuery($sql);
165 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
166 $fieldValueDAO->price_field_id
= $fieldId;
168 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes);
169 $addWhere = "financial_type_id IN (0)";
170 if (!empty($financialTypes)) {
171 $addWhere = "financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ")";
173 $fieldValueDAO->whereAdd($addWhere);
175 $fieldValueDAO->orderBy($orderBy, 'label');
177 $fieldValueDAO->is_active
= 1;
179 $fieldValueDAO->find();
181 while ($fieldValueDAO->fetch()) {
182 CRM_Core_DAO
::storeValues($fieldValueDAO, $values[$fieldValueDAO->id
]);
189 * Get the price field option label.
192 * Id of field option.
198 public static function getOptionLabel($id) {
199 return CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'label');
203 * Update the is_active flag in the db.
206 * Id of the database record.
207 * @param bool $is_active
208 * Value we want to set the is_active field.
211 * DAO object on success, null otherwise
214 public static function setIsActive($id, $is_active) {
215 return CRM_Core_DAO
::setFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'is_active', $is_active);
219 * Delete all values of the given field id.
221 * @param int $fieldId
226 public static function deleteValues($fieldId) {
231 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
232 $fieldValueDAO->price_field_id
= $fieldId;
233 $fieldValueDAO->delete();
245 public static function del($id) {
250 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
251 $fieldValueDAO->id
= $id;
252 return $fieldValueDAO->delete();
256 * Update civicrm_price_field_value.financial_type_id
257 * when financial_type_id of contribution_page or event is changed
259 * @param int $entityId
261 * @param string $entityTable table.
263 * @param string $financialTypeID type id.
267 public static function updateFinancialType($entityId, $entityTable, $financialTypeID) {
268 if (!$entityId ||
!$entityTable ||
!$financialTypeID) {
272 1 => array($entityId, 'Integer'),
273 2 => array($entityTable, 'String'),
274 3 => array($financialTypeID, 'Integer'),
276 // for event discount
278 if ($entityTable == 'civicrm_event') {
279 $join = " LEFT JOIN civicrm_discount cd ON cd.price_set_id = cps.id AND cd.entity_id = %1 AND cd.entity_table = %2 ";
280 $where = ' OR cd.id IS NOT NULL ';
282 $sql = "UPDATE civicrm_price_set cps
283 LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = cps.id AND cpse.entity_id = %1 AND cpse.entity_table = %2
284 LEFT JOIN civicrm_price_field cpf ON cpf.price_set_id = cps.id
285 LEFT JOIN civicrm_price_field_value cpfv ON cpf.id = cpfv.price_field_id
287 SET cpfv.financial_type_id = CASE
288 WHEN cpfv.membership_type_id IS NOT NULL
289 THEN cpfv.financial_type_id
292 cps.financial_type_id = %3
293 WHERE cpse.id IS NOT NULL {$where}";
295 CRM_Core_DAO
::executeQuery($sql, $params);
299 * Update price option label in line_item, civicrm_contribution and civicrm_participant.
301 * @param int $id - id of the price_field_value
302 * @param string $prevLabel
303 * @param string $newLabel
306 public static function updateAmountAndFeeLevel($id, $prevLabel, $newLabel) {
307 // update price field label in line item.
308 $lineItem = new CRM_Price_DAO_LineItem();
309 $lineItem->price_field_value_id
= $id;
310 $lineItem->label
= $prevLabel;
312 while ($lineItem->fetch()) {
313 $lineItemParams['id'] = $lineItem->id
;
314 $lineItemParams['label'] = $newLabel;
315 CRM_Price_BAO_LineItem
::create($lineItemParams);
317 // update amount and fee level in civicrm_contribution and civicrm_participant
319 1 => array(CRM_Core_DAO
::VALUE_SEPARATOR
. $prevLabel . ' -', 'String'),
320 2 => array(CRM_Core_DAO
::VALUE_SEPARATOR
. $newLabel . ' -', 'String'),
322 // Update contribution
323 if (!empty($lineItem->contribution_id
)) {
324 CRM_Core_DAO
::executeQuery("UPDATE `civicrm_contribution` SET `amount_level` = REPLACE(amount_level, %1, %2) WHERE id = {$lineItem->contribution_id}", $params);
326 // Update participant
327 if ($lineItem->entity_table
== 'civicrm_participant') {
328 CRM_Core_DAO
::executeQuery("UPDATE `civicrm_participant` SET `fee_level` = REPLACE(fee_level, %1, %2) WHERE id = {$lineItem->entity_id}", $params);