Commit | Line | Data |
---|---|---|
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 | class CRM_Member_BAO_MembershipStatus extends CRM_Member_DAO_MembershipStatus { | |
36 | ||
37 | /** | |
38 | * static holder for the default LT | |
39 | */ | |
40 | static $_defaultMembershipStatus = NULL; | |
41 | ||
42 | /** | |
43 | * class constructor | |
44 | */ | |
45 | function __construct() { | |
46 | parent::__construct(); | |
47 | } | |
48 | ||
49 | /** | |
50 | * Takes a bunch of params that are needed to match certain criteria and | |
51 | * retrieves the relevant objects. Typically the valid params are only | |
52 | * contact_id. We'll tweak this function to be more full featured over a period | |
53 | * of time. This is the inverse function of create. It also stores all the retrieved | |
54 | * values in the default array | |
55 | * | |
56 | * @param array $params (reference ) an assoc array of name/value pairs | |
57 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
58 | * | |
59 | * @return object CRM_Member_BAO_MembershipStatus object | |
60 | * @access public | |
61 | * @static | |
62 | */ | |
63 | static function retrieve(&$params, &$defaults) { | |
64 | $membershipStatus = new CRM_Member_DAO_MembershipStatus(); | |
65 | $membershipStatus->copyValues($params); | |
66 | if ($membershipStatus->find(TRUE)) { | |
67 | CRM_Core_DAO::storeValues($membershipStatus, $defaults); | |
68 | return $membershipStatus; | |
69 | } | |
70 | return NULL; | |
71 | } | |
72 | ||
73 | /** | |
74 | * update the is_active flag in the db | |
75 | * | |
76 | * @param int $id id of the database record | |
77 | * @param boolean $is_active value we want to set the is_active field | |
78 | * | |
79 | * @return Object DAO object on sucess, null otherwise | |
80 | * @static | |
81 | */ | |
82 | static function setIsActive($id, $is_active) { | |
83 | return CRM_Core_DAO::setFieldValue('CRM_Member_DAO_MembershipStatus', $id, 'is_active', $is_active); | |
84 | } | |
85 | ||
86 | /** | |
87 | * Takes an associative array and creates a membership Status object | |
88 | * See http://wiki.civicrm.org/confluence/display/CRM/Database+layer | |
6a488035 | 89 | * |
d9f93da9 EM |
90 | * @param array $params (reference ) an assoc array of name/value pairs |
91 | * | |
92 | * @throws Exception | |
6a488035 TO |
93 | * @return object CRM_Member_BAO_MembershipStatus object |
94 | * @access public | |
95 | * @static | |
96 | */ | |
97 | static function create($params){ | |
98 | $ids = array(); | |
99 | if(!empty($params['id'])){ | |
100 | $ids['membershipStatus'] = $params['id']; | |
101 | } | |
102 | else{ | |
103 | //don't allow duplicate names - if id not set | |
104 | $status = new CRM_Member_DAO_MembershipStatus(); | |
105 | $status->name = $params['name']; | |
106 | if ($status->find(TRUE)) { | |
107 | throw new Exception('A membership status with this name already exists.'); | |
108 | } | |
109 | } | |
110 | $membershipStatusBAO = CRM_Member_BAO_MembershipStatus::add($params, $ids); | |
111 | return $membershipStatusBAO; | |
112 | } | |
113 | /** | |
114 | * function to add the membership types | |
115 | * | |
116 | * @param array $params reference array contains the values submitted by the form | |
dd12dc87 | 117 | * @param array $ids array contains the id - this param is deprecated |
6a488035 TO |
118 | * |
119 | * @access public | |
120 | * @static | |
121 | * | |
122 | * @return object | |
123 | */ | |
dd12dc87 | 124 | static function add(&$params, $ids = array()) { |
64d24a64 EM |
125 | $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('membershipStatus', $ids)); |
126 | if (!$id) { | |
127 | CRM_Core_DAO::setCreateDefaults($params, self::getDefaults()); | |
128 | //copy name to label when not passed. | |
129 | if (empty($params['label']) && !empty($params['name'])) { | |
130 | $params['label'] = $params['name']; | |
131 | } | |
132 | ||
133 | if (empty($params['name']) && !empty($params['label'])) { | |
134 | $params['name'] = $params['label']; | |
135 | } | |
136 | } | |
6a488035 TO |
137 | |
138 | // set all other defaults to false. | |
64d24a64 | 139 | if (!empty($params['is_default'])) { |
6a488035 TO |
140 | $query = "UPDATE civicrm_membership_status SET is_default = 0"; |
141 | CRM_Core_DAO::executeQuery($query, | |
142 | CRM_Core_DAO::$_nullArray | |
143 | ); | |
144 | } | |
145 | ||
6a488035 | 146 | |
6a488035 TO |
147 | |
148 | // action is taken depending upon the mode | |
149 | $membershipStatus = new CRM_Member_DAO_MembershipStatus(); | |
150 | $membershipStatus->copyValues($params); | |
151 | ||
64d24a64 | 152 | $membershipStatus->id = $id; |
6a488035 TO |
153 | |
154 | $membershipStatus->save(); | |
155 | return $membershipStatus; | |
156 | } | |
157 | ||
64d24a64 EM |
158 | /** |
159 | * Get defaults for new entity | |
160 | * @return array | |
161 | */ | |
162 | static function getDefaults() { | |
163 | return array( | |
164 | 'is_active' => FALSE, | |
165 | 'is_current_member' => FALSE, | |
166 | 'is_admin' => FALSE, | |
167 | 'is_default' => FALSE, | |
168 | ); | |
169 | } | |
170 | ||
6a488035 TO |
171 | /** |
172 | * Function to get membership status | |
173 | * | |
174 | * @param int $membershipStatusId | |
d9f93da9 EM |
175 | * |
176 | * @return array | |
6a488035 TO |
177 | * @static |
178 | */ | |
179 | public static function getMembershipStatus($membershipStatusId) { | |
180 | $statusDetails = array(); | |
181 | $membershipStatus = new CRM_Member_DAO_MembershipStatus(); | |
182 | $membershipStatus->id = $membershipStatusId; | |
183 | if ($membershipStatus->find(TRUE)) { | |
184 | CRM_Core_DAO::storeValues($membershipStatus, $statusDetails); | |
185 | } | |
186 | return $statusDetails; | |
187 | } | |
188 | ||
189 | /** | |
190 | * Function to delete membership Types | |
191 | * | |
192 | * @param int $membershipStatusId | |
77b97be7 EM |
193 | * |
194 | * @throws CRM_Core_Exception | |
195 | * @internal param $ | |
6a488035 TO |
196 | * @static |
197 | */ | |
dcc4f6a7 | 198 | static function del($membershipStatusId) { |
6a488035 TO |
199 | //check dependencies |
200 | //checking if membership status is present in some other table | |
201 | $check = FALSE; | |
202 | ||
203 | $dependancy = array('Membership', 'MembershipLog'); | |
204 | foreach ($dependancy as $name) { | |
4d5c2eb5 | 205 | $baoString = 'CRM_Member_BAO_' . $name; |
206 | $dao = new $baoString(); | |
6a488035 TO |
207 | $dao->status_id = $membershipStatusId; |
208 | if ($dao->find(TRUE)) { | |
dcc4f6a7 | 209 | throw new CRM_Core_Exception(ts('This membership status cannot be deleted as memberships exist with this status')); |
6a488035 TO |
210 | } |
211 | } | |
dcc4f6a7 | 212 | CRM_Utils_Weight::delWeight('CRM_Member_DAO_MembershipStatus', $membershipStatusId); |
6a488035 TO |
213 | //delete from membership Type table |
214 | $membershipStatus = new CRM_Member_DAO_MembershipStatus(); | |
215 | $membershipStatus->id = $membershipStatusId; | |
216 | $membershipStatus->delete(); | |
217 | $membershipStatus->free(); | |
218 | } | |
219 | ||
220 | /** | |
221 | * Function to find the membership status based on start date, end date, join date & status date. | |
222 | * | |
f5d25ee9 EM |
223 | * @param string $startDate start date of the member whose membership status is to be calculated. |
224 | * @param string $endDate end date of the member whose membership status is to be calculated. | |
225 | * @param string $joinDate join date of the member whose membership status is to be calculated. | |
da6b46f4 | 226 | * @param \date|string $statusDate status date of the member whose membership status is to be calculated. |
6a488035 | 227 | * @param boolean $excludeIsAdmin exclude the statuses those having is_admin = 1 |
da6b46f4 | 228 | * @param $membershipTypeID |
5f11bbcc | 229 | * @param array $membership membership params as available to calling function - passed to the hook |
6a488035 | 230 | * |
da6b46f4 EM |
231 | * @internal param int $membershipType membership type id - passed to the hook |
232 | * @return array | |
233 | @static | |
6a488035 TO |
234 | */ |
235 | static function getMembershipStatusByDate($startDate, $endDate, $joinDate, | |
5f11bbcc | 236 | $statusDate = 'today', $excludeIsAdmin = FALSE, $membershipTypeID, $membership = array() |
6a488035 TO |
237 | ) { |
238 | $membershipDetails = array(); | |
5f11bbcc | 239 | |
6a488035 TO |
240 | if (!$statusDate || $statusDate == 'today') { |
241 | $statusDate = getDate(); | |
242 | $statusDate = date('Ymd', | |
243 | mktime($statusDate['hours'], | |
244 | $statusDate['minutes'], | |
245 | $statusDate['seconds'], | |
246 | $statusDate['mon'], | |
247 | $statusDate['mday'], | |
248 | $statusDate['year'] | |
249 | ) | |
250 | ); | |
251 | } | |
252 | else { | |
253 | $statusDate = CRM_Utils_Date::customFormat($statusDate, '%Y%m%d'); | |
254 | } | |
255 | ||
6a488035 TO |
256 | $dates = array('start', 'end', 'join'); |
257 | $events = array('start', 'end'); | |
258 | ||
259 | foreach ($dates as $dat) { | |
a0713e70 | 260 | if (${$dat . 'Date'} && ${$dat . 'Date'} != "null") { |
261 | ${$dat . 'Date'} = CRM_Utils_Date::customFormat(${$dat . 'Date'}, '%Y%m%d'); | |
262 | ||
6a488035 TO |
263 | ${$dat . 'Year'} = substr(${$dat . 'Date'}, 0, 4); |
264 | ||
265 | ${$dat . 'Month'} = substr(${$dat . 'Date'}, 4, 2); | |
266 | ||
267 | ${$dat . 'Day'} = substr(${$dat . 'Date'}, 6, 2); | |
268 | } | |
a0713e70 | 269 | else { |
270 | ${$dat . 'Date'} = ''; | |
271 | } | |
6a488035 TO |
272 | } |
273 | ||
274 | //fix for CRM-3570, if we have statuses with is_admin=1, | |
275 | //exclude these statuses from calculatation during import. | |
276 | $where = "is_active = 1"; | |
277 | if ($excludeIsAdmin) { | |
278 | $where .= " AND is_admin != 1"; | |
279 | } | |
280 | ||
281 | $query = " | |
282 | SELECT * | |
283 | FROM civicrm_membership_status | |
284 | WHERE {$where} | |
285 | ORDER BY weight ASC"; | |
286 | ||
287 | $membershipStatus = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
288 | $hour = $minute = $second = 0; | |
289 | ||
290 | while ($membershipStatus->fetch()) { | |
291 | $startEvent = NULL; | |
292 | $endEvent = NULL; | |
293 | foreach ($events as $eve) { | |
294 | foreach ($dates as $dat) { | |
295 | // calculate start-event/date and end-event/date | |
296 | if (($membershipStatus->{$eve . '_event'} == $dat . '_date') && | |
297 | ${$dat . 'Date'} | |
298 | ) { | |
299 | if ($membershipStatus->{$eve . '_event_adjust_unit'} && | |
300 | $membershipStatus->{$eve . '_event_adjust_interval'} | |
301 | ) { | |
302 | // add in months | |
303 | if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'month') { | |
304 | ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second, | |
305 | ${$dat . 'Month'} + $membershipStatus->{$eve . '_event_adjust_interval'}, | |
306 | ${$dat . 'Day'}, | |
307 | ${$dat . 'Year'} | |
308 | )); | |
309 | } | |
310 | // add in days | |
311 | if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'day') { | |
312 | ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second, | |
313 | ${$dat . 'Month'}, | |
314 | ${$dat . 'Day'} + $membershipStatus->{$eve . '_event_adjust_interval'}, | |
315 | ${$dat . 'Year'} | |
316 | )); | |
317 | } | |
318 | // add in years | |
319 | if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'year') { | |
320 | ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second, | |
321 | ${$dat . 'Month'}, | |
322 | ${$dat . 'Day'}, | |
323 | ${$dat . 'Year'} + $membershipStatus->{$eve . '_event_adjust_interval'} | |
324 | )); | |
325 | } | |
326 | // if no interval and unit, present | |
327 | } | |
328 | else { | |
329 | ${$eve . 'Event'} = ${$dat . 'Date'}; | |
330 | } | |
331 | } | |
332 | } | |
333 | } | |
334 | ||
335 | // check if statusDate is in the range of start & end events. | |
336 | if ($startEvent && $endEvent) { | |
337 | if (($statusDate >= $startEvent) && ($statusDate <= $endEvent)) { | |
338 | $membershipDetails['id'] = $membershipStatus->id; | |
339 | $membershipDetails['name'] = $membershipStatus->name; | |
340 | } | |
341 | } | |
342 | elseif ($startEvent) { | |
343 | if ($statusDate >= $startEvent) { | |
344 | $membershipDetails['id'] = $membershipStatus->id; | |
345 | $membershipDetails['name'] = $membershipStatus->name; | |
346 | } | |
347 | } | |
348 | elseif ($endEvent) { | |
349 | if ($statusDate <= $endEvent) { | |
350 | $membershipDetails['id'] = $membershipStatus->id; | |
351 | $membershipDetails['name'] = $membershipStatus->name; | |
352 | } | |
353 | } | |
354 | ||
355 | // returns FIRST status record for which status_date is in range. | |
356 | if ($membershipDetails) { | |
5f11bbcc | 357 | break; |
6a488035 TO |
358 | } |
359 | } | |
360 | //end fetch | |
361 | ||
362 | $membershipStatus->free(); | |
5f11bbcc EM |
363 | |
364 | //we bundle the arguments into an array as we can't pass 8 variables to the hook otherwise | |
365 | // the membership array might contain the pre-altered settings so we don't want to merge this | |
366 | $arguments = array( | |
367 | 'start_date' => $startDate, | |
368 | 'end_date' => $endDate, | |
369 | 'join_date' => $joinDate, | |
370 | 'status_date' => $statusDate, | |
371 | 'exclude_is_admin' => $endDate, | |
372 | 'membership_type_id' => $membershipTypeID, | |
373 | 'start_event' => $startEvent, | |
374 | 'end_event' => $endEvent, | |
375 | ); | |
376 | CRM_Utils_Hook::alterCalculatedMembershipStatus($membershipDetails, $arguments, $membership); | |
6a488035 TO |
377 | return $membershipDetails; |
378 | } | |
379 | ||
380 | /** | |
381 | * Function that return the status ids whose is_current_member is set | |
382 | * | |
77b97be7 EM |
383 | * @return array |
384 | @static | |
6a488035 TO |
385 | */ |
386 | public static function getMembershipStatusCurrent() { | |
387 | $statusIds = array(); | |
388 | $membershipStatus = new CRM_Member_DAO_MembershipStatus(); | |
389 | $membershipStatus->is_current_member = 1; | |
390 | $membershipStatus->find(); | |
391 | $membershipStatus->selectAdd(); | |
392 | $membershipStatus->selectAdd('id'); | |
393 | while ($membershipStatus->fetch()) { | |
394 | $statusIds[] = $membershipStatus->id; | |
395 | } | |
396 | $membershipStatus->free(); | |
397 | return $statusIds; | |
398 | } | |
399 | } | |
400 |