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() {
37 $dao = new CRM_Member_DAO_Membership();
38 $dao->contact_id
= $this->_contactId
;
41 //CRM--4418, check for view, edit, delete
42 $permissions = [CRM_Core_Permission
::VIEW
];
43 if (CRM_Core_Permission
::check('edit memberships')) {
44 $permissions[] = CRM_Core_Permission
::EDIT
;
46 if (CRM_Core_Permission
::check('delete in CiviMember')) {
47 $permissions[] = CRM_Core_Permission
::DELETE
;
49 $mask = CRM_Core_Action
::mask($permissions);
51 // get deceased status id
52 $allStatus = CRM_Member_PseudoConstant
::membershipStatus();
53 $deceasedStatusId = array_search('Deceased', $allStatus);
56 $allCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
58 //checks membership of contact itself
59 while ($dao->fetch()) {
60 $membership[$dao->id
] = [];
61 CRM_Core_DAO
::storeValues($dao, $membership[$dao->id
]);
64 $membership[$dao->id
]['campaign'] = $allCampaigns[$dao->campaign_id
] ??
NULL;
66 //get the membership status and type values.
67 $statusANDType = CRM_Member_BAO_Membership
::getStatusANDTypeValues($dao->id
);
68 foreach (['status', 'membership_type'] as $fld) {
69 $membership[$dao->id
][$fld] = $statusANDType[$dao->id
][$fld] ??
NULL;
71 if (!empty($statusANDType[$dao->id
]['is_current_member'])) {
72 $membership[$dao->id
]['active'] = TRUE;
74 if (empty($dao->owner_membership_id
)) {
75 // unset renew and followup link for deceased membership
77 if ($dao->status_id
== $deceasedStatusId) {
78 $currentMask = $currentMask & ~CRM_Core_Action
::RENEW
& ~CRM_Core_Action
::FOLLOWUP
;
81 $isUpdateBilling = FALSE;
82 // It would be better to determine if there is a recurring contribution &
83 // is so get the entity for the recurring contribution (& skip if not).
84 $paymentObject = CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity(
85 $membership[$dao->id
]['membership_id'], 'membership', 'obj');
86 if (!empty($paymentObject)) {
87 $isUpdateBilling = $paymentObject->supports('updateSubscriptionBillingInfo');
90 // @todo - get this working with syntax style $paymentObject->supports(array
91 //('CancelSubscriptionSupported'));
92 $isCancelSupported = CRM_Member_BAO_Membership
::isCancelSubscriptionSupported(
93 $membership[$dao->id
]['membership_id']);
94 $links = self
::links('all',
100 self
::getPermissionedLinks($dao->membership_type_id
, $links);
101 $membership[$dao->id
]['action'] = CRM_Core_Action
::formLink($links,
105 'cid' => $this->_contactId
,
109 'membership.tab.row',
115 $links = self
::links('view');
116 self
::getPermissionedLinks($dao->membership_type_id
, $links);
117 $membership[$dao->id
]['action'] = CRM_Core_Action
::formLink($links,
121 'cid' => $this->_contactId
,
125 'membership.tab.row',
131 // Display Auto-renew status on page (0=disabled, 1=enabled, 2=enabled, but error
132 if (!empty($membership[$dao->id
]['contribution_recur_id'])) {
133 if (CRM_Member_BAO_Membership
::isSubscriptionCancelled($membership[$dao->id
]['membership_id'])) {
134 $membership[$dao->id
]['auto_renew'] = 2;
137 $membership[$dao->id
]['auto_renew'] = 1;
141 $membership[$dao->id
]['auto_renew'] = 0;
144 // if relevant--membership is active and type allows inheritance--count related memberships
145 if (!empty($statusANDType[$dao->id
]['is_current_member'])
146 && !empty($statusANDType[$dao->id
]['relationship_type_id'])
147 && empty($dao->owner_membership_id
)
149 // not an related membership
152 FROM civicrm_membership m
153 LEFT JOIN civicrm_membership_status ms ON ms.id = m.status_id
154 LEFT JOIN civicrm_contact ct ON ct.id = m.contact_id
155 WHERE m.owner_membership_id = {$dao->id} AND m.is_test = 0 AND ms.is_current_member = 1 AND ct.is_deleted = 0";
156 $num_related = CRM_Core_DAO
::singleValueQuery($query);
157 $max_related = $membership[$dao->id
]['max_related'] ??
NULL;
158 $membership[$dao->id
]['related_count'] = ($max_related == '' ?
ts('%1 created', [1 => $num_related]) : ts('%1 out of %2', [
164 $membership[$dao->id
]['related_count'] = ts('N/A');
168 //Below code gives list of all Membership Types associated
169 //with an Organization(CRM-2016)
170 $membershipTypesResult = civicrm_api3('MembershipType', 'get', [
171 'member_of_contact_id' => $this->_contactId
,
176 $membershipTypes = $membershipTypesResult['values'] ??
NULL;
178 foreach ($membershipTypes as $key => $value) {
179 $membershipTypes[$key]['action'] = CRM_Core_Action
::formLink(self
::membershipTypeslinks(),
182 'id' => $value['id'],
183 'cid' => $this->_contactId
,
187 'membershipType.organization.action',
193 $activeMembers = CRM_Member_BAO_Membership
::activeMembers($membership);
194 $inActiveMembers = CRM_Member_BAO_Membership
::activeMembers($membership, 'inactive');
195 $this->assign('activeMembers', $activeMembers);
196 $this->assign('inActiveMembers', $inActiveMembers);
197 $this->assign('membershipTypes', $membershipTypes);
199 if ($this->_contactId
) {
200 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_contactId
);
201 $this->assign('displayName', $displayName);
202 $this->ajaxResponse
['tabCount'] = CRM_Contact_BAO_Contact
::getCountComponent('membership', $this->_contactId
);
203 // Refresh other tabs with related data
204 $this->ajaxResponse
['updateTabs'] = [
205 '#tab_activity' => CRM_Contact_BAO_Contact
::getCountComponent('activity', $this->_contactId
),
206 '#tab_rel' => CRM_Contact_BAO_Contact
::getCountComponent('rel', $this->_contactId
),
208 if (CRM_Core_Permission
::access('CiviContribute')) {
209 $this->ajaxResponse
['updateTabs']['#tab_contribute'] = CRM_Contact_BAO_Contact
::getCountComponent('contribution', $this->_contactId
);
215 * called when action is view.
219 public function view() {
220 $controller = new CRM_Core_Controller_Simple(
221 'CRM_Member_Form_MembershipView',
222 ts('View Membership'),
225 $controller->setEmbedded(TRUE);
226 $controller->set('id', $this->_id
);
227 $controller->set('cid', $this->_contactId
);
229 return $controller->run();
233 * called when action is update or new.
237 public function edit() {
238 // set https for offline cc transaction
239 $mode = CRM_Utils_Request
::retrieve('mode', 'Alphanumeric', $this);
240 if ($mode == 'test' ||
$mode == 'live') {
241 CRM_Utils_System
::redirectToSSL();
244 // build associated contributions ( note: this is called to show associated contributions in edit mode )
245 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
246 $this->assign('accessContribution', FALSE);
247 if (CRM_Core_Permission
::access('CiviContribute')) {
248 $this->assign('accessContribution', TRUE);
249 CRM_Member_Page_Tab
::associatedContribution($this->_contactId
, $this->_id
);
251 //show associated soft credit when contribution payment is paid by different person in edit mode
252 if ($this->_id
&& $this->_contactId
) {
253 $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$this->_id})";
254 $softCreditList = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionList($this->_contactId
, $filter);
255 if (!empty($softCreditList)) {
256 $this->assign('softCredit', TRUE);
257 $this->assign('softCreditRows', $softCreditList);
263 if ($this->_action
& CRM_Core_Action
::RENEW
) {
264 $path = 'CRM_Member_Form_MembershipRenewal';
265 $title = ts('Renew Membership');
268 $path = 'CRM_Member_Form_Membership';
269 $title = ts('Create Membership');
272 $controller = new CRM_Core_Controller_Simple($path, $title, $this->_action
);
273 $controller->setEmbedded(TRUE);
274 $controller->set('BAOName', $this->getBAOName());
275 $controller->set('id', $this->_id
);
276 $controller->set('cid', $this->_contactId
);
277 return $controller->run();
280 public function preProcess() {
281 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
282 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'browse');
283 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
285 if ($context == 'standalone') {
286 $this->_action
= CRM_Core_Action
::ADD
;
289 $this->_contactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this, TRUE);
290 $this->assign('contactId', $this->_contactId
);
292 // check logged in url permission
293 CRM_Contact_Page_View
::checkUserPermission($this);
296 $this->assign('action', $this->_action
);
298 if ($this->_permission
== CRM_Core_Permission
::EDIT
&& !CRM_Core_Permission
::check('edit memberships')) {
299 // demote to view since user does not have edit membership rights
300 $this->_permission
= CRM_Core_Permission
::VIEW
;
301 $this->assign('permission', 'view');
306 * the main function that is called when the page loads, it decides the which action has to be taken for the page.
310 public function run() {
313 // check if we can process credit card membership
314 $newCredit = CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments();
315 $this->assign('newCredit', $newCredit);
318 $this->_isPaymentProcessor
= TRUE;
321 $this->_isPaymentProcessor
= FALSE;
324 // Only show credit card membership signup if user has CiviContribute permission
325 if (CRM_Core_Permission
::access('CiviContribute')) {
326 $this->_accessContribution
= TRUE;
327 $this->assign('accessContribution', TRUE);
329 //show associated soft credit when contribution payment is paid by different person
330 if ($this->_id
&& $this->_contactId
) {
331 $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$this->_id})";
332 $softCreditList = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionList($this->_contactId
, $filter);
333 if (!empty($softCreditList)) {
334 $this->assign('softCredit', TRUE);
335 $this->assign('softCreditRows', $softCreditList);
340 $this->_accessContribution
= FALSE;
341 $this->assign('accessContribution', FALSE);
342 $this->assign('softCredit', FALSE);
345 if ($this->_action
& CRM_Core_Action
::VIEW
) {
348 elseif ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD | CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
349 self
::setContext($this);
353 self
::setContext($this);
357 return parent
::run();
361 * @param CRM_Core_Form $form
362 * @param int $contactId
364 public static function setContext(&$form, $contactId = NULL) {
365 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $form, FALSE, 'search');
367 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $form);
369 $searchContext = CRM_Utils_Request
::retrieve('searchContext', 'String', $form);
372 if (!CRM_Utils_Rule
::qfKey($qfKey)) {
377 $contactId = $form->_contactId
;
382 $url = CRM_Utils_System
::url('civicrm/member', 'reset=1');
386 $url = CRM_Utils_System
::url('civicrm/contact/view', "reset=1&force=1&cid={$contactId}&selectedChild=member");
390 $urlParams = 'force=1';
392 $urlParams .= "&qfKey=$qfKey";
394 $form->assign('searchKey', $qfKey);
396 if ($searchContext) {
397 $url = CRM_Utils_System
::url("civicrm/$searchContext/search", $urlParams);
400 $url = CRM_Utils_System
::url('civicrm/member/search', $urlParams);
405 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
409 $url = CRM_Utils_System
::url('civicrm/contact/view',
410 "reset=1&force=1&cid={$contactId}&selectedChild=activity"
415 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
419 $action = CRM_Utils_Request
::retrieve('action', 'String', $form);
421 $urlParams = 'force=1';
422 $urlString = 'civicrm/contact/search/custom';
423 if ($action == CRM_Core_Action
::UPDATE
) {
424 if ($form->_contactId
) {
425 $urlParams .= '&cid=' . $form->_contactId
;
428 $urlParams .= '&context=fulltext&action=view';
429 $urlString = 'civicrm/contact/view/membership';
432 $urlParams .= "$keyName=$qfKey";
434 $form->assign('searchKey', $qfKey);
435 $url = CRM_Utils_System
::url($urlString, $urlParams);
441 $cid = '&cid=' . $contactId;
443 $url = CRM_Utils_System
::url('civicrm/member/search', 'force=1' . $cid);
447 $session = CRM_Core_Session
::singleton();
448 $session->pushUserContext($url);
454 * @param string $status
455 * @param null $isPaymentProcessor
456 * @param null $accessContribution
457 * @param bool $isCancelSupported
458 * @param bool $isUpdateBilling
461 * (reference) of action links
463 public static function &links(
465 $isPaymentProcessor = NULL,
466 $accessContribution = NULL,
467 $isCancelSupported = FALSE,
468 $isUpdateBilling = FALSE
470 if (empty(self
::$_links['view'])) {
471 self
::$_links['view'] = [
472 CRM_Core_Action
::VIEW
=> [
473 'name' => ts('View'),
474 'url' => 'civicrm/contact/view/membership',
475 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
476 'title' => ts('View Membership'),
481 if (empty(self
::$_links['all'])) {
483 CRM_Core_Action
::UPDATE
=> [
484 'name' => ts('Edit'),
485 'url' => 'civicrm/contact/view/membership',
486 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
487 'title' => ts('Edit Membership'),
489 CRM_Core_Action
::RENEW
=> [
490 'name' => ts('Renew'),
491 'url' => 'civicrm/contact/view/membership',
492 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
493 'title' => ts('Renew Membership'),
495 CRM_Core_Action
::FOLLOWUP
=> [
496 'name' => ts('Renew-Credit Card'),
497 'url' => 'civicrm/contact/view/membership',
498 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member&mode=live',
499 'title' => ts('Renew Membership Using Credit Card'),
501 CRM_Core_Action
::DELETE
=> [
502 'name' => ts('Delete'),
503 'url' => 'civicrm/contact/view/membership',
504 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
505 'title' => ts('Delete Membership'),
508 if (!$isPaymentProcessor ||
!$accessContribution) {
509 //unset the renew with credit card when payment
510 //processor is not available or user is not permitted to create contributions
511 unset($extraLinks[CRM_Core_Action
::FOLLOWUP
]);
513 self
::$_links['all'] = self
::$_links['view'] +
$extraLinks;
516 if ($isCancelSupported) {
517 $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.');
518 self
::$_links['all'][CRM_Core_Action
::DISABLE
] = [
519 'name' => ts('Cancel Auto-renewal'),
520 'url' => 'civicrm/contribute/unsubscribe',
521 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
522 'title' => ts('Cancel Auto Renew Subscription'),
523 'extra' => 'onclick = "if (confirm(\'' . $cancelMessage . '\') ) { return true; else return false;}"',
526 elseif (isset(self
::$_links['all'][CRM_Core_Action
::DISABLE
])) {
527 unset(self
::$_links['all'][CRM_Core_Action
::DISABLE
]);
530 if ($isUpdateBilling) {
531 self
::$_links['all'][CRM_Core_Action
::MAP
] = [
532 'name' => ts('Change Billing Details'),
533 'url' => 'civicrm/contribute/updatebilling',
534 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
535 'title' => ts('Change Billing Details'),
538 elseif (isset(self
::$_links['all'][CRM_Core_Action
::MAP
])) {
539 unset(self
::$_links['all'][CRM_Core_Action
::MAP
]);
541 return self
::$_links[$status];
545 * Define action links for membership types of related organization.
548 * self::$_membershipTypesLinks array of action links
550 public static function &membershipTypesLinks() {
551 if (!self
::$_membershipTypesLinks) {
552 self
::$_membershipTypesLinks = [
553 CRM_Core_Action
::VIEW
=> [
554 'name' => ts('Members'),
555 'url' => 'civicrm/member/search/',
556 'qs' => 'reset=1&force=1&type=%%id%%',
557 'title' => ts('Search'),
559 CRM_Core_Action
::UPDATE
=> [
560 'name' => ts('Edit'),
561 'url' => 'civicrm/admin/member/membershipType',
562 'qs' => 'action=update&id=%%id%%&reset=1',
563 'title' => ts('Edit Membership Type'),
567 return self
::$_membershipTypesLinks;
571 * used for the to show the associated.
572 * contribution for the membership
574 * @param int $contactId
575 * @param int $membershipId
577 public static function associatedContribution($contactId = NULL, $membershipId = NULL) {
578 $controller = new CRM_Core_Controller_Simple(
579 'CRM_Contribute_Form_Search',
584 $controller->setEmbedded(TRUE);
585 $controller->reset();
586 $controller->set('force', 1);
587 $controller->set('cid', $contactId);
588 $controller->set('memberId', $membershipId);
589 $controller->set('context', 'contribution');
590 $controller->process();
600 public function getBAOName() {
601 return 'CRM_Member_BAO_Membership';
605 * Get a list of links based on permissioned FTs.
607 * @param int $memTypeID
610 * (reference ) action links
612 public static function getPermissionedLinks($memTypeID, &$links) {
613 if (!CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus()) {
616 $finTypeId = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $memTypeID, 'financial_type_id');
617 $finType = CRM_Contribute_PseudoConstant
::financialType($finTypeId);
618 if (!CRM_Core_Permission
::check('edit contributions of type ' . $finType)) {
619 unset($links[CRM_Core_Action
::UPDATE
]);
620 unset($links[CRM_Core_Action
::RENEW
]);
621 unset($links[CRM_Core_Action
::FOLLOWUP
]);
623 if (!CRM_Core_Permission
::check('delete contributions of type ' . $finType)) {
624 unset($links[CRM_Core_Action
::DELETE
]);