Don't show deleted contacts CRM-10012
[civicrm-core.git] / CRM / Member / Page / Tab.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35class CRM_Member_Page_Tab extends CRM_Core_Page {
36
37 /**
38 * The action links that we need to display for the browse screen
39 *
40 * @var array
41 * @static
42 */
43 static $_links = NULL;
44 static $_membershipTypesLinks = NULL;
45
46 public $_permission = NULL;
47 public $_contactId = NULL;
48
49 /**
50 * This function is called when action is browse
51 *
52 * return null
53 * @access public
54 */
55 function browse() {
56 $links = self::links('all', $this->_isPaymentProcessor, $this->_accessContribution);
57
58 $membership = array();
59 $dao = new CRM_Member_DAO_Membership();
60 $dao->contact_id = $this->_contactId;
61 $dao->is_test = 0;
62 //$dao->orderBy('name');
63 $dao->find();
64
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;
69 }
70 if (CRM_Core_Permission::check('delete in CiviMember')) {
71 $permissions[] = CRM_Core_Permission::DELETE;
72 }
73 $mask = CRM_Core_Action::mask($permissions);
74
75 // get deceased status id
76 $allStatus = CRM_Member_PseudoConstant::membershipStatus();
77 $deceasedStatusId = array_search('Deceased', $allStatus);
78
79 //get all campaigns.
80 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
81
82 //checks membership of contact itself
83 while ($dao->fetch()) {
84 $membership[$dao->id] = array();
85 CRM_Core_DAO::storeValues($dao, $membership[$dao->id]);
86
87 //carry campaign.
88 $membership[$dao->id]['campaign'] = CRM_Utils_Array::value($dao->campaign_id, $allCampaigns);
89
90 //get the membership status and type values.
91 $statusANDType = CRM_Member_BAO_Membership::getStatusANDTypeValues($dao->id);
92 foreach (array(
93 'status', 'membership_type') as $fld) {
94 $membership[$dao->id][$fld] = CRM_Utils_Array::value($fld, $statusANDType[$dao->id]);
95 }
96 if (CRM_Utils_Array::value('is_current_member', $statusANDType[$dao->id])) {
97 $membership[$dao->id]['active'] = TRUE;
98 }
99 if (empty($dao->owner_membership_id)) {
100 // unset renew and followup link for deceased membership
101 $currentMask = $mask;
102 if ($dao->status_id == $deceasedStatusId) {
103 $currentMask = $currentMask & ~CRM_Core_Action::RENEW & ~CRM_Core_Action::FOLLOWUP;
104 }
105
106 $isUpdateBilling = false;
107 $paymentObject = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($membership[$dao->id]['membership_id'], 'membership', 'obj');
108 if (!empty($paymentObject)) {
109 $isUpdateBilling = $paymentObject->isSupported('updateSubscriptionBillingInfo');
110 }
111
112 $isCancelSupported = CRM_Member_BAO_Membership::isCancelSubscriptionSupported($membership[$dao->id]['membership_id']);
113
114 $membership[$dao->id]['action'] = CRM_Core_Action::formLink(self::links('all',
115 NULL,
116 NULL,
117 $isCancelSupported,
118 $isUpdateBilling
119 ),
120 $currentMask,
121 array(
122 'id' => $dao->id,
123 'cid' => $this->_contactId,
124 )
125 );
126 }
127 else {
128 $membership[$dao->id]['action'] = CRM_Core_Action::formLink(self::links('view'),
129 $mask,
130 array(
131 'id' => $dao->id,
132 'cid' => $this->_contactId,
133 )
134 );
135 }
136
137 //does membership have auto renew CRM-7137.
138 if (CRM_Utils_Array::value('contribution_recur_id', $membership[$dao->id]) &&
139 !CRM_Member_BAO_Membership::isSubscriptionCancelled($membership[$dao->id]['membership_id'])
140 ) {
141 $membership[$dao->id]['auto_renew'] = 1;
142 }
143 else {
144 $membership[$dao->id]['auto_renew'] = 0;
145 }
146
147 // if relevant, count related memberships
148 if (CRM_Utils_Array::value('is_current_member', $statusANDType[$dao->id]) // membership is active
149 && CRM_Utils_Array::value('relationship_type_id', $statusANDType[$dao->id]) // membership type allows inheritance
150 && empty($dao->owner_membership_id)) { // not an related membership
151 $query = "
152 SELECT COUNT(m.id)
153 FROM civicrm_membership m
154 LEFT JOIN civicrm_membership_status ms ON ms.id = m.status_id
86c838ac
CW
155 LEFT JOIN civicrm_contact ct ON ct.id = m.contact_id
156 WHERE m.owner_membership_id = {$dao->id} AND m.is_test = 0 AND ms.is_current_member = 1 AND ct.is_deleted = 0";
6a488035
TO
157 $num_related = CRM_Core_DAO::singleValueQuery($query);
158 $max_related = CRM_Utils_Array::value('max_related', $membership[$dao->id]);
159 $membership[$dao->id]['related_count'] = ($max_related == '' ?
160 ts('%1 created', array(1 => $num_related)) :
161 ts('%1 out of %2', array(1 => $num_related, 2 => $max_related))
162 );
163 } else {
164 $membership[$dao->id]['related_count'] = ts('N/A');
165 }
166 }
167
168 //Below code gives list of all Membership Types associated
169 //with an Organization(CRM-2016)
170 $membershipTypes = CRM_Member_BAO_MembershipType::getMembershipTypesByOrg($this->_contactId);
171 foreach ($membershipTypes as $key => $value) {
172 $membershipTypes[$key]['action'] = CRM_Core_Action::formLink(self::membershipTypeslinks(),
173 $mask,
174 array(
175 'id' => $value['id'],
176 'cid' => $this->_contactId,
177 )
178 );
179 }
180
181 $activeMembers = CRM_Member_BAO_Membership::activeMembers($membership);
182 $inActiveMembers = CRM_Member_BAO_Membership::activeMembers($membership, 'inactive');
183 $this->assign('activeMembers', $activeMembers);
184 $this->assign('inActiveMembers', $inActiveMembers);
185 $this->assign('membershipTypes', $membershipTypes);
186
187 if ($this->_contactId) {
188 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
189 $this->assign('displayName', $displayName);
190 }
191 }
192
193 /**
194 * This function is called when action is view
195 *
196 * return null
197 * @access public
198 */
199 function view() {
200 $controller = new CRM_Core_Controller_Simple(
201 'CRM_Member_Form_MembershipView',
202 ts('View Membership'),
203 $this->_action
204 );
205 $controller->setEmbedded(TRUE);
206 $controller->set('id', $this->_id);
207 $controller->set('cid', $this->_contactId);
208
209 return $controller->run();
210 }
211
212 /**
213 * This function is called when action is update or new
214 *
215 * return null
216 * @access public
217 */
218 function edit() {
219 // set https for offline cc transaction
220 $mode = CRM_Utils_Request::retrieve('mode', 'String', $this);
221 if ($mode == 'test' || $mode == 'live') {
222 CRM_Utils_System::redirectToSSL();
223 }
224
225 if ($this->_action != CRM_Core_Action::ADD) {
226 // get associated contributions only on edit/renew/delete
227 $this->associatedContribution();
228 }
229
230 if ($this->_action & CRM_Core_Action::RENEW) {
231 $path = 'CRM_Member_Form_MembershipRenewal';
232 $title = ts('Renew Membership');
233 }
234 else {
235 $path = 'CRM_Member_Form_Membership';
236 $title = ts('Create Membership');
237 }
238 $controller = new CRM_Core_Controller_Simple(
239 $path, $title, $this->_action
240 );
241 $controller->setEmbedded(TRUE);
242 $controller->set('BAOName', $this->getBAOName());
243 $controller->set('id', $this->_id);
244 $controller->set('cid', $this->_contactId);
245 return $controller->run();
246 }
247
248 function preProcess() {
249 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
250 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
251 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
252
253 if ($context == 'standalone') {
254 $this->_action = CRM_Core_Action::ADD;
255 }
256 else {
257 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
258 $this->assign('contactId', $this->_contactId);
259
260 // check logged in url permission
261 CRM_Contact_Page_View::checkUserPermission($this);
262
263 // set page title
264 CRM_Contact_Page_View::setTitle($this->_contactId);
265 }
266
267 $this->assign('action', $this->_action);
268
269 if ($this->_permission == CRM_Core_Permission::EDIT && !CRM_Core_Permission::check('edit memberships')) {
270 // demote to view since user does not have edit membership rights
271 $this->_permission = CRM_Core_Permission::VIEW;
272 $this->assign('permission', 'view');
273 }
274 }
275
276 /**
277 * 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.
278 *
279 * return null
280 * @access public
281 */
282 function run() {
283 $this->preProcess();
284
285 // check if we can process credit card membership
286 $newCredit = CRM_Core_Payment::allowBackofficeCreditCard($this);
287 if ($newCredit) {
288 $this->_isPaymentProcessor = TRUE;
289 }
290 else {
291 $this->_isPaymentProcessor = FALSE;
292 }
293
294 // Only show credit card membership signup if user has CiviContribute permission
295 if (CRM_Core_Permission::access('CiviContribute')) {
296 $this->_accessContribution = TRUE;
297 $this->assign('accessContribution', TRUE);
298 }
299 else {
300 $this->_accessContribution = FALSE;
301 $this->assign('accessContribution', FALSE);
302 }
303
304 if ($this->_action & CRM_Core_Action::VIEW) {
305 $this->view();
306 }
307 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE | CRM_Core_Action::RENEW)) {
308 $this->setContext();
309 $this->edit();
310 }
311 else {
312 $this->setContext();
313 $this->browse();
314 }
315
316 return parent::run();
317 }
318
319 function setContext($contactId = NULL) {
320 $context = CRM_Utils_Request::retrieve('context',
321 'String', $this, FALSE, 'search'
322 );
323
324 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
325 //validate the qfKey
326 if (!CRM_Utils_Rule::qfKey($qfKey)) {
327 $qfKey = NULL;
328 }
329
330 if (!$contactId) {
331 $contactId = $this->_contactId;
332 }
333
334 switch ($context) {
335 case 'dashboard':
336 $url = CRM_Utils_System::url('civicrm/member',
337 'reset=1'
338 );
339 break;
340
341 case 'membership':
342 $url = CRM_Utils_System::url('civicrm/contact/view',
343 "reset=1&force=1&cid={$contactId}&selectedChild=member"
344 );
345 break;
346
347 case 'search':
348 $urlParams = 'force=1';
349 if ($qfKey) {
350 $urlParams .= "&qfKey=$qfKey";
351 }
352 $this->assign('searchKey', $qfKey);
353
354 $url = CRM_Utils_System::url('civicrm/member/search', $urlParams);
355 break;
356
357 case 'home':
358 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
359 break;
360
361 case 'activity':
362 $url = CRM_Utils_System::url('civicrm/contact/view',
363 "reset=1&force=1&cid={$contactId}&selectedChild=activity"
364 );
365 break;
366
367 case 'standalone':
368 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
369 break;
370
371 case 'fulltext':
372 $action = CRM_Utils_Request::retrieve('action', 'String', $this);
373 $keyName = '&qfKey';
374 $urlParams = 'force=1';
375 $urlString = 'civicrm/contact/search/custom';
376 if ($action == CRM_Core_Action::UPDATE) {
377 if ($this->_contactId) {
378 $urlParams .= '&cid=' . $this->_contactId;
379 }
380 $keyName = '&key';
381 $urlParams .= '&context=fulltext&action=view';
382 $urlString = 'civicrm/contact/view/membership';
383 }
384 if ($qfKey) {
385 $urlParams .= "$keyName=$qfKey";
386 }
387 $this->assign('searchKey', $qfKey);
388 $url = CRM_Utils_System::url($urlString, $urlParams);
389 break;
390
391 default:
392 $cid = NULL;
393 if ($contactId) {
394 $cid = '&cid=' . $contactId;
395 }
396 $url = CRM_Utils_System::url('civicrm/member/search',
397 'force=1' . $cid
398 );
399 break;
400 }
401
402 $session = CRM_Core_Session::singleton();
403 $session->pushUserContext($url);
404 }
405
406 /**
407 * Get action links
408 *
409 * @return array (reference) of action links
410 * @static
411 */
412 static function &links($status = 'all',
413 $isPaymentProcessor = NULL,
414 $accessContribution = NULL,
415 $isCancelSupported = FALSE,
416 $isUpdateBilling = FALSE
417 ) {
418 if (!CRM_Utils_Array::value('view', self::$_links)) {
419 self::$_links['view'] = array(
420 CRM_Core_Action::VIEW => array(
421 'name' => ts('View'),
422 'url' => 'civicrm/contact/view/membership',
423 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
424 'title' => ts('View Membership'),
425 ),
426 );
427 }
428
429 if (!CRM_Utils_Array::value('all', self::$_links)) {
430 $extraLinks = array(
431 CRM_Core_Action::UPDATE => array(
432 'name' => ts('Edit'),
433 'url' => 'civicrm/contact/view/membership',
434 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
435 'title' => ts('Edit Membership'),
436 ),
437 CRM_Core_Action::RENEW => array(
438 'name' => ts('Renew'),
439 'url' => 'civicrm/contact/view/membership',
440 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
441 'title' => ts('Renew Membership'),
442 ),
443 CRM_Core_Action::FOLLOWUP => array(
444 'name' => ts('Renew-Credit Card'),
445 'url' => 'civicrm/contact/view/membership',
446 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member&mode=live',
447 'title' => ts('Renew Membership Using Credit Card'),
448 ),
449 CRM_Core_Action::DELETE => array(
450 'name' => ts('Delete'),
451 'url' => 'civicrm/contact/view/membership',
452 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
453 'title' => ts('Delete Membership'),
454 ),
455 );
456 if (!$isPaymentProcessor || !$accessContribution) {
457 //unset the renew with credit card when payment
458 //processor is not available or user is not permitted to create contributions
459 unset($extraLinks[CRM_Core_Action::FOLLOWUP]);
460 }
461 self::$_links['all'] = self::$_links['view'] + $extraLinks;
462 }
463
464
465 if ($isCancelSupported) {
466 $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.');
467 self::$_links['all'][CRM_Core_Action::DISABLE] = array(
468 'name' => ts('Cancel Auto-renewal'),
469 'url' => 'civicrm/contribute/unsubscribe',
470 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
471 'title' => ts('Cancel Auto Renew Subscription'),
472 'extra' => 'onclick = "if (confirm(\'' . $cancelMessage . '\') ) { return true; else return false;}"',
473 );
474 }
475 elseif (isset(self::$_links['all'][CRM_Core_Action::DISABLE])) {
476 unset(self::$_links['all'][CRM_Core_Action::DISABLE]);
477 }
478
479 if ($isUpdateBilling) {
480 self::$_links['all'][CRM_Core_Action::MAP] = array(
481 'name' => ts('Change Billing Details'),
482 'url' => 'civicrm/contribute/updatebilling',
483 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
484 'title' => ts('Change Billing Details'),
485 );
486 }
487 elseif (isset(self::$_links['all'][CRM_Core_Action::MAP])) {
488 unset(self::$_links['all'][CRM_Core_Action::MAP]);
489 }
490 return self::$_links[$status];
491 }
492
493 /**
494 * Function to define action links for membership types of related organization
495 *
496 * @return array self::$_membershipTypesLinks array of action links
497 * @access public
498 */
499 static function &membershipTypesLinks() {
500 if (!self::$_membershipTypesLinks) {
501 self::$_membershipTypesLinks = array(
502 CRM_Core_Action::VIEW => array(
503 'name' => ts('Members'),
504 'url' => 'civicrm/member/search/',
505 'qs' => 'reset=1&force=1&type=%%id%%',
506 'title' => ts('Search'),
507 ),
508 CRM_Core_Action::UPDATE => array(
509 'name' => ts('Edit'),
510 'url' => 'civicrm/admin/member/membershipType',
511 'qs' => 'action=update&id=%%id%%&reset=1',
512 'title' => ts('Edit Membership Type'),
513 ),
514 );
515 }
516 return self::$_membershipTypesLinks;
517 }
518
519 /**
520 * This function is used for the to show the associated
521 * contribution for the membership
522 * @form array $form (ref.) an assoc array of name/value pairs
523 * return null
524 * @access public
525 */
526 function associatedContribution($contactId = NULL, $membershipId = NULL) {
527 if (!$contactId) {
528 $contactId = $this->_contactId;
529 }
530
531 if (!$membershipId) {
532 $membershipId = $this->_id;
533 }
534
535 // retrieive membership contributions if the $membershipId is set
536 if (CRM_Core_Permission::access('CiviContribute') && $membershipId) {
537 $this->assign('accessContribution', TRUE);
538 $controller = new CRM_Core_Controller_Simple(
539 'CRM_Contribute_Form_Search',
540 ts('Contributions'),
541 NULL,
542 FALSE, FALSE, TRUE
543 );
544 $controller->setEmbedded(TRUE);
545 $controller->reset();
546 $controller->set('force', 1);
547 $controller->set('cid', $contactId);
548 $controller->set('memberId', $membershipId);
549 $controller->set('context', 'contribution');
550 $controller->process();
551 $controller->run();
552 }
553 else {
554 $this->assign('accessContribution', FALSE);
555 }
556 }
557
558 /**
559 * Get BAO Name
560 *
561 * @return string Classname of BAO.
562 */
563 function getBAOName() {
564 return 'CRM_Member_BAO_Membership';
565 }
566}
567