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_Membership
extends CRM_Member_DAO_Membership
{
38 * static field for all the membership information that we can potentially import
43 static $_importableFields = NULL;
45 static $_renewalActType = NULL;
47 static $_signupActType = NULL;
53 * @return \CRM_Member_DAO_Membership
58 function __construct() {
59 parent
::__construct();
63 * takes an associative array and creates a membership object
65 * the function extracts all the params it needs to initialize the created
66 * membership object. The params array could contain additional unused name/value
69 * @param array $params (reference ) an assoc array of name/value pairs
70 * @param array $ids the array that holds all the db ids
72 * @return object CRM_Member_BAO_Membership object
76 static function add(&$params, $ids = array()) {
77 $oldStatus = $oldType = NULL;
78 if (!empty($ids['membership'])) {
79 CRM_Utils_Hook
::pre('edit', 'Membership', $ids['membership'], $params);
81 $membershipObj = new CRM_Member_DAO_Membership();
82 $membershipObj->id
= $ids['membership'];
83 $membershipObj->find();
84 while ($membershipObj->fetch()) {
85 $oldStatus = $membershipObj->status_id
;
86 $oldType = $membershipObj->membership_type_id
;
90 CRM_Utils_Hook
::pre('create', 'Membership', NULL, $params);
93 if (array_key_exists('is_override', $params) && !$params['is_override']) {
94 $params['is_override'] = 'null';
97 $membership = new CRM_Member_BAO_Membership();
98 $membership->copyValues($params);
99 $membership->id
= CRM_Utils_Array
::value('membership', $ids);
104 if (empty($membership->contact_id
) ||
empty($membership->status_id
)) {
105 // this means we are in renewal mode and are just updating the membership
106 // record or this is an API update call and all fields are not present in the update record
107 // however the hooks dont care and want all data CRM-7784
108 $tempMembership = new CRM_Member_DAO_Membership();
109 $tempMembership->id
= $membership->id
;
110 $tempMembership->find(TRUE);
111 $membership = $tempMembership;
114 //get the log start date.
115 //it is set during renewal of membership.
116 $logStartDate = CRM_Utils_Array
::value('log_start_date', $params);
117 $logStartDate = ($logStartDate) ? CRM_Utils_Date
::isoToMysql($logStartDate) : CRM_Utils_Date
::isoToMysql($membership->start_date
);
118 $values = self
::getStatusANDTypeValues($membership->id
);
120 $membershipLog = array(
121 'membership_id' => $membership->id
,
122 'status_id' => $membership->status_id
,
123 'start_date' => $logStartDate,
124 'end_date' => CRM_Utils_Date
::isoToMysql($membership->end_date
),
125 'modified_date' => date('Ymd'),
126 'membership_type_id' => $values[$membership->id
]['membership_type_id'],
127 'max_related' => $membership->max_related
,
130 $session = CRM_Core_Session
::singleton();
131 // If we have an authenticated session, set modified_id to that user's contact_id, else set to membership.contact_id
132 if ($session->get('userID')) {
133 $membershipLog['modified_id'] = $session->get('userID');
135 elseif (!empty($ids['userId'])) {
136 $membershipLog['modified_id'] = $ids['userId'];
139 $membershipLog['modified_id'] = $membership->contact_id
;
142 CRM_Member_BAO_MembershipLog
::add($membershipLog, CRM_Core_DAO
::$_nullArray);
144 // reset the group contact cache since smart groups might be affected due to this
145 CRM_Contact_BAO_GroupContactCache
::remove();
147 if (!empty($ids['membership'])) {
148 if ($membership->status_id
!= $oldStatus) {
149 $allStatus = CRM_Member_BAO_Membership
::buildOptions('status_id', 'get');
150 $activityParam = array(
151 'subject' => "Status changed from {$allStatus[$oldStatus]} to {$allStatus[$membership->status_id]}",
152 'source_contact_id' => $membershipLog['modified_id'],
153 'target_contact_id' => $membership->contact_id
,
154 'source_record_id' => $membership->id
,
155 'activity_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Change Membership Status'),
158 'activity_date_time' => date('Y-m-d H:i:s'),
160 civicrm_api3('activity', 'create', $activityParam);
162 if (isset($membership->membership_type_id
) && $membership->membership_type_id
!= $oldType) {
163 $membershipTypes = CRM_Member_BAO_Membership
::buildOptions('membership_type_id', 'get');
164 $activityParam = array(
165 'subject' => "Type changed from {$membershipTypes[$oldType]} to {$membershipTypes[$membership->membership_type_id]}",
166 'source_contact_id' => $membershipLog['modified_id'],
167 'target_contact_id' => $membership->contact_id
,
168 'source_record_id' => $membership->id
,
169 'activity_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Change Membership Type'),
172 'activity_date_time' => date('Y-m-d H:i:s'),
174 civicrm_api3('activity', 'create', $activityParam);
176 CRM_Utils_Hook
::post('edit', 'Membership', $membership->id
, $membership);
179 CRM_Utils_Hook
::post('create', 'Membership', $membership->id
, $membership);
186 * Given the list of params in the params array, fetch the object
187 * and store the values in the values array
189 * @param array $params input parameters to find object
190 * @param array $values output values of the object
191 * @param boolean $active do you want only active memberships to
194 * @return CRM_Member_BAO_Membership|null the found object or null
198 static function &getValues(&$params, &$values, $active = FALSE) {
199 if (empty($params)) {
202 $membership = new CRM_Member_BAO_Membership();
204 $membership->copyValues($params);
206 $memberships = array();
207 while ($membership->fetch()) {
209 (!CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipStatus',
210 $membership->status_id
,
217 CRM_Core_DAO
::storeValues($membership, $values[$membership->id
]);
218 $memberships[$membership->id
] = $membership;
225 * takes an associative array and creates a membership object
227 * @param array $params (reference ) an assoc array of name/value pairs
228 * @param array $ids the array that holds all the db ids
229 * @param bool $skipRedirect
230 * @param string $activityType
232 * @throws CRM_Core_Exception
233 * @internal param bool $callFromAPI Is this function called from API?
235 * @return CRM_Member_BAO_Membership object
239 static function create(&$params, &$ids, $skipRedirect = FALSE, $activityType = 'Membership Signup') {
240 // always calculate status if is_override/skipStatusCal is not true.
241 // giving respect to is_override during import. CRM-4012
243 // To skip status calculation we should use 'skipStatusCal'.
244 // eg pay later membership, membership update cron CRM-3984
246 if (empty($params['is_override']) && empty($params['skipStatusCal'])) {
247 $dates = array('start_date', 'end_date', 'join_date');
248 $start_date = $end_date = $join_date = NULL; // declare these out of courtesy as IDEs don't pick up the setting of them below
249 foreach ($dates as $date) {
250 $
$date = $params[$date] = CRM_Utils_Date
::processDate(CRM_Utils_Array
::value($date, $params), NULL, TRUE, 'Ymd');
253 //fix for CRM-3570, during import exclude the statuses those having is_admin = 1
254 $excludeIsAdmin = CRM_Utils_Array
::value('exclude_is_admin', $params, FALSE);
256 //CRM-3724 always skip is_admin if is_override != true.
257 if (!$excludeIsAdmin && empty($params['is_override'])) {
258 $excludeIsAdmin = TRUE;
261 $calcStatus = CRM_Member_BAO_MembershipStatus
::getMembershipStatusByDate($start_date, $end_date, $join_date,
262 'today', $excludeIsAdmin, CRM_Utils_Array
::value('membership_type_id', $params), $params
264 if (empty($calcStatus)) {
265 // Redirect the form in case of error
266 // @todo this redirect in the BAO layer is really bad & should be moved to the form layer
267 // however since we have no idea how (if) this is triggered we can't safely move / remove it
268 // NB I tried really hard to trigger this error from backoffice membership form in order to test it
269 // and am convinced form validation is complete on that form WRT this error.
270 $errorParams = array(
271 'message_title' => ts('No valid membership status for given dates.'),
272 'legacy_redirect_path' => 'civicrm/contact/view',
273 'legacy_redirect_query' => "reset=1&force=1&cid={$params['contact_id']}&selectedChild=member",
275 throw new CRM_Core_Exception(ts('The membership cannot be saved because the status cannot be calculated.'), 0, $errorParams);
277 $params['status_id'] = $calcStatus['id'];
280 // data cleanup only: all verifications on number of related memberships are done upstream in:
281 // CRM_Member_BAO_Membership::createRelatedMemberships()
282 // CRM_Contact_BAO_Relationship::relatedMemberships()
283 if (isset($params['owner_membership_id'])) {
284 unset($params['max_related']);
287 // if membership allows related, default max_related to value in membership_type
288 if (!array_key_exists('max_related', $params) && !empty($params['membership_type_id'])) {
289 $membershipType = CRM_Member_BAO_MembershipType
::getMembershipTypeDetails($params['membership_type_id']);
290 if (isset($membershipType['relationship_type_id'])) {
291 $params['max_related'] = CRM_Utils_Array
::value('max_related', $membershipType);
296 $transaction = new CRM_Core_Transaction();
298 $membership = self
::add($params, $ids);
300 if (is_a($membership, 'CRM_Core_Error')) {
301 $transaction->rollback();
305 // add custom field values
306 if (!empty($params['custom']) && is_array($params['custom'])
308 CRM_Core_BAO_CustomValueTable
::store($params['custom'], 'civicrm_membership', $membership->id
);
311 $params['membership_id'] = $membership->id
;
312 if (isset($ids['membership'])) {
313 $ids['contribution'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipPayment',
320 //record contribution for this membership
321 if (!empty($params['contribution_status_id']) && empty($params['relate_contribution_id'])) {
322 $memInfo = array_merge($params, array('membership_id' => $membership->id
));
323 $params['contribution'] = self
::recordMembershipContribution($memInfo, $ids);
326 //insert payment record for this membership
327 if (!empty($params['relate_contribution_id'])) {
328 $mpDAO = new CRM_Member_DAO_MembershipPayment();
329 $mpDAO->membership_id
= $membership->id
;
330 $mpDAO->contribution_id
= $params['relate_contribution_id'];
331 if (!($mpDAO->find(TRUE))) {
332 CRM_Utils_Hook
::pre('create', 'MembershipPayment', NULL, $mpDAO);
334 CRM_Utils_Hook
::post('create', 'MembershipPayment', $mpDAO->id
, $mpDAO);
338 // add activity record only during create mode and renew mode
339 // also add activity if status changed CRM-3984 and CRM-2521
340 if (empty($ids['membership']) ||
341 $activityType == 'Membership Renewal' ||
!empty($params['createActivity'])) {
342 if (!empty($ids['membership'])) {
344 CRM_Core_DAO
::commonRetrieveAll('CRM_Member_DAO_Membership',
348 array('contact_id', 'membership_type_id', 'source')
351 $membership->contact_id
= $data[$membership->id
]['contact_id'];
352 $membership->membership_type_id
= $data[$membership->id
]['membership_type_id'];
353 $membership->source
= CRM_Utils_Array
::value('source', $data[$membership->id
]);
356 // since we are going to create activity record w/
357 // individual contact as a target in case of on behalf signup,
358 // so get the copy of organization id, CRM-5551
359 $realMembershipContactId = $membership->contact_id
;
361 // create activity source = individual, target = org CRM-4027
362 $targetContactID = NULL;
363 if (!empty($params['is_for_organization'])) {
364 $targetContactID = $membership->contact_id
;
365 $membership->contact_id
= CRM_Utils_Array
::value('userId', $ids);
368 if (empty($membership->contact_id
) && (!empty($membership->owner_membership_id
))) {
369 $membership->contact_id
= $realMembershipContactId;
372 if (!empty($ids['membership']) && $activityType != 'Membership Signup') {
373 CRM_Activity_BAO_Activity
::addActivity($membership, $activityType, $targetContactID);
374 } elseif (empty($ids['membership'])) {
375 CRM_Activity_BAO_Activity
::addActivity($membership, $activityType, $targetContactID);
378 // we might created activity record w/ individual
379 // contact as target so update membership object w/
380 // original organization id, CRM-5551
381 $membership->contact_id
= $realMembershipContactId;
384 $transaction->commit();
386 self
::createRelatedMemberships($params, $membership);
388 // do not add to recent items for import, CRM-4399
389 if (empty($params['skipRecentView'])) {
390 $url = CRM_Utils_System
::url('civicrm/contact/view/membership',
391 "action=view&reset=1&id={$membership->id}&cid={$membership->contact_id}&context=home"
393 if(empty($membership->membership_type_id
)){// ie in an update situation
394 $membership->find(TRUE);
396 $membershipTypes = CRM_Member_PseudoConstant
::membershipType();
397 $title = CRM_Contact_BAO_Contact
::displayName($membership->contact_id
) . ' - ' . ts('Membership Type:') . ' ' . $membershipTypes[$membership->membership_type_id
];
399 $recentOther = array();
400 if (CRM_Core_Permission
::checkActionPermission('CiviMember', CRM_Core_Action
::UPDATE
)) {
401 $recentOther['editUrl'] = CRM_Utils_System
::url('civicrm/contact/view/membership',
402 "action=update&reset=1&id={$membership->id}&cid={$membership->contact_id}&context=home"
405 if (CRM_Core_Permission
::checkActionPermission('CiviMember', CRM_Core_Action
::DELETE
)) {
406 $recentOther['deleteUrl'] = CRM_Utils_System
::url('civicrm/contact/view/membership',
407 "action=delete&reset=1&id={$membership->id}&cid={$membership->contact_id}&context=home"
411 // add the recently created Membership
412 CRM_Utils_Recent
::add($title,
416 $membership->contact_id
,
426 * Function to check the membership extended through relationship
428 * @param int $membershipId membership id
429 * @param int $contactId contact id
431 * @param const $action
433 * @return Array array of contact_id of all related contacts.
436 static function checkMembershipRelationship($membershipId, $contactId, $action = CRM_Core_Action
::ADD
) {
438 $membershipTypeID = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership', $membershipId, 'membership_type_id');
440 $membershipType = CRM_Member_BAO_MembershipType
::getMembershipTypeDetails($membershipTypeID);
441 $relationships = array();
442 if (isset($membershipType['relationship_type_id'])) {
443 $relationships = CRM_Contact_BAO_Relationship
::getRelationship($contactId,
444 CRM_Contact_BAO_Relationship
::CURRENT
446 if ($action & CRM_Core_Action
::UPDATE
) {
447 $pastRelationships = CRM_Contact_BAO_Relationship
::getRelationship($contactId,
448 CRM_Contact_BAO_Relationship
::PAST
450 $relationships = array_merge($relationships, $pastRelationships);
454 if (!empty($relationships)) {
455 // check for each contact relationships
456 foreach ($relationships as $values) {
457 //get details of the relationship type
458 $relType = array('id' => $values['civicrm_relationship_type_id']);
459 $relValues = array();
460 CRM_Contact_BAO_RelationshipType
::retrieve($relType, $relValues);
461 // Check if contact's relationship type exists in membership type
462 $relTypeDirs = array();
463 $relTypeIds = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $membershipType['relationship_type_id']);
464 $relDirections = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $membershipType['relationship_direction']);
465 $bidirectional = FALSE;
466 foreach ($relTypeIds as $key => $value) {
467 $relTypeDirs[] = $value . '_' . $relDirections[$key];
468 if (in_array($value, $relType) &&
469 $relValues['name_a_b'] == $relValues['name_b_a']
471 $bidirectional = TRUE;
475 $relTypeDir = $values['civicrm_relationship_type_id'] . '_' . $values['rtype'];
476 if ($bidirectional ||
in_array($relTypeDir, $relTypeDirs)) {
477 // $values['status'] is going to have value for
478 // current or past relationships.
479 $contacts[$values['cid']] = $values['status'];
484 // Sort by contact_id ascending
490 * Takes a bunch of params that are needed to match certain criteria and
491 * retrieves the relevant objects. We'll tweak this function to be more
492 * full featured over a period of time. This is the inverse function of
493 * create. It also stores all the retrieved values in the default array
495 * @param array $params (reference ) an assoc array of name/value pairs
496 * @param array $defaults (reference ) an assoc array to hold the name / value pairs
497 * in a hierarchical manner
499 * @internal param array $ids (reference) the array that holds all the db ids
501 * @return object CRM_Member_BAO_Membership object
505 static function retrieve(&$params, &$defaults) {
506 $membership = new CRM_Member_DAO_Membership();
508 $membership->copyValues($params);
510 if ($membership->find(TRUE)) {
511 CRM_Core_DAO
::storeValues($membership, $defaults);
513 //get the membership status and type values.
514 $statusANDType = self
::getStatusANDTypeValues($membership->id
);
516 'status', 'membership_type') as $fld) {
517 $defaults[$fld] = CRM_Utils_Array
::value($fld, $statusANDType[$membership->id
]);
519 if (!empty($statusANDType[$membership->id
]['is_current_member'])) {
520 $defaults['active'] = TRUE;
533 * Function to get membership status and membership type values
535 * @param int $membershipId membership id of values to return
537 * @return array of key value pairs
540 static function getStatusANDTypeValues($membershipId) {
542 if (!$membershipId) {
546 SELECT membership.id as id,
547 status.id as status_id,
548 status.label as status,
549 status.is_current_member as is_current_member,
550 type.id as membership_type_id,
551 type.name as membership_type,
552 type.relationship_type_id as relationship_type_id
553 FROM civicrm_membership membership
554 INNER JOIN civicrm_membership_status status ON ( status.id = membership.status_id )
555 INNER JOIN civicrm_membership_type type ON ( type.id = membership.membership_type_id )
556 WHERE membership.id = %1';
557 $dao = CRM_Core_DAO
::executeQuery($sql, array(1 => array($membershipId, 'Positive')));
558 $properties = array('status', 'status_id', 'membership_type', 'membership_type_id', 'is_current_member', 'relationship_type_id');
559 while ($dao->fetch()) {
560 foreach ($properties as $property) {
561 $values[$dao->id
][$property] = $dao->$property;
569 * Function to delete membership.
570 * Wrapper for most delete calls. Use this unless you JUST want to delete related memberships w/o deleting the parent.
572 * @param int $membershipId membership id that needs to be deleted
576 * @return $results no of deleted Membership on success, false otherwise
579 static function del($membershipId) {
580 //delete related first and then delete parent.
581 self
::deleteRelatedMemberships($membershipId);
582 return self
::deleteMembership($membershipId);
586 * Function to delete membership.
588 * @param int $membershipId membership id that needs to be deleted
592 * @return $results no of deleted Membership on success, false otherwise
595 static function deleteMembership($membershipId) {
596 // CRM-12147, retrieve membership data before we delete it for hooks
597 $params = array('id' => $membershipId);
598 $memValues = array();
599 $memberships = self
::getValues($params, $memValues);
601 $membership = $memberships[$membershipId];
603 CRM_Utils_Hook
::pre('delete', 'Membership', $membershipId, $memValues);
605 $transaction = new CRM_Core_Transaction();
608 //delete activity record
609 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, FALSE, FALSE, 'name');
612 $deleteActivity = false;
613 $membershipActivities = array(
615 'Membership Renewal',
616 'Change Membership Status',
617 'Change Membership Type',
618 'Membership Renewal Reminder'
620 foreach($membershipActivities as $membershipActivity) {
621 $activityId = array_search($membershipActivity, $activityTypes);
623 $params['activity_type_id'][] = $activityId;
624 $deleteActivity = true;
627 if ($deleteActivity) {
628 $params['source_record_id'] = $membershipId;
629 CRM_Activity_BAO_Activity
::deleteActivity($params);
631 self
::deleteMembershipPayment($membershipId);
633 $results = $membership->delete();
634 $transaction->commit();
636 CRM_Utils_Hook
::post('delete', 'Membership', $membership->id
, $membership);
638 // delete the recently created Membership
639 $membershipRecent = array(
640 'id' => $membershipId,
641 'type' => 'Membership',
643 CRM_Utils_Recent
::del($membershipRecent);
649 * Function to delete related memberships
651 * @param int $ownerMembershipId
652 * @param int $contactId
657 static function deleteRelatedMemberships($ownerMembershipId, $contactId = NULL) {
658 if (!$ownerMembershipId && !$contactId) {
662 $membership = new CRM_Member_DAO_Membership();
663 $membership->owner_membership_id
= $ownerMembershipId;
666 $membership->contact_id
= $contactId;
670 while ($membership->fetch()) {
671 //delete related first and then delete parent.
672 self
::deleteRelatedMemberships($membership->id
);
673 self
::deleteMembership($membership->id
);
679 * Function to obtain active/inactive memberships from the list of memberships passed to it.
681 * @param array $memberships membership records
682 * @param string $status active or inactive
684 * @return array $actives array of memberships based on status
688 static function activeMembers($memberships, $status = 'active') {
690 if ($status == 'active') {
691 foreach ($memberships as $f => $v) {
692 if (!empty($v['active'])) {
698 elseif ($status == 'inactive') {
699 foreach ($memberships as $f => $v) {
700 if (empty($v['active'])) {
710 * Function to build Membership Block in Contribution Pages
712 * @param object $form form object
715 * @param boolean $formItems
716 * @param int $selectedMembershipTypeID selected membership id
717 * @param boolean $thankPage thank you page
718 * @param null $isTest
720 * @return bool Is this a separate membership payment
721 * @internal param int $pageId contribution page id
722 * @internal param bool $memContactId contact who is to be
723 * checked for having a current membership for a particular membership
727 static function buildMembershipBlock(&$form,
731 $selectedMembershipTypeID = NULL,
736 $separateMembershipPayment = FALSE;
737 if ($form->_membershipBlock
) {
738 $form->_currentMemberships
= array();
740 $membershipBlock = $form->_membershipBlock
;
741 $membershipTypeIds = $membershipTypes = $radio = array();
742 $membershipPriceset = (!empty($form->_priceSetId
) && $form->_useForMember
) ?
TRUE : FALSE;
744 $allowAutoRenewMembership = $autoRenewOption = FALSE;
745 $autoRenewMembershipTypeOptions = array();
747 $paymentProcessor = CRM_Core_PseudoConstant
::paymentProcessor(FALSE, FALSE, 'is_recur = 1');
749 $separateMembershipPayment = CRM_Utils_Array
::value('is_separate_payment', $membershipBlock);
751 if ($membershipPriceset) {
752 foreach ($form->_priceSet
['fields'] as $pField) {
753 if (empty($pField['options'])) {
756 foreach ($pField['options'] as $opId => $opValues) {
757 if (empty($opValues['membership_type_id'])) {
760 $membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id'];
764 elseif (!empty($membershipBlock['membership_types'])) {
765 $membershipTypeIds = explode(',', $membershipBlock['membership_types']);
768 if (!empty($membershipTypeIds)) {
769 //set status message if wrong membershipType is included in membershipBlock
770 if (isset($form->_mid
) && !$membershipPriceset) {
771 $membershipTypeID = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership',
775 if (!in_array($membershipTypeID, $membershipTypeIds)) {
776 CRM_Core_Session
::setStatus(ts("Oops. The membership you're trying to renew appears to be invalid. Contact your site administrator if you need assistance. If you continue, you will be issued a new membership."), ts('Invalid Membership'), 'error');
780 $membershipTypeValues = self
::buildMembershipTypeValues($form, $membershipTypeIds);
781 $form->_membershipTypeValues
= $membershipTypeValues;
783 foreach ($membershipTypeIds as $value) {
784 $memType = $membershipTypeValues[$value];
785 if ($selectedMembershipTypeID != NULL) {
786 if ($memType['id'] == $selectedMembershipTypeID) {
787 $form->assign('minimum_fee',
788 CRM_Utils_Array
::value('minimum_fee', $memType)
790 $form->assign('membership_name', $memType['name']);
791 if (!$thankPage && $cid) {
792 $membership = new CRM_Member_DAO_Membership();
793 $membership->contact_id
= $cid;
794 $membership->membership_type_id
= $memType['id'];
795 if ($membership->find(TRUE)) {
796 $form->assign('renewal_mode', TRUE);
797 $memType['current_membership'] = $membership->end_date
;
798 $form->_currentMemberships
[$membership->membership_type_id
] = $membership->membership_type_id
;
801 $membershipTypes[] = $memType;
804 elseif ($memType['is_active']) {
805 $javascriptMethod = NULL;
806 $allowAutoRenewOpt = 1;
807 if (is_array($form->_paymentProcessors
)){
808 foreach ($form->_paymentProcessors
as $id => $val) {
809 if (!$val['is_recur']) {
810 $allowAutoRenewOpt = 0;
816 $javascriptMethod = array('onclick' => "return showHideAutoRenew( this.value );");
817 $autoRenewMembershipTypeOptions["autoRenewMembershipType_{$value}"] = (int)$allowAutoRenewOpt * CRM_Utils_Array
::value($value, CRM_Utils_Array
::value('auto_renew', $form->_membershipBlock
));;
819 if ($allowAutoRenewOpt) {
820 $allowAutoRenewMembership = TRUE;
823 //add membership type.
824 $radio[$memType['id']] = $form->createElement('radio', NULL, NULL, NULL,
825 $memType['id'], $javascriptMethod
828 $membership = new CRM_Member_DAO_Membership();
829 $membership->contact_id
= $cid;
830 $membership->membership_type_id
= $memType['id'];
832 //show current membership, skip pending and cancelled membership records,
833 //because we take first membership record id for renewal
834 $membership->whereAdd('status_id != 5 AND status_id !=6');
836 if (!is_null($isTest)) {
837 $membership->is_test
= $isTest;
841 $membership->orderBy('end_date DESC');
843 if ($membership->find(TRUE)) {
844 if (!$membership->end_date
) {
845 unset($radio[$memType['id']]);
846 $form->assign('islifetime', TRUE);
849 $form->assign('renewal_mode', TRUE);
850 $form->_currentMemberships
[$membership->membership_type_id
] = $membership->membership_type_id
;
851 $memType['current_membership'] = $membership->end_date
;
853 $endDate = $memType['current_membership'];
854 $form->_defaultMemTypeId
= $memType['id'];
856 if ($memType['current_membership'] < $endDate) {
857 $endDate = $memType['current_membership'];
858 $form->_defaultMemTypeId
= $memType['id'];
862 $membershipTypes[] = $memType;
867 $form->assign('showRadio', $formItems);
869 if (!$membershipPriceset) {
870 if (!$membershipBlock['is_required']) {
871 $form->assign('showRadioNoThanks', TRUE);
872 $radio[''] = $form->createElement('radio', NULL, NULL, NULL, 'no_thanks', NULL);
873 $form->addGroup($radio, 'selectMembership', NULL);
875 elseif ($membershipBlock['is_required'] && count($radio) == 1) {
876 $temp = array_keys($radio);
877 $form->add('hidden', 'selectMembership', $temp[0], array('id' => 'selectMembership'));
878 $form->assign('singleMembership', TRUE);
879 $form->assign('showRadio', FALSE);
882 $form->addGroup($radio, 'selectMembership', NULL);
885 $form->addRule('selectMembership', ts('Please select one of the memberships.'), 'required');
888 $autoRenewOption = CRM_Price_BAO_PriceSet
::checkAutoRenewForPriceSet($form->_priceSetId
);
889 $form->assign('autoRenewOption', $autoRenewOption);
892 if (!$form->_values
['is_pay_later'] && is_array($form->_paymentProcessors
) && ($allowAutoRenewMembership ||
$autoRenewOption)) {
893 $form->addElement('checkbox', 'auto_renew', ts('Please renew my membership automatically.'));
898 $form->assign('membershipBlock', $membershipBlock);
899 $form->assign('membershipTypes', $membershipTypes);
900 $form->assign('allowAutoRenewMembership', $allowAutoRenewMembership);
901 $form->assign('autoRenewMembershipTypeOptions', json_encode($autoRenewMembershipTypeOptions));
903 //give preference to user submitted auto_renew value.
904 $takeUserSubmittedAutoRenew = (!empty($_POST) ||
$form->isSubmitted()) ?
TRUE : FALSE;
905 $form->assign('takeUserSubmittedAutoRenew', $takeUserSubmittedAutoRenew);
908 return $separateMembershipPayment;
912 * Function to return Membership Block info in Contribution Pages
917 * @internal param int $pageId contribution page id
921 static function getMembershipBlock($pageID) {
922 $membershipBlock = array();
923 $dao = new CRM_Member_DAO_MembershipBlock();
924 $dao->entity_table
= 'civicrm_contribution_page';
926 $dao->entity_id
= $pageID;
928 if ($dao->find(TRUE)) {
929 CRM_Core_DAO
::storeValues($dao, $membershipBlock);
930 if (!empty($membershipBlock['membership_types'])) {
931 $membershipTypes = unserialize($membershipBlock['membership_types']);
932 if (!is_array($membershipTypes)) {
933 return $membershipBlock;
936 foreach ($membershipTypes as $key => $value) {
937 $membershipBlock['auto_renew'][$key] = $value;
938 $memTypes[$key] = $key;
940 $membershipBlock['membership_types'] = implode(',', $memTypes);
947 return $membershipBlock;
951 * Function to return a current membership of given contact
952 * NB: if more than one membership meets criteria, a randomly selected one is returned.
954 * @param int $contactID contact id
955 * @param int $memType membership type, null to retrieve all types
957 * @param null $membershipId
958 * @param boolean $onlySameParentOrg true if only Memberships with same parent org as the $memType wanted, false otherwise
961 * @internal param int $membershipID if provided, then determine if it is current
964 static function getContactMembership($contactID, $memType, $isTest, $membershipId = NULL, $onlySameParentOrg = FALSE) {
965 $dao = new CRM_Member_DAO_Membership();
967 $dao->id
= $membershipId;
969 $dao->contact_id
= $contactID;
970 $dao->membership_type_id
= $memType;
972 //fetch proper membership record.
974 $dao->is_test
= $isTest;
977 $dao->whereAdd('is_test IS NULL OR is_test = 0');
980 //avoid pending membership as current membership: CRM-3027
981 $statusIds[] = array_search('Pending', CRM_Member_PseudoConstant
::membershipStatus());
982 if (!$membershipId) {
983 $statusIds[] = array_search('Cancelled', CRM_Member_PseudoConstant
::membershipStatus());
985 $dao->whereAdd('status_id NOT IN ( ' . implode(',', $statusIds) . ')');
987 // order by start date to find most recent membership first, CRM-4545
988 $dao->orderBy('start_date DESC');
991 if ($onlySameParentOrg && $memType) {
992 // require the same parent org as the $memType
993 $params = array('id' => $memType);
994 $membershipType = array();
995 if (CRM_Member_BAO_MembershipType
::retrieve($params, $membershipType)) {
996 $memberTypesSameParentOrg = CRM_Member_BAO_MembershipType
::getMembershipTypesByOrg($membershipType['member_of_contact_id']);
997 $memberTypesSameParentOrgList = implode(',', array_keys($memberTypesSameParentOrg));
998 $dao->whereAdd('membership_type_id IN (' . $memberTypesSameParentOrgList . ')');
1002 if ($dao->find(TRUE)) {
1003 $membership = array();
1004 CRM_Core_DAO
::storeValues($dao, $membership);
1005 $membership['is_current_member'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipStatus',
1006 $membership['status_id'],
1007 'is_current_member', 'id'
1013 if ($onlySameParentOrg && $memType) {
1014 // see if there is a membership that has same parent as $memType but different parent than $membershipID
1016 if ( $dao->id
&& CRM_Core_Permission
::check( 'edit memberships' ) ) {
1017 // CRM-10016, This is probably a backend renewal, and make sure we return the same membership thats being renewed.
1023 unset($dao->membership_type_id
);
1024 if ($dao->find(TRUE)) {
1025 $membership = array();
1026 CRM_Core_DAO
::storeValues($dao, $membership);
1027 $membership['is_current_member'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipStatus',
1028 $membership['status_id'],
1029 'is_current_member', 'id'
1038 * Combine all the importable fields from the lower levels object
1040 * @param string $contactType contact type
1041 * @param boolean $status
1043 * @return array array of importable Fields
1047 static function &importableFields($contactType = 'Individual', $status = TRUE) {
1048 if (!self
::$_importableFields) {
1049 if (!self
::$_importableFields) {
1050 self
::$_importableFields = array();
1054 $fields = array('' => array('title' => '- ' . ts('do not import') . ' -'));
1057 $fields = array('' => array('title' => '- ' . ts('Membership Fields') . ' -'));
1060 $tmpFields = CRM_Member_DAO_Membership
::import();
1061 $contactFields = CRM_Contact_BAO_Contact
::importableFields($contactType, NULL);
1063 // Using new Dedupe rule.
1064 $ruleParams = array(
1065 'contact_type' => $contactType,
1066 'used' => 'Unsupervised',
1068 $fieldsArray = CRM_Dedupe_BAO_Rule
::dedupeRuleFields($ruleParams);
1070 $tmpContactField = array();
1071 if (is_array($fieldsArray)) {
1072 foreach ($fieldsArray as $value) {
1073 $customFieldId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField',
1078 $value = $customFieldId ?
'custom_' . $customFieldId : $value;
1079 $tmpContactField[trim($value)] = CRM_Utils_Array
::value(trim($value), $contactFields);
1081 $title = $tmpContactField[trim($value)]['title'] . " " . ts('(match to contact)');
1084 $title = $tmpContactField[trim($value)]['title'];
1086 $tmpContactField[trim($value)]['title'] = $title;
1089 $tmpContactField['external_identifier'] = $contactFields['external_identifier'];
1090 $tmpContactField['external_identifier']['title'] = $contactFields['external_identifier']['title'] . " " . ts('(match to contact)');
1092 $tmpFields['membership_contact_id']['title'] = $tmpFields['membership_contact_id']['title'] . " " . ts('(match to contact)');;
1094 $fields = array_merge($fields, $tmpContactField);
1095 $fields = array_merge($fields, $tmpFields);
1096 $fields = array_merge($fields, CRM_Core_BAO_CustomField
::getFieldsForImport('Membership'));
1097 self
::$_importableFields = $fields;
1099 return self
::$_importableFields;
1103 * function to get all exportable fields
1105 * @retun array return array of all exportable fields
1108 static function &exportableFields() {
1109 $expFieldMembership = CRM_Member_DAO_Membership
::export();
1111 $expFieldsMemType = CRM_Member_DAO_MembershipType
::export();
1112 $fields = array_merge($expFieldMembership, $expFieldsMemType);
1113 $fields = array_merge($fields, $expFieldMembership);
1114 $membershipStatus = array(
1115 'membership_status' => array('title' => 'Membership Status',
1116 'name' => 'membership_status',
1117 'type' => CRM_Utils_Type
::T_STRING
,
1118 'where' => 'civicrm_membership_status.name',
1120 //CRM-6161 fix for customdata export
1121 $fields = array_merge($fields, $membershipStatus, CRM_Core_BAO_CustomField
::getFieldsForImport('Membership'));
1126 * Function to get membership joins/renewals for a specified membership
1127 * type. Specifically, retrieves a count of memberships whose "Membership
1128 * Signup" or "Membership Renewal" activity falls in the given date range.
1129 * Dates match the pattern "yyyy-mm-dd".
1131 * @param int $membershipTypeId membership type id
1132 * @param int $startDate date on which to start counting
1133 * @param int $endDate date on which to end counting
1134 * @param bool|int $isTest if true, membership is for a test site
1135 * @param bool|int $isOwner if true, only retrieve membership records for owners //LCD
1137 * @return integer the number of members of type $membershipTypeId whose
1138 * start_date is between $startDate and $endDate
1141 public static function getMembershipStarts($membershipTypeId, $startDate, $endDate, $isTest = 0, $isOwner = 0) {
1143 $testClause = 'membership.is_test = 1';
1145 $testClause = '( membership.is_test IS NULL OR membership.is_test = 0 )';
1148 if (!self
::$_signupActType ||
!self
::$_renewalActType) {
1149 self
::_getActTypes();
1152 if (!self
::$_signupActType ||
!self
::$_renewalActType) {
1157 SELECT COUNT(DISTINCT membership.id) as member_count
1158 FROM civicrm_membership membership
1159 INNER JOIN civicrm_activity activity ON (activity.source_record_id = membership.id AND activity.activity_type_id in (%1, %2))
1160 INNER JOIN civicrm_membership_status status ON ( membership.status_id = status.id AND status.is_current_member = 1 )
1161 INNER JOIN civicrm_contact contact ON ( contact.id = membership.contact_id AND contact.is_deleted = 0 )
1162 WHERE membership.membership_type_id = %3
1163 AND activity.activity_date_time >= '$startDate' AND activity.activity_date_time <= '$endDate 23:59:59'
1166 $query .= ($isOwner) ?
' AND owner_membership_id IS NULL' : '';
1169 1 => array(self
::$_signupActType, 'Integer'),
1170 2 => array(self
::$_renewalActType, 'Integer'),
1171 3 => array($membershipTypeId, 'Integer'),
1174 $memberCount = CRM_Core_DAO
::singleValueQuery($query, $params);
1175 return (int)$memberCount;
1179 * Function to get a count of membership for a specified membership type,
1180 * optionally for a specified date. The date must have the form yyyy-mm-dd.
1182 * If $date is omitted, this function counts as a member anyone whose
1183 * membership status_id indicates they're a current member.
1184 * If $date is given, this function counts as a member anyone who:
1185 * -- Has a start_date before $date and end_date after $date, or
1186 * -- Has a start_date before $date and is currently a member, as indicated
1187 * by the the membership's status_id.
1188 * The second condition takes care of records that have no end_date. These
1189 * are assumed to be lifetime memberships.
1191 * @param int $membershipTypeId membership type id
1192 * @param string $date the date for which to retrieve the count
1193 * @param bool|int $isTest if true, membership is for a test site
1194 * @param bool|int $isOwner if true, only retrieve membership records for owners //LCD
1196 * @return returns the number of members of type $membershipTypeId as of
1199 public static function getMembershipCount($membershipTypeId, $date = NULL, $isTest = 0, $isOwner = 0) {
1200 if (!CRM_Utils_Rule
::date($date)) {
1201 CRM_Core_Error
::fatal(ts('Invalid date "%1" (must have form yyyy-mm-dd).', array(1 => $date)));
1204 $params = array(1 => array($membershipTypeId, 'Integer'),
1205 2 => array($isTest, 'Boolean'),
1207 $query = "SELECT count(civicrm_membership.id ) as member_count
1208 FROM civicrm_membership left join civicrm_membership_status on ( civicrm_membership.status_id = civicrm_membership_status.id )
1209 WHERE civicrm_membership.membership_type_id = %1
1210 AND civicrm_membership.contact_id NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)
1211 AND civicrm_membership.is_test = %2";
1213 $query .= " AND civicrm_membership_status.is_current_member = 1";
1216 $query .= " AND civicrm_membership.start_date <= '$date' AND civicrm_membership_status.is_current_member = 1";
1219 $query .= ($isOwner) ?
' AND owner_membership_id IS NULL' : '';
1220 $memberCount = CRM_Core_DAO
::singleValueQuery($query, $params);
1221 return (int)$memberCount;
1225 * Function check the status of the membership before adding membership for a contact
1227 * @param int $contactId contact id
1231 static function statusAvailabilty($contactId) {
1232 $membership = new CRM_Member_DAO_MembershipStatus();
1233 $membership->whereAdd('is_active=1');
1234 return $membership->count();
1238 * Process the Memberships
1240 * @param array $membershipParams array of membership fields
1241 * @param int $contactID contact id
1242 * @param object $form form object
1244 * @param $premiumParams
1245 * @param null $customFieldsFormatted
1246 * @param null $includeFieldTypes
1248 * @param array $membershipDetails
1250 * @param array $membershipTypeID
1252 * @param bool $isPaidMembership
1253 * @param integer $membershipID
1255 * @param $isProcessSeparateMembershipTransaction
1257 * @param $defaultContributionTypeID
1259 * @throws CRM_Core_Exception
1260 * @internal param \isProcessSeparateMembershipTransaction $bool $
1262 * @internal param $singleMembershipTypeID
1267 public static function postProcessMembership($membershipParams, $contactID, &$form, $premiumParams,
1268 $customFieldsFormatted = NULL, $includeFieldTypes = NULL, $membershipDetails, $membershipTypeID, $isPaidMembership, $membershipID,
1269 $isProcessSeparateMembershipTransaction, $defaultContributionTypeID) {
1271 $isTest = CRM_Utils_Array
::value('is_test', $membershipParams, FALSE);
1272 $errors = $createdMemberships = array();
1274 if ($isPaidMembership) {
1275 $result = CRM_Contribute_BAO_Contribution_Utils
::processConfirm($form, $membershipParams,
1276 $premiumParams, $contactID,
1277 $defaultContributionTypeID,
1280 if (is_a($result[1], 'CRM_Core_Error')) {
1281 $errors[1] = CRM_Core_Error
::getMessages($result[1]);
1283 elseif (!empty($result[1])) {
1284 // Save the contribution ID so that I can be used in email receipts
1285 // For example, if you need to generate a tax receipt for the donation only.
1286 $form->_values
['contribution_other_id'] = $result[1]->id
;
1287 //note that this will be over-written if we are using a separate membership transaction. Otherwise there is only one
1288 $membershipContribution = $result[1];
1292 if ($isProcessSeparateMembershipTransaction) {
1294 $membershipContribution = self
::processSecondaryFinancialTransaction($contactID, $form, $membershipDetails, $membershipParams, $isTest);
1296 catch (CRM_Core_Exception
$e) {
1297 $errors[2] = $e->getMessage();
1298 $membershipContribution = NULL;
1303 if (!empty($membershipContribution) && !is_a($membershipContribution, 'CRM_Core_Error')) { {
1304 $membershipContributionID = $membershipContribution->id
;
1306 //@todo - why is this nested so deep? it seems like it could be just set on the calling function on the form layer
1307 if (isset($membershipParams['onbehalf']) && !empty($membershipParams['onbehalf']['member_campaign_id'])) {
1308 $form->_params
['campaign_id'] = $membershipParams['onbehalf']['member_campaign_id'];
1310 //@todo it should no longer be possible for it to get to this point & membership to not be an array
1311 if (is_array($membershipTypeID)) {
1312 $typesTerms = CRM_Utils_Array
::value('types_terms', $membershipParams, array());
1313 foreach ($membershipTypeID as $memType) {
1314 $numTerms = CRM_Utils_Array
::value($memType, $typesTerms, 1);
1315 $createdMemberships[$memType] = self
::createOrRenewMembership($membershipParams, $contactID, $customFieldsFormatted, $membershipID, $memType, $isTest, $numTerms, $membershipContribution, $form);
1317 if ($form->_priceSetId
&& !empty($form->_useForMember
) && !empty($form->_lineItem
)) {
1318 foreach ($form->_lineItem
[$form->_priceSetId
] as & $priceFieldOp) {
1319 if (!empty($priceFieldOp['membership_type_id']) &&
1320 isset($createdMemberships[$priceFieldOp['membership_type_id']])
1322 $membershipOb = $createdMemberships[$priceFieldOp['membership_type_id']];
1323 $priceFieldOp['start_date'] = $membershipOb->start_date ? CRM_Utils_Date
::customFormat($membershipOb->start_date
, '%d%f %b, %Y') : '-';
1324 $priceFieldOp['end_date'] = $membershipOb->end_date ? CRM_Utils_Date
::customFormat($membershipOb->end_date
, '%d%f %b, %Y') : '-';
1327 $priceFieldOp['start_date'] = $priceFieldOp['end_date'] = 'N/A';
1330 $form->_values
['lineItem'] = $form->_lineItem
;
1331 $form->assign('lineItem', $form->_lineItem
);
1336 if (!empty($errors)) {
1337 $message = self
::compileErrorMessage($errors);
1338 throw new CRM_Core_Exception($message);
1340 $form->_params
['createdMembershipIDs'] = array();
1342 // CRM-7851 - Moved after processing Payment Errors
1343 //@todo - the reasoning for this being here seems a little outdated
1344 foreach ($createdMemberships as $createdMembership) {
1345 CRM_Core_BAO_CustomValueTable
::postProcess(
1347 CRM_Core_DAO
::$_nullArray,
1348 'civicrm_membership',
1349 $createdMembership->id
,
1352 $form->_params
['createdMembershipIDs'][] = $createdMembership->id
;
1354 if(count($createdMemberships) == 1) {
1355 //presumably this is only relevant for exactly 1 membership
1356 $form->_params
['membershipID'] = $form->_values
['membership_id'] = $createdMembership->id
;
1359 if ($form->_contributeMode
== 'notify') {
1360 if ($form->_values
['is_monetary'] && $form->_amount
> 0.0 && !$form->_params
['is_pay_later']) {
1361 // call postProcess hook before leaving
1362 $form->postProcessHook();
1363 // this does not return
1364 $payment = CRM_Core_Payment
::singleton($form->_mode
, $form->_paymentProcessor
, $form);
1365 $payment->doTransferCheckout($form->_params
, 'contribute');
1369 if (isset($membershipContributionID)) {
1370 $form->_values
['contribution_id'] = $membershipContributionID;
1373 // Do not send an email if Recurring transaction is done via Direct Mode
1374 // Email will we sent when the IPN is received.
1375 if (!empty($form->_params
['is_recur']) && $form->_contributeMode
== 'direct') {
1379 //finally send an email receipt
1380 CRM_Contribute_BAO_ContributionPage
::sendMail($contactID,
1388 * Function for updating a membership record's contribution_recur_id
1390 * @param object CRM_Member_DAO_Membership $membership
1391 * @param \CRM_Contribute_BAO_Contribution|\CRM_Contribute_DAO_Contribution $contribution
1397 static public function updateRecurMembership(CRM_Member_DAO_Membership
$membership,
1398 CRM_Contribute_BAO_Contribution
$contribution) {
1400 if (empty($contribution->contribution_recur_id
)) {
1405 1 => array($contribution->contribution_recur_id
, 'Integer'),
1406 2 => array($membership->id
, 'Integer'),
1409 $sql = "UPDATE civicrm_membership SET contribution_recur_id = %1 WHERE id = %2";
1410 CRM_Core_DAO
::executeQuery($sql, $params);
1415 * A wrapper for renewing memberships from a form - including the form in the membership processing adds complexity
1416 * as the forms are being forced to pretend similarity
1417 * Try to call the renewMembership directly
1418 * @todo - this form method needs to have the interaction with the form layer removed from it
1419 * as a BAO function. Note that the api now supports membership renewals & it is not clear this function does anything
1420 * not done by the membership.create api (with a lot less unit tests)
1422 * This method will renew / create the membership depending on
1423 * whether the given contact has a membership or not. And will add
1424 * the modified dates for membership and in the log table.
1426 * @param int $contactID id of the contact
1427 * @param int $membershipTypeID id of the new membership type
1428 * @param boolean $is_test if this is test contribution or live contribution
1429 * @param object $form form object
1430 * @param null $changeToday
1431 * @param int $modifiedID individual contact id in case of On Behalf signup (CRM-4027 )
1432 * @param null $customFieldsFormatted
1433 * @param int $numRenewTerms how many membership terms are being added to end date (default is 1)
1435 * @param integer $membershipID Membership ID, this should always be passed in & optionality should be removed
1437 * @throws CRM_Core_Exception
1438 * @internal param array $ipnParams array of name value pairs, to be used (for e.g source) when $form not present
1439 * @return CRM_Member_DAO_Membership $membership object of membership
1444 static function renewMembershipFormWrapper(
1449 $changeToday = NULL,
1451 $customFieldsFormatted = NULL,
1453 $membershipID = NULL
1455 $statusFormat = '%Y-%m-%d';
1458 //@todo would be better to make $membershipID a compulsory function param & make form layer responsible for extracting it
1459 if(!$membershipID && isset($form->_membershipId
)) {
1460 $membershipID = $form->_membershipId
;
1463 //get all active statuses of membership.
1464 $allStatus = CRM_Member_PseudoConstant
::membershipStatus();
1466 $membershipTypeDetails = CRM_Member_BAO_MembershipType
::getMembershipTypeDetails($membershipTypeID);
1468 // check is it pending. - CRM-4555
1469 list($pending, $contributionRecurID, $changeToday, $membershipSource, $isPayLater, $campaignId) = self
::extractFormValues($form, $changeToday, $membershipTypeDetails);
1470 list($membership, $renewalMode, $dates) = self
::renewMembership($contactID, $membershipTypeID, $is_test, $changeToday, $modifiedID, $customFieldsFormatted, $numRenewTerms, $membershipID, $pending, $allStatus, $membershipTypeDetails, $contributionRecurID, $format, $membershipSource, $ids, $statusFormat, $isPayLater, $campaignId);
1471 $form->set('renewal_mode', $renewalMode);
1472 if (!empty($dates)) {
1473 $form->assign('mem_start_date',
1474 CRM_Utils_Date
::customFormat($dates['start_date'], $format)
1476 $form->assign('mem_end_date',
1477 CRM_Utils_Date
::customFormat($dates['end_date'], $format)
1485 * Method to fix membership status of stale membership
1487 * This method first checks if the membership is stale. If it is,
1488 * then status will be updated based on existing start and end
1489 * dates and log will be added for the status change.
1491 * @param array $currentMembership referance to the array
1492 * containing all values of
1493 * the current membership
1494 * @param array $changeToday array of month, day, year
1495 * values in case today needs
1496 * to be customised, null otherwise
1501 static function fixMembershipStatusBeforeRenew(&$currentMembership, $changeToday) {
1504 $today = CRM_Utils_Date
::processDate($changeToday, NULL, FALSE, 'Y-m-d');
1507 $status = CRM_Member_BAO_MembershipStatus
::getMembershipStatusByDate(
1508 CRM_Utils_Array
::value('start_date', $currentMembership),
1509 CRM_Utils_Array
::value('end_date', $currentMembership),
1510 CRM_Utils_Array
::value('join_date', $currentMembership),
1513 $currentMembership['membership_type_id'],
1517 if (empty($status) ||
1518 empty($status['id'])
1520 CRM_Core_Error
::fatal(ts('Oops, it looks like there is no valid membership status corresponding to the membership start and end dates for this membership. Contact the site administrator for assistance.'));
1523 $currentMembership['today_date'] = $today;
1525 if ($status['id'] !== $currentMembership['status_id']) {
1526 $memberDAO = new CRM_Member_DAO_Membership();
1527 $memberDAO->id
= $currentMembership['id'];
1528 $memberDAO->find(TRUE);
1530 $memberDAO->status_id
= $status['id'];
1531 $memberDAO->join_date
= CRM_Utils_Date
::isoToMysql($memberDAO->join_date
);
1532 $memberDAO->start_date
= CRM_Utils_Date
::isoToMysql($memberDAO->start_date
);
1533 $memberDAO->end_date
= CRM_Utils_Date
::isoToMysql($memberDAO->end_date
);
1535 CRM_Core_DAO
::storeValues($memberDAO, $currentMembership);
1538 $currentMembership['is_current_member'] = CRM_Core_DAO
::getFieldValue(
1539 'CRM_Member_DAO_MembershipStatus',
1540 $currentMembership['status_id'],
1546 'membership_id' => $currentMembership['id'],
1547 'status_id' => $status['id'],
1548 'start_date' => CRM_Utils_Date
::customFormat(
1549 $currentMembership['start_date'],
1552 'end_date' => CRM_Utils_Date
::customFormat(
1553 $currentMembership['end_date'],
1556 'modified_date' => CRM_Utils_Date
::customFormat(
1557 $currentMembership['today_date'],
1560 'membership_type_id' => $currentMembership['membership_type_id'],
1561 'max_related' => $currentMembership['max_related'],
1564 $session = CRM_Core_Session
::singleton();
1565 // If we have an authenticated session, set modified_id to that user's contact_id, else set to membership.contact_id
1566 if ($session->get('userID')) {
1567 $logParams['modified_id'] = $session->get('userID');
1570 $logParams['modified_id'] = $currentMembership['contact_id'];
1572 CRM_Member_BAO_MembershipLog
::add($logParams, CRM_Core_DAO
::$_nullArray);
1577 * Function to get the contribution page id from the membership record
1579 * @param int membershipId membership id
1581 * @return int $contributionPageId contribution page id
1585 static function getContributionPageId($membershipID) {
1587 SELECT c.contribution_page_id as pageID
1588 FROM civicrm_membership_payment mp, civicrm_contribution c
1589 WHERE mp.contribution_id = c.id
1590 AND c.contribution_page_id IS NOT NULL
1591 AND mp.membership_id = " . CRM_Utils_Type
::escape($membershipID, 'Integer')
1592 . " ORDER BY mp.id DESC";
1594 return CRM_Core_DAO
::singleValueQuery($query,
1595 CRM_Core_DAO
::$_nullArray
1600 * Function to updated related memberships
1602 * @param int $ownerMembershipId owner Membership Id
1603 * @param array $params formatted array of key => value..
1606 static function updateRelatedMemberships($ownerMembershipId, $params) {
1607 $membership = new CRM_Member_DAO_Membership();
1608 $membership->owner_membership_id
= $ownerMembershipId;
1609 $membership->find();
1611 while ($membership->fetch()) {
1612 $relatedMembership = new CRM_Member_DAO_Membership();
1613 $relatedMembership->id
= $membership->id
;
1614 $relatedMembership->copyValues($params);
1615 $relatedMembership->save();
1616 $relatedMembership->free();
1619 $membership->free();
1623 * Function to get list of membership fields for profile
1624 * For now we only allow custom membership fields to be in
1629 * @return array the list of membership fields
1633 static function getMembershipFields($mode = NULL) {
1634 $fields = CRM_Member_DAO_Membership
::export();
1636 unset($fields['membership_contact_id']);
1637 $fields = array_merge($fields, CRM_Core_BAO_CustomField
::getFieldsForImport('Membership'));
1639 $membershipType = CRM_Member_DAO_MembershipType
::export();
1641 $membershipStatus = CRM_Member_DAO_MembershipStatus
::export();
1643 $fields = array_merge($fields, $membershipType, $membershipStatus);
1649 * function to get the sort name of a contact for a particular membership
1651 * @param int $id id of the membership
1653 * @return null|string sort name of the contact if found
1657 static function sortName($id) {
1658 $id = CRM_Utils_Type
::escape($id, 'Integer');
1661 SELECT civicrm_contact.sort_name
1662 FROM civicrm_membership, civicrm_contact
1663 WHERE civicrm_membership.contact_id = civicrm_contact.id
1664 AND civicrm_membership.id = {$id}
1666 return CRM_Core_DAO
::singleValueQuery($query, CRM_Core_DAO
::$_nullArray);
1670 * function to create memberships for related contacts
1671 * takes into account the maximum related memberships
1673 * @param array $params array of key - value pairs
1676 * @internal param object $membership membership object
1678 * @return null|relatedMembership array of memberships if created
1682 static function createRelatedMemberships(&$params, &$dao) {
1683 static $relatedContactIds = array();
1685 $membership = new CRM_Member_DAO_Membership();
1686 $membership->id
= $dao->id
;
1688 // required since create method doesn't return all the
1689 // parameters in the returned membership object
1690 if (!$membership->find(TRUE)) {
1693 $deceasedStatusId = array_search('Deceased', CRM_Member_PseudoConstant
::membershipStatus());
1694 // FIXME : While updating/ renewing the
1695 // membership, if the relationship is PAST then
1696 // the membership of the related contact must be
1698 // For that, getting Membership Status for which
1699 // is_current_member is 0. It works for the
1700 // generated data as there is only one membership
1701 // status having is_current_member = 0.
1702 // But this wont work exactly if there will be
1703 // more than one status having is_current_member = 0.
1704 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
1705 $membershipStatus->is_current_member
= 0;
1706 if ($membershipStatus->find(TRUE)) {
1707 $expiredStatusId = $membershipStatus->id
;
1709 $expiredStatusId = array_search('Expired', CRM_Member_PseudoConstant
::membershipStatus());
1712 $allRelatedContacts = array();
1713 $relatedContacts = array();
1714 if (!is_a($membership, 'CRM_Core_Error')) {
1715 $allRelatedContacts = CRM_Member_BAO_Membership
::checkMembershipRelationship($membership->id
,
1716 $membership->contact_id
,
1717 CRM_Utils_Array
::value('action', $params)
1721 // check for loops. CRM-4213
1722 // remove repeated related contacts, which already inherited membership.
1723 $relatedContactIds[$membership->contact_id
] = TRUE;
1724 foreach ($allRelatedContacts as $cid => $status) {
1725 if (empty($relatedContactIds[$cid])) {
1726 $relatedContactIds[$cid] = TRUE;
1728 //don't create membership again for owner contact.
1729 $nestedRelationship = FALSE;
1730 if ($membership->owner_membership_id
) {
1731 $nestedRelMembership = new CRM_Member_DAO_Membership();
1732 $nestedRelMembership->id
= $membership->owner_membership_id
;
1733 $nestedRelMembership->contact_id
= $cid;
1734 $nestedRelationship = $nestedRelMembership->find(TRUE);
1735 $nestedRelMembership->free();
1737 if (!$nestedRelationship) {
1738 $relatedContacts[$cid] = $status;
1743 //lets cleanup related membership if any.
1744 if (empty($relatedContacts)) {
1745 self
::deleteRelatedMemberships($membership->id
);
1748 // Edit the params array
1749 unset($params['id']);
1750 // Reminder should be sent only to the direct membership
1751 unset($params['reminder_date']);
1752 // unset the custom value ids
1753 if (is_array(CRM_Utils_Array
::value('custom', $params))) {
1754 foreach ($params['custom'] as $k => $v) {
1755 unset($params['custom'][$k]['id']);
1758 if (!isset($params['membership_type_id'])) {
1759 $params['membership_type_id'] = $membership->membership_type_id
;
1762 // max_related should be set in the parent membership
1763 unset($params['max_related']);
1764 // Number of inherited memberships available - NULL is interpreted as unlimited, '0' as none
1765 $available = ($membership->max_related
== NULL ? PHP_INT_MAX
: $membership->max_related
);
1766 $queue = array(); // will be used to queue potential memberships to be created
1768 foreach ($relatedContacts as $contactId => $relationshipStatus) {
1769 //use existing membership record.
1770 $relMembership = new CRM_Member_DAO_Membership();
1771 $relMembership->contact_id
= $contactId;
1772 $relMembership->owner_membership_id
= $membership->id
;
1773 $relMemIds = array();
1774 if ($relMembership->find(TRUE)) {
1775 $params['id'] = $relMemIds['membership'] = $relMembership->id
;
1777 $params['contact_id'] = $contactId;
1778 $params['owner_membership_id'] = $membership->id
;
1780 // set status_id as it might have been changed for
1781 // past relationship
1782 $params['status_id'] = $membership->status_id
;
1784 if ($deceasedStatusId &&
1785 CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'is_deceased')
1787 $params['status_id'] = $deceasedStatusId;
1789 elseif ((CRM_Utils_Array
::value('action', $params) & CRM_Core_Action
::UPDATE
) &&
1790 ($relationshipStatus == CRM_Contact_BAO_Relationship
::PAST
)
1792 $params['status_id'] = $expiredStatusId;
1795 //don't calculate status again in create( );
1796 $params['skipStatusCal'] = TRUE;
1798 //do create activity if we changed status.
1799 if ($params['status_id'] != $relMembership->status_id
) {
1800 $params['createActivity'] = TRUE;
1803 // we should not created contribution record for related contacts, CRM-3371
1804 unset($params['contribution_status_id']);
1806 if (($params['status_id'] == $deceasedStatusId) ||
($params['status_id'] == $expiredStatusId)) {
1807 // related membership is not active so does not count towards maximum
1808 CRM_Member_BAO_Membership
::create($params, $relMemIds);
1811 // related membership already exists, so this is just an update
1812 if (isset($params['id'])) {
1813 if ($available > 0) {
1814 CRM_Member_BAO_Membership
::create($params, $relMemIds);
1816 } else { // we have run out of inherited memberships, so delete extras
1817 self
::deleteMembership($params['id']);
1819 // we need to first check if there will remain inherited memberships, so queue it up
1825 // now go over the queue and create any available related memberships
1827 while (($available > 0) && ($params = each($queue))) {
1828 CRM_Member_BAO_Membership
::create($params['value'], $relMemIds);
1835 * Delete the record that are associated with this Membership Payment
1837 * @param int $membershipId membsership id.
1839 * @return boolean true if deleted false otherwise
1842 static function deleteMembershipPayment($membershipId) {
1844 $membesrshipPayment = new CRM_Member_DAO_MembershipPayment();
1845 $membesrshipPayment->membership_id
= $membershipId;
1846 $membesrshipPayment->find();
1848 while ($membesrshipPayment->fetch()) {
1849 CRM_Contribute_BAO_Contribution
::deleteContribution($membesrshipPayment->contribution_id
);
1850 CRM_Utils_Hook
::pre('delete', 'MembershipPayment', $membesrshipPayment->id
, $membesrshipPayment);
1851 $membesrshipPayment->delete();
1852 CRM_Utils_Hook
::post('delete', 'MembershipPayment', $membesrshipPayment->id
, $membesrshipPayment);
1854 return $membesrshipPayment;
1859 * @param null $membershipTypeID
1863 static function &buildMembershipTypeValues(&$form, $membershipTypeID = NULL) {
1864 $whereClause = " WHERE domain_id = ". CRM_Core_Config
::domainID();
1866 if (is_array($membershipTypeID)) {
1867 $allIDs = implode(',', $membershipTypeID);
1868 $whereClause .= " AND id IN ( $allIDs )";
1870 elseif (is_numeric($membershipTypeID) &&
1871 $membershipTypeID > 0
1873 $whereClause .= " AND id = $membershipTypeID";
1878 FROM civicrm_membership_type
1881 $dao = CRM_Core_DAO
::executeQuery($query);
1883 $membershipTypeValues = array();
1884 $membershipTypeFields = array(
1885 'id', 'minimum_fee', 'name', 'is_active',
1886 'description', 'financial_type_id', 'auto_renew','member_of_contact_id',
1887 'relationship_type_id', 'relationship_direction', 'max_related',
1890 while ($dao->fetch()) {
1891 $membershipTypeValues[$dao->id
] = array();
1892 foreach ($membershipTypeFields as $mtField) {
1893 $membershipTypeValues[$dao->id
][$mtField] = $dao->$mtField;
1898 CRM_Utils_Hook
::membershipTypeValues($form, $membershipTypeValues);
1900 if (is_numeric($membershipTypeID) &&
1901 $membershipTypeID > 0
1903 return $membershipTypeValues[$membershipTypeID];
1906 return $membershipTypeValues;
1911 * Function to get membership record count for a Contact
1914 * @param boolean $activeOnly
1916 * @internal param int $contactId Contact ID
1917 * @return int count of membership records
1921 static function getContactMembershipCount($contactID, $activeOnly = FALSE) {
1922 $select = "SELECT count(*) FROM civicrm_membership ";
1923 $where = "WHERE civicrm_membership.contact_id = {$contactID} AND civicrm_membership.is_test = 0 ";
1925 // CRM-6627, all status below 3 (active, pending, grace) are considered active
1927 $select .= " INNER JOIN civicrm_membership_status ON civicrm_membership.status_id = civicrm_membership_status.id ";
1928 $where .= " and civicrm_membership_status.is_current_member = 1";
1931 $query = $select . $where;
1932 return CRM_Core_DAO
::singleValueQuery($query);
1936 * Function to check whether payment processor supports
1937 * cancellation of membership subscription
1939 * @param int $mid membership id
1941 * @param bool $isNotCancelled
1947 static function isCancelSubscriptionSupported($mid, $isNotCancelled = TRUE) {
1948 $cacheKeyString = "$mid";
1949 $cacheKeyString .= $isNotCancelled ?
'_1' : '_0';
1951 static $supportsCancel = array();
1953 if (!array_key_exists($cacheKeyString, $supportsCancel)) {
1954 $supportsCancel[$cacheKeyString] = FALSE;
1955 $isCancelled = FALSE;
1957 if ($isNotCancelled) {
1958 $isCancelled = self
::isSubscriptionCancelled($mid);
1961 $paymentObject = CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($mid, 'membership', 'obj');
1962 if (!empty($paymentObject)) {
1963 $supportsCancel[$cacheKeyString] = $paymentObject->isSupported('cancelSubscription') && !$isCancelled;
1966 return $supportsCancel[$cacheKeyString];
1970 * Function to check whether subscription is already cancelled
1972 * @param int $mid membership id
1974 * @return string $status contribution status
1978 static function isSubscriptionCancelled($mid) {
1980 SELECT cr.contribution_status_id
1981 FROM civicrm_contribution_recur cr
1982 LEFT JOIN civicrm_membership mem ON ( cr.id = mem.contribution_recur_id )
1983 WHERE mem.id = %1 LIMIT 1";
1984 $params = array(1 => array($mid, 'Integer'));
1985 $statusId = CRM_Core_DAO
::singleValueQuery($sql, $params);
1986 $status = CRM_Contribute_PseudoConstant
::contributionStatus($statusId);
1987 if ($status == 'Cancelled') {
1994 * Function to get membership joins for a specified membership
1995 * type. Specifically, retrieves a count of still current memberships whose
1996 * join_date and start_date are within a specified date range. Dates match
1997 * the pattern "yyyy-mm-dd".
1999 * @param int $membershipTypeId membership type id
2000 * @param int $startDate date on which to start counting
2001 * @param int $endDate date on which to end counting
2002 * @param bool|int $isTest if true, membership is for a test site
2004 * @return returns the number of members of type $membershipTypeId
2005 * whose join_date is between $startDate and $endDate and
2006 * whose start_date is between $startDate and $endDate
2008 static function getMembershipJoins($membershipTypeId, $startDate, $endDate, $isTest = 0) {
2009 $testClause = 'membership.is_test = 1';
2011 $testClause = '( membership.is_test IS NULL OR membership.is_test = 0 )';
2013 if (!self
::$_signupActType) {
2014 self
::_getActTypes();
2017 if (!self
::$_signupActType) {
2022 SELECT COUNT(DISTINCT membership.id) as member_count
2023 FROM civicrm_membership membership
2024 INNER JOIN civicrm_activity activity ON (activity.source_record_id = membership.id AND activity.activity_type_id = %1)
2025 INNER JOIN civicrm_membership_status status ON ( membership.status_id = status.id AND status.is_current_member = 1 )
2026 INNER JOIN civicrm_contact contact ON ( contact.id = membership.contact_id AND contact.is_deleted = 0 )
2027 WHERE membership.membership_type_id = %2
2028 AND activity.activity_date_time >= '$startDate' AND activity.activity_date_time <= '$endDate 23:59:59'
2032 1 => array(self
::$_signupActType, 'Integer'),
2033 2 => array($membershipTypeId, 'Integer'),
2036 $memberCount = CRM_Core_DAO
::singleValueQuery($query, $params);
2038 return (int)$memberCount;
2042 * Function to get membership renewals for a specified membership
2043 * type. Specifically, retrieves a count of still current memberships
2044 * whose join_date is before and start_date is within a specified date
2045 * range. Dates match the pattern "yyyy-mm-dd".
2047 * @param int $membershipTypeId membership type id
2048 * @param int $startDate date on which to start counting
2049 * @param int $endDate date on which to end counting
2050 * @param bool|int $isTest if true, membership is for a test site
2052 * @return integer returns the number of members of type $membershipTypeId
2053 * whose join_date is before $startDate and
2054 * whose start_date is between $startDate and $endDate
2056 static function getMembershipRenewals($membershipTypeId, $startDate, $endDate, $isTest = 0) {
2057 $testClause = 'membership.is_test = 1';
2059 $testClause = '( membership.is_test IS NULL OR membership.is_test = 0 )';
2061 if (!self
::$_renewalActType) {
2062 self
::_getActTypes();
2065 if (!self
::$_renewalActType) {
2070 SELECT COUNT(DISTINCT membership.id) as member_count
2071 FROM civicrm_membership membership
2072 INNER JOIN civicrm_activity activity ON (activity.source_record_id = membership.id AND activity.activity_type_id = %1)
2073 INNER JOIN civicrm_membership_status status ON ( membership.status_id = status.id AND status.is_current_member = 1 )
2074 INNER JOIN civicrm_contact contact ON ( contact.id = membership.contact_id AND contact.is_deleted = 0 )
2075 WHERE membership.membership_type_id = %2
2076 AND activity.activity_date_time >= '$startDate' AND activity.activity_date_time <= '$endDate 23:59:59'
2080 1 => array(self
::$_renewalActType, 'Integer'),
2081 2 => array($membershipTypeId, 'Integer'),
2083 $memberCount = CRM_Core_DAO
::singleValueQuery($query, $params);
2085 return (int)$memberCount;
2089 * Where a second separate financial transaction is supported we will process it here
2093 * @param $membershipDetails
2094 * @param $tempParams
2099 * @internal param $minimumFee
2100 * @return CRM_Contribute_BAO_Contribution
2102 public static function processSecondaryFinancialTransaction($contactID, &$form, $membershipDetails, $tempParams, $isTest) {
2103 $minimumFee = CRM_Utils_Array
::value('minimum_fee', $membershipDetails);
2104 $lineItems = $form->_lineItem
= $form->_memLineItem
;
2105 $contributionType = new CRM_Financial_DAO_FinancialType();
2106 $contributionType->id
= CRM_Utils_Array
::value('financial_type_id', $membershipDetails);
2107 if (!$contributionType->find(TRUE)) {
2108 CRM_Core_Error
::fatal(ts("Could not find a system table"));
2110 $tempParams['amount'] = $minimumFee;
2111 $tempParams['invoiceID'] = md5(uniqid(rand(), TRUE));
2114 if ($form->_values
['is_monetary'] && !$form->_params
['is_pay_later'] && $minimumFee > 0.0) {
2115 $payment = CRM_Core_Payment
::singleton($form->_mode
, $form->_paymentProcessor
, $form);
2117 if ($form->_contributeMode
== 'express') {
2118 $result = $payment->doExpressCheckout($tempParams);
2121 $result = $payment->doDirectPayment($tempParams);
2125 if (is_a($result, 'CRM_Core_Error')) {
2126 throw new CRM_Core_Exception(CRM_Core_Error
::getMessages($result));
2129 //assign receive date when separate membership payment
2130 //and contribution amount not selected.
2131 if ($form->_amount
== 0) {
2132 $now = date('YmdHis');
2133 $form->_params
['receive_date'] = $now;
2134 $receiveDate = CRM_Utils_Date
::mysqlToIso($now);
2135 $form->set('params', $form->_params
);
2136 $form->assign('receive_date', $receiveDate);
2139 $form->set('membership_trx_id', $result['trxn_id']);
2140 $form->set('membership_amount', $minimumFee);
2142 // we don't need to create the user twice, so lets disable cms_create_account
2143 // irrespective of the value, CRM-2888
2144 $tempParams['cms_create_account'] = 0;
2146 $pending = $form->_params
['is_pay_later'] ?
((CRM_Utils_Array
::value('minimum_fee', $membershipDetails, 0) > 0.0) ?
TRUE : FALSE) : FALSE;
2148 //set this variable as we are not creating pledge for
2149 //separate membership payment contribution.
2150 //so for differentiating membership contributon from
2151 //main contribution.
2152 $form->_params
['separate_membership_payment'] = 1;
2153 $membershipContribution = CRM_Contribute_Form_Contribution_Confirm
::processContribution($form,
2163 return $membershipContribution;
2168 * Create linkages between membership & contribution - note this is the wrong place for this code but this is a
2169 * refactoring step. This should be BAO functionality
2170 * @param $membership
2171 * @param $membershipContribution
2173 public static function linkMembershipPayment($membership, $membershipContribution) {
2174 CRM_Member_BAO_MembershipPayment
::create(array('membership_id' => $membership->id
, 'contribution_id' => $membershipContribution->id
));
2178 * @param $membershipParams
2180 * @param $customFieldsFormatted
2181 * @param $membershipID
2185 * @param $membershipContribution
2188 * @internal param $createdMemberships
2192 public static function createOrRenewMembership($membershipParams, $contactID, $customFieldsFormatted, $membershipID, $memType, $isTest, $numTerms, $membershipContribution, &$form) {
2193 $membership = self
::renewMembershipFormWrapper($contactID, $memType,
2194 $isTest, $form, NULL,
2195 CRM_Utils_Array
::value('cms_contactID', $membershipParams),
2196 $customFieldsFormatted, $numTerms,
2200 // update recurring id for membership record
2201 self
::updateRecurMembership($membership, $membershipContribution);
2203 if (!empty($membershipContribution)) {
2204 self
::linkMembershipPayment($membership, $membershipContribution);
2210 * Turn array of errors into message string
2212 * @param array $errors
2214 * @internal param $message
2218 public static function compileErrorMessage($errors)
2220 foreach($errors as $error) {
2221 if (is_string($error)) {
2222 $message[] = $error;
2225 return ts('Payment Processor Error message') . ': ' . implode('<br/>', $message);
2229 * Extract relevant values from the form so we can separate form logic from BAO logcis
2231 * @param $changeToday
2232 * @param $membershipTypeDetails
2236 public static function extractFormValues($form, $changeToday, $membershipTypeDetails)
2239 //@todo this is a BAO function & should not inspect the form - the form should do this
2240 // & pass required params to the BAO
2241 if (CRM_Utils_Array
::value('minimum_fee', $membershipTypeDetails) > 0.0) {
2242 if (((isset($form->_contributeMode
) && $form->_contributeMode
== 'notify') ||
!empty($form->_params
['is_pay_later']) ||
2243 (!empty($form->_params
['is_recur']) && $form->_contributeMode
== 'direct'
2246 (($form->_values
['is_monetary'] && $form->_amount
> 0.0) ||
!empty($form->_params
['separate_membership_payment']) ||
2247 CRM_Utils_Array
::value('record_contribution', $form->_params
)
2253 $contributionRecurID = isset($form->_params
['contributionRecurID']) ?
$form->_params
['contributionRecurID'] : NULL;
2255 //we renew expired membership, CRM-6277
2256 if (!$changeToday) {
2257 if ($form->get('renewalDate')) {
2258 $changeToday = $form->get('renewalDate');
2260 elseif (get_class($form) == 'CRM_Contribute_Form_Contribution_Confirm') {
2261 $changeToday = date('YmdHis');
2265 $membershipSource = NULL;
2266 if (!empty($form->_params
['membership_source'])) {
2267 $membershipSource = $form->_params
['membership_source'];
2269 elseif (isset($form->_values
['title']) && !empty($form->_values
['title'])) {
2270 $membershipSource = ts('Online Contribution:') . ' ' . $form->_values
['title'];
2273 if(isset($form->_params
)) {
2274 $isPayLater = CRM_Utils_Array
::value('is_pay_later', $form->_params
);
2277 if (isset($form->_values
) && is_array($form->_values
) && !empty($form->_values
)) {
2278 $campaignId = CRM_Utils_Array
::value('campaign_id', $form->_params
);
2279 if (!array_key_exists('campaign_id', $form->_params
)) {
2280 $campaignId = CRM_Utils_Array
::value('campaign_id', $form->_values
);
2283 return array($pending, $contributionRecurID, $changeToday, $membershipSource, $isPayLater, $campaignId);
2287 * @param integer $contactID
2288 * @param $membershipTypeID
2289 * @param bool $is_test
2290 * @param $changeToday
2291 * @param integer $modifiedID
2292 * @param $customFieldsFormatted
2293 * @param $numRenewTerms
2294 * @param $membershipID
2297 * @param array $membershipTypeDetails
2298 * @param integer $contributionRecurID
2300 * @param $membershipSource
2302 * @param $statusFormat
2303 * @param $isPayLater
2304 * @param integer $campaignId
2306 * @throws CRM_Core_Exception
2309 public static function renewMembership($contactID, $membershipTypeID, $is_test, $changeToday, $modifiedID, $customFieldsFormatted, $numRenewTerms, $membershipID, $pending, $allStatus, $membershipTypeDetails, $contributionRecurID, $format, $membershipSource, $ids, $statusFormat, $isPayLater, $campaignId) {
2310 $renewalMode = $updateStatusId = FALSE;
2312 // CRM-7297 - allow membership type to be be changed during renewal so long as the parent org of new membershipType
2313 // is the same as the parent org of an existing membership of the contact
2314 $currentMembership = CRM_Member_BAO_Membership
::getContactMembership($contactID, $membershipTypeID,
2315 $is_test, $membershipID, TRUE
2317 if ($currentMembership) {
2318 $activityType = 'Membership Renewal';
2319 $renewalMode = TRUE;
2321 // Do NOT do anything.
2322 //1. membership with status : PENDING/CANCELLED (CRM-2395)
2323 //2. Paylater/IPN renew. CRM-4556.
2324 if ($pending ||
in_array($currentMembership['status_id'], array(array_search('Pending', $allStatus),
2325 array_search('Cancelled', $allStatus),
2328 $membership = new CRM_Member_DAO_Membership();
2329 $membership->id
= $currentMembership['id'];
2330 $membership->find(TRUE);
2332 // CRM-8141 create a membership_log entry so that we will know the membership_type_id to change to when payment completed
2334 // note that we are logging the requested new membership_type_id that may be different than current membership_type_id
2335 // it will be used when payment is received to update the membership_type_id to what was paid for
2337 'membership_id' => $membership->id
,
2338 'status_id' => $membership->status_id
,
2339 'start_date' => CRM_Utils_Date
::customFormat(
2340 $membership->start_date
,
2343 'end_date' => CRM_Utils_Date
::customFormat(
2344 $membership->end_date
,
2347 'modified_date' => CRM_Utils_Date
::customFormat(
2351 'membership_type_id' => $membershipTypeID,
2352 'max_related' => $membershipTypeDetails['max_related'],
2354 $session = CRM_Core_Session
::singleton();
2355 // If we have an authenticated session, set modified_id to that user's contact_id, else set to membership.contact_id
2356 if ($session->get('userID')) {
2357 $logParams['modified_id'] = $session->get('userID');
2360 $logParams['modified_id'] = $membership->contact_id
;
2362 CRM_Member_BAO_MembershipLog
::add($logParams, CRM_Core_DAO
::$_nullArray);
2364 if (!empty($contributionRecurID)) {
2365 CRM_Core_DAO
::setFieldValue('CRM_Member_DAO_Membership', $membership->id
,
2366 'contribution_recur_id', $contributionRecurID
2373 // Check and fix the membership if it is STALE
2374 self
::fixMembershipStatusBeforeRenew($currentMembership, $changeToday);
2376 // Now Renew the membership
2377 if (!$currentMembership['is_current_member']) {
2378 // membership is not CURRENT
2380 // CRM-7297 Membership Upsell - calculate dates based on new membership type
2381 $dates = CRM_Member_BAO_MembershipType
::getRenewalDatesForMembershipType($currentMembership['id'],
2387 $currentMembership['join_date'] = CRM_Utils_Date
::customFormat($currentMembership['join_date'], $format);
2388 $currentMembership['start_date'] = CRM_Utils_Array
::value('start_date', $dates);
2389 $currentMembership['end_date'] = CRM_Utils_Array
::value('end_date', $dates);
2390 $currentMembership['is_test'] = $is_test;
2392 if (!empty($membershipSource)) {
2393 $currentMembership['source'] = $membershipSource;
2396 $currentMembership['source'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership',
2397 $currentMembership['id'],
2402 if (!empty($currentMembership['id'])) {
2403 $ids['membership'] = $currentMembership['id'];
2405 $memParams = $currentMembership;
2406 $memParams['membership_type_id'] = $membershipTypeID;
2408 //set the log start date.
2409 $memParams['log_start_date'] = CRM_Utils_Date
::customFormat($dates['log_start_date'], $format);
2413 // CURRENT Membership
2414 $membership = new CRM_Member_DAO_Membership();
2415 $membership->id
= $currentMembership['id'];
2416 $membership->find(TRUE);
2417 // CRM-7297 Membership Upsell - calculate dates based on new membership type
2418 $dates = CRM_Member_BAO_MembershipType
::getRenewalDatesForMembershipType($membership->id
,
2424 // Insert renewed dates for CURRENT membership
2425 $memParams = array();
2426 $memParams['join_date'] = CRM_Utils_Date
::isoToMysql($membership->join_date
);
2427 $memParams['start_date'] = CRM_Utils_Date
::isoToMysql($membership->start_date
);
2428 $memParams['end_date'] = CRM_Utils_Array
::value('end_date', $dates);
2429 $memParams['membership_type_id'] = $membershipTypeID;
2431 //set the log start date.
2432 $memParams['log_start_date'] = CRM_Utils_Date
::customFormat($dates['log_start_date'], $format);
2433 if (empty($membership->source
)) {
2434 if (!empty($membershipSource)) {
2435 $memParams['source'] = $membershipSource;
2438 $memParams['source'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership',
2439 $currentMembership['id'],
2445 if (!empty($currentMembership['id'])) {
2446 $ids['membership'] = $currentMembership['id'];
2451 $updateStatusId = array_search('Pending', $allStatus);
2457 $activityType = 'Membership Signup';
2459 'contact_id' => $contactID,
2460 'membership_type_id' => $membershipTypeID,
2464 $dates = CRM_Member_BAO_MembershipType
::getDatesForMembershipType($membershipTypeID, NULL, NULL, NULL, $numRenewTerms);
2466 $memParams['join_date'] = CRM_Utils_Array
::value('join_date', $dates);
2467 $memParams['start_date'] = CRM_Utils_Array
::value('start_date', $dates);
2468 $memParams['end_date'] = CRM_Utils_Array
::value('end_date', $dates);
2470 $status = CRM_Member_BAO_MembershipStatus
::getMembershipStatusByDate(CRM_Utils_Date
::customFormat($dates['start_date'],
2473 CRM_Utils_Date
::customFormat($dates['end_date'],
2476 CRM_Utils_Date
::customFormat($dates['join_date'],
2484 $updateStatusId = CRM_Utils_Array
::value('id', $status);
2487 // if IPN/Pay-Later set status to: PENDING
2488 $updateStatusId = array_search('Pending', $allStatus);
2491 if (!empty($membershipSource)) {
2492 $memParams['source'] = $membershipSource;
2494 $memParams['contribution_recur_id'] = $contributionRecurID;
2496 $memParams['is_test'] = $is_test;
2497 $memParams['is_pay_later'] = $isPayLater;
2501 //if we decided status here and want to skip status
2502 //calculation in create( ); then need to pass 'skipStatusCal'.
2503 if ($updateStatusId) {
2504 $memParams['status_id'] = $updateStatusId;
2505 $memParams['skipStatusCal'] = TRUE;
2508 //since we are renewing,
2509 //make status override false.
2510 $memParams['is_override'] = FALSE;
2512 //CRM-4027, create log w/ individual contact.
2514 $ids['userId'] = $modifiedID;
2515 $memParams['is_for_organization'] = TRUE;
2518 $ids['userId'] = $contactID;
2521 //inherit campaign from contrib page.
2522 if (isset($campaignId)) {
2523 $memParams['campaign_id'] = $campaignId;
2526 $memParams['custom'] = $customFieldsFormatted;
2527 $membership = self
::create($memParams, $ids, FALSE, $activityType);
2529 // not sure why this statement is here, seems quite odd :( - Lobo: 12/26/2010
2530 // related to: http://forum.civicrm.org/index.php/topic,11416.msg49072.html#msg49072
2531 $membership->find(TRUE);
2533 return array($membership, $renewalMode, $dates);
2537 * Function to process price set and line items.
2541 * @param $membershipId
2546 function processPriceSet($membershipId, $lineItem) {
2547 //FIXME : need to move this too
2548 if (!$membershipId ||
!is_array($lineItem)
2549 || CRM_Utils_System
::isNull($lineItem)
2554 foreach ($lineItem as $priceSetId => $values) {
2558 foreach ($values as $line) {
2559 $line['entity_table'] = 'civicrm_membership';
2560 $line['entity_id'] = $membershipId;
2561 CRM_Price_BAO_LineItem
::create($line);
2567 * retrieve the contribution id for the associated Membership id
2568 * @todo we should get this off the line item
2570 * @param int $membershipId membership id.
2572 * @return integer contribution id
2575 static function getMembershipContributionId($membershipId) {
2577 $membershipPayment = new CRM_Member_DAO_MembershipPayment();
2578 $membershipPayment->membership_id
= $membershipId;
2579 if ($membershipPayment->find(TRUE)) {
2580 return $membershipPayment->contribution_id
;
2586 * The function checks and updates the status of all membership records for a given domain using the
2587 * calc_membership_status and update_contact_membership APIs.
2590 * Sending renewal reminders has been migrated from this job to the Scheduled Reminders function as of 4.3.
2592 * @return array $result
2595 static function updateAllMembershipStatus() {
2597 //get all active statuses of membership, CRM-3984
2598 $allStatus = CRM_Member_PseudoConstant
::membershipStatus();
2599 $statusLabels = CRM_Member_PseudoConstant
::membershipStatus(NULL, NULL, 'label');
2600 $allTypes = CRM_Member_PseudoConstant
::membershipType();
2602 // get only memberships with active membership types
2604 SELECT civicrm_membership.id as membership_id,
2605 civicrm_membership.is_override as is_override,
2606 civicrm_membership.membership_type_id as membership_type_id,
2607 civicrm_membership.status_id as status_id,
2608 civicrm_membership.join_date as join_date,
2609 civicrm_membership.start_date as start_date,
2610 civicrm_membership.end_date as end_date,
2611 civicrm_membership.source as source,
2612 civicrm_contact.id as contact_id,
2613 civicrm_contact.is_deceased as is_deceased,
2614 civicrm_membership.owner_membership_id as owner_membership_id,
2615 civicrm_membership.contribution_recur_id as recur_id
2616 FROM civicrm_membership
2617 INNER JOIN civicrm_contact ON ( civicrm_membership.contact_id = civicrm_contact.id )
2618 INNER JOIN civicrm_membership_type ON
2619 (civicrm_membership.membership_type_id = civicrm_membership_type.id AND civicrm_membership_type.is_active = 1)
2620 WHERE civicrm_membership.is_test = 0";
2623 $dao = CRM_Core_DAO
::executeQuery($query, $params);
2628 $smarty = CRM_Core_Smarty
::singleton();
2630 while ($dao->fetch()) {
2634 // Put common parameters into array for easy access
2635 $memberParams = array(
2636 'id' => $dao->membership_id
,
2637 'status_id' => $dao->status_id
,
2638 'contact_id' => $dao->contact_id
,
2639 'membership_type_id' => $dao->membership_type_id
,
2640 'membership_type' => $allTypes[$dao->membership_type_id
],
2641 'join_date' => $dao->join_date
,
2642 'start_date' => $dao->start_date
,
2643 'end_date' => $dao->end_date
,
2644 'source' => $dao->source
,
2645 'skipStatusCal' => TRUE,
2646 'skipRecentView' => TRUE,
2649 $smarty->assign_by_ref('memberParams', $memberParams);
2651 //update membership record to Deceased if contact is deceased
2652 if ($dao->is_deceased
) {
2653 // check for 'Deceased' membership status, CRM-5636
2654 $deceaseStatusId = array_search('Deceased', $allStatus);
2655 if (!$deceaseStatusId) {
2656 CRM_Core_Error
::fatal(ts("Deceased Membership status is missing or not active. <a href='%1'>Click here to check</a>.", array(1 => CRM_Utils_System
::url('civicrm/admin/member/membershipStatus', 'reset=1'))));
2659 //process only when status change.
2660 if ($dao->status_id
!= $deceaseStatusId) {
2661 //take all params that need to save.
2662 $deceasedMembership = $memberParams;
2663 $deceasedMembership['status_id'] = $deceaseStatusId;
2664 $deceasedMembership['createActivity'] = TRUE;
2665 $deceasedMembership['version'] = 3;
2667 //since there is change in status.
2668 $statusChange = array('status_id' => $deceaseStatusId);
2669 $smarty->append_by_ref('memberParams', $statusChange, TRUE);
2671 $deceasedMembership['contact_id'],
2672 $deceasedMembership['membership_type_id'],
2673 $deceasedMembership['membership_type'],
2674 $deceasedMembership['join_date'],
2675 $deceasedMembership['start_date'],
2676 $deceasedMembership['end_date'],
2677 $deceasedMembership['source']
2680 //process membership record.
2681 civicrm_api('membership', 'create', $deceasedMembership);
2686 //we fetch related, since we need to check for deceased
2687 //now further processing is handle w/ main membership record.
2688 if ($dao->owner_membership_id
) {
2692 //update membership records where status is NOT - Pending OR Cancelled.
2693 //as well as membership is not override.
2694 //skipping Expired membership records -> reduced extra processing( kiran )
2695 if (!$dao->is_override
&&
2696 !in_array($dao->status_id
, array(array_search('Pending', $allStatus),
2697 array_search('Cancelled', $allStatus),
2698 array_search('Expired', $allStatus),
2702 // CRM-7248: added excludeIsAdmin param to the following fn call to prevent moving to admin statuses
2703 //get the membership status as per id.
2704 $newStatus = civicrm_api('membership_status', 'calc',
2706 'membership_id' => $dao->membership_id
, 'version' => 3, 'ignore_admin_only'=> FALSE), TRUE
2708 $statusId = CRM_Utils_Array
::value('id', $newStatus);
2710 //process only when status change.
2712 $statusId != $dao->status_id
2714 //take all params that need to save.
2715 $memParams = $memberParams;
2716 $memParams['status_id'] = $statusId;
2717 $memParams['createActivity'] = TRUE;
2718 $memParams['version'] = 3;
2720 // Unset columns which should remain unchanged from their current saved
2721 // values. This avoids race condition in which these values may have
2722 // been changed by other processes.
2724 $memParams['contact_id'],
2725 $memParams['membership_type_id'],
2726 $memParams['membership_type'],
2727 $memParams['join_date'],
2728 $memParams['start_date'],
2729 $memParams['end_date'],
2730 $memParams['source']
2732 //since there is change in status.
2733 $statusChange = array('status_id' => $statusId);
2734 $smarty->append_by_ref('memberParams', $statusChange, TRUE);
2736 //process member record.
2737 civicrm_api('membership', 'create', $memParams);
2741 // CRM_Core_Error::debug( 'fEnd', count( $GLOBALS['_DB_DATAOBJECT']['RESULTS'] ) );
2743 $result['is_error'] = 0;
2744 $result['messages'] = ts('Processed %1 membership records. Updated %2 records.', array(1 => $processCount, 2 => $updateCount));
2749 * The function returns the membershiptypes for a particular contact
2750 * who has lifetime membership without end date.
2753 * @param bool $isTest
2754 * @param bool $onlyLifeTime
2757 * @internal param array $contactMembershipType array of allMembershipTypes Key - value pairs
2760 static function getAllContactMembership($contactID, $isTest = FALSE, $onlyLifeTime = FALSE) {
2761 $contactMembershipType = array();
2763 return $contactMembershipType;
2766 $dao = new CRM_Member_DAO_Membership();
2767 $dao->contact_id
= $contactID;
2768 $pendingStatusId = array_search('Pending', CRM_Member_PseudoConstant
::membershipStatus());
2769 $dao->whereAdd("status_id != $pendingStatusId");
2772 $dao->is_test
= $isTest;
2775 $dao->whereAdd('is_test IS NULL OR is_test = 0');
2778 if ($onlyLifeTime) {
2779 $dao->whereAdd('end_date IS NULL');
2783 while ($dao->fetch()) {
2784 $membership = array();
2785 CRM_Core_DAO
::storeValues($dao, $membership);
2786 $contactMembershipType[$dao->membership_type_id
] = $membership;
2788 return $contactMembershipType;
2792 * Function to record contribution record associated with membership
2794 * @param array $params array of submitted params
2795 * @param array $ids (param in process of being removed - try to use params) array of ids
2797 * @return CRM_Contribute_BAO_Contribution
2800 static function recordMembershipContribution( &$params, $ids = array()) {
2801 $membershipId = $params['membership_id'];
2802 $contributionParams = array();
2803 $config = CRM_Core_Config
::singleton();
2804 $contributionParams['currency'] = $config->defaultCurrency
;
2805 $contributionParams['receipt_date'] = (CRM_Utils_Array
::value('receipt_date', $params)) ?
$params['receipt_date'] : 'null';
2806 $contributionParams['source'] = CRM_Utils_Array
::value('contribution_source', $params);
2807 $contributionParams['non_deductible_amount'] = 'null';
2808 $contributionSoftParams = CRM_Utils_Array
::value('soft_credit', $params);
2809 $recordContribution = array(
2810 'contact_id', 'total_amount', 'receive_date', 'financial_type_id',
2811 'payment_instrument_id', 'trxn_id', 'invoice_id', 'is_test',
2812 'contribution_status_id', 'check_number', 'campaign_id', 'is_pay_later',
2815 foreach ($recordContribution as $f) {
2816 $contributionParams[$f] = CRM_Utils_Array
::value($f, $params);
2819 // make entry in batch entity batch table
2820 if (!empty($params['batch_id'])) {
2821 $contributionParams['batch_id'] = $params['batch_id'];
2824 if (!empty($params['contribution_contact_id'])) {
2825 // deal with possibility of a different person paying for contribution
2826 $contributionParams['contact_id'] = $params['contribution_contact_id'];
2829 if (!empty($params['processPriceSet']) &&
2830 !empty($params['lineItems'])
2832 $contributionParams['line_item'] = CRM_Utils_Array
::value('lineItems', $params, NULL);
2835 $contribution = CRM_Contribute_BAO_Contribution
::create($contributionParams, $ids);
2837 //CRM-13981, create new soft-credit record as to record payment from differnt person for this membership
2838 if (!empty($contributionSoftParams)) {
2839 $contributionSoftParams['contribution_id'] = $contribution->id
;
2840 $contributionSoftParams['currency'] = $contribution->currency
;
2841 $contributionSoftParams['amount'] = $contribution->total_amount
;
2842 CRM_Contribute_BAO_ContributionSoft
::add($contributionSoftParams);
2845 // store contribution id
2846 $params['contribution_id'] = $contribution->id
;
2849 //insert payment record for this membership
2850 if (empty($ids['contribution']) ||
!empty($params['is_recur'])) {
2851 $mpDAO = new CRM_Member_DAO_MembershipPayment();
2852 $mpDAO->membership_id
= $membershipId;
2853 $mpDAO->contribution_id
= $contribution->id
;
2854 if (!empty($params['is_recur'])) {
2858 CRM_Utils_Hook
::pre('create', 'MembershipPayment', NULL, $mpDAO);
2860 CRM_Utils_Hook
::post('create', 'MembershipPayment', $mpDAO->id
, $mpDAO);
2862 return $contribution;
2866 * Function to record line items for default membership
2870 * @param $membershipType array with membership type and organization
2872 * @param $priceSetId
2874 * @internal param $ $ priceSetId priceset id
2878 static function createLineItems(&$qf, $membershipType, &$priceSetId) {
2879 $qf->_priceSetId
= $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', 'default_membership_type_amount', 'id', 'name');
2881 $qf->_priceSet
= $priceSets = current(CRM_Price_BAO_PriceSet
::getSetDetail($priceSetId));
2883 $editedFieldParams = array(
2884 'price_set_id' => $priceSetId,
2885 'name' => $membershipType[0],
2887 $editedResults = array();
2888 CRM_Price_BAO_PriceField
::retrieve($editedFieldParams, $editedResults);
2890 if (!empty($editedResults)) {
2891 unset($qf->_priceSet
['fields']);
2892 $qf->_priceSet
['fields'][$editedResults['id']] = $priceSets['fields'][$editedResults['id']];
2893 unset($qf->_priceSet
['fields'][$editedResults['id']]['options']);
2894 $fid = $editedResults['id'];
2895 $editedFieldParams = array(
2896 'price_field_id' => $editedResults['id'],
2897 'membership_type_id' => $membershipType[1],
2899 $editedResults = array();
2900 CRM_Price_BAO_PriceFieldValue
::retrieve($editedFieldParams, $editedResults);
2901 $qf->_priceSet
['fields'][$fid]['options'][$editedResults['id']] = $priceSets['fields'][$fid]['options'][$editedResults['id']];
2902 if (!empty($qf->_params
['total_amount'])) {
2903 $qf->_priceSet
['fields'][$fid]['options'][$editedResults['id']]['amount'] = $qf->_params
['total_amount'];
2907 $fieldID = key($qf->_priceSet
['fields']);
2908 $qf->_params
['price_' . $fieldID] = CRM_Utils_Array
::value('id', $editedResults);
2912 * @todo document me - I seem a bit out of date....
2914 static function _getActTypes() {
2915 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, FALSE, FALSE, 'name');
2916 self
::$_renewalActType = CRM_Utils_Array
::key('Membership Renewal', $activityTypes);
2917 self
::$_signupActType = CRM_Utils_Array
::key('Membership Signup', $activityTypes);
2921 * Get all Cancelled Membership(s) for a contact
2923 * @param int $contactID contact id
2924 * @param boolean $isTest mode of payment
2926 * @return array of membership type
2930 static function getContactsCancelledMembership($contactID, $isTest = FALSE) {
2934 $allStatus = CRM_Member_PseudoConstant
::membershipStatus();
2935 $query = 'SELECT membership_type_id FROM civicrm_membership WHERE contact_id = %1 AND status_id = %2 AND is_test = %3';
2936 $queryParams = array(
2937 1 => array($contactID, 'Integer'),
2938 2 => array(array_search('Cancelled', $allStatus), 'Integer'),
2939 3 => array($isTest, 'Boolean'),
2942 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
2943 $cancelledMembershipIds = array();
2944 while ($dao->fetch()) {
2945 $cancelledMembershipIds[] = $dao->membership_type_id
;
2947 return $cancelledMembershipIds;