3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 class CRM_Member_Page_Tab
extends CRM_Core_Page
{
22 * The action links that we need to display for the browse screen.
26 public static $_links = NULL;
27 public static $_membershipTypesLinks = NULL;
29 public $_permission = NULL;
30 public $_contactId = NULL;
33 * called when action is browse.
35 public function browse() {
36 $links = self
::links('all', $this->_isPaymentProcessor
, $this->_accessContribution
);
37 CRM_Financial_BAO_FinancialType
::getAvailableMembershipTypes($membershipTypes);
38 $addWhere = "membership_type_id IN (0)";
39 if (!empty($membershipTypes)) {
40 $addWhere = "membership_type_id IN (" . implode(',', array_keys($membershipTypes)) . ")";
44 $dao = new CRM_Member_DAO_Membership();
45 $dao->contact_id
= $this->_contactId
;
46 $dao->whereAdd($addWhere);
49 //CRM--4418, check for view, edit, delete
50 $permissions = [CRM_Core_Permission
::VIEW
];
51 if (CRM_Core_Permission
::check('edit memberships')) {
52 $permissions[] = CRM_Core_Permission
::EDIT
;
54 if (CRM_Core_Permission
::check('delete in CiviMember')) {
55 $permissions[] = CRM_Core_Permission
::DELETE
;
57 $mask = CRM_Core_Action
::mask($permissions);
59 // get deceased status id
60 $allStatus = CRM_Member_PseudoConstant
::membershipStatus();
61 $deceasedStatusId = array_search('Deceased', $allStatus);
64 $allCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
66 //checks membership of contact itself
67 while ($dao->fetch()) {
68 $membership[$dao->id
] = [];
69 CRM_Core_DAO
::storeValues($dao, $membership[$dao->id
]);
72 $membership[$dao->id
]['campaign'] = $allCampaigns[$dao->campaign_id
] ??
NULL;
74 //get the membership status and type values.
75 $statusANDType = CRM_Member_BAO_Membership
::getStatusANDTypeValues($dao->id
);
76 foreach (['status', 'membership_type'] as $fld) {
77 $membership[$dao->id
][$fld] = $statusANDType[$dao->id
][$fld] ??
NULL;
79 if (!empty($statusANDType[$dao->id
]['is_current_member'])) {
80 $membership[$dao->id
]['active'] = TRUE;
82 if (empty($dao->owner_membership_id
)) {
83 // unset renew and followup link for deceased membership
85 if ($dao->status_id
== $deceasedStatusId) {
86 $currentMask = $currentMask & ~CRM_Core_Action
::RENEW
& ~CRM_Core_Action
::FOLLOWUP
;
89 $isUpdateBilling = FALSE;
90 // It would be better to determine if there is a recurring contribution &
91 // is so get the entity for the recurring contribution (& skip if not).
92 $paymentObject = CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity(
93 $membership[$dao->id
]['membership_id'], 'membership', 'obj');
94 if (!empty($paymentObject)) {
95 $isUpdateBilling = $paymentObject->supports('updateSubscriptionBillingInfo');
98 // @todo - get this working with syntax style $paymentObject->supports(array
99 //('CancelSubscriptionSupported'));
100 $isCancelSupported = CRM_Member_BAO_Membership
::isCancelSubscriptionSupported(
101 $membership[$dao->id
]['membership_id']);
102 $links = self
::links('all',
108 self
::getPermissionedLinks($dao->membership_type_id
, $links);
109 $membership[$dao->id
]['action'] = CRM_Core_Action
::formLink($links,
113 'cid' => $this->_contactId
,
117 'membership.tab.row',
123 $links = self
::links('view');
124 self
::getPermissionedLinks($dao->membership_type_id
, $links);
125 $membership[$dao->id
]['action'] = CRM_Core_Action
::formLink($links,
129 'cid' => $this->_contactId
,
133 'membership.tab.row',
139 // Display Auto-renew status on page (0=disabled, 1=enabled, 2=enabled, but error
140 if (!empty($membership[$dao->id
]['contribution_recur_id'])) {
141 if (CRM_Member_BAO_Membership
::isSubscriptionCancelled($membership[$dao->id
]['membership_id'])) {
142 $membership[$dao->id
]['auto_renew'] = 2;
145 $membership[$dao->id
]['auto_renew'] = 1;
149 $membership[$dao->id
]['auto_renew'] = 0;
152 // if relevant--membership is active and type allows inheritance--count related memberships
153 if (!empty($statusANDType[$dao->id
]['is_current_member'])
154 && !empty($statusANDType[$dao->id
]['relationship_type_id'])
155 && empty($dao->owner_membership_id
)
157 // not an related membership
160 FROM civicrm_membership m
161 LEFT JOIN civicrm_membership_status ms ON ms.id = m.status_id
162 LEFT JOIN civicrm_contact ct ON ct.id = m.contact_id
163 WHERE m.owner_membership_id = {$dao->id} AND m.is_test = 0 AND ms.is_current_member = 1 AND ct.is_deleted = 0";
164 $num_related = CRM_Core_DAO
::singleValueQuery($query);
165 $max_related = $membership[$dao->id
]['max_related'] ??
NULL;
166 $membership[$dao->id
]['related_count'] = ($max_related == '' ?
ts('%1 created', [1 => $num_related]) : ts('%1 out of %2', [
172 $membership[$dao->id
]['related_count'] = ts('N/A');
176 //Below code gives list of all Membership Types associated
177 //with an Organization(CRM-2016)
178 $membershipTypesResult = civicrm_api3('MembershipType', 'get', [
179 'member_of_contact_id' => $this->_contactId
,
184 $membershipTypes = $membershipTypesResult['values'] ??
NULL;
186 foreach ($membershipTypes as $key => $value) {
187 $membershipTypes[$key]['action'] = CRM_Core_Action
::formLink(self
::membershipTypeslinks(),
190 'id' => $value['id'],
191 'cid' => $this->_contactId
,
195 'membershipType.organization.action',
201 $activeMembers = CRM_Member_BAO_Membership
::activeMembers($membership);
202 $inActiveMembers = CRM_Member_BAO_Membership
::activeMembers($membership, 'inactive');
203 $this->assign('activeMembers', $activeMembers);
204 $this->assign('inActiveMembers', $inActiveMembers);
205 $this->assign('membershipTypes', $membershipTypes);
207 if ($this->_contactId
) {
208 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_contactId
);
209 $this->assign('displayName', $displayName);
210 $this->ajaxResponse
['tabCount'] = CRM_Contact_BAO_Contact
::getCountComponent('membership', $this->_contactId
);
211 // Refresh other tabs with related data
212 $this->ajaxResponse
['updateTabs'] = [
213 '#tab_activity' => CRM_Contact_BAO_Contact
::getCountComponent('activity', $this->_contactId
),
214 '#tab_rel' => CRM_Contact_BAO_Contact
::getCountComponent('rel', $this->_contactId
),
216 if (CRM_Core_Permission
::access('CiviContribute')) {
217 $this->ajaxResponse
['updateTabs']['#tab_contribute'] = CRM_Contact_BAO_Contact
::getCountComponent('contribution', $this->_contactId
);
223 * called when action is view.
227 public function view() {
228 $controller = new CRM_Core_Controller_Simple(
229 'CRM_Member_Form_MembershipView',
230 ts('View Membership'),
233 $controller->setEmbedded(TRUE);
234 $controller->set('id', $this->_id
);
235 $controller->set('cid', $this->_contactId
);
237 return $controller->run();
241 * called when action is update or new.
245 public function edit() {
246 // set https for offline cc transaction
247 $mode = CRM_Utils_Request
::retrieve('mode', 'Alphanumeric', $this);
248 if ($mode == 'test' ||
$mode == 'live') {
249 CRM_Utils_System
::redirectToSSL();
252 // build associated contributions ( note: this is called to show associated contributions in edit mode )
253 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
254 $this->assign('accessContribution', FALSE);
255 if (CRM_Core_Permission
::access('CiviContribute')) {
256 $this->assign('accessContribution', TRUE);
257 CRM_Member_Page_Tab
::associatedContribution($this->_contactId
, $this->_id
);
259 //show associated soft credit when contribution payment is paid by different person in edit mode
260 if ($this->_id
&& $this->_contactId
) {
261 $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$this->_id})";
262 $softCreditList = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionList($this->_contactId
, $filter);
263 if (!empty($softCreditList)) {
264 $this->assign('softCredit', TRUE);
265 $this->assign('softCreditRows', $softCreditList);
271 if ($this->_action
& CRM_Core_Action
::RENEW
) {
272 $path = 'CRM_Member_Form_MembershipRenewal';
273 $title = ts('Renew Membership');
276 $path = 'CRM_Member_Form_Membership';
277 $title = ts('Create Membership');
280 $controller = new CRM_Core_Controller_Simple($path, $title, $this->_action
);
281 $controller->setEmbedded(TRUE);
282 $controller->set('BAOName', $this->getBAOName());
283 $controller->set('id', $this->_id
);
284 $controller->set('cid', $this->_contactId
);
285 return $controller->run();
288 public function preProcess() {
289 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
290 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'browse');
291 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
293 if ($context == 'standalone') {
294 $this->_action
= CRM_Core_Action
::ADD
;
297 $this->_contactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this, TRUE);
298 $this->assign('contactId', $this->_contactId
);
300 // check logged in url permission
301 CRM_Contact_Page_View
::checkUserPermission($this);
304 $this->assign('action', $this->_action
);
306 if ($this->_permission
== CRM_Core_Permission
::EDIT
&& !CRM_Core_Permission
::check('edit memberships')) {
307 // demote to view since user does not have edit membership rights
308 $this->_permission
= CRM_Core_Permission
::VIEW
;
309 $this->assign('permission', 'view');
314 * the main function that is called when the page loads, it decides the which action has to be taken for the page.
318 public function run() {
321 // check if we can process credit card membership
322 $newCredit = CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments();
323 $this->assign('newCredit', $newCredit);
326 $this->_isPaymentProcessor
= TRUE;
329 $this->_isPaymentProcessor
= FALSE;
332 // Only show credit card membership signup if user has CiviContribute permission
333 if (CRM_Core_Permission
::access('CiviContribute')) {
334 $this->_accessContribution
= TRUE;
335 $this->assign('accessContribution', TRUE);
337 //show associated soft credit when contribution payment is paid by different person
338 if ($this->_id
&& $this->_contactId
) {
339 $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$this->_id})";
340 $softCreditList = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionList($this->_contactId
, $filter);
341 if (!empty($softCreditList)) {
342 $this->assign('softCredit', TRUE);
343 $this->assign('softCreditRows', $softCreditList);
348 $this->_accessContribution
= FALSE;
349 $this->assign('accessContribution', FALSE);
350 $this->assign('softCredit', FALSE);
353 if ($this->_action
& CRM_Core_Action
::VIEW
) {
356 elseif ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD | CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
357 self
::setContext($this);
361 self
::setContext($this);
365 return parent
::run();
369 * @param CRM_Core_Form $form
370 * @param int $contactId
372 public static function setContext(&$form, $contactId = NULL) {
373 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $form, FALSE, 'search');
375 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $form);
377 $searchContext = CRM_Utils_Request
::retrieve('searchContext', 'String', $form);
380 if (!CRM_Utils_Rule
::qfKey($qfKey)) {
385 $contactId = $form->_contactId
;
390 $url = CRM_Utils_System
::url('civicrm/member', 'reset=1');
394 $url = CRM_Utils_System
::url('civicrm/contact/view', "reset=1&force=1&cid={$contactId}&selectedChild=member");
398 $urlParams = 'force=1';
400 $urlParams .= "&qfKey=$qfKey";
402 $form->assign('searchKey', $qfKey);
404 if ($searchContext) {
405 $url = CRM_Utils_System
::url("civicrm/$searchContext/search", $urlParams);
408 $url = CRM_Utils_System
::url('civicrm/member/search', $urlParams);
413 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
417 $url = CRM_Utils_System
::url('civicrm/contact/view',
418 "reset=1&force=1&cid={$contactId}&selectedChild=activity"
423 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
427 $action = CRM_Utils_Request
::retrieve('action', 'String', $form);
429 $urlParams = 'force=1';
430 $urlString = 'civicrm/contact/search/custom';
431 if ($action == CRM_Core_Action
::UPDATE
) {
432 if ($form->_contactId
) {
433 $urlParams .= '&cid=' . $form->_contactId
;
436 $urlParams .= '&context=fulltext&action=view';
437 $urlString = 'civicrm/contact/view/membership';
440 $urlParams .= "$keyName=$qfKey";
442 $form->assign('searchKey', $qfKey);
443 $url = CRM_Utils_System
::url($urlString, $urlParams);
449 $cid = '&cid=' . $contactId;
451 $url = CRM_Utils_System
::url('civicrm/member/search', 'force=1' . $cid);
455 $session = CRM_Core_Session
::singleton();
456 $session->pushUserContext($url);
462 * @param string $status
463 * @param null $isPaymentProcessor
464 * @param null $accessContribution
465 * @param bool $isCancelSupported
466 * @param bool $isUpdateBilling
469 * (reference) of action links
471 public static function &links(
473 $isPaymentProcessor = NULL,
474 $accessContribution = NULL,
475 $isCancelSupported = FALSE,
476 $isUpdateBilling = FALSE
478 if (empty(self
::$_links['view'])) {
479 self
::$_links['view'] = [
480 CRM_Core_Action
::VIEW
=> [
481 'name' => ts('View'),
482 'url' => 'civicrm/contact/view/membership',
483 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
484 'title' => ts('View Membership'),
489 if (empty(self
::$_links['all'])) {
491 CRM_Core_Action
::UPDATE
=> [
492 'name' => ts('Edit'),
493 'url' => 'civicrm/contact/view/membership',
494 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
495 'title' => ts('Edit Membership'),
497 CRM_Core_Action
::RENEW
=> [
498 'name' => ts('Renew'),
499 'url' => 'civicrm/contact/view/membership',
500 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
501 'title' => ts('Renew Membership'),
503 CRM_Core_Action
::FOLLOWUP
=> [
504 'name' => ts('Renew-Credit Card'),
505 'url' => 'civicrm/contact/view/membership',
506 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member&mode=live',
507 'title' => ts('Renew Membership Using Credit Card'),
509 CRM_Core_Action
::DELETE
=> [
510 'name' => ts('Delete'),
511 'url' => 'civicrm/contact/view/membership',
512 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
513 'title' => ts('Delete Membership'),
516 if (!$isPaymentProcessor ||
!$accessContribution) {
517 //unset the renew with credit card when payment
518 //processor is not available or user is not permitted to create contributions
519 unset($extraLinks[CRM_Core_Action
::FOLLOWUP
]);
521 self
::$_links['all'] = self
::$_links['view'] +
$extraLinks;
524 if ($isCancelSupported) {
525 $cancelMessage = ts('WARNING: If you cancel the recurring contribution associated with this membership, the membership will no longer be renewed automatically. However, the current membership status will not be affected.');
526 self
::$_links['all'][CRM_Core_Action
::DISABLE
] = [
527 'name' => ts('Cancel Auto-renewal'),
528 'url' => 'civicrm/contribute/unsubscribe',
529 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
530 'title' => ts('Cancel Auto Renew Subscription'),
531 'extra' => 'onclick = "if (confirm(\'' . $cancelMessage . '\') ) { return true; else return false;}"',
534 elseif (isset(self
::$_links['all'][CRM_Core_Action
::DISABLE
])) {
535 unset(self
::$_links['all'][CRM_Core_Action
::DISABLE
]);
538 if ($isUpdateBilling) {
539 self
::$_links['all'][CRM_Core_Action
::MAP
] = [
540 'name' => ts('Change Billing Details'),
541 'url' => 'civicrm/contribute/updatebilling',
542 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
543 'title' => ts('Change Billing Details'),
546 elseif (isset(self
::$_links['all'][CRM_Core_Action
::MAP
])) {
547 unset(self
::$_links['all'][CRM_Core_Action
::MAP
]);
549 return self
::$_links[$status];
553 * Define action links for membership types of related organization.
556 * self::$_membershipTypesLinks array of action links
558 public static function &membershipTypesLinks() {
559 if (!self
::$_membershipTypesLinks) {
560 self
::$_membershipTypesLinks = [
561 CRM_Core_Action
::VIEW
=> [
562 'name' => ts('Members'),
563 'url' => 'civicrm/member/search/',
564 'qs' => 'reset=1&force=1&type=%%id%%',
565 'title' => ts('Search'),
567 CRM_Core_Action
::UPDATE
=> [
568 'name' => ts('Edit'),
569 'url' => 'civicrm/admin/member/membershipType',
570 'qs' => 'action=update&id=%%id%%&reset=1',
571 'title' => ts('Edit Membership Type'),
575 return self
::$_membershipTypesLinks;
579 * used for the to show the associated.
580 * contribution for the membership
582 * @param int $contactId
583 * @param int $membershipId
585 public static function associatedContribution($contactId = NULL, $membershipId = NULL) {
586 $controller = new CRM_Core_Controller_Simple(
587 'CRM_Contribute_Form_Search',
592 $controller->setEmbedded(TRUE);
593 $controller->reset();
594 $controller->set('force', 1);
595 $controller->set('cid', $contactId);
596 $controller->set('memberId', $membershipId);
597 $controller->set('context', 'contribution');
598 $controller->process();
608 public function getBAOName() {
609 return 'CRM_Member_BAO_Membership';
613 * Get a list of links based on permissioned FTs.
615 * @param int $memTypeID
618 * (reference ) action links
620 public static function getPermissionedLinks($memTypeID, &$links) {
621 if (!CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus()) {
624 $finTypeId = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $memTypeID, 'financial_type_id');
625 $finType = CRM_Contribute_PseudoConstant
::financialType($finTypeId);
626 if (!CRM_Core_Permission
::check('edit contributions of type ' . $finType)) {
627 unset($links[CRM_Core_Action
::UPDATE
]);
628 unset($links[CRM_Core_Action
::RENEW
]);
629 unset($links[CRM_Core_Action
::FOLLOWUP
]);
631 if (!CRM_Core_Permission
::check('delete contributions of type ' . $finType)) {
632 unset($links[CRM_Core_Action
::DELETE
]);