3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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
);
54 $membership = array();
55 $dao = new CRM_Member_DAO_Membership();
56 $dao->contact_id
= $this->_contactId
;
58 //$dao->orderBy('name');
61 //CRM--4418, check for view, edit, delete
62 $permissions = array(CRM_Core_Permission
::VIEW
);
63 if (CRM_Core_Permission
::check('edit memberships')) {
64 $permissions[] = CRM_Core_Permission
::EDIT
;
66 if (CRM_Core_Permission
::check('delete in CiviMember')) {
67 $permissions[] = CRM_Core_Permission
::DELETE
;
69 $mask = CRM_Core_Action
::mask($permissions);
71 // get deceased status id
72 $allStatus = CRM_Member_PseudoConstant
::membershipStatus();
73 $deceasedStatusId = array_search('Deceased', $allStatus);
76 $allCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
78 //checks membership of contact itself
79 while ($dao->fetch()) {
80 $membership[$dao->id
] = array();
81 CRM_Core_DAO
::storeValues($dao, $membership[$dao->id
]);
84 $membership[$dao->id
]['campaign'] = CRM_Utils_Array
::value($dao->campaign_id
, $allCampaigns);
86 //get the membership status and type values.
87 $statusANDType = CRM_Member_BAO_Membership
::getStatusANDTypeValues($dao->id
);
88 foreach (array('status', 'membership_type') as $fld) {
89 $membership[$dao->id
][$fld] = CRM_Utils_Array
::value($fld, $statusANDType[$dao->id
]);
91 if (!empty($statusANDType[$dao->id
]['is_current_member'])) {
92 $membership[$dao->id
]['active'] = TRUE;
94 if (empty($dao->owner_membership_id
)) {
95 // unset renew and followup link for deceased membership
97 if ($dao->status_id
== $deceasedStatusId) {
98 $currentMask = $currentMask & ~CRM_Core_Action
::RENEW
& ~CRM_Core_Action
::FOLLOWUP
;
101 $isUpdateBilling = FALSE;
102 // It would be better to determine if there is a recurring contribution &
103 // is so get the entity for the recurring contribution (& skip if not).
104 $paymentObject = CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity(
105 $membership[$dao->id
]['membership_id'], 'membership', 'obj');
106 if (!empty($paymentObject)) {
107 // @todo - get this working with syntax style $paymentObject->supports(array
108 //('updateSubscriptionBillingInfo'));
109 $isUpdateBilling = $paymentObject->isSupported('updateSubscriptionBillingInfo');
112 // @todo - get this working with syntax style $paymentObject->supports(array
113 //('CancelSubscriptionSupported'));
114 $isCancelSupported = CRM_Member_BAO_Membership
::isCancelSubscriptionSupported(
115 $membership[$dao->id
]['membership_id']);
117 $membership[$dao->id
]['action'] = CRM_Core_Action
::formLink(self
::links('all',
126 'cid' => $this->_contactId
,
130 'membership.tab.row',
136 $membership[$dao->id
]['action'] = CRM_Core_Action
::formLink(self
::links('view'),
140 'cid' => $this->_contactId
,
144 'membership.tab.row',
150 //does membership have auto renew CRM-7137.
151 if (!empty($membership[$dao->id
]['contribution_recur_id']) &&
152 !CRM_Member_BAO_Membership
::isSubscriptionCancelled($membership[$dao->id
]['membership_id'])
154 $membership[$dao->id
]['auto_renew'] = 1;
157 $membership[$dao->id
]['auto_renew'] = 0;
160 // if relevant, count related memberships
161 if (CRM_Utils_Array
::value('is_current_member', $statusANDType[$dao->id
]) // membership is active
162 && CRM_Utils_Array
::value('relationship_type_id', $statusANDType[$dao->id
]) // membership type allows inheritance
163 && empty($dao->owner_membership_id
)
165 // not an related membership
168 FROM civicrm_membership m
169 LEFT JOIN civicrm_membership_status ms ON ms.id = m.status_id
170 LEFT JOIN civicrm_contact ct ON ct.id = m.contact_id
171 WHERE m.owner_membership_id = {$dao->id} AND m.is_test = 0 AND ms.is_current_member = 1 AND ct.is_deleted = 0";
172 $num_related = CRM_Core_DAO
::singleValueQuery($query);
173 $max_related = CRM_Utils_Array
::value('max_related', $membership[$dao->id
]);
174 $membership[$dao->id
]['related_count'] = ($max_related == '' ?
ts('%1 created', array(1 => $num_related)) : ts('%1 out of %2', array(
181 $membership[$dao->id
]['related_count'] = ts('N/A');
185 //Below code gives list of all Membership Types associated
186 //with an Organization(CRM-2016)
187 $membershipTypes = CRM_Member_BAO_MembershipType
::getMembershipTypesByOrg($this->_contactId
);
188 foreach ($membershipTypes as $key => $value) {
189 $membershipTypes[$key]['action'] = CRM_Core_Action
::formLink(self
::membershipTypeslinks(),
192 'id' => $value['id'],
193 'cid' => $this->_contactId
,
197 'membershipType.organization.action',
203 $activeMembers = CRM_Member_BAO_Membership
::activeMembers($membership);
204 $inActiveMembers = CRM_Member_BAO_Membership
::activeMembers($membership, 'inactive');
205 $this->assign('activeMembers', $activeMembers);
206 $this->assign('inActiveMembers', $inActiveMembers);
207 $this->assign('membershipTypes', $membershipTypes);
209 if ($this->_contactId
) {
210 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_contactId
);
211 $this->assign('displayName', $displayName);
212 $this->ajaxResponse
['tabCount'] = CRM_Contact_BAO_Contact
::getCountComponent('membership', $this->_contactId
);
213 // Refresh other tabs with related data
214 $this->ajaxResponse
['updateTabs'] = array(
215 '#tab_activity' => CRM_Contact_BAO_Contact
::getCountComponent('activity', $this->_contactId
),
216 '#tab_rel' => CRM_Contact_BAO_Contact
::getCountComponent('rel', $this->_contactId
),
218 if (CRM_Core_Permission
::access('CiviContribute')) {
219 $this->ajaxResponse
['updateTabs']['#tab_contribute'] = CRM_Contact_BAO_Contact
::getCountComponent('contribution', $this->_contactId
);
225 * called when action is view.
229 public function view() {
230 $controller = new CRM_Core_Controller_Simple(
231 'CRM_Member_Form_MembershipView',
232 ts('View Membership'),
235 $controller->setEmbedded(TRUE);
236 $controller->set('id', $this->_id
);
237 $controller->set('cid', $this->_contactId
);
239 return $controller->run();
243 * called when action is update or new.
247 public function edit() {
248 // set https for offline cc transaction
249 $mode = CRM_Utils_Request
::retrieve('mode', 'String', $this);
250 if ($mode == 'test' ||
$mode == 'live') {
251 CRM_Utils_System
::redirectToSSL();
254 // build associated contributions ( note: this is called to show associated contributions in edit mode )
255 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
256 $this->assign('accessContribution', FALSE);
257 if (CRM_Core_Permission
::access('CiviContribute')) {
258 $this->assign('accessContribution', TRUE);
259 CRM_Member_Page_Tab
::associatedContribution($this->_contactId
, $this->_id
);
261 //show associated soft credit when contribution payment is paid by different person in edit mode
262 if ($this->_id
&& $this->_contactId
) {
263 $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$this->_id})";
264 $softCreditList = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionList($this->_contactId
, $filter);
265 if (!empty($softCreditList)) {
266 $this->assign('softCredit', TRUE);
267 $this->assign('softCreditRows', $softCreditList);
273 if ($this->_action
& CRM_Core_Action
::RENEW
) {
274 $path = 'CRM_Member_Form_MembershipRenewal';
275 $title = ts('Renew Membership');
278 $path = 'CRM_Member_Form_Membership';
279 $title = ts('Create Membership');
282 $controller = new CRM_Core_Controller_Simple($path, $title, $this->_action
);
283 $controller->setEmbedded(TRUE);
284 $controller->set('BAOName', $this->getBAOName());
285 $controller->set('id', $this->_id
);
286 $controller->set('cid', $this->_contactId
);
287 return $controller->run();
290 public function preProcess() {
291 $context = CRM_Utils_Request
::retrieve('context', 'String', $this);
292 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'browse');
293 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
295 if ($context == 'standalone') {
296 $this->_action
= CRM_Core_Action
::ADD
;
299 $this->_contactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this, TRUE);
300 $this->assign('contactId', $this->_contactId
);
302 // check logged in url permission
303 CRM_Contact_Page_View
::checkUserPermission($this);
306 $this->assign('action', $this->_action
);
308 if ($this->_permission
== CRM_Core_Permission
::EDIT
&& !CRM_Core_Permission
::check('edit memberships')) {
309 // demote to view since user does not have edit membership rights
310 $this->_permission
= CRM_Core_Permission
::VIEW
;
311 $this->assign('permission', 'view');
316 * the main function that is called when the page loads, it decides the which action has to be taken for the page.
320 public function run() {
323 // check if we can process credit card membership
324 $newCredit = CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments();
325 $this->assign('newCredit', $newCredit);
328 $this->_isPaymentProcessor
= TRUE;
331 $this->_isPaymentProcessor
= FALSE;
334 // Only show credit card membership signup if user has CiviContribute permission
335 if (CRM_Core_Permission
::access('CiviContribute')) {
336 $this->_accessContribution
= TRUE;
337 $this->assign('accessContribution', TRUE);
339 //show associated soft credit when contribution payment is paid by different person
340 if ($this->_id
&& $this->_contactId
) {
341 $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$this->_id})";
342 $softCreditList = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionList($this->_contactId
, $filter);
343 if (!empty($softCreditList)) {
344 $this->assign('softCredit', TRUE);
345 $this->assign('softCreditRows', $softCreditList);
350 $this->_accessContribution
= FALSE;
351 $this->assign('accessContribution', FALSE);
352 $this->assign('softCredit', FALSE);
355 if ($this->_action
& CRM_Core_Action
::VIEW
) {
358 elseif ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD | CRM_Core_Action
::DELETE | CRM_Core_Action
::RENEW
)) {
359 self
::setContext($this);
363 self
::setContext($this);
367 return parent
::run();
371 * @param CRM_Core_Form $form
372 * @param int $contactId
374 public static function setContext(&$form, $contactId = NULL) {
375 $context = CRM_Utils_Request
::retrieve('context', 'String', $form, FALSE, 'search');
377 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $form);
379 $searchContext = CRM_Utils_Request
::retrieve('searchContext', 'String', $form);
382 if (!CRM_Utils_Rule
::qfKey($qfKey)) {
387 $contactId = $form->_contactId
;
392 $url = CRM_Utils_System
::url('civicrm/member', 'reset=1');
396 $url = CRM_Utils_System
::url('civicrm/contact/view', "reset=1&force=1&cid={$contactId}&selectedChild=member");
400 $urlParams = 'force=1';
402 $urlParams .= "&qfKey=$qfKey";
404 $form->assign('searchKey', $qfKey);
406 if ($searchContext) {
407 $url = CRM_Utils_System
::url("civicrm/$searchContext/search", $urlParams);
410 $url = CRM_Utils_System
::url('civicrm/member/search', $urlParams);
415 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
419 $url = CRM_Utils_System
::url('civicrm/contact/view',
420 "reset=1&force=1&cid={$contactId}&selectedChild=activity"
425 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
429 $action = CRM_Utils_Request
::retrieve('action', 'String', $form);
431 $urlParams = 'force=1';
432 $urlString = 'civicrm/contact/search/custom';
433 if ($action == CRM_Core_Action
::UPDATE
) {
434 if ($form->_contactId
) {
435 $urlParams .= '&cid=' . $form->_contactId
;
438 $urlParams .= '&context=fulltext&action=view';
439 $urlString = 'civicrm/contact/view/membership';
442 $urlParams .= "$keyName=$qfKey";
444 $form->assign('searchKey', $qfKey);
445 $url = CRM_Utils_System
::url($urlString, $urlParams);
451 $cid = '&cid=' . $contactId;
453 $url = CRM_Utils_System
::url('civicrm/member/search', 'force=1' . $cid);
457 $session = CRM_Core_Session
::singleton();
458 $session->pushUserContext($url);
464 * @param string $status
465 * @param null $isPaymentProcessor
466 * @param null $accessContribution
467 * @param bool $isCancelSupported
468 * @param bool $isUpdateBilling
471 * (reference) of action links
473 public static function &links(
475 $isPaymentProcessor = NULL,
476 $accessContribution = NULL,
477 $isCancelSupported = FALSE,
478 $isUpdateBilling = FALSE
480 if (!CRM_Utils_Array
::value('view', self
::$_links)) {
481 self
::$_links['view'] = array(
482 CRM_Core_Action
::VIEW
=> array(
483 'name' => ts('View'),
484 'url' => 'civicrm/contact/view/membership',
485 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
486 'title' => ts('View Membership'),
491 if (!CRM_Utils_Array
::value('all', self
::$_links)) {
493 CRM_Core_Action
::UPDATE
=> array(
494 'name' => ts('Edit'),
495 'url' => 'civicrm/contact/view/membership',
496 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
497 'title' => ts('Edit Membership'),
499 CRM_Core_Action
::RENEW
=> array(
500 'name' => ts('Renew'),
501 'url' => 'civicrm/contact/view/membership',
502 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
503 'title' => ts('Renew Membership'),
505 CRM_Core_Action
::FOLLOWUP
=> array(
506 'name' => ts('Renew-Credit Card'),
507 'url' => 'civicrm/contact/view/membership',
508 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member&mode=live',
509 'title' => ts('Renew Membership Using Credit Card'),
511 CRM_Core_Action
::DELETE
=> array(
512 'name' => ts('Delete'),
513 'url' => 'civicrm/contact/view/membership',
514 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
515 'title' => ts('Delete Membership'),
518 if (!$isPaymentProcessor ||
!$accessContribution) {
519 //unset the renew with credit card when payment
520 //processor is not available or user is not permitted to create contributions
521 unset($extraLinks[CRM_Core_Action
::FOLLOWUP
]);
523 self
::$_links['all'] = self
::$_links['view'] +
$extraLinks;
526 if ($isCancelSupported) {
527 $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.');
528 self
::$_links['all'][CRM_Core_Action
::DISABLE
] = array(
529 'name' => ts('Cancel Auto-renewal'),
530 'url' => 'civicrm/contribute/unsubscribe',
531 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
532 'title' => ts('Cancel Auto Renew Subscription'),
533 'extra' => 'onclick = "if (confirm(\'' . $cancelMessage . '\') ) { return true; else return false;}"',
536 elseif (isset(self
::$_links['all'][CRM_Core_Action
::DISABLE
])) {
537 unset(self
::$_links['all'][CRM_Core_Action
::DISABLE
]);
540 if ($isUpdateBilling) {
541 self
::$_links['all'][CRM_Core_Action
::MAP
] = array(
542 'name' => ts('Change Billing Details'),
543 'url' => 'civicrm/contribute/updatebilling',
544 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
545 'title' => ts('Change Billing Details'),
548 elseif (isset(self
::$_links['all'][CRM_Core_Action
::MAP
])) {
549 unset(self
::$_links['all'][CRM_Core_Action
::MAP
]);
551 return self
::$_links[$status];
555 * Define action links for membership types of related organization.
558 * self::$_membershipTypesLinks array of action links
560 public static function &membershipTypesLinks() {
561 if (!self
::$_membershipTypesLinks) {
562 self
::$_membershipTypesLinks = array(
563 CRM_Core_Action
::VIEW
=> array(
564 'name' => ts('Members'),
565 'url' => 'civicrm/member/search/',
566 'qs' => 'reset=1&force=1&type=%%id%%',
567 'title' => ts('Search'),
569 CRM_Core_Action
::UPDATE
=> array(
570 'name' => ts('Edit'),
571 'url' => 'civicrm/admin/member/membershipType',
572 'qs' => 'action=update&id=%%id%%&reset=1',
573 'title' => ts('Edit Membership Type'),
577 return self
::$_membershipTypesLinks;
581 * used for the to show the associated.
582 * contribution for the membership
584 * @param int $contactId
585 * @param int $membershipId
587 public static function associatedContribution($contactId = NULL, $membershipId = NULL) {
588 $controller = new CRM_Core_Controller_Simple(
589 'CRM_Contribute_Form_Search',
594 $controller->setEmbedded(TRUE);
595 $controller->reset();
596 $controller->set('force', 1);
597 $controller->set('cid', $contactId);
598 $controller->set('memberId', $membershipId);
599 $controller->set('context', 'contribution');
600 $controller->process();
610 public function getBAOName() {
611 return 'CRM_Member_BAO_Membership';