INFRA-132 - CRM/Price - phpcbf
[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 object
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 object
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'])) { unset($params['name']);
93 }
94
95 $oldWeight = NULL;
96 if ($id) {
97 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'weight', 'id');
98 }
99
100 $fieldValues = array('price_field_id' => CRM_Utils_Array::value('price_field_id', $params, 0));
101 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Price_DAO_PriceFieldValue', $oldWeight, $params['weight'], $fieldValues);
102 }
103 else {
104 if (!$id) {
105 CRM_Core_DAO::setCreateDefaults($params, self::getDefaults());
106 if (empty($params['name'])) {
107 $params['name'] = CRM_Utils_String::munge(CRM_Utils_Array::value('label', $params), '_', 64);
108 }
109 }
110 }
111 return self::add($params, $ids);
112 }
113
114 /**
115 * Get defaults for new entity
116 * @return array
117 */
118 public static function getDefaults() {
119 return array(
120 'is_active' => 1,
121 'weight' => 1,
122 );
123
124 }
125
126 /**
127 * Takes a bunch of params that are needed to match certain criteria and
128 * retrieves the relevant objects.
129 *
130 * @param array $params
131 * (reference ) an assoc array.
132 * @param array $defaults
133 * (reference ) an assoc array to hold the flattened values.
134 *
135 * @return CRM_Price_DAO_PriceFieldValue object
136 * @static
137 */
138 public static function retrieve(&$params, &$defaults) {
139 return CRM_Core_DAO::commonRetrieve('CRM_Price_DAO_PriceFieldValue', $params, $defaults);
140 }
141
142 /**
143 * Retrive the all values for given field id
144 *
145 * @param int $fieldId
146 * Price_field_id.
147 * @param array $values
148 * (reference ) to hold the values.
149 * @param string $orderBy
150 * For order by, default weight.
151 * @param bool|int $isActive is_active, default false
152 *
153 * @return array $values
154 *
155 * @static
156 */
157 public static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE) {
158 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
159 $fieldValueDAO->price_field_id = $fieldId;
160 $fieldValueDAO->orderBy($orderBy, 'label');
161 if ($isActive) {
162 $fieldValueDAO->is_active = 1;
163 }
164 $fieldValueDAO->find();
165
166 while ($fieldValueDAO->fetch()) {
167 CRM_Core_DAO::storeValues($fieldValueDAO, $values[$fieldValueDAO->id]);
168 }
169
170 return $values;
171 }
172
173 /**
174 * Get the price field option label.
175 *
176 * @param int $id
177 * Id of field option.
178 *
179 * @return string name
180 *
181 * @static
182 *
183 */
184 public static function getOptionLabel($id) {
185 return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'label');
186 }
187
188 /**
189 * Update the is_active flag in the db
190 *
191 * @param int $id
192 * Id of the database record.
193 * @param bool $is_active
194 * Value we want to set the is_active field.
195 *
196 * @return Object DAO object on sucess, null otherwise
197 *
198 * @static
199 */
200 public static function setIsActive($id, $is_active) {
201 return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'is_active', $is_active);
202 }
203
204 /**
205 * Delete all values of the given field id
206 *
207 * @param int $fieldId
208 * Price field id.
209 *
210 *
211 * @static
212 */
213 public static function deleteValues($fieldId) {
214 if (!$fieldId) {
215 return;
216 }
217
218 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
219 $fieldValueDAO->price_field_id = $fieldId;
220 $fieldValueDAO->delete();
221 }
222
223 /**
224 * Delete the value.
225 *
226 * @param int $id
227 * Id.
228 *
229 * @return boolean
230 *
231 * @static
232 */
233 public static function del($id) {
234 if (!$id) {
235 return FALSE;
236 }
237
238 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
239 $fieldValueDAO->id = $id;
240 return $fieldValueDAO->delete();
241 }
242
243 /**
244 * Update civicrm_price_field_value.financial_type_id
245 * when financial_type_id of contribution_page or event is changed
246 *
247 * @param int $entityId
248 * Id.
249 * @param string $entityTableEntity table.
250 * Entity table.
251 * @param string $financialTypeIDFinancial type id.
252 * Financial type id.
253 *
254 * @static
255 */
256 public static function updateFinancialType($entityId, $entityTable, $financialTypeID) {
257 if (!$entityId || !$entityTable || !$financialTypeID) {
258 return;
259 }
260 $params = array(
261 1 => array($entityId, 'Integer'),
262 2 => array($entityTable, 'String'),
263 3 => array($financialTypeID, 'Integer'),
264 );
265 // for event discount
266 $join = $where = '';
267 if ($entityTable == 'civicrm_event') {
268 $join = " LEFT JOIN civicrm_discount cd ON cd.price_set_id = cps.id AND cd.entity_id = %1 AND cd.entity_table = %2 ";
269 $where = ' OR cd.id IS NOT NULL ';
270 }
271 $sql = "UPDATE civicrm_price_set cps
272 LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = cps.id AND cpse.entity_id = %1 AND cpse.entity_table = %2
273 LEFT JOIN civicrm_price_field cpf ON cpf.price_set_id = cps.id
274 LEFT JOIN civicrm_price_field_value cpfv ON cpf.id = cpfv.price_field_id
275 {$join}
276 SET cpfv.financial_type_id = CASE
277 WHEN cpfv.membership_type_id IS NOT NULL
278 THEN cpfv.financial_type_id
279 ELSE %3
280 END,
281 cps.financial_type_id = %3
282 WHERE cpse.id IS NOT NULL {$where}";
283
284 CRM_Core_DAO::executeQuery($sql, $params);
285 }
286 }