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
;
51 $linkButtons['add_membership'] = [
52 'title' => ts('Add Membership'),
53 'url' => 'civicrm/contact/view/membership',
54 'qs' => "reset=1&action=add&cid={$this->_contactId}&context=membership",
55 'icon' => 'fa-plus-circle',
58 if ($this->_accessContribution
&& CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments()) {
59 $linkButtons['creditcard_membership'] = [
60 'title' => ts('Submit Credit Card Membership'),
61 'url' => 'civicrm/contact/view/membership',
62 'qs' => "reset=1&action=add&cid={$this->_contactId}&context=membership&mode=live",
63 'icon' => 'fa-credit-card',
67 $this->assign('linkButtons', $linkButtons ??
[]);
69 if (CRM_Core_Permission
::check('delete in CiviMember')) {
70 $permissions[] = CRM_Core_Permission
::DELETE
;
72 $mask = CRM_Core_Action
::mask($permissions);
74 // get deceased status id
75 $allStatus = CRM_Member_PseudoConstant
::membershipStatus();
76 $deceasedStatusId = array_search('Deceased', $allStatus);
79 $allCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
81 //checks membership of contact itself
82 while ($dao->fetch()) {
83 $membership[$dao->id
] = [];
84 CRM_Core_DAO
::storeValues($dao, $membership[$dao->id
]);
87 $membership[$dao->id
]['campaign'] = $allCampaigns[$dao->campaign_id
] ??
NULL;
89 //get the membership status and type values.
90 $statusANDType = CRM_Member_BAO_Membership
::getStatusANDTypeValues($dao->id
);
91 foreach (['status', 'membership_type'] as $fld) {
92 $membership[$dao->id
][$fld] = $statusANDType[$dao->id
][$fld] ??
NULL;
94 if (!empty($statusANDType[$dao->id
]['is_current_member'])) {
95 $membership[$dao->id
]['active'] = TRUE;
97 if (empty($dao->owner_membership_id
)) {
98 // unset renew and followup link for deceased membership
100 if ($dao->status_id
== $deceasedStatusId) {
101 $currentMask = $currentMask & ~CRM_Core_Action
::RENEW
& ~CRM_Core_Action
::FOLLOWUP
;
104 $isUpdateBilling = FALSE;
105 // It would be better to determine if there is a recurring contribution &
106 // is so get the entity for the recurring contribution (& skip if not).
107 $paymentObject = CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity(
108 $membership[$dao->id
]['membership_id'], 'membership', 'obj');
109 if (!empty($paymentObject)) {
110 $isUpdateBilling = $paymentObject->supports('updateSubscriptionBillingInfo');
113 // @todo - get this working with syntax style $paymentObject->supports(array
114 //('CancelSubscriptionSupported'));
115 $isCancelSupported = CRM_Member_BAO_Membership
::isCancelSubscriptionSupported(
116 $membership[$dao->id
]['membership_id']);
117 $links = self
::links('all',
123 self
::getPermissionedLinks($dao->membership_type_id
, $links);
124 $membership[$dao->id
]['action'] = CRM_Core_Action
::formLink($links,
128 'cid' => $this->_contactId
,
132 'membership.tab.row',
138 $links = self
::links('view');
139 self
::getPermissionedLinks($dao->membership_type_id
, $links);
140 $membership[$dao->id
]['action'] = CRM_Core_Action
::formLink($links,
144 'cid' => $this->_contactId
,
148 'membership.tab.row',
154 // Display Auto-renew status on page (0=disabled, 1=enabled, 2=enabled, but error
155 if (!empty($membership[$dao->id
]['contribution_recur_id'])) {
156 if (CRM_Member_BAO_Membership
::isSubscriptionCancelled($membership[$dao->id
]['membership_id'])) {
157 $membership[$dao->id
]['auto_renew'] = 2;
160 $membership[$dao->id
]['auto_renew'] = 1;
164 $membership[$dao->id
]['auto_renew'] = 0;
167 // if relevant--membership is active and type allows inheritance--count related memberships
168 if (!empty($statusANDType[$dao->id
]['is_current_member'])
169 && !empty($statusANDType[$dao->id
]['relationship_type_id'])
170 && empty($dao->owner_membership_id
)
172 // not an related membership
175 FROM civicrm_membership m
176 LEFT JOIN civicrm_membership_status ms ON ms.id = m.status_id
177 LEFT JOIN civicrm_contact ct ON ct.id = m.contact_id
178 WHERE m.owner_membership_id = {$dao->id} AND m.is_test = 0 AND ms.is_current_member = 1 AND ct.is_deleted = 0";
179 $num_related = CRM_Core_DAO
::singleValueQuery($query);
180 $max_related = $membership[$dao->id
]['max_related'] ??
NULL;
181 $membership[$dao->id
]['related_count'] = ($max_related == '' ?
ts('%1 created', [1 => $num_related]) : ts('%1 out of %2', [
187 $membership[$dao->id
]['related_count'] = ts('N/A');
191 //Below code gives list of all Membership Types associated
192 //with an Organization(CRM-2016)
193 $membershipTypesResult = civicrm_api3('MembershipType', 'get', [
194 'member_of_contact_id' => $this->_contactId
,
199 $membershipTypes = $membershipTypesResult['values'] ??
NULL;
201 foreach ($membershipTypes as $key => $value) {
202 $membershipTypes[$key]['action'] = CRM_Core_Action
::formLink(self
::membershipTypeslinks(),
205 'id' => $value['id'],
206 'cid' => $this->_contactId
,
210 'membershipType.organization.action',
216 $activeMembers = CRM_Member_BAO_Membership
::activeMembers($membership);
217 $inActiveMembers = CRM_Member_BAO_Membership
::activeMembers($membership, 'inactive');
218 $this->assign('activeMembers', $activeMembers);
219 $this->assign('inActiveMembers', $inActiveMembers);
220 $this->assign('membershipTypes', $membershipTypes);
222 if ($this->_contactId
) {
223 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_contactId
);
224 $this->assign('displayName', $displayName);
225 $this->ajaxResponse
['tabCount'] = CRM_Contact_BAO_Contact
::getCountComponent('membership', $this->_contactId
);
226 // Refresh other tabs with related data
227 $this->ajaxResponse
['updateTabs'] = [
228 '#tab_activity' => CRM_Contact_BAO_Contact
::getCountComponent('activity', $this->_contactId
),
229 '#tab_rel' => CRM_Contact_BAO_Contact
::getCountComponent('rel', $this->_contactId
),
231 if (CRM_Core_Permission
::access('CiviContribute')) {
232 $this->ajaxResponse
['updateTabs']['#tab_contribute'] = CRM_Contact_BAO_Contact
::getCountComponent('contribution', $this->_contactId
);
238 * called when action is view.
242 public function view() {
243 $controller = new CRM_Core_Controller_Simple(
244 'CRM_Member_Form_MembershipView',
245 ts('View Membership'),
248 $controller->setEmbedded(TRUE);
249 $controller->set('id', $this->_id
);
250 $controller->set('cid', $this->_contactId
);
252 return $controller->run();
256 * called when action is update or new.
260 public function edit() {
261 // set https for offline cc transaction
262 $mode = CRM_Utils_Request
::retrieve('mode', 'Alphanumeric', $this);
263 if ($mode == 'test' ||
$mode == 'live') {
264 CRM_Utils_System
::redirectToSSL();
267 // build associated contributions ( note: this is called to show associated contributions in edit mode )
268 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
269 $this->assign('accessContribution', FALSE);
270 if (CRM_Core_Permission
::access('CiviContribute')) {
271 $this->assign('accessContribution', TRUE);
272 CRM_Member_Page_Tab
::associatedContribution($this->_contactId
, $this->_id
);
274 //show associated soft credit when contribution payment is paid by different person in edit mode
275 if ($this->_id
&& $this->_contactId
) {
276 $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$this->_id})";
277 $softCreditList = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionList($this->_contactId
, $filter);
278 if (!empty($softCreditList)) {
279 $this->assign('softCredit', TRUE);
280 $this->assign('softCreditRows', $softCreditList);
286 if ($this->_action
& CRM_Core_Action
::RENEW
) {
287 $path = 'CRM_Member_Form_MembershipRenewal';
288 $title = ts('Renew Membership');
291 $path = 'CRM_Member_Form_Membership';
292 $title = ts('Create Membership');
295 $controller = new CRM_Core_Controller_Simple($path, $title, $this->_action
);
296 $controller->setEmbedded(TRUE);
297 $controller->set('BAOName', $this->getBAOName());
298 $controller->set('id', $this->_id
);
299 $controller->set('cid', $this->_contactId
);
300 return $controller->run();
303 public function preProcess() {
304 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
305 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'browse');
306 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
308 if ($context == 'standalone') {
309 $this->_action
= CRM_Core_Action
::ADD
;
312 $this->_contactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this, TRUE);
313 $this->assign('contactId', $this->_contactId
);
315 // check logged in url permission
316 CRM_Contact_Page_View
::checkUserPermission($this);
319 $this->assign('action', $this->_action
);
321 if ($this->_permission
== CRM_Core_Permission
::EDIT
&& !CRM_Core_Permission
::check('edit memberships')) {
322 // demote to view since user does not have edit membership rights
323 $this->_permission
= CRM_Core_Permission
::VIEW
;
324 $this->assign('permission', 'view');
329 * the main function that is called when the page loads, it decides the which action has to be taken for the page.
333 public function run() {
336 // check if we can process credit card membership
337 if (CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments()) {
338 $this->_isPaymentProcessor
= TRUE;
341 $this->_isPaymentProcessor
= FALSE;
344 // Only show credit card membership signup if user has CiviContribute permission
345 if (CRM_Core_Permission
::access('CiviContribute')) {
346 $this->_accessContribution
= TRUE;
347 $this->assign('accessContribution', TRUE);
349 //show associated soft credit when contribution payment is paid by different person
350 if ($this->_id
&& $this->_contactId
) {
351 $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$this->_id})";
352 $softCreditList = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionList($this->_contactId
, $filter);
353 if (!empty($softCreditList)) {
354 $this->assign('softCredit', TRUE);
355 $this->assign('softCreditRows', $softCreditList);
360 $this->_accessContribution
= FALSE;
361 $this->assign('accessContribution', FALSE);
362 $this->assign('softCredit', FALSE);
365 if ($this->_action
& CRM_Core_Action
::VIEW
) {
368 elseif ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD | CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
369 self
::setContext($this);
373 self
::setContext($this);
377 return parent
::run();
381 * @param CRM_Core_Form $form
382 * @param int $contactId
384 public static function setContext(&$form, $contactId = NULL) {
385 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $form, FALSE, 'search');
387 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $form);
389 $searchContext = CRM_Utils_Request
::retrieve('searchContext', 'String', $form);
392 if (!CRM_Utils_Rule
::qfKey($qfKey)) {
397 $contactId = $form->_contactId
;
402 $url = CRM_Utils_System
::url('civicrm/member', 'reset=1');
406 $url = CRM_Utils_System
::url('civicrm/contact/view', "reset=1&force=1&cid={$contactId}&selectedChild=member");
410 $urlParams = 'force=1';
412 $urlParams .= "&qfKey=$qfKey";
414 $form->assign('searchKey', $qfKey);
416 if ($searchContext) {
417 $url = CRM_Utils_System
::url("civicrm/$searchContext/search", $urlParams);
420 $url = CRM_Utils_System
::url('civicrm/member/search', $urlParams);
425 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
429 $url = CRM_Utils_System
::url('civicrm/contact/view',
430 "reset=1&force=1&cid={$contactId}&selectedChild=activity"
435 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
439 $action = CRM_Utils_Request
::retrieve('action', 'String', $form);
441 $urlParams = 'force=1';
442 $urlString = 'civicrm/contact/search/custom';
443 if ($action == CRM_Core_Action
::UPDATE
) {
444 if ($form->_contactId
) {
445 $urlParams .= '&cid=' . $form->_contactId
;
448 $urlParams .= '&context=fulltext&action=view';
449 $urlString = 'civicrm/contact/view/membership';
452 $urlParams .= "$keyName=$qfKey";
454 $form->assign('searchKey', $qfKey);
455 $url = CRM_Utils_System
::url($urlString, $urlParams);
461 $cid = '&cid=' . $contactId;
463 $url = CRM_Utils_System
::url('civicrm/member/search', 'force=1' . $cid);
467 $session = CRM_Core_Session
::singleton();
468 $session->pushUserContext($url);
474 * @param string $status
475 * @param null $isPaymentProcessor
476 * @param null $accessContribution
477 * @param bool $isCancelSupported
478 * @param bool $isUpdateBilling
481 * (reference) of action links
483 public static function &links(
485 $isPaymentProcessor = NULL,
486 $accessContribution = NULL,
487 $isCancelSupported = FALSE,
488 $isUpdateBilling = FALSE
490 if (empty(self
::$_links['view'])) {
491 self
::$_links['view'] = [
492 CRM_Core_Action
::VIEW
=> [
493 'name' => ts('View'),
494 'url' => 'civicrm/contact/view/membership',
495 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
496 'title' => ts('View Membership'),
501 if (empty(self
::$_links['all'])) {
503 CRM_Core_Action
::UPDATE
=> [
504 'name' => ts('Edit'),
505 'url' => 'civicrm/contact/view/membership',
506 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
507 'title' => ts('Edit Membership'),
509 CRM_Core_Action
::RENEW
=> [
510 'name' => ts('Renew'),
511 'url' => 'civicrm/contact/view/membership',
512 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
513 'title' => ts('Renew Membership'),
515 CRM_Core_Action
::FOLLOWUP
=> [
516 'name' => ts('Renew-Credit Card'),
517 'url' => 'civicrm/contact/view/membership',
518 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member&mode=live',
519 'title' => ts('Renew Membership Using Credit Card'),
521 CRM_Core_Action
::DELETE
=> [
522 'name' => ts('Delete'),
523 'url' => 'civicrm/contact/view/membership',
524 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
525 'title' => ts('Delete Membership'),
528 if (!$isPaymentProcessor ||
!$accessContribution) {
529 //unset the renew with credit card when payment
530 //processor is not available or user is not permitted to create contributions
531 unset($extraLinks[CRM_Core_Action
::FOLLOWUP
]);
533 self
::$_links['all'] = self
::$_links['view'] +
$extraLinks;
536 if ($isCancelSupported) {
537 $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.');
538 self
::$_links['all'][CRM_Core_Action
::DISABLE
] = [
539 'name' => ts('Cancel Auto-renewal'),
540 'url' => 'civicrm/contribute/unsubscribe',
541 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
542 'title' => ts('Cancel Auto Renew Subscription'),
543 'extra' => 'onclick = "if (confirm(\'' . $cancelMessage . '\') ) { return true; else return false;}"',
546 elseif (isset(self
::$_links['all'][CRM_Core_Action
::DISABLE
])) {
547 unset(self
::$_links['all'][CRM_Core_Action
::DISABLE
]);
550 if ($isUpdateBilling) {
551 self
::$_links['all'][CRM_Core_Action
::MAP
] = [
552 'name' => ts('Change Billing Details'),
553 'url' => 'civicrm/contribute/updatebilling',
554 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
555 'title' => ts('Change Billing Details'),
558 elseif (isset(self
::$_links['all'][CRM_Core_Action
::MAP
])) {
559 unset(self
::$_links['all'][CRM_Core_Action
::MAP
]);
561 return self
::$_links[$status];
565 * Define action links for membership types of related organization.
568 * self::$_membershipTypesLinks array of action links
570 public static function &membershipTypesLinks() {
571 if (!self
::$_membershipTypesLinks) {
572 self
::$_membershipTypesLinks = [
573 CRM_Core_Action
::VIEW
=> [
574 'name' => ts('Members'),
575 'url' => 'civicrm/member/search/',
576 'qs' => 'reset=1&force=1&type=%%id%%',
577 'title' => ts('Search'),
579 CRM_Core_Action
::UPDATE
=> [
580 'name' => ts('Edit'),
581 'url' => 'civicrm/admin/member/membershipType',
582 'qs' => 'action=update&id=%%id%%&reset=1',
583 'title' => ts('Edit Membership Type'),
587 return self
::$_membershipTypesLinks;
591 * used for the to show the associated.
592 * contribution for the membership
594 * @param int $contactId
595 * @param int $membershipId
597 public static function associatedContribution($contactId = NULL, $membershipId = NULL) {
598 $controller = new CRM_Core_Controller_Simple(
599 'CRM_Contribute_Form_Search',
604 $controller->setEmbedded(TRUE);
605 $controller->reset();
606 $controller->set('force', 1);
607 $controller->set('skip_cid', TRUE);
608 $controller->set('memberId', $membershipId);
609 $controller->set('context', 'contribution');
610 $controller->process();
620 public function getBAOName() {
621 return 'CRM_Member_BAO_Membership';
625 * Get a list of links based on permissioned FTs.
627 * @param int $memTypeID
630 * (reference ) action links
632 public static function getPermissionedLinks($memTypeID, &$links) {
633 if (!CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus()) {
636 $finTypeId = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $memTypeID, 'financial_type_id');
637 $finType = CRM_Contribute_PseudoConstant
::financialType($finTypeId);
638 if (!CRM_Core_Permission
::check('edit contributions of type ' . $finType)) {
639 unset($links[CRM_Core_Action
::UPDATE
]);
640 unset($links[CRM_Core_Action
::RENEW
]);
641 unset($links[CRM_Core_Action
::FOLLOWUP
]);
643 if (!CRM_Core_Permission
::check('delete contributions of type ' . $finType)) {
644 unset($links[CRM_Core_Action
::DELETE
]);