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 public 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
58 public static function retrieve(&$params, &$defaults) {
59 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
60 $membershipStatus->copyValues($params);
61 if ($membershipStatus->find(TRUE)) {
62 CRM_Core_DAO
::storeValues($membershipStatus, $defaults);
63 return $membershipStatus;
69 * Update the is_active flag in the db
71 * @param int $id id of the database record
72 * @param boolean $is_active value we want to set the is_active field
74 * @return Object DAO object on sucess, null otherwise
77 public static function setIsActive($id, $is_active) {
78 return CRM_Core_DAO
::setFieldValue('CRM_Member_DAO_MembershipStatus', $id, 'is_active', $is_active);
82 * Takes an associative array and creates a membership Status object
83 * See http://wiki.civicrm.org/confluence/display/CRM/Database+layer
85 * @param array $params (reference ) an assoc array of name/value pairs
88 * @return CRM_Member_BAO_MembershipStatus object
91 public static function create($params){
93 if(!empty($params['id'])){
94 $ids['membershipStatus'] = $params['id'];
97 //don't allow duplicate names - if id not set
98 $status = new CRM_Member_DAO_MembershipStatus();
99 $status->name
= $params['name'];
100 if ($status->find(TRUE)) {
101 throw new Exception('A membership status with this name already exists.');
104 $membershipStatusBAO = CRM_Member_BAO_MembershipStatus
::add($params, $ids);
105 return $membershipStatusBAO;
108 * Add the membership types
110 * @param array $params reference array contains the values submitted by the form
111 * @param array $ids array contains the id - this param is deprecated
117 public static function add(&$params, $ids = array()) {
118 $id = CRM_Utils_Array
::value('id', $params, CRM_Utils_Array
::value('membershipStatus', $ids));
120 CRM_Core_DAO
::setCreateDefaults($params, self
::getDefaults());
121 //copy name to label when not passed.
122 if (empty($params['label']) && !empty($params['name'])) {
123 $params['label'] = $params['name'];
126 if (empty($params['name']) && !empty($params['label'])) {
127 $params['name'] = $params['label'];
131 // set all other defaults to false.
132 if (!empty($params['is_default'])) {
133 $query = "UPDATE civicrm_membership_status SET is_default = 0";
134 CRM_Core_DAO
::executeQuery($query,
135 CRM_Core_DAO
::$_nullArray
141 // action is taken depending upon the mode
142 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
143 $membershipStatus->copyValues($params);
145 $membershipStatus->id
= $id;
147 $membershipStatus->save();
148 return $membershipStatus;
152 * Get defaults for new entity
155 public static function getDefaults() {
157 'is_active' => FALSE,
158 'is_current_member' => FALSE,
160 'is_default' => FALSE,
165 * Get membership status
167 * @param int $membershipStatusId
172 public static function getMembershipStatus($membershipStatusId) {
173 $statusDetails = array();
174 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
175 $membershipStatus->id
= $membershipStatusId;
176 if ($membershipStatus->find(TRUE)) {
177 CRM_Core_DAO
::storeValues($membershipStatus, $statusDetails);
179 return $statusDetails;
183 * Delete membership Types
185 * @param int $membershipStatusId
187 * @throws CRM_Core_Exception
190 public static function del($membershipStatusId) {
192 //checking if membership status is present in some other table
195 $dependancy = array('Membership', 'MembershipLog');
196 foreach ($dependancy as $name) {
197 $baoString = 'CRM_Member_BAO_' . $name;
198 $dao = new $baoString();
199 $dao->status_id
= $membershipStatusId;
200 if ($dao->find(TRUE)) {
201 throw new CRM_Core_Exception(ts('This membership status cannot be deleted as memberships exist with this status'));
204 CRM_Utils_Weight
::delWeight('CRM_Member_DAO_MembershipStatus', $membershipStatusId);
205 //delete from membership Type table
206 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
207 $membershipStatus->id
= $membershipStatusId;
208 $membershipStatus->delete();
209 $membershipStatus->free();
213 * Find the membership status based on start date, end date, join date & status date.
215 * @param string $startDate start date of the member whose membership status is to be calculated.
216 * @param string $endDate end date of the member whose membership status is to be calculated.
217 * @param string $joinDate join date of the member whose membership status is to be calculated.
218 * @param \date|string $statusDate status date of the member whose membership status is to be calculated.
219 * @param boolean $excludeIsAdmin exclude the statuses those having is_admin = 1
220 * @param int $membershipTypeID
221 * @param array $membership membership params as available to calling function - passed to the hook
226 static function getMembershipStatusByDate($startDate, $endDate, $joinDate,
227 $statusDate = 'today', $excludeIsAdmin = FALSE, $membershipTypeID, $membership = array()
229 $membershipDetails = array();
231 if (!$statusDate ||
$statusDate == 'today') {
232 $statusDate = getDate();
233 $statusDate = date('Ymd',
234 mktime($statusDate['hours'],
235 $statusDate['minutes'],
236 $statusDate['seconds'],
244 $statusDate = CRM_Utils_Date
::customFormat($statusDate, '%Y%m%d');
247 $dates = array('start', 'end', 'join');
248 $events = array('start', 'end');
250 foreach ($dates as $dat) {
251 if ($
{$dat . 'Date'} && $
{$dat . 'Date'} != "null") {
252 $
{$dat . 'Date'} = CRM_Utils_Date
::customFormat($
{$dat . 'Date'}, '%Y%m%d');
254 $
{$dat . 'Year'} = substr($
{$dat . 'Date'}, 0, 4);
256 $
{$dat . 'Month'} = substr($
{$dat . 'Date'}, 4, 2);
258 $
{$dat . 'Day'} = substr($
{$dat . 'Date'}, 6, 2);
261 $
{$dat . 'Date'} = '';
265 //fix for CRM-3570, if we have statuses with is_admin=1,
266 //exclude these statuses from calculatation during import.
267 $where = "is_active = 1";
268 if ($excludeIsAdmin) {
269 $where .= " AND is_admin != 1";
274 FROM civicrm_membership_status
276 ORDER BY weight ASC";
278 $membershipStatus = CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
279 $hour = $minute = $second = 0;
281 while ($membershipStatus->fetch()) {
284 foreach ($events as $eve) {
285 foreach ($dates as $dat) {
286 // calculate start-event/date and end-event/date
287 if (($membershipStatus->{$eve . '_event'} == $dat . '_date') &&
290 if ($membershipStatus->{$eve . '_event_adjust_unit'} &&
291 $membershipStatus->{$eve . '_event_adjust_interval'}
294 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'month') {
295 $
{$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
296 $
{$dat . 'Month'} +
$membershipStatus->{$eve . '_event_adjust_interval'},
302 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'day') {
303 $
{$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
305 $
{$dat . 'Day'} +
$membershipStatus->{$eve . '_event_adjust_interval'},
310 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'year') {
311 $
{$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
314 $
{$dat . 'Year'} +
$membershipStatus->{$eve . '_event_adjust_interval'}
317 // if no interval and unit, present
320 $
{$eve . 'Event'} = $
{$dat . 'Date'};
326 // check if statusDate is in the range of start & end events.
327 if ($startEvent && $endEvent) {
328 if (($statusDate >= $startEvent) && ($statusDate <= $endEvent)) {
329 $membershipDetails['id'] = $membershipStatus->id
;
330 $membershipDetails['name'] = $membershipStatus->name
;
333 elseif ($startEvent) {
334 if ($statusDate >= $startEvent) {
335 $membershipDetails['id'] = $membershipStatus->id
;
336 $membershipDetails['name'] = $membershipStatus->name
;
340 if ($statusDate <= $endEvent) {
341 $membershipDetails['id'] = $membershipStatus->id
;
342 $membershipDetails['name'] = $membershipStatus->name
;
346 // returns FIRST status record for which status_date is in range.
347 if ($membershipDetails) {
353 $membershipStatus->free();
355 //we bundle the arguments into an array as we can't pass 8 variables to the hook otherwise
356 // the membership array might contain the pre-altered settings so we don't want to merge this
358 'start_date' => $startDate,
359 'end_date' => $endDate,
360 'join_date' => $joinDate,
361 'status_date' => $statusDate,
362 'exclude_is_admin' => $endDate,
363 'membership_type_id' => $membershipTypeID,
364 'start_event' => $startEvent,
365 'end_event' => $endEvent,
367 CRM_Utils_Hook
::alterCalculatedMembershipStatus($membershipDetails, $arguments, $membership);
368 return $membershipDetails;
372 * Function that return the status ids whose is_current_member is set
377 public static function getMembershipStatusCurrent() {
378 $statusIds = array();
379 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
380 $membershipStatus->is_current_member
= 1;
381 $membershipStatus->find();
382 $membershipStatus->selectAdd();
383 $membershipStatus->selectAdd('id');
384 while ($membershipStatus->fetch()) {
385 $statusIds[] = $membershipStatus->id
;
387 $membershipStatus->free();