Import from SVN (r45945, r596)
[civicrm-core.git] / CRM / Member / BAO / MembershipStatus.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35class 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
89 * @param array $params (reference ) an assoc array of name/value pairs
90 *
91 * @return object CRM_Member_BAO_MembershipStatus object
92 * @access public
93 * @static
94 */
95 static function create($params){
96 $ids = array();
97 if(!empty($params['id'])){
98 $ids['membershipStatus'] = $params['id'];
99 }
100 else{
101 //don't allow duplicate names - if id not set
102 $status = new CRM_Member_DAO_MembershipStatus();
103 $status->name = $params['name'];
104 if ($status->find(TRUE)) {
105 throw new Exception('A membership status with this name already exists.');
106 }
107 }
108 $membershipStatusBAO = CRM_Member_BAO_MembershipStatus::add($params, $ids);
109 return $membershipStatusBAO;
110 }
111 /**
112 * function to add the membership types
113 *
114 * @param array $params reference array contains the values submitted by the form
115 * @param array $ids reference array contains the id
116 *
117 * @access public
118 * @static
119 *
120 * @return object
121 */
122 static function add(&$params, &$ids) {
123 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
124 $params['is_current_member'] = CRM_Utils_Array::value('is_current_member', $params, FALSE);
125 $params['is_admin'] = CRM_Utils_Array::value('is_admin', $params, FALSE);
126 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
127
128 // set all other defaults to false.
129 if ($params['is_default']) {
130 $query = "UPDATE civicrm_membership_status SET is_default = 0";
131 CRM_Core_DAO::executeQuery($query,
132 CRM_Core_DAO::$_nullArray
133 );
134 }
135
136 //copy name to label when not passed.
137 if (!CRM_Utils_Array::value('label', $params) &&
138 CRM_Utils_Array::value('name', $params)
139 ) {
140 $params['label'] = $params['name'];
141 }
142
143 //for add mode, copy label to name.
144 $statusId = CRM_Utils_Array::value('membershipStatus', $ids);
145 if (!$statusId &&
146 CRM_Utils_Array::value('label', $params) &&
147 !CRM_Utils_Array::value('name', $params)
148 ) {
149 $params['name'] = $params['label'];
150 }
151
152 // action is taken depending upon the mode
153 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
154 $membershipStatus->copyValues($params);
155
156 $membershipStatus->id = $statusId;
157
158 $membershipStatus->save();
159 return $membershipStatus;
160 }
161
162 /**
163 * Function to get membership status
164 *
165 * @param int $membershipStatusId
166 * @static
167 */
168 public static function getMembershipStatus($membershipStatusId) {
169 $statusDetails = array();
170 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
171 $membershipStatus->id = $membershipStatusId;
172 if ($membershipStatus->find(TRUE)) {
173 CRM_Core_DAO::storeValues($membershipStatus, $statusDetails);
174 }
175 return $statusDetails;
176 }
177
178 /**
179 * Function to delete membership Types
180 *
181 * @param int $membershipStatusId
182 * @param
183 * @static
184 */
185 static function del($membershipStatusId, $skipRedirect = FALSE) {
186 //check dependencies
187 //checking if membership status is present in some other table
188 $check = FALSE;
189
190 $dependancy = array('Membership', 'MembershipLog');
191 foreach ($dependancy as $name) {
192 require_once (str_replace('_', DIRECTORY_SEPARATOR, "CRM_Member_BAO_" . $name) . ".php");
193 eval('$dao = new CRM_Member_BAO_' . $name . '();');
194 $dao->status_id = $membershipStatusId;
195 if ($dao->find(TRUE)) {
196 $check = TRUE;
197 }
198 }
199
200 if ($check) {
201 if (!$skipRedirect) {
202 $session = CRM_Core_Session::singleton();
203 CRM_Core_Session::setStatus(ts('This membership status cannot be deleted'), ts('Deletion Error'), 'error');
204 return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/member/membershipStatus', "reset=1"));
205 }
206
207 // Return the error message to the api
208 $error = array();
209 $error['is_error'] = 1;
210 //don't translate as api error message are not translated
211 $error['error_message'] = 'The membership status cannot be deleted as memberships of this status exist';
212 return $error;
213 }
214
215 //delete from membership Type table
216 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
217 $membershipStatus->id = $membershipStatusId;
218 $membershipStatus->delete();
219 $membershipStatus->free();
220 }
221
222 /**
223 * Function to find the membership status based on start date, end date, join date & status date.
224 *
225 * @param date $startDate start date of the member whose membership status is to be calculated.
226 * @param date $endDate end date of the member whose membership status is to be calculated.
227 * @param date $joinDate join date of the member whose membership status is to be calculated.
228 * @param date $statusDate status date of the member whose membership status is to be calculated.
229 * @param boolean $excludeIsAdmin exclude the statuses those having is_admin = 1
230 *
231 * @return
232 * @static
233 */
234 static function getMembershipStatusByDate($startDate, $endDate, $joinDate,
235 $statusDate = 'today', $excludeIsAdmin = FALSE
236 ) {
237 $membershipDetails = array();
238 if (!$statusDate || $statusDate == 'today') {
239 $statusDate = getDate();
240 $statusDate = date('Ymd',
241 mktime($statusDate['hours'],
242 $statusDate['minutes'],
243 $statusDate['seconds'],
244 $statusDate['mon'],
245 $statusDate['mday'],
246 $statusDate['year']
247 )
248 );
249 }
250 else {
251 $statusDate = CRM_Utils_Date::customFormat($statusDate, '%Y%m%d');
252 }
253
254 $startDate = CRM_Utils_Date::customFormat($startDate, '%Y%m%d');
255 $endDate = CRM_Utils_Date::customFormat($endDate, '%Y%m%d');
256 $joinDate = CRM_Utils_Date::customFormat($joinDate, '%Y%m%d');
257
258 $dates = array('start', 'end', 'join');
259 $events = array('start', 'end');
260
261 foreach ($dates as $dat) {
262 if (${$dat . 'Date'}) {
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 }
269 }
270
271 //fix for CRM-3570, if we have statuses with is_admin=1,
272 //exclude these statuses from calculatation during import.
273 $where = "is_active = 1";
274 if ($excludeIsAdmin) {
275 $where .= " AND is_admin != 1";
276 }
277
278 $query = "
279 SELECT *
280 FROM civicrm_membership_status
281 WHERE {$where}
282 ORDER BY weight ASC";
283
284 $membershipStatus = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
285 $hour = $minute = $second = 0;
286
287 while ($membershipStatus->fetch()) {
288 $startEvent = NULL;
289 $endEvent = NULL;
290 foreach ($events as $eve) {
291 foreach ($dates as $dat) {
292 // calculate start-event/date and end-event/date
293 if (($membershipStatus->{$eve . '_event'} == $dat . '_date') &&
294 ${$dat . 'Date'}
295 ) {
296 if ($membershipStatus->{$eve . '_event_adjust_unit'} &&
297 $membershipStatus->{$eve . '_event_adjust_interval'}
298 ) {
299 // add in months
300 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'month') {
301 ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
302 ${$dat . 'Month'} + $membershipStatus->{$eve . '_event_adjust_interval'},
303 ${$dat . 'Day'},
304 ${$dat . 'Year'}
305 ));
306 }
307 // add in days
308 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'day') {
309 ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
310 ${$dat . 'Month'},
311 ${$dat . 'Day'} + $membershipStatus->{$eve . '_event_adjust_interval'},
312 ${$dat . 'Year'}
313 ));
314 }
315 // add in years
316 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'year') {
317 ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
318 ${$dat . 'Month'},
319 ${$dat . 'Day'},
320 ${$dat . 'Year'} + $membershipStatus->{$eve . '_event_adjust_interval'}
321 ));
322 }
323 // if no interval and unit, present
324 }
325 else {
326 ${$eve . 'Event'} = ${$dat . 'Date'};
327 }
328 }
329 }
330 }
331
332 // check if statusDate is in the range of start & end events.
333 if ($startEvent && $endEvent) {
334 if (($statusDate >= $startEvent) && ($statusDate <= $endEvent)) {
335 $membershipDetails['id'] = $membershipStatus->id;
336 $membershipDetails['name'] = $membershipStatus->name;
337 }
338 }
339 elseif ($startEvent) {
340 if ($statusDate >= $startEvent) {
341 $membershipDetails['id'] = $membershipStatus->id;
342 $membershipDetails['name'] = $membershipStatus->name;
343 }
344 }
345 elseif ($endEvent) {
346 if ($statusDate <= $endEvent) {
347 $membershipDetails['id'] = $membershipStatus->id;
348 $membershipDetails['name'] = $membershipStatus->name;
349 }
350 }
351
352 // returns FIRST status record for which status_date is in range.
353 if ($membershipDetails) {
354 $membershipStatus->free();
355 return $membershipDetails;
356 }
357 }
358 //end fetch
359
360 $membershipStatus->free();
361 return $membershipDetails;
362 }
363
364 /**
365 * Function that return the status ids whose is_current_member is set
366 *
367 * @return
368 * @static
369 */
370 public static function getMembershipStatusCurrent() {
371 $statusIds = array();
372 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
373 $membershipStatus->is_current_member = 1;
374 $membershipStatus->find();
375 $membershipStatus->selectAdd();
376 $membershipStatus->selectAdd('id');
377 while ($membershipStatus->fetch()) {
378 $statusIds[] = $membershipStatus->id;
379 }
380 $membershipStatus->free();
381 return $statusIds;
382 }
383}
384