Merge branch 'JohnFF-patch-1'
[civicrm-core.git] / CRM / Upgrade / Snapshot / V4p2 / Price / BAO / FieldValue.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * Business objects for managing price fields values.
38 *
39 */
40 class 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 * @param $ids
48 *
49 * @return object CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue object
50 * @access public
51 * @static
52 */
53 static function &add(&$params, $ids) {
54
55 $fieldValueBAO = new CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue();
56 $fieldValueBAO->copyValues($params);
57
58 if ($id = CRM_Utils_Array::value('id', $ids)) {
59 $fieldValueBAO->id = $id;
60 }
61 if (!empty($params['is_default'])) {
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 *
76 * @param $ids
77 *
78 * @return object CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue object
79 * @access public
80 * @static
81 */
82 static function create(&$params, $ids) {
83
84 if (!is_array($params) || empty($params)) {
85 return;
86 }
87
88 if ($id = CRM_Utils_Array::value('id', $ids)) {
89 if (isset($params['name']))unset($params['name']);
90
91 $oldWeight = NULL;
92 if ($id) {
93 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $id, 'weight', 'id');
94 }
95
96 $fieldValues = array('price_field_id' => CRM_Utils_Array::value('price_field_id', $params, 0));
97 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $oldWeight, $params['weight'], $fieldValues);
98 }
99 else {
100 if (empty($params['name'])) {
101 $params['name'] = CRM_Utils_String::munge(CRM_Utils_Array::value('label', $params), '_', 64);
102 }
103 if (empty($params['weight'])) {
104 $params['weight'] = 1;
105 }
106 }
107 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, 0);
108
109 return self::add($params, $ids);
110 }
111
112 /**
113 * Takes a bunch of params that are needed to match certain criteria and
114 * retrieves the relevant objects.
115 *
116 * @param array $params (reference ) an assoc array
117 * @param array $defaults (reference ) an assoc array to hold the flattened values
118 *
119 * @return object CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue object
120 * @access public
121 * @static
122 */
123 static function retrieve(&$params, &$defaults) {
124 return CRM_Core_DAO::commonRetrieve('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $params, $defaults);
125 }
126
127 /**
128 * Retrive the all values for given field id
129 *
130 * @param int $fieldId price_field_id
131 * @param array $values (reference ) to hold the values
132 * @param string $orderBy for order by, default weight
133 * @param bool|int $isActive is_active, default false
134 *
135 * @return array $values
136 *
137 * @access public
138 * @static
139 */
140 static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE) {
141 $fieldValueDAO = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue();
142 $fieldValueDAO->price_field_id = $fieldId;
143 $fieldValueDAO->orderBy($orderBy, 'label');
144 if ($isActive) {
145 $fieldValueDAO->is_active = 1;
146 }
147 $fieldValueDAO->find();
148
149 while ($fieldValueDAO->fetch()) {
150 CRM_Core_DAO::storeValues($fieldValueDAO, $values[$fieldValueDAO->id]);
151 }
152
153 return $values;
154 }
155
156 /**
157 * Get the price field option label.
158 *
159 * @param int $id id of field option.
160 *
161 * @return string name
162 *
163 * @access public
164 * @static
165 *
166 */
167 public static function getOptionLabel($id) {
168 return CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $id, 'label');
169 }
170
171 /**
172 * update the is_active flag in the db
173 *
174 * @param int $id Id of the database record
175 * @param boolean $is_active Value we want to set the is_active field
176 *
177 * @return Object DAO object on sucess, null otherwise
178 *
179 * @access public
180 * @static
181 */
182 static function setIsActive($id, $is_active) {
183 return CRM_Core_DAO::setFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $id, 'is_active', $is_active);
184 }
185
186 /**
187 * delete all values of the given field id
188 *
189 * @param int $fieldId Price field id
190 *
191 * @return boolean
192 *
193 * @access public
194 * @static
195 */
196 static function deleteValues($fieldId) {
197 if (!$fieldId) {
198 return FALSE;
199 }
200
201 $fieldValueDAO = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue();
202 $fieldValueDAO->price_field_id = $fieldId;
203 $fieldValueDAO->delete();
204 }
205
206 /**
207 * Delete the value.
208 *
209 * @param int $id Id
210 *
211 * @return boolean
212 *
213 * @access public
214 * @static
215 */
216 static function del($id) {
217 if (!$id) {
218 return FALSE;
219 }
220
221 $fieldValueDAO = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue();
222 $fieldValueDAO->id = $id;
223 return $fieldValueDAO->delete();
224 }
225 }
226