Commit | Line | Data |
---|---|---|
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
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 | */ | |
40 | class 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 | * | |
16b10e64 | 50 | * @return CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue |
6a488035 | 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 | * | |
c68f8bfa TO |
73 | * @param array $params |
74 | * (reference), array $ids. | |
6a488035 | 75 | * |
6c8f6e67 EM |
76 | * @param $ids |
77 | * | |
16b10e64 | 78 | * @return CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue |
6a488035 | 79 | */ |
00be9182 | 80 | public static function create(&$params, $ids) { |
6a488035 TO |
81 | |
82 | if (!is_array($params) || empty($params)) { | |
28a04ea9 | 83 | return NULL; |
6a488035 TO |
84 | } |
85 | ||
86 | if ($id = CRM_Utils_Array::value('id', $ids)) { | |
4f99ca55 TO |
87 | if (isset($params['name'])) { |
88 | unset($params['name']); | |
e418776c | 89 | } |
6a488035 TO |
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 | /** | |
fe482240 EM |
113 | * Retrieve DB object based on input parameters. |
114 | * | |
115 | * It also stores all the retrieved values in the default array. | |
6a488035 | 116 | * |
c68f8bfa TO |
117 | * @param array $params |
118 | * (reference ) an assoc array. | |
119 | * @param array $defaults | |
120 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 121 | * |
16b10e64 | 122 | * @return CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue |
6a488035 | 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 | /** | |
fe482240 | 129 | * Retrive the all values for given field id. |
6a488035 | 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 | 138 | * |
a6c01b45 | 139 | * @return array |
6a488035 | 140 | * |
6a488035 | 141 | */ |
00be9182 | 142 | public static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE) { |
6a488035 TO |
143 | $fieldValueDAO = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue(); |
144 | $fieldValueDAO->price_field_id = $fieldId; | |
145 | $fieldValueDAO->orderBy($orderBy, 'label'); | |
146 | if ($isActive) { | |
147 | $fieldValueDAO->is_active = 1; | |
148 | } | |
149 | $fieldValueDAO->find(); | |
150 | ||
151 | while ($fieldValueDAO->fetch()) { | |
152 | CRM_Core_DAO::storeValues($fieldValueDAO, $values[$fieldValueDAO->id]); | |
153 | } | |
154 | ||
155 | return $values; | |
156 | } | |
157 | ||
158 | /** | |
159 | * Get the price field option label. | |
160 | * | |
c68f8bfa TO |
161 | * @param int $id |
162 | * Id of field option. | |
6a488035 | 163 | * |
a6c01b45 CW |
164 | * @return string |
165 | * name | |
6a488035 | 166 | * |
6a488035 TO |
167 | */ |
168 | public static function getOptionLabel($id) { | |
169 | return CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $id, 'label'); | |
170 | } | |
171 | ||
172 | /** | |
fe482240 | 173 | * Update the is_active flag in the db. |
6a488035 | 174 | * |
c68f8bfa TO |
175 | * @param int $id |
176 | * Id of the database record. | |
177 | * @param bool $is_active | |
178 | * Value we want to set the is_active field. | |
6a488035 | 179 | * |
a6c01b45 CW |
180 | * @return Object |
181 | * DAO object on sucess, null otherwise | |
6a488035 | 182 | * |
6a488035 | 183 | */ |
00be9182 | 184 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
185 | return CRM_Core_DAO::setFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $id, 'is_active', $is_active); |
186 | } | |
187 | ||
188 | /** | |
fe482240 | 189 | * Delete all values of the given field id. |
6a488035 | 190 | * |
c68f8bfa TO |
191 | * @param int $fieldId |
192 | * Price field id. | |
6a488035 | 193 | * |
28a04ea9 | 194 | * @return bool |
6a488035 | 195 | * |
6a488035 | 196 | */ |
00be9182 | 197 | public static function deleteValues($fieldId) { |
6a488035 TO |
198 | if (!$fieldId) { |
199 | return FALSE; | |
200 | } | |
201 | ||
202 | $fieldValueDAO = new CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue(); | |
203 | $fieldValueDAO->price_field_id = $fieldId; | |
204 | $fieldValueDAO->delete(); | |
205 | } | |
206 | ||
207 | /** | |
208 | * Delete the value. | |
209 | * | |
c68f8bfa TO |
210 | * @param int $id |
211 | * Id. | |
6a488035 | 212 | * |
28a04ea9 | 213 | * @return bool |
6a488035 | 214 | * |
6a488035 | 215 | */ |
00be9182 | 216 | public static function del($id) { |
6a488035 TO |
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 | } | |
96025800 | 225 | |
6a488035 | 226 | } |