CRM-14696 - make sold out options a bit easier to see by making the entire option...
[civicrm-core.git] / CRM / Price / BAO / PriceFieldValue.php
CommitLineData
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 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
TO
44 *
45 * @param array $params (reference), array $ids
46 *
77b97be7
EM
47 * @param $ids
48 *
c490a46a 49 * @return 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 *
c490a46a 78 * @return CRM_Price_DAO_PriceFieldValue object
6a488035
TO
79 * @access public
80 * @static
81 */
291ebece 82 static function create(&$params, $ids = array()) {
db232378 83 $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('id', $ids));
6a488035
TO
84 if (!is_array($params) || empty($params)) {
85 return;
86 }
db232378 87 if(!$id && empty($params['name'])) {
b10b974d 88 $params['name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 242));
291ebece 89 }
6a488035 90
db232378 91 if ($id && !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 {
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
EM
113 /**
114 * Get defaults for new entity
115 * @return array
116 */
117 static function getDefaults() {
118 return array(
119 'is_active' => 1,
120 'weight' => 1,
121 );
122
123 }
124
6a488035
TO
125 /**
126 * Takes a bunch of params that are needed to match certain criteria and
127 * retrieves the relevant objects.
128 *
129 * @param array $params (reference ) an assoc array
130 * @param array $defaults (reference ) an assoc array to hold the flattened values
131 *
c490a46a 132 * @return CRM_Price_DAO_PriceFieldValue object
6a488035
TO
133 * @access public
134 * @static
135 */
136 static function retrieve(&$params, &$defaults) {
9da8dc8c 137 return CRM_Core_DAO::commonRetrieve('CRM_Price_DAO_PriceFieldValue', $params, $defaults);
6a488035
TO
138 }
139
140 /**
141 * Retrive the all values for given field id
142 *
143 * @param int $fieldId price_field_id
144 * @param array $values (reference ) to hold the values
145 * @param string $orderBy for order by, default weight
dd244018 146 * @param bool|int $isActive is_active, default false
6a488035
TO
147 *
148 * @return array $values
149 *
150 * @access public
151 * @static
152 */
153 static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE) {
9da8dc8c 154 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
6a488035
TO
155 $fieldValueDAO->price_field_id = $fieldId;
156 $fieldValueDAO->orderBy($orderBy, 'label');
157 if ($isActive) {
158 $fieldValueDAO->is_active = 1;
159 }
160 $fieldValueDAO->find();
161
162 while ($fieldValueDAO->fetch()) {
163 CRM_Core_DAO::storeValues($fieldValueDAO, $values[$fieldValueDAO->id]);
164 }
165
166 return $values;
167 }
168
169 /**
170 * Get the price field option label.
171 *
172 * @param int $id id of field option.
173 *
174 * @return string name
175 *
176 * @access public
177 * @static
178 *
179 */
180 public static function getOptionLabel($id) {
9da8dc8c 181 return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'label');
6a488035
TO
182 }
183
184 /**
100fef9d 185 * Update the is_active flag in the db
6a488035
TO
186 *
187 * @param int $id Id of the database record
188 * @param boolean $is_active Value we want to set the is_active field
189 *
190 * @return Object DAO object on sucess, null otherwise
191 *
192 * @access public
193 * @static
194 */
195 static function setIsActive($id, $is_active) {
9da8dc8c 196 return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'is_active', $is_active);
6a488035
TO
197 }
198
199 /**
100fef9d 200 * Delete all values of the given field id
6a488035
TO
201 *
202 * @param int $fieldId Price field id
203 *
6a488035
TO
204 *
205 * @access public
206 * @static
207 */
208 static function deleteValues($fieldId) {
209 if (!$fieldId) {
291ebece 210 return;
6a488035
TO
211 }
212
9da8dc8c 213 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
6a488035
TO
214 $fieldValueDAO->price_field_id = $fieldId;
215 $fieldValueDAO->delete();
216 }
217
218 /**
219 * Delete the value.
220 *
221 * @param int $id Id
222 *
223 * @return boolean
224 *
225 * @access public
226 * @static
227 */
228 static function del($id) {
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 *
77b97be7
EM
242 * @param int $entityId Id
243 * @param String $entityTable entity table
157b21d8
PN
244 * @param String $financialTypeID financial type id
245 *
246 * @access public
247 * @static
248 */
249 static function updateFinancialType($entityId, $entityTable, $financialTypeID) {
250 if (!$entityId || !$entityTable || !$financialTypeID) {
291ebece 251 return;
157b21d8 252 }
157b21d8
PN
253 $params = array(
254 1 => array($entityId, 'Integer'),
255 2 => array($entityTable, 'String'),
256 3 => array($financialTypeID, 'Integer'),
157b21d8 257 );
cc9b58f3
PN
258 // for event discount
259 $join = $where = '';
260 if ($entityTable == 'civicrm_event') {
261 $join = " LEFT JOIN civicrm_discount cd ON cd.price_set_id = cps.id AND cd.entity_id = %1 AND cd.entity_table = %2 ";
262 $where = ' OR cd.id IS NOT NULL ';
157b21d8 263 }
77b97be7
EM
264 $sql = "UPDATE civicrm_price_set cps
265LEFT 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
266LEFT JOIN civicrm_price_field cpf ON cpf.price_set_id = cps.id
267LEFT JOIN civicrm_price_field_value cpfv ON cpf.id = cpfv.price_field_id
268{$join}
269SET cpfv.financial_type_id = CASE
270 WHEN cpfv.membership_type_id IS NOT NULL
271 THEN cpfv.financial_type_id
272 ELSE %3
273END,
274cps.financial_type_id = %3
275WHERE cpse.id IS NOT NULL {$where}";
77b97be7 276
157b21d8
PN
277 CRM_Core_DAO::executeQuery($sql, $params);
278 }
6a488035
TO
279}
280