Ian province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Price / BAO / PriceFieldValue.php
... / ...
CommitLineData
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 * $Id$
33 *
34 */
35
36/**
37 * Business objects for managing price fields values.
38 *
39 */
40class 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 */
52 public static function add(&$params, $ids = array()) {
53
54 $fieldValueBAO = new CRM_Price_BAO_PriceFieldValue();
55 $fieldValueBAO->copyValues($params);
56
57 if ($id = CRM_Utils_Array::value('id', $ids)) {
58 $fieldValueBAO->id = $id;
59 }
60 if (!empty($params['is_default'])) {
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 // Reset the cached values in this function.
68 CRM_Price_BAO_PriceField::getOptions(CRM_Utils_Array::value('price_field_id', $params), FALSE, TRUE);
69 return $fieldValueBAO;
70 }
71
72 /**
73 * Creates a new entry in the database.
74 *
75 * @param array $params
76 * (reference), array $ids.
77 *
78 * @param $ids
79 *
80 * @return CRM_Price_DAO_PriceFieldValue
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 NULL;
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 * Retrieve DB object based on input parameters.
129 *
130 * It also stores all the retrieved values in the default array.
131 *
132 * @param array $params
133 * (reference ) an assoc array.
134 * @param array $defaults
135 * (reference ) an assoc array to hold the flattened values.
136 *
137 * @return CRM_Price_DAO_PriceFieldValue
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 */
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
180 * name
181 *
182 */
183 public static function getOptionLabel($id) {
184 return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'label');
185 }
186
187 /**
188 * Update the is_active flag in the db.
189 *
190 * @param int $id
191 * Id of the database record.
192 * @param bool $is_active
193 * Value we want to set the is_active field.
194 *
195 * @return Object
196 * DAO object on success, null otherwise
197 *
198 */
199 public static function setIsActive($id, $is_active) {
200 return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'is_active', $is_active);
201 }
202
203 /**
204 * Delete all values of the given field id.
205 *
206 * @param int $fieldId
207 * Price field id.
208 *
209 *
210 */
211 public static function deleteValues($fieldId) {
212 if (!$fieldId) {
213 return;
214 }
215
216 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
217 $fieldValueDAO->price_field_id = $fieldId;
218 $fieldValueDAO->delete();
219 }
220
221 /**
222 * Delete the value.
223 *
224 * @param int $id
225 * Id.
226 *
227 * @return bool
228 *
229 */
230 public static function del($id) {
231 if (!$id) {
232 return FALSE;
233 }
234
235 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
236 $fieldValueDAO->id = $id;
237 return $fieldValueDAO->delete();
238 }
239
240 /**
241 * Update civicrm_price_field_value.financial_type_id
242 * when financial_type_id of contribution_page or event is changed
243 *
244 * @param int $entityId
245 * Id.
246 * @param string $entityTable table.
247 * Entity table.
248 * @param string $financialTypeID type id.
249 * Financial type id.
250 *
251 */
252 public static function updateFinancialType($entityId, $entityTable, $financialTypeID) {
253 if (!$entityId || !$entityTable || !$financialTypeID) {
254 return;
255 }
256 $params = array(
257 1 => array($entityId, 'Integer'),
258 2 => array($entityTable, 'String'),
259 3 => array($financialTypeID, 'Integer'),
260 );
261 // for event discount
262 $join = $where = '';
263 if ($entityTable == 'civicrm_event') {
264 $join = " LEFT JOIN civicrm_discount cd ON cd.price_set_id = cps.id AND cd.entity_id = %1 AND cd.entity_table = %2 ";
265 $where = ' OR cd.id IS NOT NULL ';
266 }
267 $sql = "UPDATE civicrm_price_set cps
268LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = cps.id AND cpse.entity_id = %1 AND cpse.entity_table = %2
269LEFT JOIN civicrm_price_field cpf ON cpf.price_set_id = cps.id
270LEFT JOIN civicrm_price_field_value cpfv ON cpf.id = cpfv.price_field_id
271{$join}
272SET cpfv.financial_type_id = CASE
273 WHEN cpfv.membership_type_id IS NOT NULL
274 THEN cpfv.financial_type_id
275 ELSE %3
276END,
277cps.financial_type_id = %3
278WHERE cpse.id IS NOT NULL {$where}";
279
280 CRM_Core_DAO::executeQuery($sql, $params);
281 }
282
283}