3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
35 class CRM_Member_Page_Tab
extends CRM_Core_Page
{
38 * The action links that we need to display for the browse screen.
42 static $_links = NULL;
43 static $_membershipTypesLinks = NULL;
45 public $_permission = NULL;
46 public $_contactId = NULL;
49 * called when action is browse.
51 public function browse() {
52 $links = self
::links('all', $this->_isPaymentProcessor
, $this->_accessContribution
);
53 CRM_Financial_BAO_FinancialType
::getAvailableMembershipTypes($membershipTypes);
54 $addWhere = "membership_type_id IN (0)";
55 if (!empty($membershipTypes)) {
56 $addWhere = "membership_type_id IN (" . implode(',', array_keys($membershipTypes)) . ")";
59 $membership = array();
60 $dao = new CRM_Member_DAO_Membership();
61 $dao->contact_id
= $this->_contactId
;
63 $dao->whereAdd($addWhere);
64 //$dao->orderBy('name');
67 //CRM--4418, check for view, edit, delete
68 $permissions = array(CRM_Core_Permission
::VIEW
);
69 if (CRM_Core_Permission
::check('edit memberships')) {
70 $permissions[] = CRM_Core_Permission
::EDIT
;
72 if (CRM_Core_Permission
::check('delete in CiviMember')) {
73 $permissions[] = CRM_Core_Permission
::DELETE
;
75 $mask = CRM_Core_Action
::mask($permissions);
77 // get deceased status id
78 $allStatus = CRM_Member_PseudoConstant
::membershipStatus();
79 $deceasedStatusId = array_search('Deceased', $allStatus);
82 $allCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
84 //checks membership of contact itself
85 while ($dao->fetch()) {
86 $membership[$dao->id
] = array();
87 CRM_Core_DAO
::storeValues($dao, $membership[$dao->id
]);
90 $membership[$dao->id
]['campaign'] = CRM_Utils_Array
::value($dao->campaign_id
, $allCampaigns);
92 //get the membership status and type values.
93 $statusANDType = CRM_Member_BAO_Membership
::getStatusANDTypeValues($dao->id
);
94 foreach (array('status', 'membership_type') as $fld) {
95 $membership[$dao->id
][$fld] = CRM_Utils_Array
::value($fld, $statusANDType[$dao->id
]);
97 if (!empty($statusANDType[$dao->id
]['is_current_member'])) {
98 $membership[$dao->id
]['active'] = TRUE;
100 if (empty($dao->owner_membership_id
)) {
101 // unset renew and followup link for deceased membership
102 $currentMask = $mask;
103 if ($dao->status_id
== $deceasedStatusId) {
104 $currentMask = $currentMask & ~CRM_Core_Action
::RENEW
& ~CRM_Core_Action
::FOLLOWUP
;
107 $isUpdateBilling = FALSE;
108 // It would be better to determine if there is a recurring contribution &
109 // is so get the entity for the recurring contribution (& skip if not).
110 $paymentObject = CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity(
111 $membership[$dao->id
]['membership_id'], 'membership', 'obj');
112 if (!empty($paymentObject)) {
113 // @todo - get this working with syntax style $paymentObject->supports(array
114 //('updateSubscriptionBillingInfo'));
115 $isUpdateBilling = $paymentObject->isSupported('updateSubscriptionBillingInfo');
118 // @todo - get this working with syntax style $paymentObject->supports(array
119 //('CancelSubscriptionSupported'));
120 $isCancelSupported = CRM_Member_BAO_Membership
::isCancelSubscriptionSupported(
121 $membership[$dao->id
]['membership_id']);
122 $links = self
::links('all',
128 self
::getPermissionedLinks($dao->membership_type_id
, $links);
129 $membership[$dao->id
]['action'] = CRM_Core_Action
::formLink($links,
133 'cid' => $this->_contactId
,
137 'membership.tab.row',
143 $links = self
::links('view');
144 self
::getPermissionedLinks($dao->membership_type_id
, $links);
145 $membership[$dao->id
]['action'] = CRM_Core_Action
::formLink($links,
149 'cid' => $this->_contactId
,
153 'membership.tab.row',
159 // Display Auto-renew status on page (0=disabled, 1=enabled, 2=enabled, but error
160 if (!empty($membership[$dao->id
]['contribution_recur_id'])) {
161 if (CRM_Member_BAO_Membership
::isSubscriptionCancelled($membership[$dao->id
]['membership_id'])) {
162 $membership[$dao->id
]['auto_renew'] = 2;
165 $membership[$dao->id
]['auto_renew'] = 1;
169 $membership[$dao->id
]['auto_renew'] = 0;
172 // if relevant--membership is active and type allows inheritance--count related memberships
173 if (CRM_Utils_Array
::value('is_current_member', $statusANDType[$dao->id
])
174 && CRM_Utils_Array
::value('relationship_type_id', $statusANDType[$dao->id
])
175 && empty($dao->owner_membership_id
)
177 // not an related membership
180 FROM civicrm_membership m
181 LEFT JOIN civicrm_membership_status ms ON ms.id = m.status_id
182 LEFT JOIN civicrm_contact ct ON ct.id = m.contact_id
183 WHERE m.owner_membership_id = {$dao->id} AND m.is_test = 0 AND ms.is_current_member = 1 AND ct.is_deleted = 0";
184 $num_related = CRM_Core_DAO
::singleValueQuery($query);
185 $max_related = CRM_Utils_Array
::value('max_related', $membership[$dao->id
]);
186 $membership[$dao->id
]['related_count'] = ($max_related == '' ?
ts('%1 created', array(1 => $num_related)) : ts('%1 out of %2', array(
192 $membership[$dao->id
]['related_count'] = ts('N/A');
196 //Below code gives list of all Membership Types associated
197 //with an Organization(CRM-2016)
198 $membershipTypesResult = civicrm_api3('MembershipType', 'get', array(
199 'member_of_contact_id' => $this->_contactId
,
204 $membershipTypes = CRM_Utils_Array
::value('values', $membershipTypesResult, NULL);
206 foreach ($membershipTypes as $key => $value) {
207 $membershipTypes[$key]['action'] = CRM_Core_Action
::formLink(self
::membershipTypeslinks(),
210 'id' => $value['id'],
211 'cid' => $this->_contactId
,
215 'membershipType.organization.action',
221 $activeMembers = CRM_Member_BAO_Membership
::activeMembers($membership);
222 $inActiveMembers = CRM_Member_BAO_Membership
::activeMembers($membership, 'inactive');
223 $this->assign('activeMembers', $activeMembers);
224 $this->assign('inActiveMembers', $inActiveMembers);
225 $this->assign('membershipTypes', $membershipTypes);
227 if ($this->_contactId
) {
228 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_contactId
);
229 $this->assign('displayName', $displayName);
230 $this->ajaxResponse
['tabCount'] = CRM_Contact_BAO_Contact
::getCountComponent('membership', $this->_contactId
);
231 // Refresh other tabs with related data
232 $this->ajaxResponse
['updateTabs'] = array(
233 '#tab_activity' => CRM_Contact_BAO_Contact
::getCountComponent('activity', $this->_contactId
),
234 '#tab_rel' => CRM_Contact_BAO_Contact
::getCountComponent('rel', $this->_contactId
),
236 if (CRM_Core_Permission
::access('CiviContribute')) {
237 $this->ajaxResponse
['updateTabs']['#tab_contribute'] = CRM_Contact_BAO_Contact
::getCountComponent('contribution', $this->_contactId
);
243 * called when action is view.
247 public function view() {
248 $controller = new CRM_Core_Controller_Simple(
249 'CRM_Member_Form_MembershipView',
250 ts('View Membership'),
253 $controller->setEmbedded(TRUE);
254 $controller->set('id', $this->_id
);
255 $controller->set('cid', $this->_contactId
);
257 return $controller->run();
261 * called when action is update or new.
265 public function edit() {
266 // set https for offline cc transaction
267 $mode = CRM_Utils_Request
::retrieve('mode', 'String', $this);
268 if ($mode == 'test' ||
$mode == 'live') {
269 CRM_Utils_System
::redirectToSSL();
272 // build associated contributions ( note: this is called to show associated contributions in edit mode )
273 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
274 $this->assign('accessContribution', FALSE);
275 if (CRM_Core_Permission
::access('CiviContribute')) {
276 $this->assign('accessContribution', TRUE);
277 CRM_Member_Page_Tab
::associatedContribution($this->_contactId
, $this->_id
);
279 //show associated soft credit when contribution payment is paid by different person in edit mode
280 if ($this->_id
&& $this->_contactId
) {
281 $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$this->_id})";
282 $softCreditList = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionList($this->_contactId
, $filter);
283 if (!empty($softCreditList)) {
284 $this->assign('softCredit', TRUE);
285 $this->assign('softCreditRows', $softCreditList);
291 if ($this->_action
& CRM_Core_Action
::RENEW
) {
292 $path = 'CRM_Member_Form_MembershipRenewal';
293 $title = ts('Renew Membership');
296 $path = 'CRM_Member_Form_Membership';
297 $title = ts('Create Membership');
300 $controller = new CRM_Core_Controller_Simple($path, $title, $this->_action
);
301 $controller->setEmbedded(TRUE);
302 $controller->set('BAOName', $this->getBAOName());
303 $controller->set('id', $this->_id
);
304 $controller->set('cid', $this->_contactId
);
305 return $controller->run();
308 public function preProcess() {
309 $context = CRM_Utils_Request
::retrieve('context', 'String', $this);
310 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'browse');
311 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
313 if ($context == 'standalone') {
314 $this->_action
= CRM_Core_Action
::ADD
;
317 $this->_contactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this, TRUE);
318 $this->assign('contactId', $this->_contactId
);
320 // check logged in url permission
321 CRM_Contact_Page_View
::checkUserPermission($this);
324 $this->assign('action', $this->_action
);
326 if ($this->_permission
== CRM_Core_Permission
::EDIT
&& !CRM_Core_Permission
::check('edit memberships')) {
327 // demote to view since user does not have edit membership rights
328 $this->_permission
= CRM_Core_Permission
::VIEW
;
329 $this->assign('permission', 'view');
334 * the main function that is called when the page loads, it decides the which action has to be taken for the page.
338 public function run() {
341 // check if we can process credit card membership
342 $newCredit = CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments();
343 $this->assign('newCredit', $newCredit);
346 $this->_isPaymentProcessor
= TRUE;
349 $this->_isPaymentProcessor
= FALSE;
352 // Only show credit card membership signup if user has CiviContribute permission
353 if (CRM_Core_Permission
::access('CiviContribute')) {
354 $this->_accessContribution
= TRUE;
355 $this->assign('accessContribution', TRUE);
357 //show associated soft credit when contribution payment is paid by different person
358 if ($this->_id
&& $this->_contactId
) {
359 $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$this->_id})";
360 $softCreditList = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionList($this->_contactId
, $filter);
361 if (!empty($softCreditList)) {
362 $this->assign('softCredit', TRUE);
363 $this->assign('softCreditRows', $softCreditList);
368 $this->_accessContribution
= FALSE;
369 $this->assign('accessContribution', FALSE);
370 $this->assign('softCredit', FALSE);
373 if ($this->_action
& CRM_Core_Action
::VIEW
) {
376 elseif ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD | CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
377 self
::setContext($this);
381 self
::setContext($this);
385 return parent
::run();
389 * @param CRM_Core_Form $form
390 * @param int $contactId
392 public static function setContext(&$form, $contactId = NULL) {
393 $context = CRM_Utils_Request
::retrieve('context', 'String', $form, FALSE, 'search');
395 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $form);
397 $searchContext = CRM_Utils_Request
::retrieve('searchContext', 'String', $form);
400 if (!CRM_Utils_Rule
::qfKey($qfKey)) {
405 $contactId = $form->_contactId
;
410 $url = CRM_Utils_System
::url('civicrm/member', 'reset=1');
414 $url = CRM_Utils_System
::url('civicrm/contact/view', "reset=1&force=1&cid={$contactId}&selectedChild=member");
418 $urlParams = 'force=1';
420 $urlParams .= "&qfKey=$qfKey";
422 $form->assign('searchKey', $qfKey);
424 if ($searchContext) {
425 $url = CRM_Utils_System
::url("civicrm/$searchContext/search", $urlParams);
428 $url = CRM_Utils_System
::url('civicrm/member/search', $urlParams);
433 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
437 $url = CRM_Utils_System
::url('civicrm/contact/view',
438 "reset=1&force=1&cid={$contactId}&selectedChild=activity"
443 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
447 $action = CRM_Utils_Request
::retrieve('action', 'String', $form);
449 $urlParams = 'force=1';
450 $urlString = 'civicrm/contact/search/custom';
451 if ($action == CRM_Core_Action
::UPDATE
) {
452 if ($form->_contactId
) {
453 $urlParams .= '&cid=' . $form->_contactId
;
456 $urlParams .= '&context=fulltext&action=view';
457 $urlString = 'civicrm/contact/view/membership';
460 $urlParams .= "$keyName=$qfKey";
462 $form->assign('searchKey', $qfKey);
463 $url = CRM_Utils_System
::url($urlString, $urlParams);
469 $cid = '&cid=' . $contactId;
471 $url = CRM_Utils_System
::url('civicrm/member/search', 'force=1' . $cid);
475 $session = CRM_Core_Session
::singleton();
476 $session->pushUserContext($url);
482 * @param string $status
483 * @param null $isPaymentProcessor
484 * @param null $accessContribution
485 * @param bool $isCancelSupported
486 * @param bool $isUpdateBilling
489 * (reference) of action links
491 public static function &links(
493 $isPaymentProcessor = NULL,
494 $accessContribution = NULL,
495 $isCancelSupported = FALSE,
496 $isUpdateBilling = FALSE
498 if (!CRM_Utils_Array
::value('view', self
::$_links)) {
499 self
::$_links['view'] = array(
500 CRM_Core_Action
::VIEW
=> array(
501 'name' => ts('View'),
502 'url' => 'civicrm/contact/view/membership',
503 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
504 'title' => ts('View Membership'),
509 if (!CRM_Utils_Array
::value('all', self
::$_links)) {
511 CRM_Core_Action
::UPDATE
=> array(
512 'name' => ts('Edit'),
513 'url' => 'civicrm/contact/view/membership',
514 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
515 'title' => ts('Edit Membership'),
517 CRM_Core_Action
::RENEW
=> array(
518 'name' => ts('Renew'),
519 'url' => 'civicrm/contact/view/membership',
520 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
521 'title' => ts('Renew Membership'),
523 CRM_Core_Action
::FOLLOWUP
=> array(
524 'name' => ts('Renew-Credit Card'),
525 'url' => 'civicrm/contact/view/membership',
526 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member&mode=live',
527 'title' => ts('Renew Membership Using Credit Card'),
529 CRM_Core_Action
::DELETE
=> array(
530 'name' => ts('Delete'),
531 'url' => 'civicrm/contact/view/membership',
532 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
533 'title' => ts('Delete Membership'),
536 if (!$isPaymentProcessor ||
!$accessContribution) {
537 //unset the renew with credit card when payment
538 //processor is not available or user is not permitted to create contributions
539 unset($extraLinks[CRM_Core_Action
::FOLLOWUP
]);
541 self
::$_links['all'] = self
::$_links['view'] +
$extraLinks;
544 if ($isCancelSupported) {
545 $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.');
546 self
::$_links['all'][CRM_Core_Action
::DISABLE
] = array(
547 'name' => ts('Cancel Auto-renewal'),
548 'url' => 'civicrm/contribute/unsubscribe',
549 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
550 'title' => ts('Cancel Auto Renew Subscription'),
551 'extra' => 'onclick = "if (confirm(\'' . $cancelMessage . '\') ) { return true; else return false;}"',
554 elseif (isset(self
::$_links['all'][CRM_Core_Action
::DISABLE
])) {
555 unset(self
::$_links['all'][CRM_Core_Action
::DISABLE
]);
558 if ($isUpdateBilling) {
559 self
::$_links['all'][CRM_Core_Action
::MAP
] = array(
560 'name' => ts('Change Billing Details'),
561 'url' => 'civicrm/contribute/updatebilling',
562 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
563 'title' => ts('Change Billing Details'),
566 elseif (isset(self
::$_links['all'][CRM_Core_Action
::MAP
])) {
567 unset(self
::$_links['all'][CRM_Core_Action
::MAP
]);
569 return self
::$_links[$status];
573 * Define action links for membership types of related organization.
576 * self::$_membershipTypesLinks array of action links
578 public static function &membershipTypesLinks() {
579 if (!self
::$_membershipTypesLinks) {
580 self
::$_membershipTypesLinks = array(
581 CRM_Core_Action
::VIEW
=> array(
582 'name' => ts('Members'),
583 'url' => 'civicrm/member/search/',
584 'qs' => 'reset=1&force=1&type=%%id%%',
585 'title' => ts('Search'),
587 CRM_Core_Action
::UPDATE
=> array(
588 'name' => ts('Edit'),
589 'url' => 'civicrm/admin/member/membershipType',
590 'qs' => 'action=update&id=%%id%%&reset=1',
591 'title' => ts('Edit Membership Type'),
595 return self
::$_membershipTypesLinks;
599 * used for the to show the associated.
600 * contribution for the membership
602 * @param int $contactId
603 * @param int $membershipId
605 public static function associatedContribution($contactId = NULL, $membershipId = NULL) {
606 $controller = new CRM_Core_Controller_Simple(
607 'CRM_Contribute_Form_Search',
612 $controller->setEmbedded(TRUE);
613 $controller->reset();
614 $controller->set('force', 1);
615 $controller->set('cid', $contactId);
616 $controller->set('memberId', $membershipId);
617 $controller->set('context', 'contribution');
618 $controller->process();
628 public function getBAOName() {
629 return 'CRM_Member_BAO_Membership';
633 * Get a list of links based on permissioned FTs.
635 * @param int $memTypeID
638 * (reference ) action links
640 public static function getPermissionedLinks($memTypeID, &$links) {
641 if (!CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus()) {
644 $finTypeId = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $memTypeID, 'financial_type_id');
645 $finType = CRM_Contribute_PseudoConstant
::financialType($finTypeId);
646 if (!CRM_Core_Permission
::check('edit contributions of type ' . $finType)) {
647 unset($links[CRM_Core_Action
::UPDATE
]);
648 unset($links[CRM_Core_Action
::RENEW
]);
649 unset($links[CRM_Core_Action
::FOLLOWUP
]);
651 if (!CRM_Core_Permission
::check('delete contributions of type ' . $finType)) {
652 unset($links[CRM_Core_Action
::DELETE
]);