(dev/core#3141) Membership should be listed chronologically by join date, the most...
[civicrm-core.git] / CRM / Member / Page / Tab.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Member_Page_Tab extends CRM_Core_Page {
18
19 /**
fe482240 20 * The action links that we need to display for the browse screen.
6a488035
TO
21 *
22 * @var array
6a488035 23 */
971e129b
SL
24 public static $_links = NULL;
25 public static $_membershipTypesLinks = NULL;
6a488035
TO
26
27 public $_permission = NULL;
28 public $_contactId = NULL;
29
0c9f4988
BT
30 /**
31 * @var bool
32 */
33 private $_isPaymentProcessor = FALSE;
34
35 /**
36 * @var bool
37 */
38 private $_accessContribution = FALSE;
39
6a488035 40 /**
fe482240 41 * called when action is browse.
6a488035 42 */
00be9182 43 public function browse() {
51b9c47e 44 $links = self::links('all', $this->_isPaymentProcessor, $this->_accessContribution);
2dc76e8d
MW
45 $membershipTypes = \Civi\Api4\MembershipType::get(TRUE)
46 ->execute()
47 ->indexBy('id')
48 ->column('name');
51b9c47e
SL
49 $addWhere = "membership_type_id IN (0)";
50 if (!empty($membershipTypes)) {
51 $addWhere = "membership_type_id IN (" . implode(',', array_keys($membershipTypes)) . ")";
52 }
53
be2fb01f 54 $membership = [];
8d8bd076 55 $dao = new CRM_Member_DAO_Membership();
6a488035 56 $dao->contact_id = $this->_contactId;
51b9c47e 57 $dao->whereAdd($addWhere);
6a488035
TO
58 $dao->find();
59
60 //CRM--4418, check for view, edit, delete
be2fb01f 61 $permissions = [CRM_Core_Permission::VIEW];
6a488035
TO
62 if (CRM_Core_Permission::check('edit memberships')) {
63 $permissions[] = CRM_Core_Permission::EDIT;
a3ca6fe3
MW
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',
69 'accessKey' => 'N',
70 ];
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',
77 'accessKey' => 'C',
78 ];
79 }
80 $this->assign('linkButtons', $linkButtons ?? []);
6a488035
TO
81 }
82 if (CRM_Core_Permission::check('delete in CiviMember')) {
83 $permissions[] = CRM_Core_Permission::DELETE;
84 }
85 $mask = CRM_Core_Action::mask($permissions);
86
87 // get deceased status id
88 $allStatus = CRM_Member_PseudoConstant::membershipStatus();
89 $deceasedStatusId = array_search('Deceased', $allStatus);
90
91 //get all campaigns.
92 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
93
94 //checks membership of contact itself
95 while ($dao->fetch()) {
be2fb01f 96 $membership[$dao->id] = [];
6a488035
TO
97 CRM_Core_DAO::storeValues($dao, $membership[$dao->id]);
98
99 //carry campaign.
9c1bc317 100 $membership[$dao->id]['campaign'] = $allCampaigns[$dao->campaign_id] ?? NULL;
6a488035
TO
101
102 //get the membership status and type values.
103 $statusANDType = CRM_Member_BAO_Membership::getStatusANDTypeValues($dao->id);
be2fb01f 104 foreach (['status', 'membership_type'] as $fld) {
9c1bc317 105 $membership[$dao->id][$fld] = $statusANDType[$dao->id][$fld] ?? NULL;
6a488035 106 }
a7488080 107 if (!empty($statusANDType[$dao->id]['is_current_member'])) {
6a488035
TO
108 $membership[$dao->id]['active'] = TRUE;
109 }
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;
115 }
116
8d8bd076 117 $isUpdateBilling = FALSE;
0bd096e8 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).
8d8bd076 120 $paymentObject = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity(
121 $membership[$dao->id]['membership_id'], 'membership', 'obj');
6a488035 122 if (!empty($paymentObject)) {
b690491e 123 $isUpdateBilling = $paymentObject->supports('updateSubscriptionBillingInfo');
6a488035
TO
124 }
125
0bd096e8 126 // @todo - get this working with syntax style $paymentObject->supports(array
127 //('CancelSubscriptionSupported'));
8d8bd076 128 $isCancelSupported = CRM_Member_BAO_Membership::isCancelSubscriptionSupported(
129 $membership[$dao->id]['membership_id']);
8a0604d2 130 $links = self::links('all',
0c9f4988
BT
131 FALSE,
132 FALSE,
6a488035
TO
133 $isCancelSupported,
134 $isUpdateBilling
8a0604d2
E
135 );
136 self::getPermissionedLinks($dao->membership_type_id, $links);
137 $membership[$dao->id]['action'] = CRM_Core_Action::formLink($links,
6a488035 138 $currentMask,
be2fb01f 139 [
6a488035
TO
140 'id' => $dao->id,
141 'cid' => $this->_contactId,
be2fb01f 142 ],
6957f0a9 143 ts('Renew') . '...',
87dab4a4
AH
144 FALSE,
145 'membership.tab.row',
146 'Membership',
147 $dao->id
6a488035
TO
148 );
149 }
150 else {
8a0604d2
E
151 $links = self::links('view');
152 self::getPermissionedLinks($dao->membership_type_id, $links);
153 $membership[$dao->id]['action'] = CRM_Core_Action::formLink($links,
6a488035 154 $mask,
be2fb01f 155 [
6a488035
TO
156 'id' => $dao->id,
157 'cid' => $this->_contactId,
be2fb01f 158 ],
87dab4a4
AH
159 ts('more'),
160 FALSE,
161 'membership.tab.row',
162 'Membership',
163 $dao->id
6a488035
TO
164 );
165 }
166
6bb529e7
MW
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;
171 }
172 else {
173 $membership[$dao->id]['auto_renew'] = 1;
174 }
6a488035
TO
175 }
176 else {
177 $membership[$dao->id]['auto_renew'] = 0;
178 }
179
c5c263ca 180 // if relevant--membership is active and type allows inheritance--count related memberships
b99f3e96
CW
181 if (!empty($statusANDType[$dao->id]['is_current_member'])
182 && !empty($statusANDType[$dao->id]['relationship_type_id'])
8d8bd076 183 && empty($dao->owner_membership_id)
ae5ffbb7
TO
184 ) {
185 // not an related membership
6a488035
TO
186 $query = "
187 SELECT COUNT(m.id)
188 FROM civicrm_membership m
189 LEFT JOIN civicrm_membership_status ms ON ms.id = m.status_id
86c838ac
CW
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";
6a488035 192 $num_related = CRM_Core_DAO::singleValueQuery($query);
9c1bc317 193 $max_related = $membership[$dao->id]['max_related'] ?? NULL;
be2fb01f 194 $membership[$dao->id]['related_count'] = ($max_related == '' ? ts('%1 created', [1 => $num_related]) : ts('%1 out of %2', [
c5c263ca
AH
195 1 => $num_related,
196 2 => $max_related,
be2fb01f 197 ]));
8d8bd076 198 }
199 else {
6a488035
TO
200 $membership[$dao->id]['related_count'] = ts('N/A');
201 }
202 }
203
204 //Below code gives list of all Membership Types associated
205 //with an Organization(CRM-2016)
be2fb01f 206 $membershipTypesResult = civicrm_api3('MembershipType', 'get', [
4c50ac3a 207 'member_of_contact_id' => $this->_contactId,
be2fb01f 208 'options' => [
c8eada29 209 'limit' => 0,
be2fb01f
CW
210 ],
211 ]);
9c1bc317 212 $membershipTypes = $membershipTypesResult['values'] ?? NULL;
4c50ac3a 213
6a488035
TO
214 foreach ($membershipTypes as $key => $value) {
215 $membershipTypes[$key]['action'] = CRM_Core_Action::formLink(self::membershipTypeslinks(),
216 $mask,
be2fb01f 217 [
6a488035
TO
218 'id' => $value['id'],
219 'cid' => $this->_contactId,
be2fb01f 220 ],
87dab4a4
AH
221 ts('more'),
222 FALSE,
223 'membershipType.organization.action',
224 'MembershipType',
225 $value['id']
6a488035
TO
226 );
227 }
228
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);
234
235 if ($this->_contactId) {
236 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
237 $this->assign('displayName', $displayName);
4e8065a9 238 $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('membership', $this->_contactId);
fa9fbb61 239 // Refresh other tabs with related data
be2fb01f 240 $this->ajaxResponse['updateTabs'] = [
fa9fbb61
CW
241 '#tab_activity' => CRM_Contact_BAO_Contact::getCountComponent('activity', $this->_contactId),
242 '#tab_rel' => CRM_Contact_BAO_Contact::getCountComponent('rel', $this->_contactId),
be2fb01f 243 ];
c312052e
CW
244 if (CRM_Core_Permission::access('CiviContribute')) {
245 $this->ajaxResponse['updateTabs']['#tab_contribute'] = CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId);
246 }
6a488035
TO
247 }
248 }
249
250 /**
fe482240 251 * called when action is view.
6a488035 252 *
0c9f4988 253 * @return mixed
6a488035 254 */
00be9182 255 public function view() {
6a488035
TO
256 $controller = new CRM_Core_Controller_Simple(
257 'CRM_Member_Form_MembershipView',
258 ts('View Membership'),
259 $this->_action
260 );
261 $controller->setEmbedded(TRUE);
262 $controller->set('id', $this->_id);
263 $controller->set('cid', $this->_contactId);
264
265 return $controller->run();
266 }
267
268 /**
fe482240 269 * called when action is update or new.
6a488035 270 *
0c9f4988 271 * @return mixed
6a488035 272 */
00be9182 273 public function edit() {
9888df99
MT
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!
279 ->setLimit(1)
280 ->execute();
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.'));
283 }
284
6a488035 285 // set https for offline cc transaction
19046166 286 $mode = CRM_Utils_Request::retrieve('mode', 'Alphanumeric', $this);
6a488035
TO
287 if ($mode == 'test' || $mode == 'live') {
288 CRM_Utils_System::redirectToSSL();
289 }
290
8d8bd076 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);
eb3cfcd6 297
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);
305 }
306 }
8d8bd076 307 }
6a488035
TO
308 }
309
310 if ($this->_action & CRM_Core_Action::RENEW) {
311 $path = 'CRM_Member_Form_MembershipRenewal';
312 $title = ts('Renew Membership');
313 }
314 else {
315 $path = 'CRM_Member_Form_Membership';
316 $title = ts('Create Membership');
317 }
8d8bd076 318
319 $controller = new CRM_Core_Controller_Simple($path, $title, $this->_action);
6a488035
TO
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();
325 }
326
00be9182 327 public function preProcess() {
edc80cda 328 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
6a488035 329 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
8d8bd076 330 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
6a488035
TO
331
332 if ($context == 'standalone') {
333 $this->_action = CRM_Core_Action::ADD;
334 }
335 else {
336 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
337 $this->assign('contactId', $this->_contactId);
338
339 // check logged in url permission
340 CRM_Contact_Page_View::checkUserPermission($this);
6a488035
TO
341 }
342
343 $this->assign('action', $this->_action);
344
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');
349 }
350 }
351
352 /**
dc195289 353 * the main function that is called when the page loads, it decides the which action has to be taken for the page.
6a488035 354 *
76e7a76c 355 * @return null
6a488035 356 */
00be9182 357 public function run() {
6a488035
TO
358 $this->preProcess();
359
360 // check if we can process credit card membership
a3ca6fe3 361 if (CRM_Core_Config::isEnabledBackOfficeCreditCardPayments()) {
6a488035
TO
362 $this->_isPaymentProcessor = TRUE;
363 }
364 else {
365 $this->_isPaymentProcessor = FALSE;
366 }
367
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);
91ef9be0 372
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);
380 }
381 }
6a488035
TO
382 }
383 else {
384 $this->_accessContribution = FALSE;
385 $this->assign('accessContribution', FALSE);
91ef9be0 386 $this->assign('softCredit', FALSE);
6a488035
TO
387 }
388
389 if ($this->_action & CRM_Core_Action::VIEW) {
390 $this->view();
391 }
d3e86119 392 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE | CRM_Core_Action::RENEW)) {
8d8bd076 393 self::setContext($this);
6a488035
TO
394 $this->edit();
395 }
396 else {
8d8bd076 397 self::setContext($this);
6a488035
TO
398 $this->browse();
399 }
400
401 return parent::run();
402 }
403
bb3a214a 404 /**
c490a46a 405 * @param CRM_Core_Form $form
100fef9d 406 * @param int $contactId
bb3a214a 407 */
8d8bd076 408 public static function setContext(&$form, $contactId = NULL) {
edc80cda 409 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $form, FALSE, 'search');
6a488035 410
8d8bd076 411 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $form);
29571f63 412
e803fd5a 413 $searchContext = CRM_Utils_Request::retrieve('searchContext', 'String', $form);
29571f63 414
6a488035
TO
415 //validate the qfKey
416 if (!CRM_Utils_Rule::qfKey($qfKey)) {
417 $qfKey = NULL;
418 }
419
420 if (!$contactId) {
8d8bd076 421 $contactId = $form->_contactId;
6a488035
TO
422 }
423
424 switch ($context) {
425 case 'dashboard':
8d8bd076 426 $url = CRM_Utils_System::url('civicrm/member', 'reset=1');
6a488035
TO
427 break;
428
429 case 'membership':
8d8bd076 430 $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&force=1&cid={$contactId}&selectedChild=member");
6a488035
TO
431 break;
432
433 case 'search':
434 $urlParams = 'force=1';
435 if ($qfKey) {
436 $urlParams .= "&qfKey=$qfKey";
437 }
8d8bd076 438 $form->assign('searchKey', $qfKey);
2a6da8d7 439
29571f63
AS
440 if ($searchContext) {
441 $url = CRM_Utils_System::url("civicrm/$searchContext/search", $urlParams);
442 }
443 else {
444 $url = CRM_Utils_System::url('civicrm/member/search', $urlParams);
445 }
6a488035
TO
446 break;
447
448 case 'home':
449 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
450 break;
451
452 case 'activity':
453 $url = CRM_Utils_System::url('civicrm/contact/view',
454 "reset=1&force=1&cid={$contactId}&selectedChild=activity"
455 );
456 break;
457
458 case 'standalone':
459 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
460 break;
461
462 case 'fulltext':
8d8bd076 463 $action = CRM_Utils_Request::retrieve('action', 'String', $form);
464 $keyName = '&qfKey';
6a488035
TO
465 $urlParams = 'force=1';
466 $urlString = 'civicrm/contact/search/custom';
467 if ($action == CRM_Core_Action::UPDATE) {
8d8bd076 468 if ($form->_contactId) {
ec6d9c07 469 $urlParams .= '&cid=' . $form->_contactId;
6a488035
TO
470 }
471 $keyName = '&key';
472 $urlParams .= '&context=fulltext&action=view';
473 $urlString = 'civicrm/contact/view/membership';
474 }
475 if ($qfKey) {
476 $urlParams .= "$keyName=$qfKey";
477 }
8d8bd076 478 $form->assign('searchKey', $qfKey);
6a488035
TO
479 $url = CRM_Utils_System::url($urlString, $urlParams);
480 break;
481
482 default:
483 $cid = NULL;
484 if ($contactId) {
485 $cid = '&cid=' . $contactId;
486 }
8d8bd076 487 $url = CRM_Utils_System::url('civicrm/member/search', 'force=1' . $cid);
6a488035
TO
488 break;
489 }
490
491 $session = CRM_Core_Session::singleton();
492 $session->pushUserContext($url);
493 }
494
495 /**
fe482240 496 * Get action links.
6a488035 497 *
2a6da8d7 498 * @param string $status
0c9f4988
BT
499 * @param bool $isPaymentProcessor
500 * @param bool $accessContribution
2a6da8d7
EM
501 * @param bool $isCancelSupported
502 * @param bool $isUpdateBilling
503 *
a6c01b45
CW
504 * @return array
505 * (reference) of action links
6a488035 506 */
ae5ffbb7 507 public static function &links(
500cfe81 508 $status = 'all',
0c9f4988
BT
509 $isPaymentProcessor = FALSE,
510 $accessContribution = FALSE,
353ffa53
TO
511 $isCancelSupported = FALSE,
512 $isUpdateBilling = FALSE
6a488035 513 ) {
f3acfdd9 514 if (empty(self::$_links['view'])) {
be2fb01f
CW
515 self::$_links['view'] = [
516 CRM_Core_Action::VIEW => [
6a488035
TO
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'),
be2fb01f
CW
521 ],
522 ];
6a488035
TO
523 }
524
f3acfdd9 525 if (empty(self::$_links['all'])) {
be2fb01f
CW
526 $extraLinks = [
527 CRM_Core_Action::UPDATE => [
6a488035
TO
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'),
be2fb01f
CW
532 ],
533 CRM_Core_Action::RENEW => [
6a488035
TO
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'),
be2fb01f
CW
538 ],
539 CRM_Core_Action::FOLLOWUP => [
6a488035
TO
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'),
be2fb01f
CW
544 ],
545 CRM_Core_Action::DELETE => [
6a488035
TO
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'),
be2fb01f
CW
550 ],
551 ];
6a488035
TO
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]);
556 }
557 self::$_links['all'] = self::$_links['view'] + $extraLinks;
558 }
559
6a488035
TO
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.');
be2fb01f 562 self::$_links['all'][CRM_Core_Action::DISABLE] = [
6a488035
TO
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;}"',
be2fb01f 568 ];
6a488035
TO
569 }
570 elseif (isset(self::$_links['all'][CRM_Core_Action::DISABLE])) {
571 unset(self::$_links['all'][CRM_Core_Action::DISABLE]);
572 }
573
574 if ($isUpdateBilling) {
be2fb01f 575 self::$_links['all'][CRM_Core_Action::MAP] = [
6a488035
TO
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'),
be2fb01f 580 ];
6a488035
TO
581 }
582 elseif (isset(self::$_links['all'][CRM_Core_Action::MAP])) {
583 unset(self::$_links['all'][CRM_Core_Action::MAP]);
584 }
585 return self::$_links[$status];
586 }
587
588 /**
fe482240 589 * Define action links for membership types of related organization.
6a488035 590 *
a6c01b45
CW
591 * @return array
592 * self::$_membershipTypesLinks array of action links
6a488035 593 */
00be9182 594 public static function &membershipTypesLinks() {
6a488035 595 if (!self::$_membershipTypesLinks) {
be2fb01f
CW
596 self::$_membershipTypesLinks = [
597 CRM_Core_Action::VIEW => [
6a488035
TO
598 'name' => ts('Members'),
599 'url' => 'civicrm/member/search/',
600 'qs' => 'reset=1&force=1&type=%%id%%',
601 'title' => ts('Search'),
be2fb01f
CW
602 ],
603 CRM_Core_Action::UPDATE => [
6a488035
TO
604 'name' => ts('Edit'),
605 'url' => 'civicrm/admin/member/membershipType',
606 'qs' => 'action=update&id=%%id%%&reset=1',
607 'title' => ts('Edit Membership Type'),
be2fb01f
CW
608 ],
609 ];
6a488035
TO
610 }
611 return self::$_membershipTypesLinks;
612 }
613
614 /**
fe482240 615 * used for the to show the associated.
6a488035 616 * contribution for the membership
76e7a76c
CW
617 *
618 * @param int $contactId
619 * @param int $membershipId
6a488035 620 */
141d3170 621 public static function associatedContribution($contactId = NULL, $membershipId = NULL) {
8d8bd076 622 $controller = new CRM_Core_Controller_Simple(
623 'CRM_Contribute_Form_Search',
624 ts('Contributions'),
625 NULL,
626 FALSE, FALSE, TRUE
627 );
628 $controller->setEmbedded(TRUE);
629 $controller->reset();
630 $controller->set('force', 1);
e7afe635 631 $controller->set('skip_cid', TRUE);
8d8bd076 632 $controller->set('memberId', $membershipId);
633 $controller->set('context', 'contribution');
634 $controller->process();
635 $controller->run();
6a488035
TO
636 }
637
638 /**
fe482240 639 * Get BAO Name.
6a488035 640 *
a6c01b45
CW
641 * @return string
642 * Classname of BAO.
6a488035 643 */
00be9182 644 public function getBAOName() {
6a488035
TO
645 return 'CRM_Member_BAO_Membership';
646 }
96025800 647
22806e9a
E
648 /**
649 * Get a list of links based on permissioned FTs.
650 *
651 * @param int $memTypeID
652 * membership type ID
653 * @param int $links
654 * (reference ) action links
655 */
40c655aa 656 public static function getPermissionedLinks($memTypeID, &$links) {
000f0cc9 657 if (!CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) {
66af7c48
PN
658 return FALSE;
659 }
8a0604d2
E
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]);
e9864b40
E
664 unset($links[CRM_Core_Action::RENEW]);
665 unset($links[CRM_Core_Action::FOLLOWUP]);
8a0604d2
E
666 }
667 if (!CRM_Core_Permission::check('delete contributions of type ' . $finType)) {
668 unset($links[CRM_Core_Action::DELETE]);
669 }
670 }
671
6a488035 672}