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