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