3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 * 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
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
59 * @return object CRM_Member_BAO_MembershipStatus object
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;
74 * update the is_active flag in the db
76 * @param int $id id of the database record
77 * @param boolean $is_active value we want to set the is_active field
79 * @return Object DAO object on sucess, null otherwise
82 static function setIsActive($id, $is_active) {
83 return CRM_Core_DAO
::setFieldValue('CRM_Member_DAO_MembershipStatus', $id, 'is_active', $is_active);
87 * Takes an associative array and creates a membership Status object
88 * See http://wiki.civicrm.org/confluence/display/CRM/Database+layer
90 * @param array $params (reference ) an assoc array of name/value pairs
93 * @return object CRM_Member_BAO_MembershipStatus object
97 static function create($params){
99 if(!empty($params['id'])){
100 $ids['membershipStatus'] = $params['id'];
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.');
110 $membershipStatusBAO = CRM_Member_BAO_MembershipStatus
::add($params, $ids);
111 return $membershipStatusBAO;
114 * function to add the membership types
116 * @param array $params reference array contains the values submitted by the form
117 * @param array $ids array contains the id - this param is deprecated
124 static function add(&$params, $ids = array()) {
125 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
126 $params['is_current_member'] = CRM_Utils_Array
::value('is_current_member', $params, FALSE);
127 $params['is_admin'] = CRM_Utils_Array
::value('is_admin', $params, FALSE);
128 $params['is_default'] = CRM_Utils_Array
::value('is_default', $params, FALSE);
130 // set all other defaults to false.
131 if ($params['is_default']) {
132 $query = "UPDATE civicrm_membership_status SET is_default = 0";
133 CRM_Core_DAO
::executeQuery($query,
134 CRM_Core_DAO
::$_nullArray
138 //copy name to label when not passed.
139 if (empty($params['label']) && !empty($params['name'])) {
140 $params['label'] = $params['name'];
143 //for add mode, copy label to name.
144 $statusId = !empty($params['id']) ?
$params['id'] : CRM_Utils_Array
::value('membershipStatus', $ids);
145 if (!$statusId && !empty($params['label']) && empty($params['name'])) {
146 $params['name'] = $params['label'];
149 // action is taken depending upon the mode
150 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
151 $membershipStatus->copyValues($params);
153 $membershipStatus->id
= $statusId;
155 $membershipStatus->save();
156 return $membershipStatus;
160 * Function to get membership status
162 * @param int $membershipStatusId
167 public static function getMembershipStatus($membershipStatusId) {
168 $statusDetails = array();
169 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
170 $membershipStatus->id
= $membershipStatusId;
171 if ($membershipStatus->find(TRUE)) {
172 CRM_Core_DAO
::storeValues($membershipStatus, $statusDetails);
174 return $statusDetails;
178 * Function to delete membership Types
180 * @param int $membershipStatusId
184 static function del($membershipStatusId) {
186 //checking if membership status is present in some other table
189 $dependancy = array('Membership', 'MembershipLog');
190 foreach ($dependancy as $name) {
191 $baoString = 'CRM_Member_BAO_' . $name;
192 $dao = new $baoString();
193 $dao->status_id
= $membershipStatusId;
194 if ($dao->find(TRUE)) {
195 throw new CRM_Core_Exception(ts('This membership status cannot be deleted as memberships exist with this status'));
198 CRM_Utils_Weight
::delWeight('CRM_Member_DAO_MembershipStatus', $membershipStatusId);
199 //delete from membership Type table
200 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
201 $membershipStatus->id
= $membershipStatusId;
202 $membershipStatus->delete();
203 $membershipStatus->free();
207 * Function to find the membership status based on start date, end date, join date & status date.
209 * @param date $startDate start date of the member whose membership status is to be calculated.
210 * @param date $endDate end date of the member whose membership status is to be calculated.
211 * @param date $joinDate join date of the member whose membership status is to be calculated.
212 * @param date $statusDate status date of the member whose membership status is to be calculated.
213 * @param boolean $excludeIsAdmin exclude the statuses those having is_admin = 1
214 * @param integer $membershipType membership type id - passed to the hook
215 * @param array $membership membership params as available to calling function - passed to the hook
220 static function getMembershipStatusByDate($startDate, $endDate, $joinDate,
221 $statusDate = 'today', $excludeIsAdmin = FALSE, $membershipTypeID, $membership = array()
223 $membershipDetails = array();
225 if (!$statusDate ||
$statusDate == 'today') {
226 $statusDate = getDate();
227 $statusDate = date('Ymd',
228 mktime($statusDate['hours'],
229 $statusDate['minutes'],
230 $statusDate['seconds'],
238 $statusDate = CRM_Utils_Date
::customFormat($statusDate, '%Y%m%d');
241 $dates = array('start', 'end', 'join');
242 $events = array('start', 'end');
244 foreach ($dates as $dat) {
245 if ($
{$dat . 'Date'} && $
{$dat . 'Date'} != "null") {
246 $
{$dat . 'Date'} = CRM_Utils_Date
::customFormat($
{$dat . 'Date'}, '%Y%m%d');
248 $
{$dat . 'Year'} = substr($
{$dat . 'Date'}, 0, 4);
250 $
{$dat . 'Month'} = substr($
{$dat . 'Date'}, 4, 2);
252 $
{$dat . 'Day'} = substr($
{$dat . 'Date'}, 6, 2);
255 $
{$dat . 'Date'} = '';
259 //fix for CRM-3570, if we have statuses with is_admin=1,
260 //exclude these statuses from calculatation during import.
261 $where = "is_active = 1";
262 if ($excludeIsAdmin) {
263 $where .= " AND is_admin != 1";
268 FROM civicrm_membership_status
270 ORDER BY weight ASC";
272 $membershipStatus = CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
273 $hour = $minute = $second = 0;
275 while ($membershipStatus->fetch()) {
278 foreach ($events as $eve) {
279 foreach ($dates as $dat) {
280 // calculate start-event/date and end-event/date
281 if (($membershipStatus->{$eve . '_event'} == $dat . '_date') &&
284 if ($membershipStatus->{$eve . '_event_adjust_unit'} &&
285 $membershipStatus->{$eve . '_event_adjust_interval'}
288 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'month') {
289 $
{$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
290 $
{$dat . 'Month'} +
$membershipStatus->{$eve . '_event_adjust_interval'},
296 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'day') {
297 $
{$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
299 $
{$dat . 'Day'} +
$membershipStatus->{$eve . '_event_adjust_interval'},
304 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'year') {
305 $
{$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
308 $
{$dat . 'Year'} +
$membershipStatus->{$eve . '_event_adjust_interval'}
311 // if no interval and unit, present
314 $
{$eve . 'Event'} = $
{$dat . 'Date'};
320 // check if statusDate is in the range of start & end events.
321 if ($startEvent && $endEvent) {
322 if (($statusDate >= $startEvent) && ($statusDate <= $endEvent)) {
323 $membershipDetails['id'] = $membershipStatus->id
;
324 $membershipDetails['name'] = $membershipStatus->name
;
327 elseif ($startEvent) {
328 if ($statusDate >= $startEvent) {
329 $membershipDetails['id'] = $membershipStatus->id
;
330 $membershipDetails['name'] = $membershipStatus->name
;
334 if ($statusDate <= $endEvent) {
335 $membershipDetails['id'] = $membershipStatus->id
;
336 $membershipDetails['name'] = $membershipStatus->name
;
340 // returns FIRST status record for which status_date is in range.
341 if ($membershipDetails) {
347 $membershipStatus->free();
349 //we bundle the arguments into an array as we can't pass 8 variables to the hook otherwise
350 // the membership array might contain the pre-altered settings so we don't want to merge this
352 'start_date' => $startDate,
353 'end_date' => $endDate,
354 'join_date' => $joinDate,
355 'status_date' => $statusDate,
356 'exclude_is_admin' => $endDate,
357 'membership_type_id' => $membershipTypeID,
358 'start_event' => $startEvent,
359 'end_event' => $endEvent,
361 CRM_Utils_Hook
::alterCalculatedMembershipStatus($membershipDetails, $arguments, $membership);
362 return $membershipDetails;
366 * Function that return the status ids whose is_current_member is set
371 public static function getMembershipStatusCurrent() {
372 $statusIds = array();
373 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
374 $membershipStatus->is_current_member
= 1;
375 $membershipStatus->find();
376 $membershipStatus->selectAdd();
377 $membershipStatus->selectAdd('id');
378 while ($membershipStatus->fetch()) {
379 $statusIds[] = $membershipStatus->id
;
381 $membershipStatus->free();