minor tidy ups. Don't pass param as reference if not altered
[civicrm-core.git] / CRM / Price / BAO / PriceFieldValue.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * Business objects for managing price fields values.
38 *
39 */
9da8dc8c 40class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue {
6a488035
TO
41
42 /**
100fef9d 43 * Insert/update a new entry in the database.
6a488035 44 *
414c1420
TO
45 * @param array $params
46 * (reference), array $ids.
6a488035 47 *
77b97be7
EM
48 * @param $ids
49 *
16b10e64 50 * @return CRM_Price_DAO_PriceFieldValue
6a488035 51 */
00be9182 52 public static function add(&$params, $ids = array()) {
6a488035 53
9da8dc8c 54 $fieldValueBAO = new CRM_Price_BAO_PriceFieldValue();
6a488035
TO
55 $fieldValueBAO->copyValues($params);
56
57 if ($id = CRM_Utils_Array::value('id', $ids)) {
58 $fieldValueBAO->id = $id;
59 }
a7488080 60 if (!empty($params['is_default'])) {
6a488035
TO
61 $query = 'UPDATE civicrm_price_field_value SET is_default = 0 WHERE price_field_id = %1';
62 $p = array(1 => array($params['price_field_id'], 'Integer'));
63 CRM_Core_DAO::executeQuery($query, $p);
64 }
65
66 $fieldValueBAO->save();
67 return $fieldValueBAO;
68 }
69
70 /**
71 * Creates a new entry in the database.
72 *
414c1420
TO
73 * @param array $params
74 * (reference), array $ids.
6a488035 75 *
77b97be7
EM
76 * @param $ids
77 *
16b10e64 78 * @return CRM_Price_DAO_PriceFieldValue
6a488035 79 */
00be9182 80 public static function create(&$params, $ids = array()) {
db232378 81 $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('id', $ids));
6a488035 82 if (!is_array($params) || empty($params)) {
408b79bf 83 return NULL;
6a488035 84 }
22e263ad 85 if (!$id && empty($params['name'])) {
b10b974d 86 $params['name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 242));
291ebece 87 }
6a488035 88
353ffa53 89 if ($id && !empty($params['weight'])) {
4f99ca55
TO
90 if (isset($params['name'])) {
91 unset($params['name']);
ba1dcfda 92 }
6a488035
TO
93
94 $oldWeight = NULL;
95 if ($id) {
9da8dc8c 96 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'weight', 'id');
6a488035
TO
97 }
98
99 $fieldValues = array('price_field_id' => CRM_Utils_Array::value('price_field_id', $params, 0));
9da8dc8c 100 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Price_DAO_PriceFieldValue', $oldWeight, $params['weight'], $fieldValues);
6a488035
TO
101 }
102 else {
db232378 103 if (!$id) {
64d24a64 104 CRM_Core_DAO::setCreateDefaults($params, self::getDefaults());
db232378
EM
105 if (empty($params['name'])) {
106 $params['name'] = CRM_Utils_String::munge(CRM_Utils_Array::value('label', $params), '_', 64);
107 }
6a488035
TO
108 }
109 }
6a488035
TO
110 return self::add($params, $ids);
111 }
112
db232378 113 /**
fe482240 114 * Get defaults for new entity.
db232378
EM
115 * @return array
116 */
00be9182 117 public static function getDefaults() {
db232378
EM
118 return array(
119 'is_active' => 1,
120 'weight' => 1,
121 );
122
123 }
124
6a488035 125 /**
fe482240
EM
126 * Retrieve DB object based on input parameters.
127 *
128 * It also stores all the retrieved values in the default array.
6a488035 129 *
414c1420
TO
130 * @param array $params
131 * (reference ) an assoc array.
132 * @param array $defaults
133 * (reference ) an assoc array to hold the flattened values.
6a488035 134 *
16b10e64 135 * @return CRM_Price_DAO_PriceFieldValue
6a488035 136 */
00be9182 137 public static function retrieve(&$params, &$defaults) {
9da8dc8c 138 return CRM_Core_DAO::commonRetrieve('CRM_Price_DAO_PriceFieldValue', $params, $defaults);
6a488035
TO
139 }
140
141 /**
fe482240 142 * Retrive the all values for given field id.
6a488035 143 *
414c1420
TO
144 * @param int $fieldId
145 * Price_field_id.
146 * @param array $values
147 * (reference ) to hold the values.
148 * @param string $orderBy
149 * For order by, default weight.
dd244018 150 * @param bool|int $isActive is_active, default false
6a488035 151 *
a6c01b45 152 * @return array
6a488035 153 *
6a488035 154 */
00be9182 155 public static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE) {
9da8dc8c 156 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
6a488035
TO
157 $fieldValueDAO->price_field_id = $fieldId;
158 $fieldValueDAO->orderBy($orderBy, 'label');
159 if ($isActive) {
160 $fieldValueDAO->is_active = 1;
161 }
162 $fieldValueDAO->find();
163
164 while ($fieldValueDAO->fetch()) {
165 CRM_Core_DAO::storeValues($fieldValueDAO, $values[$fieldValueDAO->id]);
166 }
167
168 return $values;
169 }
170
171 /**
172 * Get the price field option label.
173 *
414c1420
TO
174 * @param int $id
175 * Id of field option.
6a488035 176 *
a6c01b45
CW
177 * @return string
178 * name
6a488035 179 *
6a488035
TO
180 */
181 public static function getOptionLabel($id) {
9da8dc8c 182 return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'label');
6a488035
TO
183 }
184
185 /**
fe482240 186 * Update the is_active flag in the db.
6a488035 187 *
414c1420
TO
188 * @param int $id
189 * Id of the database record.
190 * @param bool $is_active
191 * Value we want to set the is_active field.
6a488035 192 *
a6c01b45
CW
193 * @return Object
194 * DAO object on sucess, null otherwise
6a488035 195 *
6a488035 196 */
00be9182 197 public static function setIsActive($id, $is_active) {
9da8dc8c 198 return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'is_active', $is_active);
6a488035
TO
199 }
200
201 /**
fe482240 202 * Delete all values of the given field id.
6a488035 203 *
414c1420
TO
204 * @param int $fieldId
205 * Price field id.
6a488035 206 *
6a488035 207 *
6a488035 208 */
00be9182 209 public static function deleteValues($fieldId) {
6a488035 210 if (!$fieldId) {
291ebece 211 return;
6a488035
TO
212 }
213
9da8dc8c 214 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
6a488035
TO
215 $fieldValueDAO->price_field_id = $fieldId;
216 $fieldValueDAO->delete();
217 }
218
219 /**
220 * Delete the value.
221 *
414c1420
TO
222 * @param int $id
223 * Id.
6a488035 224 *
408b79bf 225 * @return bool
6a488035 226 *
6a488035 227 */
00be9182 228 public static function del($id) {
6a488035
TO
229 if (!$id) {
230 return FALSE;
231 }
232
9da8dc8c 233 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
6a488035
TO
234 $fieldValueDAO->id = $id;
235 return $fieldValueDAO->delete();
236 }
77b97be7 237
157b21d8 238 /**
77b97be7 239 * Update civicrm_price_field_value.financial_type_id
157b21d8
PN
240 * when financial_type_id of contribution_page or event is changed
241 *
414c1420
TO
242 * @param int $entityId
243 * Id.
408b79bf 244 * @param string $entityTable table.
414c1420 245 * Entity table.
408b79bf 246 * @param string $financialTypeID type id.
414c1420 247 * Financial type id.
157b21d8 248 *
157b21d8 249 */
00be9182 250 public static function updateFinancialType($entityId, $entityTable, $financialTypeID) {
157b21d8 251 if (!$entityId || !$entityTable || !$financialTypeID) {
291ebece 252 return;
157b21d8 253 }
157b21d8
PN
254 $params = array(
255 1 => array($entityId, 'Integer'),
256 2 => array($entityTable, 'String'),
257 3 => array($financialTypeID, 'Integer'),
157b21d8 258 );
cc9b58f3
PN
259 // for event discount
260 $join = $where = '';
261 if ($entityTable == 'civicrm_event') {
262 $join = " LEFT JOIN civicrm_discount cd ON cd.price_set_id = cps.id AND cd.entity_id = %1 AND cd.entity_table = %2 ";
263 $where = ' OR cd.id IS NOT NULL ';
157b21d8 264 }
77b97be7
EM
265 $sql = "UPDATE civicrm_price_set cps
266LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = cps.id AND cpse.entity_id = %1 AND cpse.entity_table = %2
cc9b58f3
PN
267LEFT JOIN civicrm_price_field cpf ON cpf.price_set_id = cps.id
268LEFT JOIN civicrm_price_field_value cpfv ON cpf.id = cpfv.price_field_id
269{$join}
270SET cpfv.financial_type_id = CASE
271 WHEN cpfv.membership_type_id IS NOT NULL
272 THEN cpfv.financial_type_id
273 ELSE %3
274END,
275cps.financial_type_id = %3
276WHERE cpse.id IS NOT NULL {$where}";
77b97be7 277
157b21d8
PN
278 CRM_Core_DAO::executeQuery($sql, $params);
279 }
96025800 280
6a488035 281}