Ian province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Member / Page / Tab.php
... / ...
CommitLineData
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 */
42 static $_links = NULL;
43 static $_membershipTypesLinks = NULL;
44
45 public $_permission = NULL;
46 public $_contactId = NULL;
47
48 /**
49 * called when action is browse.
50 */
51 public function browse() {
52 $links = self::links('all', $this->_isPaymentProcessor, $this->_accessContribution);
53
54 $membership = array();
55 $dao = new CRM_Member_DAO_Membership();
56 $dao->contact_id = $this->_contactId;
57 $dao->is_test = 0;
58 //$dao->orderBy('name');
59 $dao->find();
60
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;
65 }
66 if (CRM_Core_Permission::check('delete in CiviMember')) {
67 $permissions[] = CRM_Core_Permission::DELETE;
68 }
69 $mask = CRM_Core_Action::mask($permissions);
70
71 // get deceased status id
72 $allStatus = CRM_Member_PseudoConstant::membershipStatus();
73 $deceasedStatusId = array_search('Deceased', $allStatus);
74
75 //get all campaigns.
76 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
77
78 //checks membership of contact itself
79 while ($dao->fetch()) {
80 $membership[$dao->id] = array();
81 CRM_Core_DAO::storeValues($dao, $membership[$dao->id]);
82
83 //carry campaign.
84 $membership[$dao->id]['campaign'] = CRM_Utils_Array::value($dao->campaign_id, $allCampaigns);
85
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]);
90 }
91 if (!empty($statusANDType[$dao->id]['is_current_member'])) {
92 $membership[$dao->id]['active'] = TRUE;
93 }
94 if (empty($dao->owner_membership_id)) {
95 // unset renew and followup link for deceased membership
96 $currentMask = $mask;
97 if ($dao->status_id == $deceasedStatusId) {
98 $currentMask = $currentMask & ~CRM_Core_Action::RENEW & ~CRM_Core_Action::FOLLOWUP;
99 }
100
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');
110 }
111
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']);
116
117 $membership[$dao->id]['action'] = CRM_Core_Action::formLink(self::links('all',
118 NULL,
119 NULL,
120 $isCancelSupported,
121 $isUpdateBilling
122 ),
123 $currentMask,
124 array(
125 'id' => $dao->id,
126 'cid' => $this->_contactId,
127 ),
128 ts('more'),
129 FALSE,
130 'membership.tab.row',
131 'Membership',
132 $dao->id
133 );
134 }
135 else {
136 $membership[$dao->id]['action'] = CRM_Core_Action::formLink(self::links('view'),
137 $mask,
138 array(
139 'id' => $dao->id,
140 'cid' => $this->_contactId,
141 ),
142 ts('more'),
143 FALSE,
144 'membership.tab.row',
145 'Membership',
146 $dao->id
147 );
148 }
149
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'])
153 ) {
154 $membership[$dao->id]['auto_renew'] = 1;
155 }
156 else {
157 $membership[$dao->id]['auto_renew'] = 0;
158 }
159
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)
164 ) {
165 // not an related membership
166 $query = "
167 SELECT COUNT(m.id)
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(
175 1 => $num_related,
176 2 => $max_related,
177 ))
178 );
179 }
180 else {
181 $membership[$dao->id]['related_count'] = ts('N/A');
182 }
183 }
184
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(),
190 $mask,
191 array(
192 'id' => $value['id'],
193 'cid' => $this->_contactId,
194 ),
195 ts('more'),
196 FALSE,
197 'membershipType.organization.action',
198 'MembershipType',
199 $value['id']
200 );
201 }
202
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);
208
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),
217 );
218 if (CRM_Core_Permission::access('CiviContribute')) {
219 $this->ajaxResponse['updateTabs']['#tab_contribute'] = CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId);
220 }
221 }
222 }
223
224 /**
225 * called when action is view.
226 *
227 * @return null
228 */
229 public function view() {
230 $controller = new CRM_Core_Controller_Simple(
231 'CRM_Member_Form_MembershipView',
232 ts('View Membership'),
233 $this->_action
234 );
235 $controller->setEmbedded(TRUE);
236 $controller->set('id', $this->_id);
237 $controller->set('cid', $this->_contactId);
238
239 return $controller->run();
240 }
241
242 /**
243 * called when action is update or new.
244 *
245 * @return null
246 */
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();
252 }
253
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);
260
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);
268 }
269 }
270 }
271 }
272
273 if ($this->_action & CRM_Core_Action::RENEW) {
274 $path = 'CRM_Member_Form_MembershipRenewal';
275 $title = ts('Renew Membership');
276 }
277 else {
278 $path = 'CRM_Member_Form_Membership';
279 $title = ts('Create Membership');
280 }
281
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();
288 }
289
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);
294
295 if ($context == 'standalone') {
296 $this->_action = CRM_Core_Action::ADD;
297 }
298 else {
299 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
300 $this->assign('contactId', $this->_contactId);
301
302 // check logged in url permission
303 CRM_Contact_Page_View::checkUserPermission($this);
304 }
305
306 $this->assign('action', $this->_action);
307
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');
312 }
313 }
314
315 /**
316 * the main function that is called when the page loads, it decides the which action has to be taken for the page.
317 *
318 * @return null
319 */
320 public function run() {
321 $this->preProcess();
322
323 // check if we can process credit card membership
324 $newCredit = CRM_Core_Config::isEnabledBackOfficeCreditCardPayments();
325 $this->assign('newCredit', $newCredit);
326
327 if ($newCredit) {
328 $this->_isPaymentProcessor = TRUE;
329 }
330 else {
331 $this->_isPaymentProcessor = FALSE;
332 }
333
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);
338
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);
346 }
347 }
348 }
349 else {
350 $this->_accessContribution = FALSE;
351 $this->assign('accessContribution', FALSE);
352 $this->assign('softCredit', FALSE);
353 }
354
355 if ($this->_action & CRM_Core_Action::VIEW) {
356 $this->view();
357 }
358 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE | CRM_Core_Action::RENEW)) {
359 self::setContext($this);
360 $this->edit();
361 }
362 else {
363 self::setContext($this);
364 $this->browse();
365 }
366
367 return parent::run();
368 }
369
370 /**
371 * @param CRM_Core_Form $form
372 * @param int $contactId
373 */
374 public static function setContext(&$form, $contactId = NULL) {
375 $context = CRM_Utils_Request::retrieve('context', 'String', $form, FALSE, 'search');
376
377 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $form);
378
379 $searchContext = CRM_Utils_Request::retrieve('searchContext', 'String', $form);
380
381 //validate the qfKey
382 if (!CRM_Utils_Rule::qfKey($qfKey)) {
383 $qfKey = NULL;
384 }
385
386 if (!$contactId) {
387 $contactId = $form->_contactId;
388 }
389
390 switch ($context) {
391 case 'dashboard':
392 $url = CRM_Utils_System::url('civicrm/member', 'reset=1');
393 break;
394
395 case 'membership':
396 $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&force=1&cid={$contactId}&selectedChild=member");
397 break;
398
399 case 'search':
400 $urlParams = 'force=1';
401 if ($qfKey) {
402 $urlParams .= "&qfKey=$qfKey";
403 }
404 $form->assign('searchKey', $qfKey);
405
406 if ($searchContext) {
407 $url = CRM_Utils_System::url("civicrm/$searchContext/search", $urlParams);
408 }
409 else {
410 $url = CRM_Utils_System::url('civicrm/member/search', $urlParams);
411 }
412 break;
413
414 case 'home':
415 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
416 break;
417
418 case 'activity':
419 $url = CRM_Utils_System::url('civicrm/contact/view',
420 "reset=1&force=1&cid={$contactId}&selectedChild=activity"
421 );
422 break;
423
424 case 'standalone':
425 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
426 break;
427
428 case 'fulltext':
429 $action = CRM_Utils_Request::retrieve('action', 'String', $form);
430 $keyName = '&qfKey';
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;
436 }
437 $keyName = '&key';
438 $urlParams .= '&context=fulltext&action=view';
439 $urlString = 'civicrm/contact/view/membership';
440 }
441 if ($qfKey) {
442 $urlParams .= "$keyName=$qfKey";
443 }
444 $form->assign('searchKey', $qfKey);
445 $url = CRM_Utils_System::url($urlString, $urlParams);
446 break;
447
448 default:
449 $cid = NULL;
450 if ($contactId) {
451 $cid = '&cid=' . $contactId;
452 }
453 $url = CRM_Utils_System::url('civicrm/member/search', 'force=1' . $cid);
454 break;
455 }
456
457 $session = CRM_Core_Session::singleton();
458 $session->pushUserContext($url);
459 }
460
461 /**
462 * Get action links.
463 *
464 * @param string $status
465 * @param null $isPaymentProcessor
466 * @param null $accessContribution
467 * @param bool $isCancelSupported
468 * @param bool $isUpdateBilling
469 *
470 * @return array
471 * (reference) of action links
472 */
473 public static function &links(
474 $status = 'all',
475 $isPaymentProcessor = NULL,
476 $accessContribution = NULL,
477 $isCancelSupported = FALSE,
478 $isUpdateBilling = FALSE
479 ) {
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'),
487 ),
488 );
489 }
490
491 if (!CRM_Utils_Array::value('all', self::$_links)) {
492 $extraLinks = array(
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'),
498 ),
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'),
504 ),
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'),
510 ),
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'),
516 ),
517 );
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]);
522 }
523 self::$_links['all'] = self::$_links['view'] + $extraLinks;
524 }
525
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;}"',
534 );
535 }
536 elseif (isset(self::$_links['all'][CRM_Core_Action::DISABLE])) {
537 unset(self::$_links['all'][CRM_Core_Action::DISABLE]);
538 }
539
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'),
546 );
547 }
548 elseif (isset(self::$_links['all'][CRM_Core_Action::MAP])) {
549 unset(self::$_links['all'][CRM_Core_Action::MAP]);
550 }
551 return self::$_links[$status];
552 }
553
554 /**
555 * Define action links for membership types of related organization.
556 *
557 * @return array
558 * self::$_membershipTypesLinks array of action links
559 */
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'),
568 ),
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'),
574 ),
575 );
576 }
577 return self::$_membershipTypesLinks;
578 }
579
580 /**
581 * used for the to show the associated.
582 * contribution for the membership
583 *
584 * @param int $contactId
585 * @param int $membershipId
586 */
587 public static function associatedContribution($contactId = NULL, $membershipId = NULL) {
588 $controller = new CRM_Core_Controller_Simple(
589 'CRM_Contribute_Form_Search',
590 ts('Contributions'),
591 NULL,
592 FALSE, FALSE, TRUE
593 );
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();
601 $controller->run();
602 }
603
604 /**
605 * Get BAO Name.
606 *
607 * @return string
608 * Classname of BAO.
609 */
610 public function getBAOName() {
611 return 'CRM_Member_BAO_Membership';
612 }
613
614}