Merge pull request #17143 from mattwire/donthidedisabledmemberships
[civicrm-core.git] / CRM / Member / Page / Tab.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 * $Id$
17 *
18 */
19 class CRM_Member_Page_Tab extends CRM_Core_Page {
20
21 /**
22 * The action links that we need to display for the browse screen.
23 *
24 * @var array
25 */
26 public static $_links = NULL;
27 public static $_membershipTypesLinks = NULL;
28
29 public $_permission = NULL;
30 public $_contactId = NULL;
31
32 /**
33 * called when action is browse.
34 */
35 public function browse() {
36 $membership = [];
37 $dao = new CRM_Member_DAO_Membership();
38 $dao->contact_id = $this->_contactId;
39 $dao->find();
40
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;
45 }
46 if (CRM_Core_Permission::check('delete in CiviMember')) {
47 $permissions[] = CRM_Core_Permission::DELETE;
48 }
49 $mask = CRM_Core_Action::mask($permissions);
50
51 // get deceased status id
52 $allStatus = CRM_Member_PseudoConstant::membershipStatus();
53 $deceasedStatusId = array_search('Deceased', $allStatus);
54
55 //get all campaigns.
56 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
57
58 //checks membership of contact itself
59 while ($dao->fetch()) {
60 $membership[$dao->id] = [];
61 CRM_Core_DAO::storeValues($dao, $membership[$dao->id]);
62
63 //carry campaign.
64 $membership[$dao->id]['campaign'] = $allCampaigns[$dao->campaign_id] ?? NULL;
65
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;
70 }
71 if (!empty($statusANDType[$dao->id]['is_current_member'])) {
72 $membership[$dao->id]['active'] = TRUE;
73 }
74 if (empty($dao->owner_membership_id)) {
75 // unset renew and followup link for deceased membership
76 $currentMask = $mask;
77 if ($dao->status_id == $deceasedStatusId) {
78 $currentMask = $currentMask & ~CRM_Core_Action::RENEW & ~CRM_Core_Action::FOLLOWUP;
79 }
80
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');
88 }
89
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',
95 NULL,
96 NULL,
97 $isCancelSupported,
98 $isUpdateBilling
99 );
100 self::getPermissionedLinks($dao->membership_type_id, $links);
101 $membership[$dao->id]['action'] = CRM_Core_Action::formLink($links,
102 $currentMask,
103 [
104 'id' => $dao->id,
105 'cid' => $this->_contactId,
106 ],
107 ts('Renew') . '...',
108 FALSE,
109 'membership.tab.row',
110 'Membership',
111 $dao->id
112 );
113 }
114 else {
115 $links = self::links('view');
116 self::getPermissionedLinks($dao->membership_type_id, $links);
117 $membership[$dao->id]['action'] = CRM_Core_Action::formLink($links,
118 $mask,
119 [
120 'id' => $dao->id,
121 'cid' => $this->_contactId,
122 ],
123 ts('more'),
124 FALSE,
125 'membership.tab.row',
126 'Membership',
127 $dao->id
128 );
129 }
130
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;
135 }
136 else {
137 $membership[$dao->id]['auto_renew'] = 1;
138 }
139 }
140 else {
141 $membership[$dao->id]['auto_renew'] = 0;
142 }
143
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)
148 ) {
149 // not an related membership
150 $query = "
151 SELECT COUNT(m.id)
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', [
159 1 => $num_related,
160 2 => $max_related,
161 ]));
162 }
163 else {
164 $membership[$dao->id]['related_count'] = ts('N/A');
165 }
166 }
167
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,
172 'options' => [
173 'limit' => 0,
174 ],
175 ]);
176 $membershipTypes = $membershipTypesResult['values'] ?? NULL;
177
178 foreach ($membershipTypes as $key => $value) {
179 $membershipTypes[$key]['action'] = CRM_Core_Action::formLink(self::membershipTypeslinks(),
180 $mask,
181 [
182 'id' => $value['id'],
183 'cid' => $this->_contactId,
184 ],
185 ts('more'),
186 FALSE,
187 'membershipType.organization.action',
188 'MembershipType',
189 $value['id']
190 );
191 }
192
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);
198
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),
207 ];
208 if (CRM_Core_Permission::access('CiviContribute')) {
209 $this->ajaxResponse['updateTabs']['#tab_contribute'] = CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId);
210 }
211 }
212 }
213
214 /**
215 * called when action is view.
216 *
217 * @return null
218 */
219 public function view() {
220 $controller = new CRM_Core_Controller_Simple(
221 'CRM_Member_Form_MembershipView',
222 ts('View Membership'),
223 $this->_action
224 );
225 $controller->setEmbedded(TRUE);
226 $controller->set('id', $this->_id);
227 $controller->set('cid', $this->_contactId);
228
229 return $controller->run();
230 }
231
232 /**
233 * called when action is update or new.
234 *
235 * @return null
236 */
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();
242 }
243
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);
250
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);
258 }
259 }
260 }
261 }
262
263 if ($this->_action & CRM_Core_Action::RENEW) {
264 $path = 'CRM_Member_Form_MembershipRenewal';
265 $title = ts('Renew Membership');
266 }
267 else {
268 $path = 'CRM_Member_Form_Membership';
269 $title = ts('Create Membership');
270 }
271
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();
278 }
279
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);
284
285 if ($context == 'standalone') {
286 $this->_action = CRM_Core_Action::ADD;
287 }
288 else {
289 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
290 $this->assign('contactId', $this->_contactId);
291
292 // check logged in url permission
293 CRM_Contact_Page_View::checkUserPermission($this);
294 }
295
296 $this->assign('action', $this->_action);
297
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');
302 }
303 }
304
305 /**
306 * the main function that is called when the page loads, it decides the which action has to be taken for the page.
307 *
308 * @return null
309 */
310 public function run() {
311 $this->preProcess();
312
313 // check if we can process credit card membership
314 $newCredit = CRM_Core_Config::isEnabledBackOfficeCreditCardPayments();
315 $this->assign('newCredit', $newCredit);
316
317 if ($newCredit) {
318 $this->_isPaymentProcessor = TRUE;
319 }
320 else {
321 $this->_isPaymentProcessor = FALSE;
322 }
323
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);
328
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);
336 }
337 }
338 }
339 else {
340 $this->_accessContribution = FALSE;
341 $this->assign('accessContribution', FALSE);
342 $this->assign('softCredit', FALSE);
343 }
344
345 if ($this->_action & CRM_Core_Action::VIEW) {
346 $this->view();
347 }
348 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE | CRM_Core_Action::RENEW)) {
349 self::setContext($this);
350 $this->edit();
351 }
352 else {
353 self::setContext($this);
354 $this->browse();
355 }
356
357 return parent::run();
358 }
359
360 /**
361 * @param CRM_Core_Form $form
362 * @param int $contactId
363 */
364 public static function setContext(&$form, $contactId = NULL) {
365 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $form, FALSE, 'search');
366
367 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $form);
368
369 $searchContext = CRM_Utils_Request::retrieve('searchContext', 'String', $form);
370
371 //validate the qfKey
372 if (!CRM_Utils_Rule::qfKey($qfKey)) {
373 $qfKey = NULL;
374 }
375
376 if (!$contactId) {
377 $contactId = $form->_contactId;
378 }
379
380 switch ($context) {
381 case 'dashboard':
382 $url = CRM_Utils_System::url('civicrm/member', 'reset=1');
383 break;
384
385 case 'membership':
386 $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&force=1&cid={$contactId}&selectedChild=member");
387 break;
388
389 case 'search':
390 $urlParams = 'force=1';
391 if ($qfKey) {
392 $urlParams .= "&qfKey=$qfKey";
393 }
394 $form->assign('searchKey', $qfKey);
395
396 if ($searchContext) {
397 $url = CRM_Utils_System::url("civicrm/$searchContext/search", $urlParams);
398 }
399 else {
400 $url = CRM_Utils_System::url('civicrm/member/search', $urlParams);
401 }
402 break;
403
404 case 'home':
405 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
406 break;
407
408 case 'activity':
409 $url = CRM_Utils_System::url('civicrm/contact/view',
410 "reset=1&force=1&cid={$contactId}&selectedChild=activity"
411 );
412 break;
413
414 case 'standalone':
415 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
416 break;
417
418 case 'fulltext':
419 $action = CRM_Utils_Request::retrieve('action', 'String', $form);
420 $keyName = '&qfKey';
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;
426 }
427 $keyName = '&key';
428 $urlParams .= '&context=fulltext&action=view';
429 $urlString = 'civicrm/contact/view/membership';
430 }
431 if ($qfKey) {
432 $urlParams .= "$keyName=$qfKey";
433 }
434 $form->assign('searchKey', $qfKey);
435 $url = CRM_Utils_System::url($urlString, $urlParams);
436 break;
437
438 default:
439 $cid = NULL;
440 if ($contactId) {
441 $cid = '&cid=' . $contactId;
442 }
443 $url = CRM_Utils_System::url('civicrm/member/search', 'force=1' . $cid);
444 break;
445 }
446
447 $session = CRM_Core_Session::singleton();
448 $session->pushUserContext($url);
449 }
450
451 /**
452 * Get action links.
453 *
454 * @param string $status
455 * @param null $isPaymentProcessor
456 * @param null $accessContribution
457 * @param bool $isCancelSupported
458 * @param bool $isUpdateBilling
459 *
460 * @return array
461 * (reference) of action links
462 */
463 public static function &links(
464 $status = 'all',
465 $isPaymentProcessor = NULL,
466 $accessContribution = NULL,
467 $isCancelSupported = FALSE,
468 $isUpdateBilling = FALSE
469 ) {
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'),
477 ],
478 ];
479 }
480
481 if (empty(self::$_links['all'])) {
482 $extraLinks = [
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'),
488 ],
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'),
494 ],
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'),
500 ],
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'),
506 ],
507 ];
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]);
512 }
513 self::$_links['all'] = self::$_links['view'] + $extraLinks;
514 }
515
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;}"',
524 ];
525 }
526 elseif (isset(self::$_links['all'][CRM_Core_Action::DISABLE])) {
527 unset(self::$_links['all'][CRM_Core_Action::DISABLE]);
528 }
529
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'),
536 ];
537 }
538 elseif (isset(self::$_links['all'][CRM_Core_Action::MAP])) {
539 unset(self::$_links['all'][CRM_Core_Action::MAP]);
540 }
541 return self::$_links[$status];
542 }
543
544 /**
545 * Define action links for membership types of related organization.
546 *
547 * @return array
548 * self::$_membershipTypesLinks array of action links
549 */
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'),
558 ],
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'),
564 ],
565 ];
566 }
567 return self::$_membershipTypesLinks;
568 }
569
570 /**
571 * used for the to show the associated.
572 * contribution for the membership
573 *
574 * @param int $contactId
575 * @param int $membershipId
576 */
577 public static function associatedContribution($contactId = NULL, $membershipId = NULL) {
578 $controller = new CRM_Core_Controller_Simple(
579 'CRM_Contribute_Form_Search',
580 ts('Contributions'),
581 NULL,
582 FALSE, FALSE, TRUE
583 );
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();
591 $controller->run();
592 }
593
594 /**
595 * Get BAO Name.
596 *
597 * @return string
598 * Classname of BAO.
599 */
600 public function getBAOName() {
601 return 'CRM_Member_BAO_Membership';
602 }
603
604 /**
605 * Get a list of links based on permissioned FTs.
606 *
607 * @param int $memTypeID
608 * membership type ID
609 * @param int $links
610 * (reference ) action links
611 */
612 public static function getPermissionedLinks($memTypeID, &$links) {
613 if (!CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) {
614 return FALSE;
615 }
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]);
622 }
623 if (!CRM_Core_Permission::check('delete contributions of type ' . $finType)) {
624 unset($links[CRM_Core_Action::DELETE]);
625 }
626 }
627
628 }