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
53 * (reference ) an assoc array of name/value pairs.
54 * @param array $defaults
55 * (reference ) an assoc array to hold the flattened values.
57 * @return CRM_Member_BAO_MembershipStatus
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;
71 * Update the is_active flag in the db
74 * Id of the database record.
75 * @param bool $is_active
76 * Value we want to set the is_active field.
79 * DAO object on sucess, null otherwise
82 public 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
91 * (reference ) an assoc array of name/value pairs.
94 * @return CRM_Member_BAO_MembershipStatus
97 public 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;
115 * Add the membership types
117 * @param array $params
118 * Reference array contains the values submitted by the form.
120 * Array contains the id - this param is deprecated.
126 public static function add(&$params, $ids = array()) {
127 $id = CRM_Utils_Array
::value('id', $params, CRM_Utils_Array
::value('membershipStatus', $ids));
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'];
135 if (empty($params['name']) && !empty($params['label'])) {
136 $params['name'] = $params['label'];
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
148 // action is taken depending upon the mode
149 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
150 $membershipStatus->copyValues($params);
152 $membershipStatus->id
= $id;
154 $membershipStatus->save();
155 return $membershipStatus;
159 * Get defaults for new entity
162 public static function getDefaults() {
164 'is_active' => FALSE,
165 'is_current_member' => FALSE,
167 'is_default' => FALSE,
172 * Get membership status
174 * @param int $membershipStatusId
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);
186 return $statusDetails;
190 * Delete membership Types
192 * @param int $membershipStatusId
194 * @throws CRM_Core_Exception
197 public static function del($membershipStatusId) {
199 //checking if membership status is present in some other table
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'));
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();
220 * Find the membership status based on start date, end date, join date & status date.
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.
238 static function getMembershipStatusByDate(
239 $startDate, $endDate, $joinDate,
240 $statusDate = 'today', $excludeIsAdmin = FALSE, $membershipTypeID, $membership = array()
242 $membershipDetails = array();
244 if (!$statusDate ||
$statusDate == 'today') {
245 $statusDate = getdate();
246 $statusDate = date('Ymd',
247 mktime($statusDate['hours'],
248 $statusDate['minutes'],
249 $statusDate['seconds'],
257 $statusDate = CRM_Utils_Date
::customFormat($statusDate, '%Y%m%d');
260 $dates = array('start', 'end', 'join');
261 $events = array('start', 'end');
263 foreach ($dates as $dat) {
264 if ($
{$dat . 'Date'} && $
{$dat . 'Date'} != "null") {
265 $
{$dat . 'Date'} = CRM_Utils_Date
::customFormat($
{$dat . 'Date'}, '%Y%m%d');
267 $
{$dat . 'Year'} = substr($
{$dat . 'Date'}, 0, 4);
269 $
{$dat . 'Month'} = substr($
{$dat . 'Date'}, 4, 2);
271 $
{$dat . 'Day'} = substr($
{$dat . 'Date'}, 6, 2);
274 $
{$dat . 'Date'} = '';
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";
287 FROM civicrm_membership_status
289 ORDER BY weight ASC";
291 $membershipStatus = CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
292 $hour = $minute = $second = 0;
294 while ($membershipStatus->fetch()) {
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') &&
303 if ($membershipStatus->{$eve . '_event_adjust_unit'} &&
304 $membershipStatus->{$eve . '_event_adjust_interval'}
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'},
315 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'day') {
316 $
{$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
318 $
{$dat . 'Day'} +
$membershipStatus->{$eve . '_event_adjust_interval'},
323 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'year') {
324 $
{$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
327 $
{$dat . 'Year'} +
$membershipStatus->{$eve . '_event_adjust_interval'}
330 // if no interval and unit, present
333 $
{$eve . 'Event'} = $
{$dat . 'Date'};
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
;
346 elseif ($startEvent) {
347 if ($statusDate >= $startEvent) {
348 $membershipDetails['id'] = $membershipStatus->id
;
349 $membershipDetails['name'] = $membershipStatus->name
;
353 if ($statusDate <= $endEvent) {
354 $membershipDetails['id'] = $membershipStatus->id
;
355 $membershipDetails['name'] = $membershipStatus->name
;
359 // returns FIRST status record for which status_date is in range.
360 if ($membershipDetails) {
366 $membershipStatus->free();
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
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,
380 CRM_Utils_Hook
::alterCalculatedMembershipStatus($membershipDetails, $arguments, $membership);
381 return $membershipDetails;
385 * Function that return the status ids whose is_current_member is set
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
;
400 $membershipStatus->free();