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 object
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.
78 * @return Object DAO object on sucess, null otherwise
81 public static function setIsActive($id, $is_active) {
82 return CRM_Core_DAO
::setFieldValue('CRM_Member_DAO_MembershipStatus', $id, 'is_active', $is_active);
86 * Takes an associative array and creates a membership Status object
87 * See http://wiki.civicrm.org/confluence/display/CRM/Database+layer
89 * @param array $params
90 * (reference ) an assoc array of name/value pairs.
93 * @return CRM_Member_BAO_MembershipStatus object
96 public static function create($params){
98 if(!empty($params['id'])){
99 $ids['membershipStatus'] = $params['id'];
102 //don't allow duplicate names - if id not set
103 $status = new CRM_Member_DAO_MembershipStatus();
104 $status->name
= $params['name'];
105 if ($status->find(TRUE)) {
106 throw new Exception('A membership status with this name already exists.');
109 $membershipStatusBAO = CRM_Member_BAO_MembershipStatus
::add($params, $ids);
110 return $membershipStatusBAO;
113 * Add the membership types
115 * @param array $params
116 * Reference array contains the values submitted by the form.
118 * Array contains the id - this param is deprecated.
124 public static function add(&$params, $ids = array()) {
125 $id = CRM_Utils_Array
::value('id', $params, CRM_Utils_Array
::value('membershipStatus', $ids));
127 CRM_Core_DAO
::setCreateDefaults($params, self
::getDefaults());
128 //copy name to label when not passed.
129 if (empty($params['label']) && !empty($params['name'])) {
130 $params['label'] = $params['name'];
133 if (empty($params['name']) && !empty($params['label'])) {
134 $params['name'] = $params['label'];
138 // set all other defaults to false.
139 if (!empty($params['is_default'])) {
140 $query = "UPDATE civicrm_membership_status SET is_default = 0";
141 CRM_Core_DAO
::executeQuery($query,
142 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 boolean $excludeIsAdmin
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($startDate, $endDate, $joinDate,
239 $statusDate = 'today', $excludeIsAdmin = FALSE, $membershipTypeID, $membership = array()
241 $membershipDetails = array();
243 if (!$statusDate ||
$statusDate == 'today') {
244 $statusDate = getDate();
245 $statusDate = date('Ymd',
246 mktime($statusDate['hours'],
247 $statusDate['minutes'],
248 $statusDate['seconds'],
256 $statusDate = CRM_Utils_Date
::customFormat($statusDate, '%Y%m%d');
259 $dates = array('start', 'end', 'join');
260 $events = array('start', 'end');
262 foreach ($dates as $dat) {
263 if ($
{$dat . 'Date'} && $
{$dat . 'Date'} != "null") {
264 $
{$dat . 'Date'} = CRM_Utils_Date
::customFormat($
{$dat . 'Date'}, '%Y%m%d');
266 $
{$dat . 'Year'} = substr($
{$dat . 'Date'}, 0, 4);
268 $
{$dat . 'Month'} = substr($
{$dat . 'Date'}, 4, 2);
270 $
{$dat . 'Day'} = substr($
{$dat . 'Date'}, 6, 2);
273 $
{$dat . 'Date'} = '';
277 //fix for CRM-3570, if we have statuses with is_admin=1,
278 //exclude these statuses from calculatation during import.
279 $where = "is_active = 1";
280 if ($excludeIsAdmin) {
281 $where .= " AND is_admin != 1";
286 FROM civicrm_membership_status
288 ORDER BY weight ASC";
290 $membershipStatus = CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
291 $hour = $minute = $second = 0;
293 while ($membershipStatus->fetch()) {
296 foreach ($events as $eve) {
297 foreach ($dates as $dat) {
298 // calculate start-event/date and end-event/date
299 if (($membershipStatus->{$eve . '_event'} == $dat . '_date') &&
302 if ($membershipStatus->{$eve . '_event_adjust_unit'} &&
303 $membershipStatus->{$eve . '_event_adjust_interval'}
306 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'month') {
307 $
{$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
308 $
{$dat . 'Month'} +
$membershipStatus->{$eve . '_event_adjust_interval'},
314 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'day') {
315 $
{$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
317 $
{$dat . 'Day'} +
$membershipStatus->{$eve . '_event_adjust_interval'},
322 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'year') {
323 $
{$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
326 $
{$dat . 'Year'} +
$membershipStatus->{$eve . '_event_adjust_interval'}
329 // if no interval and unit, present
332 $
{$eve . 'Event'} = $
{$dat . 'Date'};
338 // check if statusDate is in the range of start & end events.
339 if ($startEvent && $endEvent) {
340 if (($statusDate >= $startEvent) && ($statusDate <= $endEvent)) {
341 $membershipDetails['id'] = $membershipStatus->id
;
342 $membershipDetails['name'] = $membershipStatus->name
;
345 elseif ($startEvent) {
346 if ($statusDate >= $startEvent) {
347 $membershipDetails['id'] = $membershipStatus->id
;
348 $membershipDetails['name'] = $membershipStatus->name
;
352 if ($statusDate <= $endEvent) {
353 $membershipDetails['id'] = $membershipStatus->id
;
354 $membershipDetails['name'] = $membershipStatus->name
;
358 // returns FIRST status record for which status_date is in range.
359 if ($membershipDetails) {
365 $membershipStatus->free();
367 //we bundle the arguments into an array as we can't pass 8 variables to the hook otherwise
368 // the membership array might contain the pre-altered settings so we don't want to merge this
370 'start_date' => $startDate,
371 'end_date' => $endDate,
372 'join_date' => $joinDate,
373 'status_date' => $statusDate,
374 'exclude_is_admin' => $endDate,
375 'membership_type_id' => $membershipTypeID,
376 'start_event' => $startEvent,
377 'end_event' => $endEvent,
379 CRM_Utils_Hook
::alterCalculatedMembershipStatus($membershipDetails, $arguments, $membership);
380 return $membershipDetails;
384 * Function that return the status ids whose is_current_member is set
389 public static function getMembershipStatusCurrent() {
390 $statusIds = array();
391 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
392 $membershipStatus->is_current_member
= 1;
393 $membershipStatus->find();
394 $membershipStatus->selectAdd();
395 $membershipStatus->selectAdd('id');
396 while ($membershipStatus->fetch()) {
397 $statusIds[] = $membershipStatus->id
;
399 $membershipStatus->free();