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
17 class CRM_Member_Page_Tab
extends CRM_Core_Page
{
20 * The action links that we need to display for the browse screen.
24 public static $_links = NULL;
25 public static $_membershipTypesLinks = NULL;
27 public $_permission = NULL;
28 public $_contactId = NULL;
31 * called when action is browse.
33 public function browse() {
34 $links = self
::links('all', $this->_isPaymentProcessor
, $this->_accessContribution
);
35 $membershipTypes = \Civi\Api4\MembershipType
::get(TRUE)
39 $addWhere = "membership_type_id IN (0)";
40 if (!empty($membershipTypes)) {
41 $addWhere = "membership_type_id IN (" . implode(',', array_keys($membershipTypes)) . ")";
45 $dao = new CRM_Member_DAO_Membership();
46 $dao->contact_id
= $this->_contactId
;
47 $dao->whereAdd($addWhere);
50 //CRM--4418, check for view, edit, delete
51 $permissions = [CRM_Core_Permission
::VIEW
];
52 if (CRM_Core_Permission
::check('edit memberships')) {
53 $permissions[] = CRM_Core_Permission
::EDIT
;
54 $linkButtons['add_membership'] = [
55 'title' => ts('Add Membership'),
56 'url' => 'civicrm/contact/view/membership',
57 'qs' => "reset=1&action=add&cid={$this->_contactId}&context=membership",
58 'icon' => 'fa-plus-circle',
61 if ($this->_accessContribution
&& CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments()) {
62 $linkButtons['creditcard_membership'] = [
63 'title' => ts('Submit Credit Card Membership'),
64 'url' => 'civicrm/contact/view/membership',
65 'qs' => "reset=1&action=add&cid={$this->_contactId}&context=membership&mode=live",
66 'icon' => 'fa-credit-card',
70 $this->assign('linkButtons', $linkButtons ??
[]);
72 if (CRM_Core_Permission
::check('delete in CiviMember')) {
73 $permissions[] = CRM_Core_Permission
::DELETE
;
75 $mask = CRM_Core_Action
::mask($permissions);
77 // get deceased status id
78 $allStatus = CRM_Member_PseudoConstant
::membershipStatus();
79 $deceasedStatusId = array_search('Deceased', $allStatus);
82 $allCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
84 //checks membership of contact itself
85 while ($dao->fetch()) {
86 $membership[$dao->id
] = [];
87 CRM_Core_DAO
::storeValues($dao, $membership[$dao->id
]);
90 $membership[$dao->id
]['campaign'] = $allCampaigns[$dao->campaign_id
] ??
NULL;
92 //get the membership status and type values.
93 $statusANDType = CRM_Member_BAO_Membership
::getStatusANDTypeValues($dao->id
);
94 foreach (['status', 'membership_type'] as $fld) {
95 $membership[$dao->id
][$fld] = $statusANDType[$dao->id
][$fld] ??
NULL;
97 if (!empty($statusANDType[$dao->id
]['is_current_member'])) {
98 $membership[$dao->id
]['active'] = TRUE;
100 if (empty($dao->owner_membership_id
)) {
101 // unset renew and followup link for deceased membership
102 $currentMask = $mask;
103 if ($dao->status_id
== $deceasedStatusId) {
104 $currentMask = $currentMask & ~CRM_Core_Action
::RENEW
& ~CRM_Core_Action
::FOLLOWUP
;
107 $isUpdateBilling = FALSE;
108 // It would be better to determine if there is a recurring contribution &
109 // is so get the entity for the recurring contribution (& skip if not).
110 $paymentObject = CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity(
111 $membership[$dao->id
]['membership_id'], 'membership', 'obj');
112 if (!empty($paymentObject)) {
113 $isUpdateBilling = $paymentObject->supports('updateSubscriptionBillingInfo');
116 // @todo - get this working with syntax style $paymentObject->supports(array
117 //('CancelSubscriptionSupported'));
118 $isCancelSupported = CRM_Member_BAO_Membership
::isCancelSubscriptionSupported(
119 $membership[$dao->id
]['membership_id']);
120 $links = self
::links('all',
126 self
::getPermissionedLinks($dao->membership_type_id
, $links);
127 $membership[$dao->id
]['action'] = CRM_Core_Action
::formLink($links,
131 'cid' => $this->_contactId
,
135 'membership.tab.row',
141 $links = self
::links('view');
142 self
::getPermissionedLinks($dao->membership_type_id
, $links);
143 $membership[$dao->id
]['action'] = CRM_Core_Action
::formLink($links,
147 'cid' => $this->_contactId
,
151 'membership.tab.row',
157 // Display Auto-renew status on page (0=disabled, 1=enabled, 2=enabled, but error
158 if (!empty($membership[$dao->id
]['contribution_recur_id'])) {
159 if (CRM_Member_BAO_Membership
::isSubscriptionCancelled($membership[$dao->id
]['membership_id'])) {
160 $membership[$dao->id
]['auto_renew'] = 2;
163 $membership[$dao->id
]['auto_renew'] = 1;
167 $membership[$dao->id
]['auto_renew'] = 0;
170 // if relevant--membership is active and type allows inheritance--count related memberships
171 if (!empty($statusANDType[$dao->id
]['is_current_member'])
172 && !empty($statusANDType[$dao->id
]['relationship_type_id'])
173 && empty($dao->owner_membership_id
)
175 // not an related membership
178 FROM civicrm_membership m
179 LEFT JOIN civicrm_membership_status ms ON ms.id = m.status_id
180 LEFT JOIN civicrm_contact ct ON ct.id = m.contact_id
181 WHERE m.owner_membership_id = {$dao->id} AND m.is_test = 0 AND ms.is_current_member = 1 AND ct.is_deleted = 0";
182 $num_related = CRM_Core_DAO
::singleValueQuery($query);
183 $max_related = $membership[$dao->id
]['max_related'] ??
NULL;
184 $membership[$dao->id
]['related_count'] = ($max_related == '' ?
ts('%1 created', [1 => $num_related]) : ts('%1 out of %2', [
190 $membership[$dao->id
]['related_count'] = ts('N/A');
194 //Below code gives list of all Membership Types associated
195 //with an Organization(CRM-2016)
196 $membershipTypesResult = civicrm_api3('MembershipType', 'get', [
197 'member_of_contact_id' => $this->_contactId
,
202 $membershipTypes = $membershipTypesResult['values'] ??
NULL;
204 foreach ($membershipTypes as $key => $value) {
205 $membershipTypes[$key]['action'] = CRM_Core_Action
::formLink(self
::membershipTypeslinks(),
208 'id' => $value['id'],
209 'cid' => $this->_contactId
,
213 'membershipType.organization.action',
219 $activeMembers = CRM_Member_BAO_Membership
::activeMembers($membership);
220 $inActiveMembers = CRM_Member_BAO_Membership
::activeMembers($membership, 'inactive');
221 $this->assign('activeMembers', $activeMembers);
222 $this->assign('inActiveMembers', $inActiveMembers);
223 $this->assign('membershipTypes', $membershipTypes);
225 if ($this->_contactId
) {
226 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_contactId
);
227 $this->assign('displayName', $displayName);
228 $this->ajaxResponse
['tabCount'] = CRM_Contact_BAO_Contact
::getCountComponent('membership', $this->_contactId
);
229 // Refresh other tabs with related data
230 $this->ajaxResponse
['updateTabs'] = [
231 '#tab_activity' => CRM_Contact_BAO_Contact
::getCountComponent('activity', $this->_contactId
),
232 '#tab_rel' => CRM_Contact_BAO_Contact
::getCountComponent('rel', $this->_contactId
),
234 if (CRM_Core_Permission
::access('CiviContribute')) {
235 $this->ajaxResponse
['updateTabs']['#tab_contribute'] = CRM_Contact_BAO_Contact
::getCountComponent('contribution', $this->_contactId
);
241 * called when action is view.
245 public function view() {
246 $controller = new CRM_Core_Controller_Simple(
247 'CRM_Member_Form_MembershipView',
248 ts('View Membership'),
251 $controller->setEmbedded(TRUE);
252 $controller->set('id', $this->_id
);
253 $controller->set('cid', $this->_contactId
);
255 return $controller->run();
259 * called when action is update or new.
263 public function edit() {
264 // set https for offline cc transaction
265 $mode = CRM_Utils_Request
::retrieve('mode', 'Alphanumeric', $this);
266 if ($mode == 'test' ||
$mode == 'live') {
267 CRM_Utils_System
::redirectToSSL();
270 // build associated contributions ( note: this is called to show associated contributions in edit mode )
271 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
272 $this->assign('accessContribution', FALSE);
273 if (CRM_Core_Permission
::access('CiviContribute')) {
274 $this->assign('accessContribution', TRUE);
275 CRM_Member_Page_Tab
::associatedContribution($this->_contactId
, $this->_id
);
277 //show associated soft credit when contribution payment is paid by different person in edit mode
278 if ($this->_id
&& $this->_contactId
) {
279 $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$this->_id})";
280 $softCreditList = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionList($this->_contactId
, $filter);
281 if (!empty($softCreditList)) {
282 $this->assign('softCredit', TRUE);
283 $this->assign('softCreditRows', $softCreditList);
289 if ($this->_action
& CRM_Core_Action
::RENEW
) {
290 $path = 'CRM_Member_Form_MembershipRenewal';
291 $title = ts('Renew Membership');
294 $path = 'CRM_Member_Form_Membership';
295 $title = ts('Create Membership');
298 $controller = new CRM_Core_Controller_Simple($path, $title, $this->_action
);
299 $controller->setEmbedded(TRUE);
300 $controller->set('BAOName', $this->getBAOName());
301 $controller->set('id', $this->_id
);
302 $controller->set('cid', $this->_contactId
);
303 return $controller->run();
306 public function preProcess() {
307 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
308 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'browse');
309 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
311 if ($context == 'standalone') {
312 $this->_action
= CRM_Core_Action
::ADD
;
315 $this->_contactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this, TRUE);
316 $this->assign('contactId', $this->_contactId
);
318 // check logged in url permission
319 CRM_Contact_Page_View
::checkUserPermission($this);
322 $this->assign('action', $this->_action
);
324 if ($this->_permission
== CRM_Core_Permission
::EDIT
&& !CRM_Core_Permission
::check('edit memberships')) {
325 // demote to view since user does not have edit membership rights
326 $this->_permission
= CRM_Core_Permission
::VIEW
;
327 $this->assign('permission', 'view');
332 * the main function that is called when the page loads, it decides the which action has to be taken for the page.
336 public function run() {
339 // check if we can process credit card membership
340 if (CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments()) {
341 $this->_isPaymentProcessor
= TRUE;
344 $this->_isPaymentProcessor
= FALSE;
347 // Only show credit card membership signup if user has CiviContribute permission
348 if (CRM_Core_Permission
::access('CiviContribute')) {
349 $this->_accessContribution
= TRUE;
350 $this->assign('accessContribution', TRUE);
352 //show associated soft credit when contribution payment is paid by different person
353 if ($this->_id
&& $this->_contactId
) {
354 $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$this->_id})";
355 $softCreditList = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionList($this->_contactId
, $filter);
356 if (!empty($softCreditList)) {
357 $this->assign('softCredit', TRUE);
358 $this->assign('softCreditRows', $softCreditList);
363 $this->_accessContribution
= FALSE;
364 $this->assign('accessContribution', FALSE);
365 $this->assign('softCredit', FALSE);
368 if ($this->_action
& CRM_Core_Action
::VIEW
) {
371 elseif ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD | CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
372 self
::setContext($this);
376 self
::setContext($this);
380 return parent
::run();
384 * @param CRM_Core_Form $form
385 * @param int $contactId
387 public static function setContext(&$form, $contactId = NULL) {
388 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $form, FALSE, 'search');
390 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $form);
392 $searchContext = CRM_Utils_Request
::retrieve('searchContext', 'String', $form);
395 if (!CRM_Utils_Rule
::qfKey($qfKey)) {
400 $contactId = $form->_contactId
;
405 $url = CRM_Utils_System
::url('civicrm/member', 'reset=1');
409 $url = CRM_Utils_System
::url('civicrm/contact/view', "reset=1&force=1&cid={$contactId}&selectedChild=member");
413 $urlParams = 'force=1';
415 $urlParams .= "&qfKey=$qfKey";
417 $form->assign('searchKey', $qfKey);
419 if ($searchContext) {
420 $url = CRM_Utils_System
::url("civicrm/$searchContext/search", $urlParams);
423 $url = CRM_Utils_System
::url('civicrm/member/search', $urlParams);
428 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
432 $url = CRM_Utils_System
::url('civicrm/contact/view',
433 "reset=1&force=1&cid={$contactId}&selectedChild=activity"
438 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
442 $action = CRM_Utils_Request
::retrieve('action', 'String', $form);
444 $urlParams = 'force=1';
445 $urlString = 'civicrm/contact/search/custom';
446 if ($action == CRM_Core_Action
::UPDATE
) {
447 if ($form->_contactId
) {
448 $urlParams .= '&cid=' . $form->_contactId
;
451 $urlParams .= '&context=fulltext&action=view';
452 $urlString = 'civicrm/contact/view/membership';
455 $urlParams .= "$keyName=$qfKey";
457 $form->assign('searchKey', $qfKey);
458 $url = CRM_Utils_System
::url($urlString, $urlParams);
464 $cid = '&cid=' . $contactId;
466 $url = CRM_Utils_System
::url('civicrm/member/search', 'force=1' . $cid);
470 $session = CRM_Core_Session
::singleton();
471 $session->pushUserContext($url);
477 * @param string $status
478 * @param null $isPaymentProcessor
479 * @param null $accessContribution
480 * @param bool $isCancelSupported
481 * @param bool $isUpdateBilling
484 * (reference) of action links
486 public static function &links(
488 $isPaymentProcessor = NULL,
489 $accessContribution = NULL,
490 $isCancelSupported = FALSE,
491 $isUpdateBilling = FALSE
493 if (empty(self
::$_links['view'])) {
494 self
::$_links['view'] = [
495 CRM_Core_Action
::VIEW
=> [
496 'name' => ts('View'),
497 'url' => 'civicrm/contact/view/membership',
498 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
499 'title' => ts('View Membership'),
504 if (empty(self
::$_links['all'])) {
506 CRM_Core_Action
::UPDATE
=> [
507 'name' => ts('Edit'),
508 'url' => 'civicrm/contact/view/membership',
509 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
510 'title' => ts('Edit Membership'),
512 CRM_Core_Action
::RENEW
=> [
513 'name' => ts('Renew'),
514 'url' => 'civicrm/contact/view/membership',
515 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
516 'title' => ts('Renew Membership'),
518 CRM_Core_Action
::FOLLOWUP
=> [
519 'name' => ts('Renew-Credit Card'),
520 'url' => 'civicrm/contact/view/membership',
521 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member&mode=live',
522 'title' => ts('Renew Membership Using Credit Card'),
524 CRM_Core_Action
::DELETE
=> [
525 'name' => ts('Delete'),
526 'url' => 'civicrm/contact/view/membership',
527 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
528 'title' => ts('Delete Membership'),
531 if (!$isPaymentProcessor ||
!$accessContribution) {
532 //unset the renew with credit card when payment
533 //processor is not available or user is not permitted to create contributions
534 unset($extraLinks[CRM_Core_Action
::FOLLOWUP
]);
536 self
::$_links['all'] = self
::$_links['view'] +
$extraLinks;
539 if ($isCancelSupported) {
540 $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.');
541 self
::$_links['all'][CRM_Core_Action
::DISABLE
] = [
542 'name' => ts('Cancel Auto-renewal'),
543 'url' => 'civicrm/contribute/unsubscribe',
544 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
545 'title' => ts('Cancel Auto Renew Subscription'),
546 'extra' => 'onclick = "if (confirm(\'' . $cancelMessage . '\') ) { return true; else return false;}"',
549 elseif (isset(self
::$_links['all'][CRM_Core_Action
::DISABLE
])) {
550 unset(self
::$_links['all'][CRM_Core_Action
::DISABLE
]);
553 if ($isUpdateBilling) {
554 self
::$_links['all'][CRM_Core_Action
::MAP
] = [
555 'name' => ts('Change Billing Details'),
556 'url' => 'civicrm/contribute/updatebilling',
557 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
558 'title' => ts('Change Billing Details'),
561 elseif (isset(self
::$_links['all'][CRM_Core_Action
::MAP
])) {
562 unset(self
::$_links['all'][CRM_Core_Action
::MAP
]);
564 return self
::$_links[$status];
568 * Define action links for membership types of related organization.
571 * self::$_membershipTypesLinks array of action links
573 public static function &membershipTypesLinks() {
574 if (!self
::$_membershipTypesLinks) {
575 self
::$_membershipTypesLinks = [
576 CRM_Core_Action
::VIEW
=> [
577 'name' => ts('Members'),
578 'url' => 'civicrm/member/search/',
579 'qs' => 'reset=1&force=1&type=%%id%%',
580 'title' => ts('Search'),
582 CRM_Core_Action
::UPDATE
=> [
583 'name' => ts('Edit'),
584 'url' => 'civicrm/admin/member/membershipType',
585 'qs' => 'action=update&id=%%id%%&reset=1',
586 'title' => ts('Edit Membership Type'),
590 return self
::$_membershipTypesLinks;
594 * used for the to show the associated.
595 * contribution for the membership
597 * @param int $contactId
598 * @param int $membershipId
600 public static function associatedContribution($contactId = NULL, $membershipId = NULL) {
601 $controller = new CRM_Core_Controller_Simple(
602 'CRM_Contribute_Form_Search',
607 $controller->setEmbedded(TRUE);
608 $controller->reset();
609 $controller->set('force', 1);
610 $controller->set('skip_cid', TRUE);
611 $controller->set('memberId', $membershipId);
612 $controller->set('context', 'contribution');
613 $controller->process();
623 public function getBAOName() {
624 return 'CRM_Member_BAO_Membership';
628 * Get a list of links based on permissioned FTs.
630 * @param int $memTypeID
633 * (reference ) action links
635 public static function getPermissionedLinks($memTypeID, &$links) {
636 if (!CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus()) {
639 $finTypeId = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $memTypeID, 'financial_type_id');
640 $finType = CRM_Contribute_PseudoConstant
::financialType($finTypeId);
641 if (!CRM_Core_Permission
::check('edit contributions of type ' . $finType)) {
642 unset($links[CRM_Core_Action
::UPDATE
]);
643 unset($links[CRM_Core_Action
::RENEW
]);
644 unset($links[CRM_Core_Action
::FOLLOWUP
]);
646 if (!CRM_Core_Permission
::check('delete contributions of type ' . $finType)) {
647 unset($links[CRM_Core_Action
::DELETE
]);