Enable api_v3_syntaxConformaceTest::testInvalidID_delete (#9068)
[civicrm-core.git] / CRM / Member / BAO / MembershipStatus.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2016
32 * $Id$
33 *
34 */
35 class CRM_Member_BAO_MembershipStatus extends CRM_Member_DAO_MembershipStatus {
36
37 /**
38 * Static holder for the default LT.
39 */
40 static $_defaultMembershipStatus = NULL;
41
42 /**
43 * Class constructor.
44 */
45 public function __construct() {
46 parent::__construct();
47 }
48
49 /**
50 * Fetch object based on array of properties.
51 *
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.
56 *
57 * @return CRM_Member_BAO_MembershipStatus
58 */
59 public static function retrieve(&$params, &$defaults) {
60 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
61 $membershipStatus->copyValues($params);
62 if ($membershipStatus->find(TRUE)) {
63 CRM_Core_DAO::storeValues($membershipStatus, $defaults);
64 return $membershipStatus;
65 }
66 return NULL;
67 }
68
69 /**
70 * Update the is_active flag in the db.
71 *
72 * @param int $id
73 * Id of the database record.
74 * @param bool $is_active
75 * Value we want to set the is_active field.
76 *
77 * @return Object
78 * DAO object on success, null otherwise
79 */
80 public static function setIsActive($id, $is_active) {
81 return CRM_Core_DAO::setFieldValue('CRM_Member_DAO_MembershipStatus', $id, 'is_active', $is_active);
82 }
83
84 /**
85 * Takes an associative array and creates a membership Status object.
86 * See http://wiki.civicrm.org/confluence/display/CRM/Database+layer
87 *
88 * @param array $params
89 * (reference ) an assoc array of name/value pairs.
90 *
91 * @throws Exception
92 * @return CRM_Member_BAO_MembershipStatus
93 */
94 public static function create($params) {
95 $ids = array();
96 if (!empty($params['id'])) {
97 $ids['membershipStatus'] = $params['id'];
98 }
99 else {
100 //don't allow duplicate names - if id not set
101 $status = new CRM_Member_DAO_MembershipStatus();
102 $status->name = $params['name'];
103 if ($status->find(TRUE)) {
104 throw new Exception('A membership status with this name already exists.');
105 }
106 }
107 $membershipStatusBAO = CRM_Member_BAO_MembershipStatus::add($params, $ids);
108 return $membershipStatusBAO;
109 }
110
111 /**
112 * Add the membership types.
113 *
114 * @param array $params
115 * Reference array contains the values submitted by the form.
116 * @param array $ids
117 * Array contains the id - this param is deprecated.
118 *
119 *
120 * @return object
121 */
122 public static function add(&$params, $ids = array()) {
123 $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('membershipStatus', $ids));
124 if (!$id) {
125 CRM_Core_DAO::setCreateDefaults($params, self::getDefaults());
126 //copy name to label when not passed.
127 if (empty($params['label']) && !empty($params['name'])) {
128 $params['label'] = $params['name'];
129 }
130
131 if (empty($params['name']) && !empty($params['label'])) {
132 $params['name'] = $params['label'];
133 }
134 }
135
136 // set all other defaults to false.
137 if (!empty($params['is_default'])) {
138 $query = "UPDATE civicrm_membership_status SET is_default = 0";
139 CRM_Core_DAO::executeQuery($query,
140 CRM_Core_DAO::$_nullArray
141 );
142 }
143
144 // action is taken depending upon the mode
145 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
146 $membershipStatus->copyValues($params);
147
148 $membershipStatus->id = $id;
149
150 $membershipStatus->save();
151 CRM_Member_PseudoConstant::flush('membershipStatus');
152 return $membershipStatus;
153 }
154
155 /**
156 * Get defaults for new entity.
157 * @return array
158 */
159 public static function getDefaults() {
160 return array(
161 'is_active' => FALSE,
162 'is_current_member' => FALSE,
163 'is_admin' => FALSE,
164 'is_default' => FALSE,
165 );
166 }
167
168 /**
169 * Get membership status.
170 *
171 * @param int $membershipStatusId
172 *
173 * @return array
174 */
175 public static function getMembershipStatus($membershipStatusId) {
176 $statusDetails = array();
177 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
178 $membershipStatus->id = $membershipStatusId;
179 if ($membershipStatus->find(TRUE)) {
180 CRM_Core_DAO::storeValues($membershipStatus, $statusDetails);
181 }
182 return $statusDetails;
183 }
184
185 /**
186 * Delete membership Types.
187 *
188 * @param int $membershipStatusId
189 *
190 * @throws CRM_Core_Exception
191 */
192 public static function del($membershipStatusId) {
193 //check dependencies
194 //checking if membership status is present in some other table
195 $check = FALSE;
196
197 $dependency = array('Membership', 'MembershipLog');
198 foreach ($dependency as $name) {
199 $baoString = 'CRM_Member_BAO_' . $name;
200 $dao = new $baoString();
201 $dao->status_id = $membershipStatusId;
202 if ($dao->find(TRUE)) {
203 throw new CRM_Core_Exception(ts('This membership status cannot be deleted as memberships exist with this status'));
204 }
205 }
206 CRM_Utils_Weight::delWeight('CRM_Member_DAO_MembershipStatus', $membershipStatusId);
207 //delete from membership Type table
208 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
209 $membershipStatus->id = $membershipStatusId;
210 if (!$membershipStatus->find()) {
211 throw new CRM_Core_Exception(ts('Cannot delete membership status ' . $membershipStatusId));
212 }
213 $membershipStatus->delete();
214 CRM_Member_PseudoConstant::flush('membershipStatus');
215 $membershipStatus->free();
216 }
217
218 /**
219 * Find the membership status based on start date, end date, join date & status date.
220 *
221 * @param string $startDate
222 * Start date of the member whose membership status is to be calculated.
223 * @param string $endDate
224 * End date of the member whose membership status is to be calculated.
225 * @param string $joinDate
226 * Join date of the member whose membership status is to be calculated.
227 * @param \date|string $statusDate status date of the member whose membership status is to be calculated.
228 * @param bool $excludeIsAdmin the statuses those having is_admin = 1.
229 * Exclude the statuses those having is_admin = 1.
230 * @param int $membershipTypeID
231 * @param array $membership
232 * Membership params as available to calling function - passed to the hook.
233 *
234 * @return array
235 */
236 public static function getMembershipStatusByDate(
237 $startDate, $endDate, $joinDate,
238 $statusDate = 'today', $excludeIsAdmin = FALSE, $membershipTypeID, $membership = array()
239 ) {
240 $membershipDetails = array();
241
242 if (!$statusDate || $statusDate == 'today') {
243 $statusDate = getdate();
244 $statusDate = date('Ymd',
245 mktime($statusDate['hours'],
246 $statusDate['minutes'],
247 $statusDate['seconds'],
248 $statusDate['mon'],
249 $statusDate['mday'],
250 $statusDate['year']
251 )
252 );
253 }
254 else {
255 $statusDate = CRM_Utils_Date::customFormat($statusDate, '%Y%m%d');
256 }
257
258 $dates = array('start', 'end', 'join');
259 $events = array('start', 'end');
260
261 foreach ($dates as $dat) {
262 if (${$dat . 'Date'} && ${$dat . 'Date'} != "null") {
263 ${$dat . 'Date'} = CRM_Utils_Date::customFormat(${$dat . 'Date'}, '%Y%m%d');
264
265 ${$dat . 'Year'} = substr(${$dat . 'Date'}, 0, 4);
266
267 ${$dat . 'Month'} = substr(${$dat . 'Date'}, 4, 2);
268
269 ${$dat . 'Day'} = substr(${$dat . 'Date'}, 6, 2);
270 }
271 else {
272 ${$dat . 'Date'} = '';
273 }
274 }
275
276 //fix for CRM-3570, if we have statuses with is_admin=1,
277 //exclude these statuses from calculatation during import.
278 $where = "is_active = 1";
279 if ($excludeIsAdmin) {
280 $where .= " AND is_admin != 1";
281 }
282
283 $query = "
284 SELECT *
285 FROM civicrm_membership_status
286 WHERE {$where}
287 ORDER BY weight ASC";
288
289 $membershipStatus = CRM_Core_DAO::executeQuery($query);
290 $hour = $minute = $second = 0;
291
292 while ($membershipStatus->fetch()) {
293 $startEvent = NULL;
294 $endEvent = NULL;
295 foreach ($events as $eve) {
296 foreach ($dates as $dat) {
297 // calculate start-event/date and end-event/date
298 if (($membershipStatus->{$eve . '_event'} == $dat . '_date') &&
299 ${$dat . 'Date'}
300 ) {
301 if ($membershipStatus->{$eve . '_event_adjust_unit'} &&
302 $membershipStatus->{$eve . '_event_adjust_interval'}
303 ) {
304 // add in months
305 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'month') {
306 ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
307 ${$dat . 'Month'} + $membershipStatus->{$eve . '_event_adjust_interval'},
308 ${$dat . 'Day'},
309 ${$dat . 'Year'}
310 ));
311 }
312 // add in days
313 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'day') {
314 ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
315 ${$dat . 'Month'},
316 ${$dat . 'Day'} + $membershipStatus->{$eve . '_event_adjust_interval'},
317 ${$dat . 'Year'}
318 ));
319 }
320 // add in years
321 if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'year') {
322 ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second,
323 ${$dat . 'Month'},
324 ${$dat . 'Day'},
325 ${$dat . 'Year'} + $membershipStatus->{$eve . '_event_adjust_interval'}
326 ));
327 }
328 // if no interval and unit, present
329 }
330 else {
331 ${$eve . 'Event'} = ${$dat . 'Date'};
332 }
333 }
334 }
335 }
336
337 // check if statusDate is in the range of start & end events.
338 if ($startEvent && $endEvent) {
339 if (($statusDate >= $startEvent) && ($statusDate <= $endEvent)) {
340 $membershipDetails['id'] = $membershipStatus->id;
341 $membershipDetails['name'] = $membershipStatus->name;
342 }
343 }
344 elseif ($startEvent) {
345 if ($statusDate >= $startEvent) {
346 $membershipDetails['id'] = $membershipStatus->id;
347 $membershipDetails['name'] = $membershipStatus->name;
348 }
349 }
350 elseif ($endEvent) {
351 if ($statusDate <= $endEvent) {
352 $membershipDetails['id'] = $membershipStatus->id;
353 $membershipDetails['name'] = $membershipStatus->name;
354 }
355 }
356
357 // returns FIRST status record for which status_date is in range.
358 if ($membershipDetails) {
359 break;
360 }
361 }
362 //end fetch
363
364 $membershipStatus->free();
365
366 //we bundle the arguments into an array as we can't pass 8 variables to the hook otherwise
367 // the membership array might contain the pre-altered settings so we don't want to merge this
368 $arguments = array(
369 'start_date' => $startDate,
370 'end_date' => $endDate,
371 'join_date' => $joinDate,
372 'status_date' => $statusDate,
373 'exclude_is_admin' => $endDate,
374 'membership_type_id' => $membershipTypeID,
375 'start_event' => $startEvent,
376 'end_event' => $endEvent,
377 );
378 CRM_Utils_Hook::alterCalculatedMembershipStatus($membershipDetails, $arguments, $membership);
379 return $membershipDetails;
380 }
381
382 /**
383 * Function that return the status ids whose is_current_member is set.
384 *
385 * @return array
386 */
387 public static function getMembershipStatusCurrent() {
388 $statusIds = array();
389 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
390 $membershipStatus->is_current_member = 1;
391 $membershipStatus->find();
392 $membershipStatus->selectAdd();
393 $membershipStatus->selectAdd('id');
394 while ($membershipStatus->fetch()) {
395 $statusIds[] = $membershipStatus->id;
396 }
397 $membershipStatus->free();
398 return $statusIds;
399 }
400
401 }