Merge pull request #4887 from pratikshad/broken-webtest
[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 public function __construct() {
46 parent::__construct();
47 }
48
49 /**
50 * Fetch object based on array of properties
51 *
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.
56 *
57 * @return CRM_Member_BAO_MembershipStatus
58 * @static
59 */
60 public static function retrieve(&$params, &$defaults) {
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 /**
71 * Update the is_active flag in the db
72 *
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.
77 *
78 * @return Object
79 * DAO object on sucess, null otherwise
80 * @static
81 */
82 public 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 *
90 * @param array $params
91 * (reference ) an assoc array of name/value pairs.
92 *
93 * @throws Exception
94 * @return CRM_Member_BAO_MembershipStatus
95 * @static
96 */
97 public static function create($params) {
98 $ids = array();
99 if (!empty($params['id'])) {
100 $ids['membershipStatus'] = $params['id'];
101 }
102 else {
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 }
113
114 /**
115 * Add the membership types
116 *
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.
121 *
122 * @static
123 *
124 * @return object
125 */
126 public static function add(&$params, $ids = array()) {
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 }
139
140 // set all other defaults to false.
141 if (!empty($params['is_default'])) {
142 $query = "UPDATE civicrm_membership_status SET is_default = 0";
143 CRM_Core_DAO::executeQuery($query,
144 CRM_Core_DAO::$_nullArray
145 );
146 }
147
148 // action is taken depending upon the mode
149 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
150 $membershipStatus->copyValues($params);
151
152 $membershipStatus->id = $id;
153
154 $membershipStatus->save();
155 return $membershipStatus;
156 }
157
158 /**
159 * Get defaults for new entity
160 * @return array
161 */
162 public static function getDefaults() {
163 return array(
164 'is_active' => FALSE,
165 'is_current_member' => FALSE,
166 'is_admin' => FALSE,
167 'is_default' => FALSE,
168 );
169 }
170
171 /**
172 * Get membership status
173 *
174 * @param int $membershipStatusId
175 *
176 * @return array
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 /**
190 * Delete membership Types
191 *
192 * @param int $membershipStatusId
193 *
194 * @throws CRM_Core_Exception
195 * @static
196 */
197 public static function del($membershipStatusId) {
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) {
204 $baoString = 'CRM_Member_BAO_' . $name;
205 $dao = new $baoString();
206 $dao->status_id = $membershipStatusId;
207 if ($dao->find(TRUE)) {
208 throw new CRM_Core_Exception(ts('This membership status cannot be deleted as memberships exist with this status'));
209 }
210 }
211 CRM_Utils_Weight::delWeight('CRM_Member_DAO_MembershipStatus', $membershipStatusId);
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 /**
220 * Find the membership status based on start date, end date, join date & status date.
221 *
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.
228 * @param \date|string $statusDate status date of the member whose membership status is to be calculated.
229 * @param bool $excludeIsAdminExclude the statuses those having is_admin = 1.
230 * Exclude the statuses those having is_admin = 1.
231 * @param int $membershipTypeID
232 * @param array $membership
233 * Membership params as available to calling function - passed to the hook.
234 *
235 * @return array
236 * @static
237 */
238 static function getMembershipStatusByDate(
239 $startDate, $endDate, $joinDate,
240 $statusDate = 'today', $excludeIsAdmin = FALSE, $membershipTypeID, $membership = array()
241 ) {
242 $membershipDetails = array();
243
244 if (!$statusDate || $statusDate == 'today') {
245 $statusDate = getdate();
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
260 $dates = array('start', 'end', 'join');
261 $events = array('start', 'end');
262
263 foreach ($dates as $dat) {
264 if (${$dat . 'Date'} && ${$dat . 'Date'} != "null") {
265 ${$dat . 'Date'} = CRM_Utils_Date::customFormat(${$dat . 'Date'}, '%Y%m%d');
266
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 }
273 else {
274 ${$dat . 'Date'} = '';
275 }
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,
309 ${$dat . 'Month'} + $membershipStatus->{$eve . '_event_adjust_interval'},
310 ${$dat . 'Day'},
311 ${$dat . 'Year'}
312 ));
313 }
314 // add in days
315 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'day') {
316 ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
317 ${$dat . 'Month'},
318 ${$dat . 'Day'} + $membershipStatus->{$eve . '_event_adjust_interval'},
319 ${$dat . 'Year'}
320 ));
321 }
322 // add in years
323 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'year') {
324 ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
325 ${$dat . 'Month'},
326 ${$dat . 'Day'},
327 ${$dat . 'Year'} + $membershipStatus->{$eve . '_event_adjust_interval'}
328 ));
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) {
361 break;
362 }
363 }
364 //end fetch
365
366 $membershipStatus->free();
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);
381 return $membershipDetails;
382 }
383
384 /**
385 * Function that return the status ids whose is_current_member is set
386 *
387 * @return array
388 * @static
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 }