CIVI-28 Added perms for price field option display
[civicrm-core.git] / CRM / Price / BAO / PriceFieldValue.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
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 44 *
414c1420
TO
45 * @param array $params
46 * (reference), array $ids.
6a488035 47 *
77b97be7
EM
48 * @param $ids
49 *
16b10e64 50 * @return CRM_Price_DAO_PriceFieldValue
6a488035 51 */
00be9182 52 public static function add(&$params, $ids = array()) {
6a488035 53
9da8dc8c 54 $fieldValueBAO = new CRM_Price_BAO_PriceFieldValue();
6a488035
TO
55 $fieldValueBAO->copyValues($params);
56
57 if ($id = CRM_Utils_Array::value('id', $ids)) {
58 $fieldValueBAO->id = $id;
59 }
a7488080 60 if (!empty($params['is_default'])) {
6a488035
TO
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();
ba6920b3 67 // Reset the cached values in this function.
183a0610 68 CRM_Price_BAO_PriceField::getOptions(CRM_Utils_Array::value('price_field_id', $params), FALSE, TRUE);
6a488035
TO
69 return $fieldValueBAO;
70 }
71
72 /**
73 * Creates a new entry in the database.
74 *
414c1420
TO
75 * @param array $params
76 * (reference), array $ids.
6a488035 77 *
77b97be7
EM
78 * @param $ids
79 *
16b10e64 80 * @return CRM_Price_DAO_PriceFieldValue
6a488035 81 */
00be9182 82 public static function create(&$params, $ids = array()) {
db232378 83 $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('id', $ids));
6a488035 84 if (!is_array($params) || empty($params)) {
408b79bf 85 return NULL;
6a488035 86 }
22e263ad 87 if (!$id && empty($params['name'])) {
b10b974d 88 $params['name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 242));
291ebece 89 }
6a488035 90
353ffa53 91 if ($id && !empty($params['weight'])) {
4f99ca55
TO
92 if (isset($params['name'])) {
93 unset($params['name']);
ba1dcfda 94 }
6a488035
TO
95
96 $oldWeight = NULL;
97 if ($id) {
9da8dc8c 98 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'weight', 'id');
6a488035
TO
99 }
100
101 $fieldValues = array('price_field_id' => CRM_Utils_Array::value('price_field_id', $params, 0));
9da8dc8c 102 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Price_DAO_PriceFieldValue', $oldWeight, $params['weight'], $fieldValues);
6a488035
TO
103 }
104 else {
db232378 105 if (!$id) {
64d24a64 106 CRM_Core_DAO::setCreateDefaults($params, self::getDefaults());
db232378
EM
107 if (empty($params['name'])) {
108 $params['name'] = CRM_Utils_String::munge(CRM_Utils_Array::value('label', $params), '_', 64);
109 }
6a488035
TO
110 }
111 }
6a488035
TO
112 return self::add($params, $ids);
113 }
114
db232378 115 /**
fe482240 116 * Get defaults for new entity.
db232378
EM
117 * @return array
118 */
00be9182 119 public static function getDefaults() {
db232378
EM
120 return array(
121 'is_active' => 1,
122 'weight' => 1,
123 );
124
125 }
126
6a488035 127 /**
fe482240
EM
128 * Retrieve DB object based on input parameters.
129 *
130 * It also stores all the retrieved values in the default array.
6a488035 131 *
414c1420
TO
132 * @param array $params
133 * (reference ) an assoc array.
134 * @param array $defaults
135 * (reference ) an assoc array to hold the flattened values.
6a488035 136 *
16b10e64 137 * @return CRM_Price_DAO_PriceFieldValue
6a488035 138 */
00be9182 139 public static function retrieve(&$params, &$defaults) {
9da8dc8c 140 return CRM_Core_DAO::commonRetrieve('CRM_Price_DAO_PriceFieldValue', $params, $defaults);
6a488035
TO
141 }
142
143 /**
fe482240 144 * Retrive the all values for given field id.
6a488035 145 *
414c1420
TO
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.
dd244018 152 * @param bool|int $isActive is_active, default false
6a488035 153 *
a6c01b45 154 * @return array
6a488035 155 *
6a488035 156 */
00be9182 157 public static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE) {
a875500e 158 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes);
9da8dc8c 159 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
6a488035
TO
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()) {
a875500e
E
168 if (!in_array($fieldValueDAO->financial_type_id, array_keys($financialTypes))) {
169 continue;
170 }
6a488035
TO
171 CRM_Core_DAO::storeValues($fieldValueDAO, $values[$fieldValueDAO->id]);
172 }
173
174 return $values;
175 }
176
177 /**
178 * Get the price field option label.
179 *
414c1420
TO
180 * @param int $id
181 * Id of field option.
6a488035 182 *
a6c01b45
CW
183 * @return string
184 * name
6a488035 185 *
6a488035
TO
186 */
187 public static function getOptionLabel($id) {
9da8dc8c 188 return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'label');
6a488035
TO
189 }
190
191 /**
fe482240 192 * Update the is_active flag in the db.
6a488035 193 *
414c1420
TO
194 * @param int $id
195 * Id of the database record.
196 * @param bool $is_active
197 * Value we want to set the is_active field.
6a488035 198 *
a6c01b45 199 * @return Object
b44e3f84 200 * DAO object on success, null otherwise
6a488035 201 *
6a488035 202 */
00be9182 203 public static function setIsActive($id, $is_active) {
9da8dc8c 204 return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'is_active', $is_active);
6a488035
TO
205 }
206
207 /**
fe482240 208 * Delete all values of the given field id.
6a488035 209 *
414c1420
TO
210 * @param int $fieldId
211 * Price field id.
6a488035 212 *
6a488035 213 *
6a488035 214 */
00be9182 215 public static function deleteValues($fieldId) {
6a488035 216 if (!$fieldId) {
291ebece 217 return;
6a488035
TO
218 }
219
9da8dc8c 220 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
6a488035
TO
221 $fieldValueDAO->price_field_id = $fieldId;
222 $fieldValueDAO->delete();
223 }
224
225 /**
226 * Delete the value.
227 *
414c1420
TO
228 * @param int $id
229 * Id.
6a488035 230 *
408b79bf 231 * @return bool
6a488035 232 *
6a488035 233 */
00be9182 234 public static function del($id) {
6a488035
TO
235 if (!$id) {
236 return FALSE;
237 }
238
9da8dc8c 239 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
6a488035
TO
240 $fieldValueDAO->id = $id;
241 return $fieldValueDAO->delete();
242 }
77b97be7 243
157b21d8 244 /**
77b97be7 245 * Update civicrm_price_field_value.financial_type_id
157b21d8
PN
246 * when financial_type_id of contribution_page or event is changed
247 *
414c1420
TO
248 * @param int $entityId
249 * Id.
408b79bf 250 * @param string $entityTable table.
414c1420 251 * Entity table.
408b79bf 252 * @param string $financialTypeID type id.
414c1420 253 * Financial type id.
157b21d8 254 *
157b21d8 255 */
00be9182 256 public static function updateFinancialType($entityId, $entityTable, $financialTypeID) {
157b21d8 257 if (!$entityId || !$entityTable || !$financialTypeID) {
291ebece 258 return;
157b21d8 259 }
157b21d8
PN
260 $params = array(
261 1 => array($entityId, 'Integer'),
262 2 => array($entityTable, 'String'),
263 3 => array($financialTypeID, 'Integer'),
157b21d8 264 );
cc9b58f3
PN
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 ';
157b21d8 270 }
77b97be7
EM
271 $sql = "UPDATE civicrm_price_set cps
272LEFT 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
273LEFT JOIN civicrm_price_field cpf ON cpf.price_set_id = cps.id
274LEFT JOIN civicrm_price_field_value cpfv ON cpf.id = cpfv.price_field_id
275{$join}
276SET cpfv.financial_type_id = CASE
277 WHEN cpfv.membership_type_id IS NOT NULL
278 THEN cpfv.financial_type_id
279 ELSE %3
280END,
281cps.financial_type_id = %3
282WHERE cpse.id IS NOT NULL {$where}";
77b97be7 283
157b21d8
PN
284 CRM_Core_DAO::executeQuery($sql, $params);
285 }
96025800 286
6a488035 287}