Merge pull request #17953 from civicrm/5.28
[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 */
17 class CRM_Member_Page_Tab extends CRM_Core_Page {
18
19 /**
20 * The action links that we need to display for the browse screen.
21 *
22 * @var array
23 */
24 public static $_links = NULL;
25 public static $_membershipTypesLinks = NULL;
26
27 public $_permission = NULL;
28 public $_contactId = NULL;
29
30 /**
31 * called when action is browse.
32 */
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)) . ")";
39 }
40
41 $membership = [];
42 $dao = new CRM_Member_DAO_Membership();
43 $dao->contact_id = $this->_contactId;
44 $dao->whereAdd($addWhere);
45 $dao->find();
46
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 }
52 if (CRM_Core_Permission::check('delete in CiviMember')) {
53 $permissions[] = CRM_Core_Permission::DELETE;
54 }
55 $mask = CRM_Core_Action::mask($permissions);
56
57 // get deceased status id
58 $allStatus = CRM_Member_PseudoConstant::membershipStatus();
59 $deceasedStatusId = array_search('Deceased', $allStatus);
60
61 //get all campaigns.
62 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
63
64 //checks membership of contact itself
65 while ($dao->fetch()) {
66 $membership[$dao->id] = [];
67 CRM_Core_DAO::storeValues($dao, $membership[$dao->id]);
68
69 //carry campaign.
70 $membership[$dao->id]['campaign'] = $allCampaigns[$dao->campaign_id] ?? NULL;
71
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;
76 }
77 if (!empty($statusANDType[$dao->id]['is_current_member'])) {
78 $membership[$dao->id]['active'] = TRUE;
79 }
80 if (empty($dao->owner_membership_id)) {
81 // unset renew and followup link for deceased membership
82 $currentMask = $mask;
83 if ($dao->status_id == $deceasedStatusId) {
84 $currentMask = $currentMask & ~CRM_Core_Action::RENEW & ~CRM_Core_Action::FOLLOWUP;
85 }
86
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');
94 }
95
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',
101 NULL,
102 NULL,
103 $isCancelSupported,
104 $isUpdateBilling
105 );
106 self::getPermissionedLinks($dao->membership_type_id, $links);
107 $membership[$dao->id]['action'] = CRM_Core_Action::formLink($links,
108 $currentMask,
109 [
110 'id' => $dao->id,
111 'cid' => $this->_contactId,
112 ],
113 ts('Renew') . '...',
114 FALSE,
115 'membership.tab.row',
116 'Membership',
117 $dao->id
118 );
119 }
120 else {
121 $links = self::links('view');
122 self::getPermissionedLinks($dao->membership_type_id, $links);
123 $membership[$dao->id]['action'] = CRM_Core_Action::formLink($links,
124 $mask,
125 [
126 'id' => $dao->id,
127 'cid' => $this->_contactId,
128 ],
129 ts('more'),
130 FALSE,
131 'membership.tab.row',
132 'Membership',
133 $dao->id
134 );
135 }
136
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;
141 }
142 else {
143 $membership[$dao->id]['auto_renew'] = 1;
144 }
145 }
146 else {
147 $membership[$dao->id]['auto_renew'] = 0;
148 }
149
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)
154 ) {
155 // not an related membership
156 $query = "
157 SELECT COUNT(m.id)
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', [
165 1 => $num_related,
166 2 => $max_related,
167 ]));
168 }
169 else {
170 $membership[$dao->id]['related_count'] = ts('N/A');
171 }
172 }
173
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,
178 'options' => [
179 'limit' => 0,
180 ],
181 ]);
182 $membershipTypes = $membershipTypesResult['values'] ?? NULL;
183
184 foreach ($membershipTypes as $key => $value) {
185 $membershipTypes[$key]['action'] = CRM_Core_Action::formLink(self::membershipTypeslinks(),
186 $mask,
187 [
188 'id' => $value['id'],
189 'cid' => $this->_contactId,
190 ],
191 ts('more'),
192 FALSE,
193 'membershipType.organization.action',
194 'MembershipType',
195 $value['id']
196 );
197 }
198
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);
204
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),
213 ];
214 if (CRM_Core_Permission::access('CiviContribute')) {
215 $this->ajaxResponse['updateTabs']['#tab_contribute'] = CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId);
216 }
217 }
218 }
219
220 /**
221 * called when action is view.
222 *
223 * @return null
224 */
225 public function view() {
226 $controller = new CRM_Core_Controller_Simple(
227 'CRM_Member_Form_MembershipView',
228 ts('View Membership'),
229 $this->_action
230 );
231 $controller->setEmbedded(TRUE);
232 $controller->set('id', $this->_id);
233 $controller->set('cid', $this->_contactId);
234
235 return $controller->run();
236 }
237
238 /**
239 * called when action is update or new.
240 *
241 * @return null
242 */
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();
248 }
249
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);
256
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);
264 }
265 }
266 }
267 }
268
269 if ($this->_action & CRM_Core_Action::RENEW) {
270 $path = 'CRM_Member_Form_MembershipRenewal';
271 $title = ts('Renew Membership');
272 }
273 else {
274 $path = 'CRM_Member_Form_Membership';
275 $title = ts('Create Membership');
276 }
277
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();
284 }
285
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);
290
291 if ($context == 'standalone') {
292 $this->_action = CRM_Core_Action::ADD;
293 }
294 else {
295 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
296 $this->assign('contactId', $this->_contactId);
297
298 // check logged in url permission
299 CRM_Contact_Page_View::checkUserPermission($this);
300 }
301
302 $this->assign('action', $this->_action);
303
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');
308 }
309 }
310
311 /**
312 * the main function that is called when the page loads, it decides the which action has to be taken for the page.
313 *
314 * @return null
315 */
316 public function run() {
317 $this->preProcess();
318
319 // check if we can process credit card membership
320 $newCredit = CRM_Core_Config::isEnabledBackOfficeCreditCardPayments();
321 $this->assign('newCredit', $newCredit);
322
323 if ($newCredit) {
324 $this->_isPaymentProcessor = TRUE;
325 }
326 else {
327 $this->_isPaymentProcessor = FALSE;
328 }
329
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);
334
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);
342 }
343 }
344 }
345 else {
346 $this->_accessContribution = FALSE;
347 $this->assign('accessContribution', FALSE);
348 $this->assign('softCredit', FALSE);
349 }
350
351 if ($this->_action & CRM_Core_Action::VIEW) {
352 $this->view();
353 }
354 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE | CRM_Core_Action::RENEW)) {
355 self::setContext($this);
356 $this->edit();
357 }
358 else {
359 self::setContext($this);
360 $this->browse();
361 }
362
363 return parent::run();
364 }
365
366 /**
367 * @param CRM_Core_Form $form
368 * @param int $contactId
369 */
370 public static function setContext(&$form, $contactId = NULL) {
371 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $form, FALSE, 'search');
372
373 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $form);
374
375 $searchContext = CRM_Utils_Request::retrieve('searchContext', 'String', $form);
376
377 //validate the qfKey
378 if (!CRM_Utils_Rule::qfKey($qfKey)) {
379 $qfKey = NULL;
380 }
381
382 if (!$contactId) {
383 $contactId = $form->_contactId;
384 }
385
386 switch ($context) {
387 case 'dashboard':
388 $url = CRM_Utils_System::url('civicrm/member', 'reset=1');
389 break;
390
391 case 'membership':
392 $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&force=1&cid={$contactId}&selectedChild=member");
393 break;
394
395 case 'search':
396 $urlParams = 'force=1';
397 if ($qfKey) {
398 $urlParams .= "&qfKey=$qfKey";
399 }
400 $form->assign('searchKey', $qfKey);
401
402 if ($searchContext) {
403 $url = CRM_Utils_System::url("civicrm/$searchContext/search", $urlParams);
404 }
405 else {
406 $url = CRM_Utils_System::url('civicrm/member/search', $urlParams);
407 }
408 break;
409
410 case 'home':
411 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
412 break;
413
414 case 'activity':
415 $url = CRM_Utils_System::url('civicrm/contact/view',
416 "reset=1&force=1&cid={$contactId}&selectedChild=activity"
417 );
418 break;
419
420 case 'standalone':
421 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
422 break;
423
424 case 'fulltext':
425 $action = CRM_Utils_Request::retrieve('action', 'String', $form);
426 $keyName = '&qfKey';
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;
432 }
433 $keyName = '&key';
434 $urlParams .= '&context=fulltext&action=view';
435 $urlString = 'civicrm/contact/view/membership';
436 }
437 if ($qfKey) {
438 $urlParams .= "$keyName=$qfKey";
439 }
440 $form->assign('searchKey', $qfKey);
441 $url = CRM_Utils_System::url($urlString, $urlParams);
442 break;
443
444 default:
445 $cid = NULL;
446 if ($contactId) {
447 $cid = '&cid=' . $contactId;
448 }
449 $url = CRM_Utils_System::url('civicrm/member/search', 'force=1' . $cid);
450 break;
451 }
452
453 $session = CRM_Core_Session::singleton();
454 $session->pushUserContext($url);
455 }
456
457 /**
458 * Get action links.
459 *
460 * @param string $status
461 * @param null $isPaymentProcessor
462 * @param null $accessContribution
463 * @param bool $isCancelSupported
464 * @param bool $isUpdateBilling
465 *
466 * @return array
467 * (reference) of action links
468 */
469 public static function &links(
470 $status = 'all',
471 $isPaymentProcessor = NULL,
472 $accessContribution = NULL,
473 $isCancelSupported = FALSE,
474 $isUpdateBilling = FALSE
475 ) {
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'),
483 ],
484 ];
485 }
486
487 if (empty(self::$_links['all'])) {
488 $extraLinks = [
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'),
494 ],
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'),
500 ],
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'),
506 ],
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'),
512 ],
513 ];
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]);
518 }
519 self::$_links['all'] = self::$_links['view'] + $extraLinks;
520 }
521
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;}"',
530 ];
531 }
532 elseif (isset(self::$_links['all'][CRM_Core_Action::DISABLE])) {
533 unset(self::$_links['all'][CRM_Core_Action::DISABLE]);
534 }
535
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'),
542 ];
543 }
544 elseif (isset(self::$_links['all'][CRM_Core_Action::MAP])) {
545 unset(self::$_links['all'][CRM_Core_Action::MAP]);
546 }
547 return self::$_links[$status];
548 }
549
550 /**
551 * Define action links for membership types of related organization.
552 *
553 * @return array
554 * self::$_membershipTypesLinks array of action links
555 */
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'),
564 ],
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'),
570 ],
571 ];
572 }
573 return self::$_membershipTypesLinks;
574 }
575
576 /**
577 * used for the to show the associated.
578 * contribution for the membership
579 *
580 * @param int $contactId
581 * @param int $membershipId
582 */
583 public static function associatedContribution($contactId = NULL, $membershipId = NULL) {
584 $controller = new CRM_Core_Controller_Simple(
585 'CRM_Contribute_Form_Search',
586 ts('Contributions'),
587 NULL,
588 FALSE, FALSE, TRUE
589 );
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();
597 $controller->run();
598 }
599
600 /**
601 * Get BAO Name.
602 *
603 * @return string
604 * Classname of BAO.
605 */
606 public function getBAOName() {
607 return 'CRM_Member_BAO_Membership';
608 }
609
610 /**
611 * Get a list of links based on permissioned FTs.
612 *
613 * @param int $memTypeID
614 * membership type ID
615 * @param int $links
616 * (reference ) action links
617 */
618 public static function getPermissionedLinks($memTypeID, &$links) {
619 if (!CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) {
620 return FALSE;
621 }
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]);
628 }
629 if (!CRM_Core_Permission::check('delete contributions of type ' . $finType)) {
630 unset($links[CRM_Core_Action::DELETE]);
631 }
632 }
633
634 }