INFRA-132 - CRM/Upgrade - Convert single-line @param to multi-line
[civicrm-core.git] / CRM / Upgrade / Snapshot / V4p2 / Price / BAO / FieldValue.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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 /**
100fef9d 43 * Insert/update a new entry in the database.
6a488035 44 *
c68f8bfa
TO
45 * @param array $params
46 * (reference), array $ids.
6a488035 47 *
6c8f6e67
EM
48 * @param $ids
49 *
c490a46a 50 * @return CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue object
6a488035
TO
51 * @static
52 */
00be9182 53 public static function &add(&$params, $ids) {
6a488035
TO
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 }
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 *
c68f8bfa
TO
74 * @param array $params
75 * (reference), array $ids.
6a488035 76 *
6c8f6e67
EM
77 * @param $ids
78 *
c490a46a 79 * @return CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue object
6a488035
TO
80 * @static
81 */
00be9182 82 public static function create(&$params, $ids) {
6a488035
TO
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 {
a7488080 100 if (empty($params['name'])) {
6a488035
TO
101 $params['name'] = CRM_Utils_String::munge(CRM_Utils_Array::value('label', $params), '_', 64);
102 }
a7488080 103 if (empty($params['weight'])) {
6a488035
TO
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 *
c68f8bfa
TO
116 * @param array $params
117 * (reference ) an assoc array.
118 * @param array $defaults
119 * (reference ) an assoc array to hold the flattened values.
6a488035 120 *
c490a46a 121 * @return CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue object
6a488035
TO
122 * @static
123 */
00be9182 124 public static function retrieve(&$params, &$defaults) {
6a488035
TO
125 return CRM_Core_DAO::commonRetrieve('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $params, $defaults);
126 }
127
128 /**
129 * Retrive the all values for given field id
130 *
c68f8bfa
TO
131 * @param int $fieldId
132 * Price_field_id.
133 * @param array $values
134 * (reference ) to hold the values.
135 * @param string $orderBy
136 * For order by, default weight.
77b97be7 137 * @param bool|int $isActive is_active, default false
6a488035
TO
138 *
139 * @return array $values
140 *
6a488035
TO
141 * @static
142 */
00be9182 143 public static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE) {
6a488035
TO
144 $fieldValueDAO = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue();
145 $fieldValueDAO->price_field_id = $fieldId;
146 $fieldValueDAO->orderBy($orderBy, 'label');
147 if ($isActive) {
148 $fieldValueDAO->is_active = 1;
149 }
150 $fieldValueDAO->find();
151
152 while ($fieldValueDAO->fetch()) {
153 CRM_Core_DAO::storeValues($fieldValueDAO, $values[$fieldValueDAO->id]);
154 }
155
156 return $values;
157 }
158
159 /**
160 * Get the price field option label.
161 *
c68f8bfa
TO
162 * @param int $id
163 * Id of field option.
6a488035
TO
164 *
165 * @return string name
166 *
6a488035
TO
167 * @static
168 *
169 */
170 public static function getOptionLabel($id) {
171 return CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $id, 'label');
172 }
173
174 /**
100fef9d 175 * Update the is_active flag in the db
6a488035 176 *
c68f8bfa
TO
177 * @param int $id
178 * Id of the database record.
179 * @param bool $is_active
180 * Value we want to set the is_active field.
6a488035
TO
181 *
182 * @return Object DAO object on sucess, null otherwise
183 *
6a488035
TO
184 * @static
185 */
00be9182 186 public static function setIsActive($id, $is_active) {
6a488035
TO
187 return CRM_Core_DAO::setFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $id, 'is_active', $is_active);
188 }
189
190 /**
100fef9d 191 * Delete all values of the given field id
6a488035 192 *
c68f8bfa
TO
193 * @param int $fieldId
194 * Price field id.
6a488035
TO
195 *
196 * @return boolean
197 *
6a488035
TO
198 * @static
199 */
00be9182 200 public static function deleteValues($fieldId) {
6a488035
TO
201 if (!$fieldId) {
202 return FALSE;
203 }
204
205 $fieldValueDAO = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue();
206 $fieldValueDAO->price_field_id = $fieldId;
207 $fieldValueDAO->delete();
208 }
209
210 /**
211 * Delete the value.
212 *
c68f8bfa
TO
213 * @param int $id
214 * Id.
6a488035
TO
215 *
216 * @return boolean
217 *
6a488035
TO
218 * @static
219 */
00be9182 220 public static function del($id) {
6a488035
TO
221 if (!$id) {
222 return FALSE;
223 }
224
225 $fieldValueDAO = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue();
226 $fieldValueDAO->id = $id;
227 return $fieldValueDAO->delete();
228 }
229}