3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
37 * Business objects for managing price fields values.
40 class CRM_Price_BAO_PriceFieldValue
extends CRM_Price_DAO_PriceFieldValue
{
43 * insert/update a new entry in the database.
45 * @param array $params (reference), array $ids
47 * @return object CRM_Price_DAO_PriceFieldValue object
51 static function &add(&$params, $ids) {
53 $fieldValueBAO = new CRM_Price_BAO_PriceFieldValue();
54 $fieldValueBAO->copyValues($params);
56 if ($id = CRM_Utils_Array
::value('id', $ids)) {
57 $fieldValueBAO->id
= $id;
59 if (CRM_Utils_Array
::value('is_default', $params)) {
60 $query = 'UPDATE civicrm_price_field_value SET is_default = 0 WHERE price_field_id = %1';
61 $p = array(1 => array($params['price_field_id'], 'Integer'));
62 CRM_Core_DAO
::executeQuery($query, $p);
65 $fieldValueBAO->save();
66 return $fieldValueBAO;
70 * Creates a new entry in the database.
72 * @param array $params (reference), array $ids
74 * @return object CRM_Price_DAO_PriceFieldValue object
78 static function create(&$params, $ids) {
80 if (!is_array($params) ||
empty($params)) {
84 if ($id = CRM_Utils_Array
::value('id', $ids)) {
85 if (isset($params['name']))unset($params['name']);
89 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'weight', 'id');
92 $fieldValues = array('price_field_id' => CRM_Utils_Array
::value('price_field_id', $params, 0));
93 $params['weight'] = CRM_Utils_Weight
::updateOtherWeights('CRM_Price_DAO_PriceFieldValue', $oldWeight, $params['weight'], $fieldValues);
96 if (!CRM_Utils_Array
::value('name', $params)) {
97 $params['name'] = CRM_Utils_String
::munge(CRM_Utils_Array
::value('label', $params), '_', 64);
99 if (!CRM_Utils_Array
::value('weight', $params)) {
100 $params['weight'] = 1;
103 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, 0);
105 return self
::add($params, $ids);
109 * Takes a bunch of params that are needed to match certain criteria and
110 * retrieves the relevant objects.
112 * @param array $params (reference ) an assoc array
113 * @param array $defaults (reference ) an assoc array to hold the flattened values
115 * @return object CRM_Price_DAO_PriceFieldValue object
119 static function retrieve(&$params, &$defaults) {
120 return CRM_Core_DAO
::commonRetrieve('CRM_Price_DAO_PriceFieldValue', $params, $defaults);
124 * Retrive the all values for given field id
126 * @param int $fieldId price_field_id
127 * @param array $values (reference ) to hold the values
128 * @param string $orderBy for order by, default weight
129 * @param int $isActive is_active, default false
131 * @return array $values
136 static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE) {
137 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
138 $fieldValueDAO->price_field_id
= $fieldId;
139 $fieldValueDAO->orderBy($orderBy, 'label');
141 $fieldValueDAO->is_active
= 1;
143 $fieldValueDAO->find();
145 while ($fieldValueDAO->fetch()) {
146 CRM_Core_DAO
::storeValues($fieldValueDAO, $values[$fieldValueDAO->id
]);
153 * Get the price field option label.
155 * @param int $id id of field option.
157 * @return string name
163 public static function getOptionLabel($id) {
164 return CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'label');
168 * update the is_active flag in the db
170 * @param int $id Id of the database record
171 * @param boolean $is_active Value we want to set the is_active field
173 * @return Object DAO object on sucess, null otherwise
178 static function setIsActive($id, $is_active) {
179 return CRM_Core_DAO
::setFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'is_active', $is_active);
183 * delete all values of the given field id
185 * @param int $fieldId Price field id
192 static function deleteValues($fieldId) {
197 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
198 $fieldValueDAO->price_field_id
= $fieldId;
199 $fieldValueDAO->delete();
212 static function del($id) {
217 $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
218 $fieldValueDAO->id
= $id;
219 return $fieldValueDAO->delete();
223 * Update civicrm_price_field_value.financial_type_id
224 * when financial_type_id of contribution_page or event is changed
226 * @param int $entityId Id
227 * @param String $entityTable entity table
228 * @param String $financialTypeID financial type id
233 static function updateFinancialType($entityId, $entityTable, $financialTypeID) {
234 if (!$entityId ||
!$entityTable ||
!$financialTypeID) {
238 1 => array($entityId, 'Integer'),
239 2 => array($entityTable, 'String'),
240 3 => array($financialTypeID, 'Integer'),
242 // for event discount
244 if ($entityTable == 'civicrm_event') {
245 $join = " LEFT JOIN civicrm_discount cd ON cd.price_set_id = cps.id AND cd.entity_id = %1 AND cd.entity_table = %2 ";
246 $where = ' OR cd.id IS NOT NULL ';
248 $sql = "UPDATE civicrm_price_set cps
249 LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = cps.id AND cpse.entity_id = %1 AND cpse.entity_table = %2
250 LEFT JOIN civicrm_price_field cpf ON cpf.price_set_id = cps.id
251 LEFT JOIN civicrm_price_field_value cpfv ON cpf.id = cpfv.price_field_id
253 SET cpfv.financial_type_id = CASE
254 WHEN cpfv.membership_type_id IS NOT NULL
255 THEN cpfv.financial_type_id
258 cps.financial_type_id = %3
259 WHERE cpse.id IS NOT NULL {$where}";
261 CRM_Core_DAO
::executeQuery($sql, $params);