Merge pull request #15834 from francescbassas/patch-16
[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 $links = self::links('all', $this->_isPaymentProcessor, $this->_accessContribution);
37 CRM_Financial_BAO_FinancialType::getAvailableMembershipTypes($membershipTypes);
38 $addWhere = "membership_type_id IN (0)";
39 if (!empty($membershipTypes)) {
40 $addWhere = "membership_type_id IN (" . implode(',', array_keys($membershipTypes)) . ")";
41 }
42
43 $membership = [];
44 $dao = new CRM_Member_DAO_Membership();
45 $dao->contact_id = $this->_contactId;
46 $dao->whereAdd($addWhere);
47 $dao->find();
48
49 //CRM--4418, check for view, edit, delete
50 $permissions = [CRM_Core_Permission::VIEW];
51 if (CRM_Core_Permission::check('edit memberships')) {
52 $permissions[] = CRM_Core_Permission::EDIT;
53 }
54 if (CRM_Core_Permission::check('delete in CiviMember')) {
55 $permissions[] = CRM_Core_Permission::DELETE;
56 }
57 $mask = CRM_Core_Action::mask($permissions);
58
59 // get deceased status id
60 $allStatus = CRM_Member_PseudoConstant::membershipStatus();
61 $deceasedStatusId = array_search('Deceased', $allStatus);
62
63 //get all campaigns.
64 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
65
66 //checks membership of contact itself
67 while ($dao->fetch()) {
68 $membership[$dao->id] = [];
69 CRM_Core_DAO::storeValues($dao, $membership[$dao->id]);
70
71 //carry campaign.
72 $membership[$dao->id]['campaign'] = CRM_Utils_Array::value($dao->campaign_id, $allCampaigns);
73
74 //get the membership status and type values.
75 $statusANDType = CRM_Member_BAO_Membership::getStatusANDTypeValues($dao->id);
76 foreach (['status', 'membership_type'] as $fld) {
77 $membership[$dao->id][$fld] = CRM_Utils_Array::value($fld, $statusANDType[$dao->id]);
78 }
79 if (!empty($statusANDType[$dao->id]['is_current_member'])) {
80 $membership[$dao->id]['active'] = TRUE;
81 }
82 if (empty($dao->owner_membership_id)) {
83 // unset renew and followup link for deceased membership
84 $currentMask = $mask;
85 if ($dao->status_id == $deceasedStatusId) {
86 $currentMask = $currentMask & ~CRM_Core_Action::RENEW & ~CRM_Core_Action::FOLLOWUP;
87 }
88
89 $isUpdateBilling = FALSE;
90 // It would be better to determine if there is a recurring contribution &
91 // is so get the entity for the recurring contribution (& skip if not).
92 $paymentObject = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity(
93 $membership[$dao->id]['membership_id'], 'membership', 'obj');
94 if (!empty($paymentObject)) {
95 $isUpdateBilling = $paymentObject->supports('updateSubscriptionBillingInfo');
96 }
97
98 // @todo - get this working with syntax style $paymentObject->supports(array
99 //('CancelSubscriptionSupported'));
100 $isCancelSupported = CRM_Member_BAO_Membership::isCancelSubscriptionSupported(
101 $membership[$dao->id]['membership_id']);
102 $links = self::links('all',
103 NULL,
104 NULL,
105 $isCancelSupported,
106 $isUpdateBilling
107 );
108 self::getPermissionedLinks($dao->membership_type_id, $links);
109 $membership[$dao->id]['action'] = CRM_Core_Action::formLink($links,
110 $currentMask,
111 [
112 'id' => $dao->id,
113 'cid' => $this->_contactId,
114 ],
115 ts('Renew') . '...',
116 FALSE,
117 'membership.tab.row',
118 'Membership',
119 $dao->id
120 );
121 }
122 else {
123 $links = self::links('view');
124 self::getPermissionedLinks($dao->membership_type_id, $links);
125 $membership[$dao->id]['action'] = CRM_Core_Action::formLink($links,
126 $mask,
127 [
128 'id' => $dao->id,
129 'cid' => $this->_contactId,
130 ],
131 ts('more'),
132 FALSE,
133 'membership.tab.row',
134 'Membership',
135 $dao->id
136 );
137 }
138
139 // Display Auto-renew status on page (0=disabled, 1=enabled, 2=enabled, but error
140 if (!empty($membership[$dao->id]['contribution_recur_id'])) {
141 if (CRM_Member_BAO_Membership::isSubscriptionCancelled($membership[$dao->id]['membership_id'])) {
142 $membership[$dao->id]['auto_renew'] = 2;
143 }
144 else {
145 $membership[$dao->id]['auto_renew'] = 1;
146 }
147 }
148 else {
149 $membership[$dao->id]['auto_renew'] = 0;
150 }
151
152 // if relevant--membership is active and type allows inheritance--count related memberships
153 if (CRM_Utils_Array::value('is_current_member', $statusANDType[$dao->id])
154 && CRM_Utils_Array::value('relationship_type_id', $statusANDType[$dao->id])
155 && empty($dao->owner_membership_id)
156 ) {
157 // not an related membership
158 $query = "
159 SELECT COUNT(m.id)
160 FROM civicrm_membership m
161 LEFT JOIN civicrm_membership_status ms ON ms.id = m.status_id
162 LEFT JOIN civicrm_contact ct ON ct.id = m.contact_id
163 WHERE m.owner_membership_id = {$dao->id} AND m.is_test = 0 AND ms.is_current_member = 1 AND ct.is_deleted = 0";
164 $num_related = CRM_Core_DAO::singleValueQuery($query);
165 $max_related = CRM_Utils_Array::value('max_related', $membership[$dao->id]);
166 $membership[$dao->id]['related_count'] = ($max_related == '' ? ts('%1 created', [1 => $num_related]) : ts('%1 out of %2', [
167 1 => $num_related,
168 2 => $max_related,
169 ]));
170 }
171 else {
172 $membership[$dao->id]['related_count'] = ts('N/A');
173 }
174 }
175
176 //Below code gives list of all Membership Types associated
177 //with an Organization(CRM-2016)
178 $membershipTypesResult = civicrm_api3('MembershipType', 'get', [
179 'member_of_contact_id' => $this->_contactId,
180 'options' => [
181 'limit' => 0,
182 ],
183 ]);
184 $membershipTypes = CRM_Utils_Array::value('values', $membershipTypesResult, NULL);
185
186 foreach ($membershipTypes as $key => $value) {
187 $membershipTypes[$key]['action'] = CRM_Core_Action::formLink(self::membershipTypeslinks(),
188 $mask,
189 [
190 'id' => $value['id'],
191 'cid' => $this->_contactId,
192 ],
193 ts('more'),
194 FALSE,
195 'membershipType.organization.action',
196 'MembershipType',
197 $value['id']
198 );
199 }
200
201 $activeMembers = CRM_Member_BAO_Membership::activeMembers($membership);
202 $inActiveMembers = CRM_Member_BAO_Membership::activeMembers($membership, 'inactive');
203 $this->assign('activeMembers', $activeMembers);
204 $this->assign('inActiveMembers', $inActiveMembers);
205 $this->assign('membershipTypes', $membershipTypes);
206
207 if ($this->_contactId) {
208 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
209 $this->assign('displayName', $displayName);
210 $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('membership', $this->_contactId);
211 // Refresh other tabs with related data
212 $this->ajaxResponse['updateTabs'] = [
213 '#tab_activity' => CRM_Contact_BAO_Contact::getCountComponent('activity', $this->_contactId),
214 '#tab_rel' => CRM_Contact_BAO_Contact::getCountComponent('rel', $this->_contactId),
215 ];
216 if (CRM_Core_Permission::access('CiviContribute')) {
217 $this->ajaxResponse['updateTabs']['#tab_contribute'] = CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId);
218 }
219 }
220 }
221
222 /**
223 * called when action is view.
224 *
225 * @return null
226 */
227 public function view() {
228 $controller = new CRM_Core_Controller_Simple(
229 'CRM_Member_Form_MembershipView',
230 ts('View Membership'),
231 $this->_action
232 );
233 $controller->setEmbedded(TRUE);
234 $controller->set('id', $this->_id);
235 $controller->set('cid', $this->_contactId);
236
237 return $controller->run();
238 }
239
240 /**
241 * called when action is update or new.
242 *
243 * @return null
244 */
245 public function edit() {
246 // set https for offline cc transaction
247 $mode = CRM_Utils_Request::retrieve('mode', 'Alphanumeric', $this);
248 if ($mode == 'test' || $mode == 'live') {
249 CRM_Utils_System::redirectToSSL();
250 }
251
252 // build associated contributions ( note: this is called to show associated contributions in edit mode )
253 if ($this->_action & CRM_Core_Action::UPDATE) {
254 $this->assign('accessContribution', FALSE);
255 if (CRM_Core_Permission::access('CiviContribute')) {
256 $this->assign('accessContribution', TRUE);
257 CRM_Member_Page_Tab::associatedContribution($this->_contactId, $this->_id);
258
259 //show associated soft credit when contribution payment is paid by different person in edit mode
260 if ($this->_id && $this->_contactId) {
261 $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$this->_id})";
262 $softCreditList = CRM_Contribute_BAO_ContributionSoft::getSoftContributionList($this->_contactId, $filter);
263 if (!empty($softCreditList)) {
264 $this->assign('softCredit', TRUE);
265 $this->assign('softCreditRows', $softCreditList);
266 }
267 }
268 }
269 }
270
271 if ($this->_action & CRM_Core_Action::RENEW) {
272 $path = 'CRM_Member_Form_MembershipRenewal';
273 $title = ts('Renew Membership');
274 }
275 else {
276 $path = 'CRM_Member_Form_Membership';
277 $title = ts('Create Membership');
278 }
279
280 $controller = new CRM_Core_Controller_Simple($path, $title, $this->_action);
281 $controller->setEmbedded(TRUE);
282 $controller->set('BAOName', $this->getBAOName());
283 $controller->set('id', $this->_id);
284 $controller->set('cid', $this->_contactId);
285 return $controller->run();
286 }
287
288 public function preProcess() {
289 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
290 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
291 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
292
293 if ($context == 'standalone') {
294 $this->_action = CRM_Core_Action::ADD;
295 }
296 else {
297 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
298 $this->assign('contactId', $this->_contactId);
299
300 // check logged in url permission
301 CRM_Contact_Page_View::checkUserPermission($this);
302 }
303
304 $this->assign('action', $this->_action);
305
306 if ($this->_permission == CRM_Core_Permission::EDIT && !CRM_Core_Permission::check('edit memberships')) {
307 // demote to view since user does not have edit membership rights
308 $this->_permission = CRM_Core_Permission::VIEW;
309 $this->assign('permission', 'view');
310 }
311 }
312
313 /**
314 * the main function that is called when the page loads, it decides the which action has to be taken for the page.
315 *
316 * @return null
317 */
318 public function run() {
319 $this->preProcess();
320
321 // check if we can process credit card membership
322 $newCredit = CRM_Core_Config::isEnabledBackOfficeCreditCardPayments();
323 $this->assign('newCredit', $newCredit);
324
325 if ($newCredit) {
326 $this->_isPaymentProcessor = TRUE;
327 }
328 else {
329 $this->_isPaymentProcessor = FALSE;
330 }
331
332 // Only show credit card membership signup if user has CiviContribute permission
333 if (CRM_Core_Permission::access('CiviContribute')) {
334 $this->_accessContribution = TRUE;
335 $this->assign('accessContribution', TRUE);
336
337 //show associated soft credit when contribution payment is paid by different person
338 if ($this->_id && $this->_contactId) {
339 $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$this->_id})";
340 $softCreditList = CRM_Contribute_BAO_ContributionSoft::getSoftContributionList($this->_contactId, $filter);
341 if (!empty($softCreditList)) {
342 $this->assign('softCredit', TRUE);
343 $this->assign('softCreditRows', $softCreditList);
344 }
345 }
346 }
347 else {
348 $this->_accessContribution = FALSE;
349 $this->assign('accessContribution', FALSE);
350 $this->assign('softCredit', FALSE);
351 }
352
353 if ($this->_action & CRM_Core_Action::VIEW) {
354 $this->view();
355 }
356 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE | CRM_Core_Action::RENEW)) {
357 self::setContext($this);
358 $this->edit();
359 }
360 else {
361 self::setContext($this);
362 $this->browse();
363 }
364
365 return parent::run();
366 }
367
368 /**
369 * @param CRM_Core_Form $form
370 * @param int $contactId
371 */
372 public static function setContext(&$form, $contactId = NULL) {
373 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $form, FALSE, 'search');
374
375 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $form);
376
377 $searchContext = CRM_Utils_Request::retrieve('searchContext', 'String', $form);
378
379 //validate the qfKey
380 if (!CRM_Utils_Rule::qfKey($qfKey)) {
381 $qfKey = NULL;
382 }
383
384 if (!$contactId) {
385 $contactId = $form->_contactId;
386 }
387
388 switch ($context) {
389 case 'dashboard':
390 $url = CRM_Utils_System::url('civicrm/member', 'reset=1');
391 break;
392
393 case 'membership':
394 $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&force=1&cid={$contactId}&selectedChild=member");
395 break;
396
397 case 'search':
398 $urlParams = 'force=1';
399 if ($qfKey) {
400 $urlParams .= "&qfKey=$qfKey";
401 }
402 $form->assign('searchKey', $qfKey);
403
404 if ($searchContext) {
405 $url = CRM_Utils_System::url("civicrm/$searchContext/search", $urlParams);
406 }
407 else {
408 $url = CRM_Utils_System::url('civicrm/member/search', $urlParams);
409 }
410 break;
411
412 case 'home':
413 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
414 break;
415
416 case 'activity':
417 $url = CRM_Utils_System::url('civicrm/contact/view',
418 "reset=1&force=1&cid={$contactId}&selectedChild=activity"
419 );
420 break;
421
422 case 'standalone':
423 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
424 break;
425
426 case 'fulltext':
427 $action = CRM_Utils_Request::retrieve('action', 'String', $form);
428 $keyName = '&qfKey';
429 $urlParams = 'force=1';
430 $urlString = 'civicrm/contact/search/custom';
431 if ($action == CRM_Core_Action::UPDATE) {
432 if ($form->_contactId) {
433 $urlParams .= '&cid=' . $form->_contactId;
434 }
435 $keyName = '&key';
436 $urlParams .= '&context=fulltext&action=view';
437 $urlString = 'civicrm/contact/view/membership';
438 }
439 if ($qfKey) {
440 $urlParams .= "$keyName=$qfKey";
441 }
442 $form->assign('searchKey', $qfKey);
443 $url = CRM_Utils_System::url($urlString, $urlParams);
444 break;
445
446 default:
447 $cid = NULL;
448 if ($contactId) {
449 $cid = '&cid=' . $contactId;
450 }
451 $url = CRM_Utils_System::url('civicrm/member/search', 'force=1' . $cid);
452 break;
453 }
454
455 $session = CRM_Core_Session::singleton();
456 $session->pushUserContext($url);
457 }
458
459 /**
460 * Get action links.
461 *
462 * @param string $status
463 * @param null $isPaymentProcessor
464 * @param null $accessContribution
465 * @param bool $isCancelSupported
466 * @param bool $isUpdateBilling
467 *
468 * @return array
469 * (reference) of action links
470 */
471 public static function &links(
472 $status = 'all',
473 $isPaymentProcessor = NULL,
474 $accessContribution = NULL,
475 $isCancelSupported = FALSE,
476 $isUpdateBilling = FALSE
477 ) {
478 if (!CRM_Utils_Array::value('view', self::$_links)) {
479 self::$_links['view'] = [
480 CRM_Core_Action::VIEW => [
481 'name' => ts('View'),
482 'url' => 'civicrm/contact/view/membership',
483 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
484 'title' => ts('View Membership'),
485 ],
486 ];
487 }
488
489 if (!CRM_Utils_Array::value('all', self::$_links)) {
490 $extraLinks = [
491 CRM_Core_Action::UPDATE => [
492 'name' => ts('Edit'),
493 'url' => 'civicrm/contact/view/membership',
494 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
495 'title' => ts('Edit Membership'),
496 ],
497 CRM_Core_Action::RENEW => [
498 'name' => ts('Renew'),
499 'url' => 'civicrm/contact/view/membership',
500 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
501 'title' => ts('Renew Membership'),
502 ],
503 CRM_Core_Action::FOLLOWUP => [
504 'name' => ts('Renew-Credit Card'),
505 'url' => 'civicrm/contact/view/membership',
506 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member&mode=live',
507 'title' => ts('Renew Membership Using Credit Card'),
508 ],
509 CRM_Core_Action::DELETE => [
510 'name' => ts('Delete'),
511 'url' => 'civicrm/contact/view/membership',
512 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
513 'title' => ts('Delete Membership'),
514 ],
515 ];
516 if (!$isPaymentProcessor || !$accessContribution) {
517 //unset the renew with credit card when payment
518 //processor is not available or user is not permitted to create contributions
519 unset($extraLinks[CRM_Core_Action::FOLLOWUP]);
520 }
521 self::$_links['all'] = self::$_links['view'] + $extraLinks;
522 }
523
524 if ($isCancelSupported) {
525 $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.');
526 self::$_links['all'][CRM_Core_Action::DISABLE] = [
527 'name' => ts('Cancel Auto-renewal'),
528 'url' => 'civicrm/contribute/unsubscribe',
529 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
530 'title' => ts('Cancel Auto Renew Subscription'),
531 'extra' => 'onclick = "if (confirm(\'' . $cancelMessage . '\') ) { return true; else return false;}"',
532 ];
533 }
534 elseif (isset(self::$_links['all'][CRM_Core_Action::DISABLE])) {
535 unset(self::$_links['all'][CRM_Core_Action::DISABLE]);
536 }
537
538 if ($isUpdateBilling) {
539 self::$_links['all'][CRM_Core_Action::MAP] = [
540 'name' => ts('Change Billing Details'),
541 'url' => 'civicrm/contribute/updatebilling',
542 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
543 'title' => ts('Change Billing Details'),
544 ];
545 }
546 elseif (isset(self::$_links['all'][CRM_Core_Action::MAP])) {
547 unset(self::$_links['all'][CRM_Core_Action::MAP]);
548 }
549 return self::$_links[$status];
550 }
551
552 /**
553 * Define action links for membership types of related organization.
554 *
555 * @return array
556 * self::$_membershipTypesLinks array of action links
557 */
558 public static function &membershipTypesLinks() {
559 if (!self::$_membershipTypesLinks) {
560 self::$_membershipTypesLinks = [
561 CRM_Core_Action::VIEW => [
562 'name' => ts('Members'),
563 'url' => 'civicrm/member/search/',
564 'qs' => 'reset=1&force=1&type=%%id%%',
565 'title' => ts('Search'),
566 ],
567 CRM_Core_Action::UPDATE => [
568 'name' => ts('Edit'),
569 'url' => 'civicrm/admin/member/membershipType',
570 'qs' => 'action=update&id=%%id%%&reset=1',
571 'title' => ts('Edit Membership Type'),
572 ],
573 ];
574 }
575 return self::$_membershipTypesLinks;
576 }
577
578 /**
579 * used for the to show the associated.
580 * contribution for the membership
581 *
582 * @param int $contactId
583 * @param int $membershipId
584 */
585 public static function associatedContribution($contactId = NULL, $membershipId = NULL) {
586 $controller = new CRM_Core_Controller_Simple(
587 'CRM_Contribute_Form_Search',
588 ts('Contributions'),
589 NULL,
590 FALSE, FALSE, TRUE
591 );
592 $controller->setEmbedded(TRUE);
593 $controller->reset();
594 $controller->set('force', 1);
595 $controller->set('cid', $contactId);
596 $controller->set('memberId', $membershipId);
597 $controller->set('context', 'contribution');
598 $controller->process();
599 $controller->run();
600 }
601
602 /**
603 * Get BAO Name.
604 *
605 * @return string
606 * Classname of BAO.
607 */
608 public function getBAOName() {
609 return 'CRM_Member_BAO_Membership';
610 }
611
612 /**
613 * Get a list of links based on permissioned FTs.
614 *
615 * @param int $memTypeID
616 * membership type ID
617 * @param int $links
618 * (reference ) action links
619 */
620 public static function getPermissionedLinks($memTypeID, &$links) {
621 if (!CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) {
622 return FALSE;
623 }
624 $finTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $memTypeID, 'financial_type_id');
625 $finType = CRM_Contribute_PseudoConstant::financialType($finTypeId);
626 if (!CRM_Core_Permission::check('edit contributions of type ' . $finType)) {
627 unset($links[CRM_Core_Action::UPDATE]);
628 unset($links[CRM_Core_Action::RENEW]);
629 unset($links[CRM_Core_Action::FOLLOWUP]);
630 }
631 if (!CRM_Core_Permission::check('delete contributions of type ' . $finType)) {
632 unset($links[CRM_Core_Action::DELETE]);
633 }
634 }
635
636 }