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 CRM_Financial_BAO_FinancialType
::getAvailableMembershipTypes($membershipTypes);
36 $addWhere = "membership_type_id IN (0)";
37 if (!empty($membershipTypes)) {
38 $addWhere = "membership_type_id IN (" . implode(',', array_keys($membershipTypes)) . ")";
42 $dao = new CRM_Member_DAO_Membership();
43 $dao->contact_id
= $this->_contactId
;
44 $dao->whereAdd($addWhere);
47 //CRM--4418, check for view, edit, delete
48 $permissions = [CRM_Core_Permission
::VIEW
];
49 if (CRM_Core_Permission
::check('edit memberships')) {
50 $permissions[] = CRM_Core_Permission
::EDIT
;
52 if (CRM_Core_Permission
::check('delete in CiviMember')) {
53 $permissions[] = CRM_Core_Permission
::DELETE
;
55 $mask = CRM_Core_Action
::mask($permissions);
57 // get deceased status id
58 $allStatus = CRM_Member_PseudoConstant
::membershipStatus();
59 $deceasedStatusId = array_search('Deceased', $allStatus);
62 $allCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
64 //checks membership of contact itself
65 while ($dao->fetch()) {
66 $membership[$dao->id
] = [];
67 CRM_Core_DAO
::storeValues($dao, $membership[$dao->id
]);
70 $membership[$dao->id
]['campaign'] = $allCampaigns[$dao->campaign_id
] ??
NULL;
72 //get the membership status and type values.
73 $statusANDType = CRM_Member_BAO_Membership
::getStatusANDTypeValues($dao->id
);
74 foreach (['status', 'membership_type'] as $fld) {
75 $membership[$dao->id
][$fld] = $statusANDType[$dao->id
][$fld] ??
NULL;
77 if (!empty($statusANDType[$dao->id
]['is_current_member'])) {
78 $membership[$dao->id
]['active'] = TRUE;
80 if (empty($dao->owner_membership_id
)) {
81 // unset renew and followup link for deceased membership
83 if ($dao->status_id
== $deceasedStatusId) {
84 $currentMask = $currentMask & ~CRM_Core_Action
::RENEW
& ~CRM_Core_Action
::FOLLOWUP
;
87 $isUpdateBilling = FALSE;
88 // It would be better to determine if there is a recurring contribution &
89 // is so get the entity for the recurring contribution (& skip if not).
90 $paymentObject = CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity(
91 $membership[$dao->id
]['membership_id'], 'membership', 'obj');
92 if (!empty($paymentObject)) {
93 $isUpdateBilling = $paymentObject->supports('updateSubscriptionBillingInfo');
96 // @todo - get this working with syntax style $paymentObject->supports(array
97 //('CancelSubscriptionSupported'));
98 $isCancelSupported = CRM_Member_BAO_Membership
::isCancelSubscriptionSupported(
99 $membership[$dao->id
]['membership_id']);
100 $links = self
::links('all',
106 self
::getPermissionedLinks($dao->membership_type_id
, $links);
107 $membership[$dao->id
]['action'] = CRM_Core_Action
::formLink($links,
111 'cid' => $this->_contactId
,
115 'membership.tab.row',
121 $links = self
::links('view');
122 self
::getPermissionedLinks($dao->membership_type_id
, $links);
123 $membership[$dao->id
]['action'] = CRM_Core_Action
::formLink($links,
127 'cid' => $this->_contactId
,
131 'membership.tab.row',
137 // Display Auto-renew status on page (0=disabled, 1=enabled, 2=enabled, but error
138 if (!empty($membership[$dao->id
]['contribution_recur_id'])) {
139 if (CRM_Member_BAO_Membership
::isSubscriptionCancelled($membership[$dao->id
]['membership_id'])) {
140 $membership[$dao->id
]['auto_renew'] = 2;
143 $membership[$dao->id
]['auto_renew'] = 1;
147 $membership[$dao->id
]['auto_renew'] = 0;
150 // if relevant--membership is active and type allows inheritance--count related memberships
151 if (!empty($statusANDType[$dao->id
]['is_current_member'])
152 && !empty($statusANDType[$dao->id
]['relationship_type_id'])
153 && empty($dao->owner_membership_id
)
155 // not an related membership
158 FROM civicrm_membership m
159 LEFT JOIN civicrm_membership_status ms ON ms.id = m.status_id
160 LEFT JOIN civicrm_contact ct ON ct.id = m.contact_id
161 WHERE m.owner_membership_id = {$dao->id} AND m.is_test = 0 AND ms.is_current_member = 1 AND ct.is_deleted = 0";
162 $num_related = CRM_Core_DAO
::singleValueQuery($query);
163 $max_related = $membership[$dao->id
]['max_related'] ??
NULL;
164 $membership[$dao->id
]['related_count'] = ($max_related == '' ?
ts('%1 created', [1 => $num_related]) : ts('%1 out of %2', [
170 $membership[$dao->id
]['related_count'] = ts('N/A');
174 //Below code gives list of all Membership Types associated
175 //with an Organization(CRM-2016)
176 $membershipTypesResult = civicrm_api3('MembershipType', 'get', [
177 'member_of_contact_id' => $this->_contactId
,
182 $membershipTypes = $membershipTypesResult['values'] ??
NULL;
184 foreach ($membershipTypes as $key => $value) {
185 $membershipTypes[$key]['action'] = CRM_Core_Action
::formLink(self
::membershipTypeslinks(),
188 'id' => $value['id'],
189 'cid' => $this->_contactId
,
193 'membershipType.organization.action',
199 $activeMembers = CRM_Member_BAO_Membership
::activeMembers($membership);
200 $inActiveMembers = CRM_Member_BAO_Membership
::activeMembers($membership, 'inactive');
201 $this->assign('activeMembers', $activeMembers);
202 $this->assign('inActiveMembers', $inActiveMembers);
203 $this->assign('membershipTypes', $membershipTypes);
205 if ($this->_contactId
) {
206 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_contactId
);
207 $this->assign('displayName', $displayName);
208 $this->ajaxResponse
['tabCount'] = CRM_Contact_BAO_Contact
::getCountComponent('membership', $this->_contactId
);
209 // Refresh other tabs with related data
210 $this->ajaxResponse
['updateTabs'] = [
211 '#tab_activity' => CRM_Contact_BAO_Contact
::getCountComponent('activity', $this->_contactId
),
212 '#tab_rel' => CRM_Contact_BAO_Contact
::getCountComponent('rel', $this->_contactId
),
214 if (CRM_Core_Permission
::access('CiviContribute')) {
215 $this->ajaxResponse
['updateTabs']['#tab_contribute'] = CRM_Contact_BAO_Contact
::getCountComponent('contribution', $this->_contactId
);
221 * called when action is view.
225 public function view() {
226 $controller = new CRM_Core_Controller_Simple(
227 'CRM_Member_Form_MembershipView',
228 ts('View Membership'),
231 $controller->setEmbedded(TRUE);
232 $controller->set('id', $this->_id
);
233 $controller->set('cid', $this->_contactId
);
235 return $controller->run();
239 * called when action is update or new.
243 public function edit() {
244 // set https for offline cc transaction
245 $mode = CRM_Utils_Request
::retrieve('mode', 'Alphanumeric', $this);
246 if ($mode == 'test' ||
$mode == 'live') {
247 CRM_Utils_System
::redirectToSSL();
250 // build associated contributions ( note: this is called to show associated contributions in edit mode )
251 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
252 $this->assign('accessContribution', FALSE);
253 if (CRM_Core_Permission
::access('CiviContribute')) {
254 $this->assign('accessContribution', TRUE);
255 CRM_Member_Page_Tab
::associatedContribution($this->_contactId
, $this->_id
);
257 //show associated soft credit when contribution payment is paid by different person in edit mode
258 if ($this->_id
&& $this->_contactId
) {
259 $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$this->_id})";
260 $softCreditList = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionList($this->_contactId
, $filter);
261 if (!empty($softCreditList)) {
262 $this->assign('softCredit', TRUE);
263 $this->assign('softCreditRows', $softCreditList);
269 if ($this->_action
& CRM_Core_Action
::RENEW
) {
270 $path = 'CRM_Member_Form_MembershipRenewal';
271 $title = ts('Renew Membership');
274 $path = 'CRM_Member_Form_Membership';
275 $title = ts('Create Membership');
278 $controller = new CRM_Core_Controller_Simple($path, $title, $this->_action
);
279 $controller->setEmbedded(TRUE);
280 $controller->set('BAOName', $this->getBAOName());
281 $controller->set('id', $this->_id
);
282 $controller->set('cid', $this->_contactId
);
283 return $controller->run();
286 public function preProcess() {
287 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
288 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'browse');
289 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
291 if ($context == 'standalone') {
292 $this->_action
= CRM_Core_Action
::ADD
;
295 $this->_contactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this, TRUE);
296 $this->assign('contactId', $this->_contactId
);
298 // check logged in url permission
299 CRM_Contact_Page_View
::checkUserPermission($this);
302 $this->assign('action', $this->_action
);
304 if ($this->_permission
== CRM_Core_Permission
::EDIT
&& !CRM_Core_Permission
::check('edit memberships')) {
305 // demote to view since user does not have edit membership rights
306 $this->_permission
= CRM_Core_Permission
::VIEW
;
307 $this->assign('permission', 'view');
312 * the main function that is called when the page loads, it decides the which action has to be taken for the page.
316 public function run() {
319 // check if we can process credit card membership
320 $newCredit = CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments();
321 $this->assign('newCredit', $newCredit);
324 $this->_isPaymentProcessor
= TRUE;
327 $this->_isPaymentProcessor
= FALSE;
330 // Only show credit card membership signup if user has CiviContribute permission
331 if (CRM_Core_Permission
::access('CiviContribute')) {
332 $this->_accessContribution
= TRUE;
333 $this->assign('accessContribution', TRUE);
335 //show associated soft credit when contribution payment is paid by different person
336 if ($this->_id
&& $this->_contactId
) {
337 $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$this->_id})";
338 $softCreditList = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionList($this->_contactId
, $filter);
339 if (!empty($softCreditList)) {
340 $this->assign('softCredit', TRUE);
341 $this->assign('softCreditRows', $softCreditList);
346 $this->_accessContribution
= FALSE;
347 $this->assign('accessContribution', FALSE);
348 $this->assign('softCredit', FALSE);
351 if ($this->_action
& CRM_Core_Action
::VIEW
) {
354 elseif ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD | CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
355 self
::setContext($this);
359 self
::setContext($this);
363 return parent
::run();
367 * @param CRM_Core_Form $form
368 * @param int $contactId
370 public static function setContext(&$form, $contactId = NULL) {
371 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $form, FALSE, 'search');
373 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $form);
375 $searchContext = CRM_Utils_Request
::retrieve('searchContext', 'String', $form);
378 if (!CRM_Utils_Rule
::qfKey($qfKey)) {
383 $contactId = $form->_contactId
;
388 $url = CRM_Utils_System
::url('civicrm/member', 'reset=1');
392 $url = CRM_Utils_System
::url('civicrm/contact/view', "reset=1&force=1&cid={$contactId}&selectedChild=member");
396 $urlParams = 'force=1';
398 $urlParams .= "&qfKey=$qfKey";
400 $form->assign('searchKey', $qfKey);
402 if ($searchContext) {
403 $url = CRM_Utils_System
::url("civicrm/$searchContext/search", $urlParams);
406 $url = CRM_Utils_System
::url('civicrm/member/search', $urlParams);
411 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
415 $url = CRM_Utils_System
::url('civicrm/contact/view',
416 "reset=1&force=1&cid={$contactId}&selectedChild=activity"
421 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
425 $action = CRM_Utils_Request
::retrieve('action', 'String', $form);
427 $urlParams = 'force=1';
428 $urlString = 'civicrm/contact/search/custom';
429 if ($action == CRM_Core_Action
::UPDATE
) {
430 if ($form->_contactId
) {
431 $urlParams .= '&cid=' . $form->_contactId
;
434 $urlParams .= '&context=fulltext&action=view';
435 $urlString = 'civicrm/contact/view/membership';
438 $urlParams .= "$keyName=$qfKey";
440 $form->assign('searchKey', $qfKey);
441 $url = CRM_Utils_System
::url($urlString, $urlParams);
447 $cid = '&cid=' . $contactId;
449 $url = CRM_Utils_System
::url('civicrm/member/search', 'force=1' . $cid);
453 $session = CRM_Core_Session
::singleton();
454 $session->pushUserContext($url);
460 * @param string $status
461 * @param null $isPaymentProcessor
462 * @param null $accessContribution
463 * @param bool $isCancelSupported
464 * @param bool $isUpdateBilling
467 * (reference) of action links
469 public static function &links(
471 $isPaymentProcessor = NULL,
472 $accessContribution = NULL,
473 $isCancelSupported = FALSE,
474 $isUpdateBilling = FALSE
476 if (empty(self
::$_links['view'])) {
477 self
::$_links['view'] = [
478 CRM_Core_Action
::VIEW
=> [
479 'name' => ts('View'),
480 'url' => 'civicrm/contact/view/membership',
481 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
482 'title' => ts('View Membership'),
487 if (empty(self
::$_links['all'])) {
489 CRM_Core_Action
::UPDATE
=> [
490 'name' => ts('Edit'),
491 'url' => 'civicrm/contact/view/membership',
492 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
493 'title' => ts('Edit Membership'),
495 CRM_Core_Action
::RENEW
=> [
496 'name' => ts('Renew'),
497 'url' => 'civicrm/contact/view/membership',
498 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
499 'title' => ts('Renew Membership'),
501 CRM_Core_Action
::FOLLOWUP
=> [
502 'name' => ts('Renew-Credit Card'),
503 'url' => 'civicrm/contact/view/membership',
504 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member&mode=live',
505 'title' => ts('Renew Membership Using Credit Card'),
507 CRM_Core_Action
::DELETE
=> [
508 'name' => ts('Delete'),
509 'url' => 'civicrm/contact/view/membership',
510 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
511 'title' => ts('Delete Membership'),
514 if (!$isPaymentProcessor ||
!$accessContribution) {
515 //unset the renew with credit card when payment
516 //processor is not available or user is not permitted to create contributions
517 unset($extraLinks[CRM_Core_Action
::FOLLOWUP
]);
519 self
::$_links['all'] = self
::$_links['view'] +
$extraLinks;
522 if ($isCancelSupported) {
523 $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.');
524 self
::$_links['all'][CRM_Core_Action
::DISABLE
] = [
525 'name' => ts('Cancel Auto-renewal'),
526 'url' => 'civicrm/contribute/unsubscribe',
527 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
528 'title' => ts('Cancel Auto Renew Subscription'),
529 'extra' => 'onclick = "if (confirm(\'' . $cancelMessage . '\') ) { return true; else return false;}"',
532 elseif (isset(self
::$_links['all'][CRM_Core_Action
::DISABLE
])) {
533 unset(self
::$_links['all'][CRM_Core_Action
::DISABLE
]);
536 if ($isUpdateBilling) {
537 self
::$_links['all'][CRM_Core_Action
::MAP
] = [
538 'name' => ts('Change Billing Details'),
539 'url' => 'civicrm/contribute/updatebilling',
540 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
541 'title' => ts('Change Billing Details'),
544 elseif (isset(self
::$_links['all'][CRM_Core_Action
::MAP
])) {
545 unset(self
::$_links['all'][CRM_Core_Action
::MAP
]);
547 return self
::$_links[$status];
551 * Define action links for membership types of related organization.
554 * self::$_membershipTypesLinks array of action links
556 public static function &membershipTypesLinks() {
557 if (!self
::$_membershipTypesLinks) {
558 self
::$_membershipTypesLinks = [
559 CRM_Core_Action
::VIEW
=> [
560 'name' => ts('Members'),
561 'url' => 'civicrm/member/search/',
562 'qs' => 'reset=1&force=1&type=%%id%%',
563 'title' => ts('Search'),
565 CRM_Core_Action
::UPDATE
=> [
566 'name' => ts('Edit'),
567 'url' => 'civicrm/admin/member/membershipType',
568 'qs' => 'action=update&id=%%id%%&reset=1',
569 'title' => ts('Edit Membership Type'),
573 return self
::$_membershipTypesLinks;
577 * used for the to show the associated.
578 * contribution for the membership
580 * @param int $contactId
581 * @param int $membershipId
583 public static function associatedContribution($contactId = NULL, $membershipId = NULL) {
584 $controller = new CRM_Core_Controller_Simple(
585 'CRM_Contribute_Form_Search',
590 $controller->setEmbedded(TRUE);
591 $controller->reset();
592 $controller->set('force', 1);
593 $controller->set('cid', $contactId);
594 $controller->set('memberId', $membershipId);
595 $controller->set('context', 'contribution');
596 $controller->process();
606 public function getBAOName() {
607 return 'CRM_Member_BAO_Membership';
611 * Get a list of links based on permissioned FTs.
613 * @param int $memTypeID
616 * (reference ) action links
618 public static function getPermissionedLinks($memTypeID, &$links) {
619 if (!CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus()) {
622 $finTypeId = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $memTypeID, 'financial_type_id');
623 $finType = CRM_Contribute_PseudoConstant
::financialType($finTypeId);
624 if (!CRM_Core_Permission
::check('edit contributions of type ' . $finType)) {
625 unset($links[CRM_Core_Action
::UPDATE
]);
626 unset($links[CRM_Core_Action
::RENEW
]);
627 unset($links[CRM_Core_Action
::FOLLOWUP
]);
629 if (!CRM_Core_Permission
::check('delete contributions of type ' . $finType)) {
630 unset($links[CRM_Core_Action
::DELETE
]);