3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
35 class CRM_Member_BAO_MembershipStatus
extends CRM_Member_DAO_MembershipStatus
{
38 * Static holder for the default LT
40 static $_defaultMembershipStatus = NULL;
45 function __construct() {
46 parent
::__construct();
50 * Fetch object based on array of properties
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
55 * @return CRM_Member_BAO_MembershipStatus object
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;
70 * Update the is_active flag in the db
72 * @param int $id id of the database record
73 * @param boolean $is_active value we want to set the is_active field
75 * @return Object DAO object on sucess, null otherwise
78 static function setIsActive($id, $is_active) {
79 return CRM_Core_DAO
::setFieldValue('CRM_Member_DAO_MembershipStatus', $id, 'is_active', $is_active);
83 * Takes an associative array and creates a membership Status object
84 * See http://wiki.civicrm.org/confluence/display/CRM/Database+layer
86 * @param array $params (reference ) an assoc array of name/value pairs
89 * @return CRM_Member_BAO_MembershipStatus object
93 static function create($params){
95 if(!empty($params['id'])){
96 $ids['membershipStatus'] = $params['id'];
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.');
106 $membershipStatusBAO = CRM_Member_BAO_MembershipStatus
::add($params, $ids);
107 return $membershipStatusBAO;
110 * Add the membership types
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
120 static function add(&$params, $ids = array()) {
121 $id = CRM_Utils_Array
::value('id', $params, CRM_Utils_Array
::value('membershipStatus', $ids));
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'];
129 if (empty($params['name']) && !empty($params['label'])) {
130 $params['name'] = $params['label'];
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
144 // action is taken depending upon the mode
145 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
146 $membershipStatus->copyValues($params);
148 $membershipStatus->id
= $id;
150 $membershipStatus->save();
151 return $membershipStatus;
155 * Get defaults for new entity
158 static function getDefaults() {
160 'is_active' => FALSE,
161 'is_current_member' => FALSE,
163 'is_default' => FALSE,
168 * Get membership status
170 * @param int $membershipStatusId
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);
182 return $statusDetails;
186 * Delete membership Types
188 * @param int $membershipStatusId
190 * @throws CRM_Core_Exception
193 static function del($membershipStatusId) {
195 //checking if membership status is present in some other table
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'));
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();
216 * Find the membership status based on start date, end date, join date & status date.
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
229 static function getMembershipStatusByDate($startDate, $endDate, $joinDate,
230 $statusDate = 'today', $excludeIsAdmin = FALSE, $membershipTypeID, $membership = array()
232 $membershipDetails = array();
234 if (!$statusDate ||
$statusDate == 'today') {
235 $statusDate = getDate();
236 $statusDate = date('Ymd',
237 mktime($statusDate['hours'],
238 $statusDate['minutes'],
239 $statusDate['seconds'],
247 $statusDate = CRM_Utils_Date
::customFormat($statusDate, '%Y%m%d');
250 $dates = array('start', 'end', 'join');
251 $events = array('start', 'end');
253 foreach ($dates as $dat) {
254 if ($
{$dat . 'Date'} && $
{$dat . 'Date'} != "null") {
255 $
{$dat . 'Date'} = CRM_Utils_Date
::customFormat($
{$dat . 'Date'}, '%Y%m%d');
257 $
{$dat . 'Year'} = substr($
{$dat . 'Date'}, 0, 4);
259 $
{$dat . 'Month'} = substr($
{$dat . 'Date'}, 4, 2);
261 $
{$dat . 'Day'} = substr($
{$dat . 'Date'}, 6, 2);
264 $
{$dat . 'Date'} = '';
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";
277 FROM civicrm_membership_status
279 ORDER BY weight ASC";
281 $membershipStatus = CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
282 $hour = $minute = $second = 0;
284 while ($membershipStatus->fetch()) {
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') &&
293 if ($membershipStatus->{$eve . '_event_adjust_unit'} &&
294 $membershipStatus->{$eve . '_event_adjust_interval'}
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'},
305 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'day') {
306 $
{$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
308 $
{$dat . 'Day'} +
$membershipStatus->{$eve . '_event_adjust_interval'},
313 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'year') {
314 $
{$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
317 $
{$dat . 'Year'} +
$membershipStatus->{$eve . '_event_adjust_interval'}
320 // if no interval and unit, present
323 $
{$eve . 'Event'} = $
{$dat . 'Date'};
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
;
336 elseif ($startEvent) {
337 if ($statusDate >= $startEvent) {
338 $membershipDetails['id'] = $membershipStatus->id
;
339 $membershipDetails['name'] = $membershipStatus->name
;
343 if ($statusDate <= $endEvent) {
344 $membershipDetails['id'] = $membershipStatus->id
;
345 $membershipDetails['name'] = $membershipStatus->name
;
349 // returns FIRST status record for which status_date is in range.
350 if ($membershipDetails) {
356 $membershipStatus->free();
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
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,
370 CRM_Utils_Hook
::alterCalculatedMembershipStatus($membershipDetails, $arguments, $membership);
371 return $membershipDetails;
375 * Function that return the status ids whose is_current_member is set
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
;
390 $membershipStatus->free();