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