INFRA-132 - Remove white space after an opening "(" or before a closing ")"
[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
TO
77 *
78 * @return Object DAO object on sucess, null otherwise
79 * @static
80 */
00be9182 81 public static function setIsActive($id, $is_active) {
6a488035
TO
82 return CRM_Core_DAO::setFieldValue('CRM_Member_DAO_MembershipStatus', $id, 'is_active', $is_active);
83 }
84
85 /**
86 * Takes an associative array and creates a membership Status object
87 * See http://wiki.civicrm.org/confluence/display/CRM/Database+layer
6a488035 88 *
b2363ea8
TO
89 * @param array $params
90 * (reference ) an assoc array of name/value pairs.
d9f93da9
EM
91 *
92 * @throws Exception
c490a46a 93 * @return CRM_Member_BAO_MembershipStatus object
6a488035
TO
94 * @static
95 */
9b873358 96 public static function create($params) {
6a488035 97 $ids = array();
9b873358 98 if (!empty($params['id'])) {
6a488035
TO
99 $ids['membershipStatus'] = $params['id'];
100 }
101 else{
102 //don't allow duplicate names - if id not set
103 $status = new CRM_Member_DAO_MembershipStatus();
104 $status->name = $params['name'];
105 if ($status->find(TRUE)) {
106 throw new Exception('A membership status with this name already exists.');
107 }
108 }
109 $membershipStatusBAO = CRM_Member_BAO_MembershipStatus::add($params, $ids);
110 return $membershipStatusBAO;
111 }
112 /**
100fef9d 113 * Add the membership types
6a488035 114 *
b2363ea8
TO
115 * @param array $params
116 * Reference array contains the values submitted by the form.
117 * @param array $ids
118 * Array contains the id - this param is deprecated.
6a488035 119 *
6a488035
TO
120 * @static
121 *
122 * @return object
123 */
00be9182 124 public 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
TO
146 // action is taken depending upon the mode
147 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
148 $membershipStatus->copyValues($params);
149
64d24a64 150 $membershipStatus->id = $id;
6a488035
TO
151
152 $membershipStatus->save();
153 return $membershipStatus;
154 }
155
64d24a64
EM
156 /**
157 * Get defaults for new entity
158 * @return array
159 */
00be9182 160 public static function getDefaults() {
64d24a64
EM
161 return array(
162 'is_active' => FALSE,
163 'is_current_member' => FALSE,
164 'is_admin' => FALSE,
165 'is_default' => FALSE,
166 );
167 }
168
6a488035 169 /**
100fef9d 170 * Get membership status
6a488035
TO
171 *
172 * @param int $membershipStatusId
d9f93da9
EM
173 *
174 * @return array
6a488035
TO
175 * @static
176 */
177 public static function getMembershipStatus($membershipStatusId) {
178 $statusDetails = array();
179 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
180 $membershipStatus->id = $membershipStatusId;
181 if ($membershipStatus->find(TRUE)) {
182 CRM_Core_DAO::storeValues($membershipStatus, $statusDetails);
183 }
184 return $statusDetails;
185 }
186
187 /**
100fef9d 188 * Delete membership Types
6a488035
TO
189 *
190 * @param int $membershipStatusId
77b97be7
EM
191 *
192 * @throws CRM_Core_Exception
6a488035
TO
193 * @static
194 */
00be9182 195 public static function del($membershipStatusId) {
6a488035
TO
196 //check dependencies
197 //checking if membership status is present in some other table
198 $check = FALSE;
199
200 $dependancy = array('Membership', 'MembershipLog');
201 foreach ($dependancy as $name) {
4d5c2eb5 202 $baoString = 'CRM_Member_BAO_' . $name;
203 $dao = new $baoString();
6a488035
TO
204 $dao->status_id = $membershipStatusId;
205 if ($dao->find(TRUE)) {
dcc4f6a7 206 throw new CRM_Core_Exception(ts('This membership status cannot be deleted as memberships exist with this status'));
6a488035
TO
207 }
208 }
dcc4f6a7 209 CRM_Utils_Weight::delWeight('CRM_Member_DAO_MembershipStatus', $membershipStatusId);
6a488035
TO
210 //delete from membership Type table
211 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
212 $membershipStatus->id = $membershipStatusId;
213 $membershipStatus->delete();
214 $membershipStatus->free();
215 }
216
217 /**
100fef9d 218 * Find the membership status based on start date, end date, join date & status date.
6a488035 219 *
b2363ea8
TO
220 * @param string $startDate
221 * Start date of the member whose membership status is to be calculated.
222 * @param string $endDate
223 * End date of the member whose membership status is to be calculated.
224 * @param string $joinDate
225 * 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.
b09fe5ed 227 * @param bool $excludeIsAdminExclude the statuses those having is_admin = 1.
b2363ea8 228 * Exclude the statuses those having is_admin = 1.
c490a46a 229 * @param int $membershipTypeID
b2363ea8
TO
230 * @param array $membership
231 * Membership params as available to calling function - passed to the hook.
6a488035 232 *
da6b46f4
EM
233 * @return array
234 @static
6a488035 235 */
500cfe81
TO
236 static function getMembershipStatusByDate(
237 $startDate, $endDate, $joinDate,
5f11bbcc 238 $statusDate = 'today', $excludeIsAdmin = FALSE, $membershipTypeID, $membership = array()
6a488035
TO
239 ) {
240 $membershipDetails = array();
5f11bbcc 241
6a488035 242 if (!$statusDate || $statusDate == 'today') {
b09fe5ed 243 $statusDate = getdate();
6a488035
TO
244 $statusDate = date('Ymd',
245 mktime($statusDate['hours'],
246 $statusDate['minutes'],
247 $statusDate['seconds'],
248 $statusDate['mon'],
249 $statusDate['mday'],
250 $statusDate['year']
251 )
252 );
253 }
254 else {
255 $statusDate = CRM_Utils_Date::customFormat($statusDate, '%Y%m%d');
256 }
257
6a488035
TO
258 $dates = array('start', 'end', 'join');
259 $events = array('start', 'end');
260
261 foreach ($dates as $dat) {
a0713e70 262 if (${$dat . 'Date'} && ${$dat . 'Date'} != "null") {
263 ${$dat . 'Date'} = CRM_Utils_Date::customFormat(${$dat . 'Date'}, '%Y%m%d');
264
6a488035
TO
265 ${$dat . 'Year'} = substr(${$dat . 'Date'}, 0, 4);
266
267 ${$dat . 'Month'} = substr(${$dat . 'Date'}, 4, 2);
268
269 ${$dat . 'Day'} = substr(${$dat . 'Date'}, 6, 2);
270 }
a0713e70 271 else {
272 ${$dat . 'Date'} = '';
273 }
6a488035
TO
274 }
275
276 //fix for CRM-3570, if we have statuses with is_admin=1,
277 //exclude these statuses from calculatation during import.
278 $where = "is_active = 1";
279 if ($excludeIsAdmin) {
280 $where .= " AND is_admin != 1";
281 }
282
283 $query = "
284 SELECT *
285 FROM civicrm_membership_status
286 WHERE {$where}
287 ORDER BY weight ASC";
288
289 $membershipStatus = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
290 $hour = $minute = $second = 0;
291
292 while ($membershipStatus->fetch()) {
293 $startEvent = NULL;
294 $endEvent = NULL;
295 foreach ($events as $eve) {
296 foreach ($dates as $dat) {
297 // calculate start-event/date and end-event/date
298 if (($membershipStatus->{$eve . '_event'} == $dat . '_date') &&
299 ${$dat . 'Date'}
300 ) {
301 if ($membershipStatus->{$eve . '_event_adjust_unit'} &&
302 $membershipStatus->{$eve . '_event_adjust_interval'}
303 ) {
304 // add in months
305 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'month') {
306 ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
307 ${$dat . 'Month'} + $membershipStatus->{$eve . '_event_adjust_interval'},
308 ${$dat . 'Day'},
309 ${$dat . 'Year'}
b09fe5ed 310 ));
6a488035
TO
311 }
312 // add in days
313 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'day') {
314 ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
315 ${$dat . 'Month'},
316 ${$dat . 'Day'} + $membershipStatus->{$eve . '_event_adjust_interval'},
317 ${$dat . 'Year'}
b09fe5ed 318 ));
6a488035
TO
319 }
320 // add in years
321 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'year') {
322 ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
323 ${$dat . 'Month'},
324 ${$dat . 'Day'},
325 ${$dat . 'Year'} + $membershipStatus->{$eve . '_event_adjust_interval'}
b09fe5ed 326 ));
6a488035
TO
327 }
328 // if no interval and unit, present
329 }
330 else {
331 ${$eve . 'Event'} = ${$dat . 'Date'};
332 }
333 }
334 }
335 }
336
337 // check if statusDate is in the range of start & end events.
338 if ($startEvent && $endEvent) {
339 if (($statusDate >= $startEvent) && ($statusDate <= $endEvent)) {
340 $membershipDetails['id'] = $membershipStatus->id;
341 $membershipDetails['name'] = $membershipStatus->name;
342 }
343 }
344 elseif ($startEvent) {
345 if ($statusDate >= $startEvent) {
346 $membershipDetails['id'] = $membershipStatus->id;
347 $membershipDetails['name'] = $membershipStatus->name;
348 }
349 }
350 elseif ($endEvent) {
351 if ($statusDate <= $endEvent) {
352 $membershipDetails['id'] = $membershipStatus->id;
353 $membershipDetails['name'] = $membershipStatus->name;
354 }
355 }
356
357 // returns FIRST status record for which status_date is in range.
358 if ($membershipDetails) {
5f11bbcc 359 break;
6a488035
TO
360 }
361 }
362 //end fetch
363
364 $membershipStatus->free();
5f11bbcc
EM
365
366 //we bundle the arguments into an array as we can't pass 8 variables to the hook otherwise
367 // the membership array might contain the pre-altered settings so we don't want to merge this
368 $arguments = array(
369 'start_date' => $startDate,
370 'end_date' => $endDate,
371 'join_date' => $joinDate,
372 'status_date' => $statusDate,
373 'exclude_is_admin' => $endDate,
374 'membership_type_id' => $membershipTypeID,
375 'start_event' => $startEvent,
376 'end_event' => $endEvent,
377 );
378 CRM_Utils_Hook::alterCalculatedMembershipStatus($membershipDetails, $arguments, $membership);
6a488035
TO
379 return $membershipDetails;
380 }
381
382 /**
383 * Function that return the status ids whose is_current_member is set
384 *
77b97be7
EM
385 * @return array
386 @static
6a488035
TO
387 */
388 public static function getMembershipStatusCurrent() {
389 $statusIds = array();
390 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
391 $membershipStatus->is_current_member = 1;
392 $membershipStatus->find();
393 $membershipStatus->selectAdd();
394 $membershipStatus->selectAdd('id');
395 while ($membershipStatus->fetch()) {
396 $statusIds[] = $membershipStatus->id;
397 }
398 $membershipStatus->free();
399 return $statusIds;
400 }
401}