Merge pull request #4875 from civicrm/minor-fix
[civicrm-core.git] / CRM / Member / BAO / MembershipStatus.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 */
35class CRM_Member_BAO_MembershipStatus extends CRM_Member_DAO_MembershipStatus {
36
37 /**
100fef9d 38 * Static holder for the default LT
6a488035
TO
39 */
40 static $_defaultMembershipStatus = NULL;
41
42 /**
100fef9d 43 * Class constructor
6a488035 44 */
00be9182 45 public function __construct() {
6a488035
TO
46 parent::__construct();
47 }
48
49 /**
c490a46a 50 * Fetch object based on array of properties
6a488035 51 *
b2363ea8
TO
52 * @param array $params
53 * (reference ) an assoc array of name/value pairs.
54 * @param array $defaults
55 * (reference ) an assoc array to hold the flattened values.
6a488035 56 *
c490a46a 57 * @return CRM_Member_BAO_MembershipStatus object
6a488035
TO
58 * @static
59 */
00be9182 60 public static function retrieve(&$params, &$defaults) {
6a488035
TO
61 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
62 $membershipStatus->copyValues($params);
63 if ($membershipStatus->find(TRUE)) {
64 CRM_Core_DAO::storeValues($membershipStatus, $defaults);
65 return $membershipStatus;
66 }
67 return NULL;
68 }
69
70 /**
100fef9d 71 * Update the is_active flag in the db
6a488035 72 *
b2363ea8
TO
73 * @param int $id
74 * Id of the database record.
75 * @param bool $is_active
76 * Value we want to set the is_active field.
6a488035 77 *
a6c01b45
CW
78 * @return Object
79 * DAO object on sucess, null otherwise
6a488035
TO
80 * @static
81 */
00be9182 82 public static function setIsActive($id, $is_active) {
6a488035
TO
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 *
b2363ea8
TO
90 * @param array $params
91 * (reference ) an assoc array of name/value pairs.
d9f93da9
EM
92 *
93 * @throws Exception
c490a46a 94 * @return CRM_Member_BAO_MembershipStatus object
6a488035
TO
95 * @static
96 */
9b873358 97 public static function create($params) {
6a488035 98 $ids = array();
9b873358 99 if (!empty($params['id'])) {
353ffa53 100 $ids['membershipStatus'] = $params['id'];
6a488035 101 }
92e4c2a5 102 else {
6a488035
TO
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 }
353ffa53 113
6a488035 114 /**
100fef9d 115 * Add the membership types
6a488035 116 *
b2363ea8
TO
117 * @param array $params
118 * Reference array contains the values submitted by the form.
119 * @param array $ids
120 * Array contains the id - this param is deprecated.
6a488035 121 *
6a488035
TO
122 * @static
123 *
124 * @return object
125 */
00be9182 126 public static function add(&$params, $ids = array()) {
64d24a64
EM
127 $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('membershipStatus', $ids));
128 if (!$id) {
129 CRM_Core_DAO::setCreateDefaults($params, self::getDefaults());
130 //copy name to label when not passed.
131 if (empty($params['label']) && !empty($params['name'])) {
132 $params['label'] = $params['name'];
133 }
134
135 if (empty($params['name']) && !empty($params['label'])) {
136 $params['name'] = $params['label'];
137 }
138 }
6a488035
TO
139
140 // set all other defaults to false.
64d24a64 141 if (!empty($params['is_default'])) {
6a488035
TO
142 $query = "UPDATE civicrm_membership_status SET is_default = 0";
143 CRM_Core_DAO::executeQuery($query,
144 CRM_Core_DAO::$_nullArray
145 );
146 }
147
6a488035
TO
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 */
00be9182 162 public static function getDefaults() {
64d24a64
EM
163 return array(
164 'is_active' => FALSE,
165 'is_current_member' => FALSE,
166 'is_admin' => FALSE,
167 'is_default' => FALSE,
168 );
169 }
170
6a488035 171 /**
100fef9d 172 * Get membership status
6a488035
TO
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 /**
100fef9d 190 * Delete membership Types
6a488035
TO
191 *
192 * @param int $membershipStatusId
77b97be7
EM
193 *
194 * @throws CRM_Core_Exception
6a488035
TO
195 * @static
196 */
00be9182 197 public static function del($membershipStatusId) {
6a488035
TO
198 //check dependencies
199 //checking if membership status is present in some other table
200 $check = FALSE;
201
202 $dependancy = array('Membership', 'MembershipLog');
203 foreach ($dependancy as $name) {
4d5c2eb5 204 $baoString = 'CRM_Member_BAO_' . $name;
205 $dao = new $baoString();
6a488035
TO
206 $dao->status_id = $membershipStatusId;
207 if ($dao->find(TRUE)) {
dcc4f6a7 208 throw new CRM_Core_Exception(ts('This membership status cannot be deleted as memberships exist with this status'));
6a488035
TO
209 }
210 }
dcc4f6a7 211 CRM_Utils_Weight::delWeight('CRM_Member_DAO_MembershipStatus', $membershipStatusId);
6a488035
TO
212 //delete from membership Type table
213 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
214 $membershipStatus->id = $membershipStatusId;
215 $membershipStatus->delete();
216 $membershipStatus->free();
217 }
218
219 /**
100fef9d 220 * Find the membership status based on start date, end date, join date & status date.
6a488035 221 *
b2363ea8
TO
222 * @param string $startDate
223 * Start date of the member whose membership status is to be calculated.
224 * @param string $endDate
225 * End date of the member whose membership status is to be calculated.
226 * @param string $joinDate
227 * Join date of the member whose membership status is to be calculated.
da6b46f4 228 * @param \date|string $statusDate status date of the member whose membership status is to be calculated.
b09fe5ed 229 * @param bool $excludeIsAdminExclude the statuses those having is_admin = 1.
b2363ea8 230 * Exclude the statuses those having is_admin = 1.
c490a46a 231 * @param int $membershipTypeID
b2363ea8
TO
232 * @param array $membership
233 * Membership params as available to calling function - passed to the hook.
6a488035 234 *
da6b46f4 235 * @return array
d60f50a8 236 * @static
6a488035 237 */
500cfe81
TO
238 static function getMembershipStatusByDate(
239 $startDate, $endDate, $joinDate,
5f11bbcc 240 $statusDate = 'today', $excludeIsAdmin = FALSE, $membershipTypeID, $membership = array()
6a488035
TO
241 ) {
242 $membershipDetails = array();
5f11bbcc 243
6a488035 244 if (!$statusDate || $statusDate == 'today') {
b09fe5ed 245 $statusDate = getdate();
6a488035
TO
246 $statusDate = date('Ymd',
247 mktime($statusDate['hours'],
248 $statusDate['minutes'],
249 $statusDate['seconds'],
250 $statusDate['mon'],
251 $statusDate['mday'],
252 $statusDate['year']
253 )
254 );
255 }
256 else {
257 $statusDate = CRM_Utils_Date::customFormat($statusDate, '%Y%m%d');
258 }
259
6a488035
TO
260 $dates = array('start', 'end', 'join');
261 $events = array('start', 'end');
262
263 foreach ($dates as $dat) {
a0713e70 264 if (${$dat . 'Date'} && ${$dat . 'Date'} != "null") {
265 ${$dat . 'Date'} = CRM_Utils_Date::customFormat(${$dat . 'Date'}, '%Y%m%d');
266
6a488035
TO
267 ${$dat . 'Year'} = substr(${$dat . 'Date'}, 0, 4);
268
269 ${$dat . 'Month'} = substr(${$dat . 'Date'}, 4, 2);
270
271 ${$dat . 'Day'} = substr(${$dat . 'Date'}, 6, 2);
272 }
a0713e70 273 else {
274 ${$dat . 'Date'} = '';
275 }
6a488035
TO
276 }
277
278 //fix for CRM-3570, if we have statuses with is_admin=1,
279 //exclude these statuses from calculatation during import.
280 $where = "is_active = 1";
281 if ($excludeIsAdmin) {
282 $where .= " AND is_admin != 1";
283 }
284
285 $query = "
286 SELECT *
287 FROM civicrm_membership_status
288 WHERE {$where}
289 ORDER BY weight ASC";
290
291 $membershipStatus = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
292 $hour = $minute = $second = 0;
293
294 while ($membershipStatus->fetch()) {
295 $startEvent = NULL;
296 $endEvent = NULL;
297 foreach ($events as $eve) {
298 foreach ($dates as $dat) {
299 // calculate start-event/date and end-event/date
300 if (($membershipStatus->{$eve . '_event'} == $dat . '_date') &&
301 ${$dat . 'Date'}
302 ) {
303 if ($membershipStatus->{$eve . '_event_adjust_unit'} &&
304 $membershipStatus->{$eve . '_event_adjust_interval'}
305 ) {
306 // add in months
307 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'month') {
308 ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
353ffa53
TO
309 ${$dat . 'Month'} + $membershipStatus->{$eve . '_event_adjust_interval'},
310 ${$dat . 'Day'},
311 ${$dat . 'Year'}
b09fe5ed 312 ));
6a488035
TO
313 }
314 // add in days
315 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'day') {
316 ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
353ffa53
TO
317 ${$dat . 'Month'},
318 ${$dat . 'Day'} + $membershipStatus->{$eve . '_event_adjust_interval'},
319 ${$dat . 'Year'}
b09fe5ed 320 ));
6a488035
TO
321 }
322 // add in years
323 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'year') {
324 ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
353ffa53
TO
325 ${$dat . 'Month'},
326 ${$dat . 'Day'},
327 ${$dat . 'Year'} + $membershipStatus->{$eve . '_event_adjust_interval'}
b09fe5ed 328 ));
6a488035
TO
329 }
330 // if no interval and unit, present
331 }
332 else {
333 ${$eve . 'Event'} = ${$dat . 'Date'};
334 }
335 }
336 }
337 }
338
339 // check if statusDate is in the range of start & end events.
340 if ($startEvent && $endEvent) {
341 if (($statusDate >= $startEvent) && ($statusDate <= $endEvent)) {
342 $membershipDetails['id'] = $membershipStatus->id;
343 $membershipDetails['name'] = $membershipStatus->name;
344 }
345 }
346 elseif ($startEvent) {
347 if ($statusDate >= $startEvent) {
348 $membershipDetails['id'] = $membershipStatus->id;
349 $membershipDetails['name'] = $membershipStatus->name;
350 }
351 }
352 elseif ($endEvent) {
353 if ($statusDate <= $endEvent) {
354 $membershipDetails['id'] = $membershipStatus->id;
355 $membershipDetails['name'] = $membershipStatus->name;
356 }
357 }
358
359 // returns FIRST status record for which status_date is in range.
360 if ($membershipDetails) {
5f11bbcc 361 break;
6a488035
TO
362 }
363 }
364 //end fetch
365
366 $membershipStatus->free();
5f11bbcc
EM
367
368 //we bundle the arguments into an array as we can't pass 8 variables to the hook otherwise
369 // the membership array might contain the pre-altered settings so we don't want to merge this
370 $arguments = array(
371 'start_date' => $startDate,
372 'end_date' => $endDate,
373 'join_date' => $joinDate,
374 'status_date' => $statusDate,
375 'exclude_is_admin' => $endDate,
376 'membership_type_id' => $membershipTypeID,
377 'start_event' => $startEvent,
378 'end_event' => $endEvent,
379 );
380 CRM_Utils_Hook::alterCalculatedMembershipStatus($membershipDetails, $arguments, $membership);
6a488035
TO
381 return $membershipDetails;
382 }
383
384 /**
385 * Function that return the status ids whose is_current_member is set
386 *
77b97be7 387 * @return array
d60f50a8 388 * @static
6a488035
TO
389 */
390 public static function getMembershipStatusCurrent() {
391 $statusIds = array();
392 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
393 $membershipStatus->is_current_member = 1;
394 $membershipStatus->find();
395 $membershipStatus->selectAdd();
396 $membershipStatus->selectAdd('id');
397 while ($membershipStatus->fetch()) {
398 $statusIds[] = $membershipStatus->id;
399 }
400 $membershipStatus->free();
401 return $statusIds;
402 }
403}