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