INFRA-132 - CRM/UF - 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
TO
44 *
45 * @param array $params (reference), array $ids
46 *
6c8f6e67
EM
47 * @param $ids
48 *
c490a46a 49 * @return CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue object
6a488035
TO
50 * @static
51 */
00be9182 52 public static function &add(&$params, $ids) {
6a488035
TO
53
54 $fieldValueBAO = new CRM_Upgrade_Snapshot_V4p2_Price_BAO_FieldValue();
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();
67 return $fieldValueBAO;
68 }
69
70 /**
71 * Creates a new entry in the database.
72 *
73 * @param array $params (reference), array $ids
74 *
6c8f6e67
EM
75 * @param $ids
76 *
c490a46a 77 * @return CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue object
6a488035
TO
78 * @static
79 */
00be9182 80 public static function create(&$params, $ids) {
6a488035
TO
81
82 if (!is_array($params) || empty($params)) {
83 return;
84 }
85
86 if ($id = CRM_Utils_Array::value('id', $ids)) {
87 if (isset($params['name']))unset($params['name']);
88
89 $oldWeight = NULL;
90 if ($id) {
91 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $id, 'weight', 'id');
92 }
93
94 $fieldValues = array('price_field_id' => CRM_Utils_Array::value('price_field_id', $params, 0));
95 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $oldWeight, $params['weight'], $fieldValues);
96 }
97 else {
a7488080 98 if (empty($params['name'])) {
6a488035
TO
99 $params['name'] = CRM_Utils_String::munge(CRM_Utils_Array::value('label', $params), '_', 64);
100 }
a7488080 101 if (empty($params['weight'])) {
6a488035
TO
102 $params['weight'] = 1;
103 }
104 }
105 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, 0);
106
107 return self::add($params, $ids);
108 }
109
110 /**
111 * Takes a bunch of params that are needed to match certain criteria and
112 * retrieves the relevant objects.
113 *
114 * @param array $params (reference ) an assoc array
115 * @param array $defaults (reference ) an assoc array to hold the flattened values
116 *
c490a46a 117 * @return CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue object
6a488035
TO
118 * @static
119 */
00be9182 120 public static function retrieve(&$params, &$defaults) {
6a488035
TO
121 return CRM_Core_DAO::commonRetrieve('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $params, $defaults);
122 }
123
124 /**
125 * Retrive the all values for given field id
126 *
127 * @param int $fieldId price_field_id
128 * @param array $values (reference ) to hold the values
129 * @param string $orderBy for order by, default weight
77b97be7 130 * @param bool|int $isActive is_active, default false
6a488035
TO
131 *
132 * @return array $values
133 *
6a488035
TO
134 * @static
135 */
00be9182 136 public static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE) {
6a488035
TO
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 *
6a488035
TO
159 * @static
160 *
161 */
162 public static function getOptionLabel($id) {
163 return CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $id, 'label');
164 }
165
166 /**
100fef9d 167 * Update the is_active flag in the db
6a488035
TO
168 *
169 * @param int $id Id of the database record
170 * @param boolean $is_active Value we want to set the is_active field
171 *
172 * @return Object DAO object on sucess, null otherwise
173 *
6a488035
TO
174 * @static
175 */
00be9182 176 public static function setIsActive($id, $is_active) {
6a488035
TO
177 return CRM_Core_DAO::setFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $id, 'is_active', $is_active);
178 }
179
180 /**
100fef9d 181 * Delete all values of the given field id
6a488035
TO
182 *
183 * @param int $fieldId Price field id
184 *
185 * @return boolean
186 *
6a488035
TO
187 * @static
188 */
00be9182 189 public static function deleteValues($fieldId) {
6a488035
TO
190 if (!$fieldId) {
191 return FALSE;
192 }
193
194 $fieldValueDAO = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue();
195 $fieldValueDAO->price_field_id = $fieldId;
196 $fieldValueDAO->delete();
197 }
198
199 /**
200 * Delete the value.
201 *
202 * @param int $id Id
203 *
204 * @return boolean
205 *
6a488035
TO
206 * @static
207 */
00be9182 208 public static function del($id) {
6a488035
TO
209 if (!$id) {
210 return FALSE;
211 }
212
213 $fieldValueDAO = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue();
214 $fieldValueDAO->id = $id;
215 return $fieldValueDAO->delete();
216 }
217}