3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Member_Page_Tab
extends CRM_Core_Page
{
38 * The action links that we need to display for the browse screen
43 static $_links = NULL;
44 static $_membershipTypesLinks = NULL;
46 public $_permission = NULL;
47 public $_contactId = NULL;
50 * This function is called when action is browse
56 $links = self
::links('all', $this->_isPaymentProcessor
, $this->_accessContribution
);
58 $membership = array();
59 $dao = new CRM_Member_DAO_Membership();
60 $dao->contact_id
= $this->_contactId
;
62 //$dao->orderBy('name');
65 //CRM--4418, check for view, edit, delete
66 $permissions = array(CRM_Core_Permission
::VIEW
);
67 if (CRM_Core_Permission
::check('edit memberships')) {
68 $permissions[] = CRM_Core_Permission
::EDIT
;
70 if (CRM_Core_Permission
::check('delete in CiviMember')) {
71 $permissions[] = CRM_Core_Permission
::DELETE
;
73 $mask = CRM_Core_Action
::mask($permissions);
75 // get deceased status id
76 $allStatus = CRM_Member_PseudoConstant
::membershipStatus();
77 $deceasedStatusId = array_search('Deceased', $allStatus);
80 $allCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
82 //checks membership of contact itself
83 while ($dao->fetch()) {
84 $membership[$dao->id
] = array();
85 CRM_Core_DAO
::storeValues($dao, $membership[$dao->id
]);
88 $membership[$dao->id
]['campaign'] = CRM_Utils_Array
::value($dao->campaign_id
, $allCampaigns);
90 //get the membership status and type values.
91 $statusANDType = CRM_Member_BAO_Membership
::getStatusANDTypeValues($dao->id
);
92 foreach (array('status', 'membership_type') as $fld) {
93 $membership[$dao->id
][$fld] = CRM_Utils_Array
::value($fld, $statusANDType[$dao->id
]);
95 if (!empty($statusANDType[$dao->id
]['is_current_member'])) {
96 $membership[$dao->id
]['active'] = TRUE;
98 if (empty($dao->owner_membership_id
)) {
99 // unset renew and followup link for deceased membership
100 $currentMask = $mask;
101 if ($dao->status_id
== $deceasedStatusId) {
102 $currentMask = $currentMask & ~CRM_Core_Action
::RENEW
& ~CRM_Core_Action
::FOLLOWUP
;
105 $isUpdateBilling = FALSE;
106 $paymentObject = CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity(
107 $membership[$dao->id
]['membership_id'], 'membership', 'obj');
108 if (!empty($paymentObject)) {
109 $isUpdateBilling = $paymentObject->isSupported('updateSubscriptionBillingInfo');
112 $isCancelSupported = CRM_Member_BAO_Membership
::isCancelSubscriptionSupported(
113 $membership[$dao->id
]['membership_id']);
115 $membership[$dao->id
]['action'] = CRM_Core_Action
::formLink(self
::links('all',
124 'cid' => $this->_contactId
,
128 'membership.tab.row',
134 $membership[$dao->id
]['action'] = CRM_Core_Action
::formLink(self
::links('view'),
138 'cid' => $this->_contactId
,
142 'membership.tab.row',
148 //does membership have auto renew CRM-7137.
149 if (!empty($membership[$dao->id
]['contribution_recur_id']) &&
150 !CRM_Member_BAO_Membership
::isSubscriptionCancelled($membership[$dao->id
]['membership_id'])
152 $membership[$dao->id
]['auto_renew'] = 1;
155 $membership[$dao->id
]['auto_renew'] = 0;
158 // if relevant, count related memberships
159 if (CRM_Utils_Array
::value('is_current_member', $statusANDType[$dao->id
]) // membership is active
160 && CRM_Utils_Array
::value('relationship_type_id', $statusANDType[$dao->id
]) // membership type allows inheritance
161 && empty($dao->owner_membership_id
)
162 ) { // not an related membership
165 FROM civicrm_membership m
166 LEFT JOIN civicrm_membership_status ms ON ms.id = m.status_id
167 LEFT JOIN civicrm_contact ct ON ct.id = m.contact_id
168 WHERE m.owner_membership_id = {$dao->id} AND m.is_test = 0 AND ms.is_current_member = 1 AND ct.is_deleted = 0";
169 $num_related = CRM_Core_DAO
::singleValueQuery($query);
170 $max_related = CRM_Utils_Array
::value('max_related', $membership[$dao->id
]);
171 $membership[$dao->id
]['related_count'] = ($max_related == '' ?
172 ts('%1 created', array(1 => $num_related)) :
173 ts('%1 out of %2', array(1 => $num_related, 2 => $max_related))
177 $membership[$dao->id
]['related_count'] = ts('N/A');
181 //Below code gives list of all Membership Types associated
182 //with an Organization(CRM-2016)
183 $membershipTypes = CRM_Member_BAO_MembershipType
::getMembershipTypesByOrg($this->_contactId
);
184 foreach ($membershipTypes as $key => $value) {
185 $membershipTypes[$key]['action'] = CRM_Core_Action
::formLink(self
::membershipTypeslinks(),
188 'id' => $value['id'],
189 'cid' => $this->_contactId
,
193 'membershipType.organization.action',
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);
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'] = array(
211 '#tab_contribute' => CRM_Contact_BAO_Contact
::getCountComponent('contribution', $this->_contactId
),
212 '#tab_activity' => CRM_Contact_BAO_Contact
::getCountComponent('activity', $this->_contactId
),
213 '#tab_rel' => CRM_Contact_BAO_Contact
::getCountComponent('rel', $this->_contactId
),
219 * This function is called when action is view
225 $controller = new CRM_Core_Controller_Simple(
226 'CRM_Member_Form_MembershipView',
227 ts('View Membership'),
230 $controller->setEmbedded(TRUE);
231 $controller->set('id', $this->_id
);
232 $controller->set('cid', $this->_contactId
);
234 return $controller->run();
238 * This function is called when action is update or new
244 // set https for offline cc transaction
245 $mode = CRM_Utils_Request
::retrieve('mode', 'String', $this);
246 if ($mode == 'test' ||
$mode == 'live') {
247 CRM_Utils_System
::redirectToSSL();
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
);
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);
269 if ($this->_action
& CRM_Core_Action
::RENEW
) {
270 $path = 'CRM_Member_Form_MembershipRenewal';
271 $title = ts('Renew Membership');
274 $path = 'CRM_Member_Form_Membership';
275 $title = ts('Create Membership');
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();
286 function preProcess() {
287 $context = CRM_Utils_Request
::retrieve('context', 'String', $this);
288 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'browse');
289 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
291 if ($context == 'standalone') {
292 $this->_action
= CRM_Core_Action
::ADD
;
295 $this->_contactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this, TRUE);
296 $this->assign('contactId', $this->_contactId
);
298 // check logged in url permission
299 CRM_Contact_Page_View
::checkUserPermission($this);
302 CRM_Contact_Page_View
::setTitle($this->_contactId
);
305 $this->assign('action', $this->_action
);
307 if ($this->_permission
== CRM_Core_Permission
::EDIT
&& !CRM_Core_Permission
::check('edit memberships')) {
308 // demote to view since user does not have edit membership rights
309 $this->_permission
= CRM_Core_Permission
::VIEW
;
310 $this->assign('permission', 'view');
315 * This function is the main function that is called when the page loads, it decides the which action has to be taken for the page.
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
359 | CRM_Core_Action
::RENEW
)) {
360 self
::setContext($this);
364 self
::setContext($this);
368 return parent
::run();
373 * @param null $contactId
375 public static function setContext(&$form, $contactId = NULL) {
376 $context = CRM_Utils_Request
::retrieve('context', 'String', $form, FALSE, 'search' );
378 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $form);
380 $searchContext = CRM_Utils_Request
::retrieve('searchContext', 'String', $this);
383 if (!CRM_Utils_Rule
::qfKey($qfKey)) {
388 $contactId = $form->_contactId
;
393 $url = CRM_Utils_System
::url('civicrm/member', 'reset=1');
397 $url = CRM_Utils_System
::url('civicrm/contact/view', "reset=1&force=1&cid={$contactId}&selectedChild=member");
401 $urlParams = 'force=1';
403 $urlParams .= "&qfKey=$qfKey";
405 $form->assign('searchKey', $qfKey);
407 if ($searchContext) {
408 $url = CRM_Utils_System
::url("civicrm/$searchContext/search", $urlParams);
411 $url = CRM_Utils_System
::url('civicrm/member/search', $urlParams);
416 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
420 $url = CRM_Utils_System
::url('civicrm/contact/view',
421 "reset=1&force=1&cid={$contactId}&selectedChild=activity"
426 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
430 $action = CRM_Utils_Request
::retrieve('action', 'String', $form);
432 $urlParams = 'force=1';
433 $urlString = 'civicrm/contact/search/custom';
434 if ($action == CRM_Core_Action
::UPDATE
) {
435 if ($form->_contactId
) {
436 $urlParams .= '&cid=' . $form->_contactId
;
439 $urlParams .= '&context=fulltext&action=view';
440 $urlString = 'civicrm/contact/view/membership';
443 $urlParams .= "$keyName=$qfKey";
445 $form->assign('searchKey', $qfKey);
446 $url = CRM_Utils_System
::url($urlString, $urlParams);
452 $cid = '&cid=' . $contactId;
454 $url = CRM_Utils_System
::url('civicrm/member/search', 'force=1' . $cid);
458 $session = CRM_Core_Session
::singleton();
459 $session->pushUserContext($url);
465 * @param string $status
466 * @param null $isPaymentProcessor
467 * @param null $accessContribution
468 * @param bool $isCancelSupported
469 * @param bool $isUpdateBilling
471 * @return array (reference) of action links
474 static function &links($status = 'all',
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;
527 if ($isCancelSupported) {
528 $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.');
529 self
::$_links['all'][CRM_Core_Action
::DISABLE
] = array(
530 'name' => ts('Cancel Auto-renewal'),
531 'url' => 'civicrm/contribute/unsubscribe',
532 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
533 'title' => ts('Cancel Auto Renew Subscription'),
534 'extra' => 'onclick = "if (confirm(\'' . $cancelMessage . '\') ) { return true; else return false;}"',
537 elseif (isset(self
::$_links['all'][CRM_Core_Action
::DISABLE
])) {
538 unset(self
::$_links['all'][CRM_Core_Action
::DISABLE
]);
541 if ($isUpdateBilling) {
542 self
::$_links['all'][CRM_Core_Action
::MAP
] = array(
543 'name' => ts('Change Billing Details'),
544 'url' => 'civicrm/contribute/updatebilling',
545 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
546 'title' => ts('Change Billing Details'),
549 elseif (isset(self
::$_links['all'][CRM_Core_Action
::MAP
])) {
550 unset(self
::$_links['all'][CRM_Core_Action
::MAP
]);
552 return self
::$_links[$status];
556 * Function to define action links for membership types of related organization
558 * @return array self::$_membershipTypesLinks array of action links
561 static function &membershipTypesLinks() {
562 if (!self
::$_membershipTypesLinks) {
563 self
::$_membershipTypesLinks = array(
564 CRM_Core_Action
::VIEW
=> array(
565 'name' => ts('Members'),
566 'url' => 'civicrm/member/search/',
567 'qs' => 'reset=1&force=1&type=%%id%%',
568 'title' => ts('Search'),
570 CRM_Core_Action
::UPDATE
=> array(
571 'name' => ts('Edit'),
572 'url' => 'civicrm/admin/member/membershipType',
573 'qs' => 'action=update&id=%%id%%&reset=1',
574 'title' => ts('Edit Membership Type'),
578 return self
::$_membershipTypesLinks;
582 * This function is used for the to show the associated
583 * contribution for the membership
584 * @form array $form (ref.) an assoc array of name/value pairs
588 public static function associatedContribution($contactId = NULL, $membershipId = NULL) {
589 $controller = new CRM_Core_Controller_Simple(
590 'CRM_Contribute_Form_Search',
595 $controller->setEmbedded(TRUE);
596 $controller->reset();
597 $controller->set('force', 1);
598 $controller->set('cid', $contactId);
599 $controller->set('memberId', $membershipId);
600 $controller->set('context', 'contribution');
601 $controller->process();
608 * @return string Classname of BAO.
610 function getBAOName() {
611 return 'CRM_Member_BAO_Membership';