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;
33 private $_isPaymentProcessor = FALSE;
38 private $_accessContribution = FALSE;
41 * called when action is browse.
43 public function browse() {
44 $links = self
::links('all', $this->_isPaymentProcessor
, $this->_accessContribution
);
45 $membershipTypes = \Civi\Api4\MembershipType
::get(TRUE)
49 $addWhere = "membership_type_id IN (0)";
50 if (!empty($membershipTypes)) {
51 $addWhere = "membership_type_id IN (" . implode(',', array_keys($membershipTypes)) . ")";
55 $dao = new CRM_Member_DAO_Membership();
56 $dao->contact_id
= $this->_contactId
;
57 $dao->whereAdd($addWhere);
60 //CRM--4418, check for view, edit, delete
61 $permissions = [CRM_Core_Permission
::VIEW
];
62 if (CRM_Core_Permission
::check('edit memberships')) {
63 $permissions[] = CRM_Core_Permission
::EDIT
;
64 $linkButtons['add_membership'] = [
65 'title' => ts('Add Membership'),
66 'url' => 'civicrm/contact/view/membership',
67 'qs' => "reset=1&action=add&cid={$this->_contactId}&context=membership",
68 'icon' => 'fa-plus-circle',
71 if ($this->_accessContribution
&& CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments()) {
72 $linkButtons['creditcard_membership'] = [
73 'title' => ts('Submit Credit Card Membership'),
74 'url' => 'civicrm/contact/view/membership',
75 'qs' => "reset=1&action=add&cid={$this->_contactId}&context=membership&mode=live",
76 'icon' => 'fa-credit-card',
80 $this->assign('linkButtons', $linkButtons ??
[]);
82 if (CRM_Core_Permission
::check('delete in CiviMember')) {
83 $permissions[] = CRM_Core_Permission
::DELETE
;
85 $mask = CRM_Core_Action
::mask($permissions);
87 // get deceased status id
88 $allStatus = CRM_Member_PseudoConstant
::membershipStatus();
89 $deceasedStatusId = array_search('Deceased', $allStatus);
92 $allCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
94 //checks membership of contact itself
95 while ($dao->fetch()) {
96 $membership[$dao->id
] = [];
97 CRM_Core_DAO
::storeValues($dao, $membership[$dao->id
]);
100 $membership[$dao->id
]['campaign'] = $allCampaigns[$dao->campaign_id
] ??
NULL;
102 //get the membership status and type values.
103 $statusANDType = CRM_Member_BAO_Membership
::getStatusANDTypeValues($dao->id
);
104 foreach (['status', 'membership_type'] as $fld) {
105 $membership[$dao->id
][$fld] = $statusANDType[$dao->id
][$fld] ??
NULL;
107 if (!empty($statusANDType[$dao->id
]['is_current_member'])) {
108 $membership[$dao->id
]['active'] = TRUE;
110 if (empty($dao->owner_membership_id
)) {
111 // unset renew and followup link for deceased membership
112 $currentMask = $mask;
113 if ($dao->status_id
== $deceasedStatusId) {
114 $currentMask = $currentMask & ~CRM_Core_Action
::RENEW
& ~CRM_Core_Action
::FOLLOWUP
;
117 $isUpdateBilling = FALSE;
118 // It would be better to determine if there is a recurring contribution &
119 // is so get the entity for the recurring contribution (& skip if not).
120 $paymentObject = CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity(
121 $membership[$dao->id
]['membership_id'], 'membership', 'obj');
122 if (!empty($paymentObject)) {
123 $isUpdateBilling = $paymentObject->supports('updateSubscriptionBillingInfo');
126 // @todo - get this working with syntax style $paymentObject->supports(array
127 //('CancelSubscriptionSupported'));
128 $isCancelSupported = CRM_Member_BAO_Membership
::isCancelSubscriptionSupported(
129 $membership[$dao->id
]['membership_id']);
130 $links = self
::links('all',
136 self
::getPermissionedLinks($dao->membership_type_id
, $links);
137 $membership[$dao->id
]['action'] = CRM_Core_Action
::formLink($links,
141 'cid' => $this->_contactId
,
145 'membership.tab.row',
151 $links = self
::links('view');
152 self
::getPermissionedLinks($dao->membership_type_id
, $links);
153 $membership[$dao->id
]['action'] = CRM_Core_Action
::formLink($links,
157 'cid' => $this->_contactId
,
161 'membership.tab.row',
167 // Display Auto-renew status on page (0=disabled, 1=enabled, 2=enabled, but error
168 if (!empty($membership[$dao->id
]['contribution_recur_id'])) {
169 if (CRM_Member_BAO_Membership
::isSubscriptionCancelled($membership[$dao->id
]['membership_id'])) {
170 $membership[$dao->id
]['auto_renew'] = 2;
173 $membership[$dao->id
]['auto_renew'] = 1;
177 $membership[$dao->id
]['auto_renew'] = 0;
180 // if relevant--membership is active and type allows inheritance--count related memberships
181 if (!empty($statusANDType[$dao->id
]['is_current_member'])
182 && !empty($statusANDType[$dao->id
]['relationship_type_id'])
183 && empty($dao->owner_membership_id
)
185 // not an related membership
188 FROM civicrm_membership m
189 LEFT JOIN civicrm_membership_status ms ON ms.id = m.status_id
190 LEFT JOIN civicrm_contact ct ON ct.id = m.contact_id
191 WHERE m.owner_membership_id = {$dao->id} AND m.is_test = 0 AND ms.is_current_member = 1 AND ct.is_deleted = 0";
192 $num_related = CRM_Core_DAO
::singleValueQuery($query);
193 $max_related = $membership[$dao->id
]['max_related'] ??
NULL;
194 $membership[$dao->id
]['related_count'] = ($max_related == '' ?
ts('%1 created', [1 => $num_related]) : ts('%1 out of %2', [
200 $membership[$dao->id
]['related_count'] = ts('N/A');
204 //Below code gives list of all Membership Types associated
205 //with an Organization(CRM-2016)
206 $membershipTypesResult = civicrm_api3('MembershipType', 'get', [
207 'member_of_contact_id' => $this->_contactId
,
212 $membershipTypes = $membershipTypesResult['values'] ??
NULL;
214 foreach ($membershipTypes as $key => $value) {
215 $membershipTypes[$key]['action'] = CRM_Core_Action
::formLink(self
::membershipTypeslinks(),
218 'id' => $value['id'],
219 'cid' => $this->_contactId
,
223 'membershipType.organization.action',
229 $activeMembers = CRM_Member_BAO_Membership
::activeMembers($membership);
230 $inActiveMembers = CRM_Member_BAO_Membership
::activeMembers($membership, 'inactive');
231 $this->assign('activeMembers', $activeMembers);
232 $this->assign('inActiveMembers', $inActiveMembers);
233 $this->assign('membershipTypes', $membershipTypes);
235 if ($this->_contactId
) {
236 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_contactId
);
237 $this->assign('displayName', $displayName);
238 $this->ajaxResponse
['tabCount'] = CRM_Contact_BAO_Contact
::getCountComponent('membership', $this->_contactId
);
239 // Refresh other tabs with related data
240 $this->ajaxResponse
['updateTabs'] = [
241 '#tab_activity' => CRM_Contact_BAO_Contact
::getCountComponent('activity', $this->_contactId
),
242 '#tab_rel' => CRM_Contact_BAO_Contact
::getCountComponent('rel', $this->_contactId
),
244 if (CRM_Core_Permission
::access('CiviContribute')) {
245 $this->ajaxResponse
['updateTabs']['#tab_contribute'] = CRM_Contact_BAO_Contact
::getCountComponent('contribution', $this->_contactId
);
251 * called when action is view.
255 public function view() {
256 $controller = new CRM_Core_Controller_Simple(
257 'CRM_Member_Form_MembershipView',
258 ts('View Membership'),
261 $controller->setEmbedded(TRUE);
262 $controller->set('id', $this->_id
);
263 $controller->set('cid', $this->_contactId
);
265 return $controller->run();
269 * called when action is update or new.
273 public function edit() {
274 // We're trying to edit existing memberships or create a new one so we'll first check that a membership
275 // type is configured and active, if we don't do this we instead show a permissions error and status bounce.
276 $membershipTypes = \Civi\Api4\MembershipType
::get(TRUE)
277 ->addWhere('is_active', '=', TRUE)
278 // we only need one, more is great but a single result lets us proceed!
281 if (empty($membershipTypes)) {
282 CRM_Core_Error
::statusBounce(ts('You do not appear to have any active membership types configured, please add an active membership type and try again.'));
285 // set https for offline cc transaction
286 $mode = CRM_Utils_Request
::retrieve('mode', 'Alphanumeric', $this);
287 if ($mode == 'test' ||
$mode == 'live') {
288 CRM_Utils_System
::redirectToSSL();
291 // build associated contributions ( note: this is called to show associated contributions in edit mode )
292 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
293 $this->assign('accessContribution', FALSE);
294 if (CRM_Core_Permission
::access('CiviContribute')) {
295 $this->assign('accessContribution', TRUE);
296 CRM_Member_Page_Tab
::associatedContribution($this->_contactId
, $this->_id
);
298 //show associated soft credit when contribution payment is paid by different person in edit mode
299 if ($this->_id
&& $this->_contactId
) {
300 $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$this->_id})";
301 $softCreditList = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionList($this->_contactId
, $filter);
302 if (!empty($softCreditList)) {
303 $this->assign('softCredit', TRUE);
304 $this->assign('softCreditRows', $softCreditList);
310 if ($this->_action
& CRM_Core_Action
::RENEW
) {
311 $path = 'CRM_Member_Form_MembershipRenewal';
312 $title = ts('Renew Membership');
315 $path = 'CRM_Member_Form_Membership';
316 $title = ts('Create Membership');
319 $controller = new CRM_Core_Controller_Simple($path, $title, $this->_action
);
320 $controller->setEmbedded(TRUE);
321 $controller->set('BAOName', $this->getBAOName());
322 $controller->set('id', $this->_id
);
323 $controller->set('cid', $this->_contactId
);
324 return $controller->run();
327 public function preProcess() {
328 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
329 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'browse');
330 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
332 if ($context == 'standalone') {
333 $this->_action
= CRM_Core_Action
::ADD
;
336 $this->_contactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this, TRUE);
337 $this->assign('contactId', $this->_contactId
);
339 // check logged in url permission
340 CRM_Contact_Page_View
::checkUserPermission($this);
343 $this->assign('action', $this->_action
);
345 if ($this->_permission
== CRM_Core_Permission
::EDIT
&& !CRM_Core_Permission
::check('edit memberships')) {
346 // demote to view since user does not have edit membership rights
347 $this->_permission
= CRM_Core_Permission
::VIEW
;
348 $this->assign('permission', 'view');
353 * the main function that is called when the page loads, it decides the which action has to be taken for the page.
357 public function run() {
360 // check if we can process credit card membership
361 if (CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments()) {
362 $this->_isPaymentProcessor
= TRUE;
365 $this->_isPaymentProcessor
= FALSE;
368 // Only show credit card membership signup if user has CiviContribute permission
369 if (CRM_Core_Permission
::access('CiviContribute')) {
370 $this->_accessContribution
= TRUE;
371 $this->assign('accessContribution', TRUE);
373 //show associated soft credit when contribution payment is paid by different person
374 if ($this->_id
&& $this->_contactId
) {
375 $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$this->_id})";
376 $softCreditList = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionList($this->_contactId
, $filter);
377 if (!empty($softCreditList)) {
378 $this->assign('softCredit', TRUE);
379 $this->assign('softCreditRows', $softCreditList);
384 $this->_accessContribution
= FALSE;
385 $this->assign('accessContribution', FALSE);
386 $this->assign('softCredit', FALSE);
389 if ($this->_action
& CRM_Core_Action
::VIEW
) {
392 elseif ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD | CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
393 self
::setContext($this);
397 self
::setContext($this);
401 return parent
::run();
405 * @param CRM_Core_Form $form
406 * @param int $contactId
408 public static function setContext(&$form, $contactId = NULL) {
409 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $form, FALSE, 'search');
411 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $form);
413 $searchContext = CRM_Utils_Request
::retrieve('searchContext', 'String', $form);
416 if (!CRM_Utils_Rule
::qfKey($qfKey)) {
421 $contactId = $form->_contactId
;
426 $url = CRM_Utils_System
::url('civicrm/member', 'reset=1');
430 $url = CRM_Utils_System
::url('civicrm/contact/view', "reset=1&force=1&cid={$contactId}&selectedChild=member");
434 $urlParams = 'force=1';
436 $urlParams .= "&qfKey=$qfKey";
438 $form->assign('searchKey', $qfKey);
440 if ($searchContext) {
441 $url = CRM_Utils_System
::url("civicrm/$searchContext/search", $urlParams);
444 $url = CRM_Utils_System
::url('civicrm/member/search', $urlParams);
449 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
453 $url = CRM_Utils_System
::url('civicrm/contact/view',
454 "reset=1&force=1&cid={$contactId}&selectedChild=activity"
459 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
463 $action = CRM_Utils_Request
::retrieve('action', 'String', $form);
465 $urlParams = 'force=1';
466 $urlString = 'civicrm/contact/search/custom';
467 if ($action == CRM_Core_Action
::UPDATE
) {
468 if ($form->_contactId
) {
469 $urlParams .= '&cid=' . $form->_contactId
;
472 $urlParams .= '&context=fulltext&action=view';
473 $urlString = 'civicrm/contact/view/membership';
476 $urlParams .= "$keyName=$qfKey";
478 $form->assign('searchKey', $qfKey);
479 $url = CRM_Utils_System
::url($urlString, $urlParams);
485 $cid = '&cid=' . $contactId;
487 $url = CRM_Utils_System
::url('civicrm/member/search', 'force=1' . $cid);
491 $session = CRM_Core_Session
::singleton();
492 $session->pushUserContext($url);
498 * @param string $status
499 * @param bool $isPaymentProcessor
500 * @param bool $accessContribution
501 * @param bool $isCancelSupported
502 * @param bool $isUpdateBilling
505 * (reference) of action links
507 public static function &links(
509 $isPaymentProcessor = FALSE,
510 $accessContribution = FALSE,
511 $isCancelSupported = FALSE,
512 $isUpdateBilling = FALSE
514 if (empty(self
::$_links['view'])) {
515 self
::$_links['view'] = [
516 CRM_Core_Action
::VIEW
=> [
517 'name' => ts('View'),
518 'url' => 'civicrm/contact/view/membership',
519 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
520 'title' => ts('View Membership'),
525 if (empty(self
::$_links['all'])) {
527 CRM_Core_Action
::UPDATE
=> [
528 'name' => ts('Edit'),
529 'url' => 'civicrm/contact/view/membership',
530 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
531 'title' => ts('Edit Membership'),
533 CRM_Core_Action
::RENEW
=> [
534 'name' => ts('Renew'),
535 'url' => 'civicrm/contact/view/membership',
536 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
537 'title' => ts('Renew Membership'),
539 CRM_Core_Action
::FOLLOWUP
=> [
540 'name' => ts('Renew-Credit Card'),
541 'url' => 'civicrm/contact/view/membership',
542 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member&mode=live',
543 'title' => ts('Renew Membership Using Credit Card'),
545 CRM_Core_Action
::DELETE
=> [
546 'name' => ts('Delete'),
547 'url' => 'civicrm/contact/view/membership',
548 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
549 'title' => ts('Delete Membership'),
552 if (!$isPaymentProcessor ||
!$accessContribution) {
553 //unset the renew with credit card when payment
554 //processor is not available or user is not permitted to create contributions
555 unset($extraLinks[CRM_Core_Action
::FOLLOWUP
]);
557 self
::$_links['all'] = self
::$_links['view'] +
$extraLinks;
560 if ($isCancelSupported) {
561 $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.');
562 self
::$_links['all'][CRM_Core_Action
::DISABLE
] = [
563 'name' => ts('Cancel Auto-renewal'),
564 'url' => 'civicrm/contribute/unsubscribe',
565 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
566 'title' => ts('Cancel Auto Renew Subscription'),
567 'extra' => 'onclick = "if (confirm(\'' . $cancelMessage . '\') ) { return true; else return false;}"',
570 elseif (isset(self
::$_links['all'][CRM_Core_Action
::DISABLE
])) {
571 unset(self
::$_links['all'][CRM_Core_Action
::DISABLE
]);
574 if ($isUpdateBilling) {
575 self
::$_links['all'][CRM_Core_Action
::MAP
] = [
576 'name' => ts('Change Billing Details'),
577 'url' => 'civicrm/contribute/updatebilling',
578 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
579 'title' => ts('Change Billing Details'),
582 elseif (isset(self
::$_links['all'][CRM_Core_Action
::MAP
])) {
583 unset(self
::$_links['all'][CRM_Core_Action
::MAP
]);
585 return self
::$_links[$status];
589 * Define action links for membership types of related organization.
592 * self::$_membershipTypesLinks array of action links
594 public static function &membershipTypesLinks() {
595 if (!self
::$_membershipTypesLinks) {
596 self
::$_membershipTypesLinks = [
597 CRM_Core_Action
::VIEW
=> [
598 'name' => ts('Members'),
599 'url' => 'civicrm/member/search/',
600 'qs' => 'reset=1&force=1&type=%%id%%',
601 'title' => ts('Search'),
603 CRM_Core_Action
::UPDATE
=> [
604 'name' => ts('Edit'),
605 'url' => 'civicrm/admin/member/membershipType',
606 'qs' => 'action=update&id=%%id%%&reset=1',
607 'title' => ts('Edit Membership Type'),
611 return self
::$_membershipTypesLinks;
615 * used for the to show the associated.
616 * contribution for the membership
618 * @param int $contactId
619 * @param int $membershipId
621 public static function associatedContribution($contactId = NULL, $membershipId = NULL) {
622 $controller = new CRM_Core_Controller_Simple(
623 'CRM_Contribute_Form_Search',
628 $controller->setEmbedded(TRUE);
629 $controller->reset();
630 $controller->set('force', 1);
631 $controller->set('skip_cid', TRUE);
632 $controller->set('memberId', $membershipId);
633 $controller->set('context', 'contribution');
634 $controller->process();
644 public function getBAOName() {
645 return 'CRM_Member_BAO_Membership';
649 * Get a list of links based on permissioned FTs.
651 * @param int $memTypeID
654 * (reference ) action links
656 public static function getPermissionedLinks($memTypeID, &$links) {
657 if (!CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus()) {
660 $finTypeId = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $memTypeID, 'financial_type_id');
661 $finType = CRM_Contribute_PseudoConstant
::financialType($finTypeId);
662 if (!CRM_Core_Permission
::check('edit contributions of type ' . $finType)) {
663 unset($links[CRM_Core_Action
::UPDATE
]);
664 unset($links[CRM_Core_Action
::RENEW
]);
665 unset($links[CRM_Core_Action
::FOLLOWUP
]);
667 if (!CRM_Core_Permission
::check('delete contributions of type ' . $finType)) {
668 unset($links[CRM_Core_Action
::DELETE
]);