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
89 * @param array $params (reference ) an assoc array of name/value pairs
91 * @return object CRM_Member_BAO_MembershipStatus object
95 static function create($params){
97 if(!empty($params['id'])){
98 $ids['membershipStatus'] = $params['id'];
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.');
108 $membershipStatusBAO = CRM_Member_BAO_MembershipStatus
::add($params, $ids);
109 return $membershipStatusBAO;
112 * function to add the membership types
114 * @param array $params reference array contains the values submitted by the form
115 * @param array $ids reference array contains the id
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);
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
136 //copy name to label when not passed.
137 if (empty($params['label']) && !empty($params['name'])) {
138 $params['label'] = $params['name'];
141 //for add mode, copy label to name.
142 $statusId = CRM_Utils_Array
::value('membershipStatus', $ids);
143 if (!$statusId && !empty($params['label']) && empty($params['name'])) {
144 $params['name'] = $params['label'];
147 // action is taken depending upon the mode
148 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
149 $membershipStatus->copyValues($params);
151 $membershipStatus->id
= $statusId;
153 $membershipStatus->save();
154 return $membershipStatus;
158 * Function to get membership status
160 * @param int $membershipStatusId
163 public static function getMembershipStatus($membershipStatusId) {
164 $statusDetails = array();
165 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
166 $membershipStatus->id
= $membershipStatusId;
167 if ($membershipStatus->find(TRUE)) {
168 CRM_Core_DAO
::storeValues($membershipStatus, $statusDetails);
170 return $statusDetails;
174 * Function to delete membership Types
176 * @param int $membershipStatusId
180 static function del($membershipStatusId) {
182 //checking if membership status is present in some other table
185 $dependancy = array('Membership', 'MembershipLog');
186 foreach ($dependancy as $name) {
187 $baoString = 'CRM_Member_BAO_' . $name;
188 $dao = new $baoString();
189 $dao->status_id
= $membershipStatusId;
190 if ($dao->find(TRUE)) {
191 throw new CRM_Core_Exception(ts('This membership status cannot be deleted as memberships exist with this status'));
194 CRM_Utils_Weight
::delWeight('CRM_Member_DAO_MembershipStatus', $membershipStatusId);
195 //delete from membership Type table
196 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
197 $membershipStatus->id
= $membershipStatusId;
198 $membershipStatus->delete();
199 $membershipStatus->free();
203 * Function to find the membership status based on start date, end date, join date & status date.
205 * @param date $startDate start date of the member whose membership status is to be calculated.
206 * @param date $endDate end date of the member whose membership status is to be calculated.
207 * @param date $joinDate join date of the member whose membership status is to be calculated.
208 * @param date $statusDate status date of the member whose membership status is to be calculated.
209 * @param boolean $excludeIsAdmin exclude the statuses those having is_admin = 1
214 static function getMembershipStatusByDate($startDate, $endDate, $joinDate,
215 $statusDate = 'today', $excludeIsAdmin = FALSE
217 $membershipDetails = array();
218 if (!$statusDate ||
$statusDate == 'today') {
219 $statusDate = getDate();
220 $statusDate = date('Ymd',
221 mktime($statusDate['hours'],
222 $statusDate['minutes'],
223 $statusDate['seconds'],
231 $statusDate = CRM_Utils_Date
::customFormat($statusDate, '%Y%m%d');
234 $dates = array('start', 'end', 'join');
235 $events = array('start', 'end');
237 foreach ($dates as $dat) {
238 if ($
{$dat . 'Date'} && $
{$dat . 'Date'} != "null") {
239 $
{$dat . 'Date'} = CRM_Utils_Date
::customFormat($
{$dat . 'Date'}, '%Y%m%d');
241 $
{$dat . 'Year'} = substr($
{$dat . 'Date'}, 0, 4);
243 $
{$dat . 'Month'} = substr($
{$dat . 'Date'}, 4, 2);
245 $
{$dat . 'Day'} = substr($
{$dat . 'Date'}, 6, 2);
248 $
{$dat . 'Date'} = '';
252 //fix for CRM-3570, if we have statuses with is_admin=1,
253 //exclude these statuses from calculatation during import.
254 $where = "is_active = 1";
255 if ($excludeIsAdmin) {
256 $where .= " AND is_admin != 1";
261 FROM civicrm_membership_status
263 ORDER BY weight ASC";
265 $membershipStatus = CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
266 $hour = $minute = $second = 0;
268 while ($membershipStatus->fetch()) {
271 foreach ($events as $eve) {
272 foreach ($dates as $dat) {
273 // calculate start-event/date and end-event/date
274 if (($membershipStatus->{$eve . '_event'} == $dat . '_date') &&
277 if ($membershipStatus->{$eve . '_event_adjust_unit'} &&
278 $membershipStatus->{$eve . '_event_adjust_interval'}
281 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'month') {
282 $
{$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
283 $
{$dat . 'Month'} +
$membershipStatus->{$eve . '_event_adjust_interval'},
289 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'day') {
290 $
{$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
292 $
{$dat . 'Day'} +
$membershipStatus->{$eve . '_event_adjust_interval'},
297 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'year') {
298 $
{$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
301 $
{$dat . 'Year'} +
$membershipStatus->{$eve . '_event_adjust_interval'}
304 // if no interval and unit, present
307 $
{$eve . 'Event'} = $
{$dat . 'Date'};
313 // check if statusDate is in the range of start & end events.
314 if ($startEvent && $endEvent) {
315 if (($statusDate >= $startEvent) && ($statusDate <= $endEvent)) {
316 $membershipDetails['id'] = $membershipStatus->id
;
317 $membershipDetails['name'] = $membershipStatus->name
;
320 elseif ($startEvent) {
321 if ($statusDate >= $startEvent) {
322 $membershipDetails['id'] = $membershipStatus->id
;
323 $membershipDetails['name'] = $membershipStatus->name
;
327 if ($statusDate <= $endEvent) {
328 $membershipDetails['id'] = $membershipStatus->id
;
329 $membershipDetails['name'] = $membershipStatus->name
;
333 // returns FIRST status record for which status_date is in range.
334 if ($membershipDetails) {
335 $membershipStatus->free();
336 return $membershipDetails;
341 $membershipStatus->free();
342 return $membershipDetails;
346 * Function that return the status ids whose is_current_member is set
351 public static function getMembershipStatusCurrent() {
352 $statusIds = array();
353 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
354 $membershipStatus->is_current_member
= 1;
355 $membershipStatus->find();
356 $membershipStatus->selectAdd();
357 $membershipStatus->selectAdd('id');
358 while ($membershipStatus->fetch()) {
359 $statusIds[] = $membershipStatus->id
;
361 $membershipStatus->free();