Merge pull request #3208 from eileenmcnaughton/comments
[civicrm-core.git] / CRM / Upgrade / Snapshot / V4p2 / Price / BAO / FieldValue.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 */
40class CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue extends CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue {
41
42 /**
43 * insert/update a new entry in the database.
44 *
45 * @param array $params (reference), array $ids
46 *
47 * @return object CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue object
48 * @access public
49 * @static
50 */
51 static function &add(&$params, $ids) {
52
53 $fieldValueBAO = new CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue();
54 $fieldValueBAO->copyValues($params);
55
56 if ($id = CRM_Utils_Array::value('id', $ids)) {
57 $fieldValueBAO->id = $id;
58 }
a7488080 59 if (!empty($params['is_default'])) {
6a488035
TO
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);
63 }
64
65 $fieldValueBAO->save();
66 return $fieldValueBAO;
67 }
68
69 /**
70 * Creates a new entry in the database.
71 *
72 * @param array $params (reference), array $ids
73 *
74 * @return object CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue object
75 * @access public
76 * @static
77 */
78 static function create(&$params, $ids) {
79
80 if (!is_array($params) || empty($params)) {
81 return;
82 }
83
84 if ($id = CRM_Utils_Array::value('id', $ids)) {
85 if (isset($params['name']))unset($params['name']);
86
87 $oldWeight = NULL;
88 if ($id) {
89 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $id, 'weight', 'id');
90 }
91
92 $fieldValues = array('price_field_id' => CRM_Utils_Array::value('price_field_id', $params, 0));
93 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $oldWeight, $params['weight'], $fieldValues);
94 }
95 else {
a7488080 96 if (empty($params['name'])) {
6a488035
TO
97 $params['name'] = CRM_Utils_String::munge(CRM_Utils_Array::value('label', $params), '_', 64);
98 }
a7488080 99 if (empty($params['weight'])) {
6a488035
TO
100 $params['weight'] = 1;
101 }
102 }
103 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, 0);
104
105 return self::add($params, $ids);
106 }
107
108 /**
109 * Takes a bunch of params that are needed to match certain criteria and
110 * retrieves the relevant objects.
111 *
112 * @param array $params (reference ) an assoc array
113 * @param array $defaults (reference ) an assoc array to hold the flattened values
114 *
115 * @return object CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue object
116 * @access public
117 * @static
118 */
119 static function retrieve(&$params, &$defaults) {
120 return CRM_Core_DAO::commonRetrieve('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $params, $defaults);
121 }
122
123 /**
124 * Retrive the all values for given field id
125 *
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
130 *
131 * @return array $values
132 *
133 * @access public
134 * @static
135 */
136 static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE) {
137 $fieldValueDAO = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue();
138 $fieldValueDAO->price_field_id = $fieldId;
139 $fieldValueDAO->orderBy($orderBy, 'label');
140 if ($isActive) {
141 $fieldValueDAO->is_active = 1;
142 }
143 $fieldValueDAO->find();
144
145 while ($fieldValueDAO->fetch()) {
146 CRM_Core_DAO::storeValues($fieldValueDAO, $values[$fieldValueDAO->id]);
147 }
148
149 return $values;
150 }
151
152 /**
153 * Get the price field option label.
154 *
155 * @param int $id id of field option.
156 *
157 * @return string name
158 *
159 * @access public
160 * @static
161 *
162 */
163 public static function getOptionLabel($id) {
164 return CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $id, 'label');
165 }
166
167 /**
168 * update the is_active flag in the db
169 *
170 * @param int $id Id of the database record
171 * @param boolean $is_active Value we want to set the is_active field
172 *
173 * @return Object DAO object on sucess, null otherwise
174 *
175 * @access public
176 * @static
177 */
178 static function setIsActive($id, $is_active) {
179 return CRM_Core_DAO::setFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $id, 'is_active', $is_active);
180 }
181
182 /**
183 * delete all values of the given field id
184 *
185 * @param int $fieldId Price field id
186 *
187 * @return boolean
188 *
189 * @access public
190 * @static
191 */
192 static function deleteValues($fieldId) {
193 if (!$fieldId) {
194 return FALSE;
195 }
196
197 $fieldValueDAO = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue();
198 $fieldValueDAO->price_field_id = $fieldId;
199 $fieldValueDAO->delete();
200 }
201
202 /**
203 * Delete the value.
204 *
205 * @param int $id Id
206 *
207 * @return boolean
208 *
209 * @access public
210 * @static
211 */
212 static function del($id) {
213 if (!$id) {
214 return FALSE;
215 }
216
217 $fieldValueDAO = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue();
218 $fieldValueDAO->id = $id;
219 return $fieldValueDAO->delete();
220 }
221}
222