Merge pull request #2287 from pradpnayak/CRM-13848
[civicrm-core.git] / CRM / Member / Page / Tab.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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 */
35 class 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('status', 'membership_type') as $fld) {
93 $membership[$dao->id][$fld] = CRM_Utils_Array::value($fld, $statusANDType[$dao->id]);
94 }
95 if (CRM_Utils_Array::value('is_current_member', $statusANDType[$dao->id])) {
96 $membership[$dao->id]['active'] = TRUE;
97 }
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;
103 }
104
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');
110 }
111
112 $isCancelSupported = CRM_Member_BAO_Membership::isCancelSubscriptionSupported(
113 $membership[$dao->id]['membership_id']);
114
115 $membership[$dao->id]['action'] = CRM_Core_Action::formLink(self::links('all',
116 NULL,
117 NULL,
118 $isCancelSupported,
119 $isUpdateBilling
120 ),
121 $currentMask,
122 array(
123 'id' => $dao->id,
124 'cid' => $this->_contactId,
125 ),
126 ts('more'),
127 FALSE,
128 'membership.tab.row',
129 'Membership',
130 $dao->id
131 );
132 }
133 else {
134 $membership[$dao->id]['action'] = CRM_Core_Action::formLink(self::links('view'),
135 $mask,
136 array(
137 'id' => $dao->id,
138 'cid' => $this->_contactId,
139 ),
140 ts('more'),
141 FALSE,
142 'membership.tab.row',
143 'Membership',
144 $dao->id
145 );
146 }
147
148 //does membership have auto renew CRM-7137.
149 if (CRM_Utils_Array::value('contribution_recur_id', $membership[$dao->id]) &&
150 !CRM_Member_BAO_Membership::isSubscriptionCancelled($membership[$dao->id]['membership_id'])
151 ) {
152 $membership[$dao->id]['auto_renew'] = 1;
153 }
154 else {
155 $membership[$dao->id]['auto_renew'] = 0;
156 }
157
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
163 $query = "
164 SELECT COUNT(m.id)
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))
174 );
175 }
176 else {
177 $membership[$dao->id]['related_count'] = ts('N/A');
178 }
179 }
180
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(),
186 $mask,
187 array(
188 'id' => $value['id'],
189 'cid' => $this->_contactId,
190 ),
191 ts('more'),
192 FALSE,
193 'membershipType.organization.action',
194 'MembershipType',
195 $value['id']
196 );
197 }
198
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);
204
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 }
210 }
211
212 /**
213 * This function is called when action is view
214 *
215 * return null
216 * @access public
217 */
218 function view() {
219 $controller = new CRM_Core_Controller_Simple(
220 'CRM_Member_Form_MembershipView',
221 ts('View Membership'),
222 $this->_action
223 );
224 $controller->setEmbedded(TRUE);
225 $controller->set('id', $this->_id);
226 $controller->set('cid', $this->_contactId);
227
228 return $controller->run();
229 }
230
231 /**
232 * This function is called when action is update or new
233 *
234 * return null
235 * @access public
236 */
237 function edit() {
238 // set https for offline cc transaction
239 $mode = CRM_Utils_Request::retrieve('mode', 'String', $this);
240 if ($mode == 'test' || $mode == 'live') {
241 CRM_Utils_System::redirectToSSL();
242 }
243
244 // build associated contributions ( note: this is called to show associated contributions in edit mode )
245 if ($this->_action & CRM_Core_Action::UPDATE) {
246 $this->assign('accessContribution', FALSE);
247 if (CRM_Core_Permission::access('CiviContribute')) {
248 $this->assign('accessContribution', TRUE);
249 CRM_Member_Page_Tab::associatedContribution($this->_contactId, $this->_id);
250 }
251 }
252
253 if ($this->_action & CRM_Core_Action::RENEW) {
254 $path = 'CRM_Member_Form_MembershipRenewal';
255 $title = ts('Renew Membership');
256 }
257 else {
258 $path = 'CRM_Member_Form_Membership';
259 $title = ts('Create Membership');
260 }
261
262 $controller = new CRM_Core_Controller_Simple($path, $title, $this->_action);
263 $controller->setEmbedded(TRUE);
264 $controller->set('BAOName', $this->getBAOName());
265 $controller->set('id', $this->_id);
266 $controller->set('cid', $this->_contactId);
267 return $controller->run();
268 }
269
270 function preProcess() {
271 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
272 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
273 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
274
275 if ($context == 'standalone') {
276 $this->_action = CRM_Core_Action::ADD;
277 }
278 else {
279 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
280 $this->assign('contactId', $this->_contactId);
281
282 // check logged in url permission
283 CRM_Contact_Page_View::checkUserPermission($this);
284
285 // set page title
286 CRM_Contact_Page_View::setTitle($this->_contactId);
287 }
288
289 $this->assign('action', $this->_action);
290
291 if ($this->_permission == CRM_Core_Permission::EDIT && !CRM_Core_Permission::check('edit memberships')) {
292 // demote to view since user does not have edit membership rights
293 $this->_permission = CRM_Core_Permission::VIEW;
294 $this->assign('permission', 'view');
295 }
296 }
297
298 /**
299 * 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.
300 *
301 * return null
302 * @access public
303 */
304 function run() {
305 $this->preProcess();
306
307 // check if we can process credit card membership
308 $newCredit = CRM_Core_Payment::allowBackofficeCreditCard($this);
309 if ($newCredit) {
310 $this->_isPaymentProcessor = TRUE;
311 }
312 else {
313 $this->_isPaymentProcessor = FALSE;
314 }
315
316 // Only show credit card membership signup if user has CiviContribute permission
317 if (CRM_Core_Permission::access('CiviContribute')) {
318 $this->_accessContribution = TRUE;
319 $this->assign('accessContribution', TRUE);
320 }
321 else {
322 $this->_accessContribution = FALSE;
323 $this->assign('accessContribution', FALSE);
324 }
325
326 if ($this->_action & CRM_Core_Action::VIEW) {
327 $this->view();
328 }
329 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE
330 | CRM_Core_Action::RENEW)) {
331 self::setContext($this);
332 $this->edit();
333 }
334 else {
335 self::setContext($this);
336 $this->browse();
337 }
338
339 return parent::run();
340 }
341
342 public static function setContext(&$form, $contactId = NULL) {
343 $context = CRM_Utils_Request::retrieve('context', 'String', $form, FALSE, 'search' );
344
345 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $form);
346 //validate the qfKey
347 if (!CRM_Utils_Rule::qfKey($qfKey)) {
348 $qfKey = NULL;
349 }
350
351 if (!$contactId) {
352 $contactId = $form->_contactId;
353 }
354
355 switch ($context) {
356 case 'dashboard':
357 $url = CRM_Utils_System::url('civicrm/member', 'reset=1');
358 break;
359
360 case 'membership':
361 $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&force=1&cid={$contactId}&selectedChild=member");
362 break;
363
364 case 'search':
365 $urlParams = 'force=1';
366 if ($qfKey) {
367 $urlParams .= "&qfKey=$qfKey";
368 }
369 $form->assign('searchKey', $qfKey);
370
371 $url = CRM_Utils_System::url('civicrm/member/search', $urlParams);
372 break;
373
374 case 'home':
375 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
376 break;
377
378 case 'activity':
379 $url = CRM_Utils_System::url('civicrm/contact/view',
380 "reset=1&force=1&cid={$contactId}&selectedChild=activity"
381 );
382 break;
383
384 case 'standalone':
385 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
386 break;
387
388 case 'fulltext':
389 $action = CRM_Utils_Request::retrieve('action', 'String', $form);
390 $keyName = '&qfKey';
391 $urlParams = 'force=1';
392 $urlString = 'civicrm/contact/search/custom';
393 if ($action == CRM_Core_Action::UPDATE) {
394 if ($form->_contactId) {
395 $urlParams .= '&cid=' . $this->_contactId;
396 }
397 $keyName = '&key';
398 $urlParams .= '&context=fulltext&action=view';
399 $urlString = 'civicrm/contact/view/membership';
400 }
401 if ($qfKey) {
402 $urlParams .= "$keyName=$qfKey";
403 }
404 $form->assign('searchKey', $qfKey);
405 $url = CRM_Utils_System::url($urlString, $urlParams);
406 break;
407
408 default:
409 $cid = NULL;
410 if ($contactId) {
411 $cid = '&cid=' . $contactId;
412 }
413 $url = CRM_Utils_System::url('civicrm/member/search', 'force=1' . $cid);
414 break;
415 }
416
417 $session = CRM_Core_Session::singleton();
418 $session->pushUserContext($url);
419 }
420
421 /**
422 * Get action links
423 *
424 * @return array (reference) of action links
425 * @static
426 */
427 static function &links($status = 'all',
428 $isPaymentProcessor = NULL,
429 $accessContribution = NULL,
430 $isCancelSupported = FALSE,
431 $isUpdateBilling = FALSE
432 ) {
433 if (!CRM_Utils_Array::value('view', self::$_links)) {
434 self::$_links['view'] = array(
435 CRM_Core_Action::VIEW => array(
436 'name' => ts('View'),
437 'url' => 'civicrm/contact/view/membership',
438 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
439 'title' => ts('View Membership'),
440 ),
441 );
442 }
443
444 if (!CRM_Utils_Array::value('all', self::$_links)) {
445 $extraLinks = array(
446 CRM_Core_Action::UPDATE => array(
447 'name' => ts('Edit'),
448 'url' => 'civicrm/contact/view/membership',
449 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
450 'title' => ts('Edit Membership'),
451 ),
452 CRM_Core_Action::RENEW => array(
453 'name' => ts('Renew'),
454 'url' => 'civicrm/contact/view/membership',
455 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
456 'title' => ts('Renew Membership'),
457 ),
458 CRM_Core_Action::FOLLOWUP => array(
459 'name' => ts('Renew-Credit Card'),
460 'url' => 'civicrm/contact/view/membership',
461 'qs' => 'action=renew&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member&mode=live',
462 'title' => ts('Renew Membership Using Credit Card'),
463 ),
464 CRM_Core_Action::DELETE => array(
465 'name' => ts('Delete'),
466 'url' => 'civicrm/contact/view/membership',
467 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&context=membership&selectedChild=member',
468 'title' => ts('Delete Membership'),
469 ),
470 );
471 if (!$isPaymentProcessor || !$accessContribution) {
472 //unset the renew with credit card when payment
473 //processor is not available or user is not permitted to create contributions
474 unset($extraLinks[CRM_Core_Action::FOLLOWUP]);
475 }
476 self::$_links['all'] = self::$_links['view'] + $extraLinks;
477 }
478
479
480 if ($isCancelSupported) {
481 $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.');
482 self::$_links['all'][CRM_Core_Action::DISABLE] = array(
483 'name' => ts('Cancel Auto-renewal'),
484 'url' => 'civicrm/contribute/unsubscribe',
485 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
486 'title' => ts('Cancel Auto Renew Subscription'),
487 'extra' => 'onclick = "if (confirm(\'' . $cancelMessage . '\') ) { return true; else return false;}"',
488 );
489 }
490 elseif (isset(self::$_links['all'][CRM_Core_Action::DISABLE])) {
491 unset(self::$_links['all'][CRM_Core_Action::DISABLE]);
492 }
493
494 if ($isUpdateBilling) {
495 self::$_links['all'][CRM_Core_Action::MAP] = array(
496 'name' => ts('Change Billing Details'),
497 'url' => 'civicrm/contribute/updatebilling',
498 'qs' => 'reset=1&cid=%%cid%%&mid=%%id%%&context=membership&selectedChild=member',
499 'title' => ts('Change Billing Details'),
500 );
501 }
502 elseif (isset(self::$_links['all'][CRM_Core_Action::MAP])) {
503 unset(self::$_links['all'][CRM_Core_Action::MAP]);
504 }
505 return self::$_links[$status];
506 }
507
508 /**
509 * Function to define action links for membership types of related organization
510 *
511 * @return array self::$_membershipTypesLinks array of action links
512 * @access public
513 */
514 static function &membershipTypesLinks() {
515 if (!self::$_membershipTypesLinks) {
516 self::$_membershipTypesLinks = array(
517 CRM_Core_Action::VIEW => array(
518 'name' => ts('Members'),
519 'url' => 'civicrm/member/search/',
520 'qs' => 'reset=1&force=1&type=%%id%%',
521 'title' => ts('Search'),
522 ),
523 CRM_Core_Action::UPDATE => array(
524 'name' => ts('Edit'),
525 'url' => 'civicrm/admin/member/membershipType',
526 'qs' => 'action=update&id=%%id%%&reset=1',
527 'title' => ts('Edit Membership Type'),
528 ),
529 );
530 }
531 return self::$_membershipTypesLinks;
532 }
533
534 /**
535 * This function is used for the to show the associated
536 * contribution for the membership
537 * @form array $form (ref.) an assoc array of name/value pairs
538 * return null
539 * @access public
540 */
541 public static function associatedContribution($contactId = NULL, $membershipId = NULL) {
542 $controller = new CRM_Core_Controller_Simple(
543 'CRM_Contribute_Form_Search',
544 ts('Contributions'),
545 NULL,
546 FALSE, FALSE, TRUE
547 );
548 $controller->setEmbedded(TRUE);
549 $controller->reset();
550 $controller->set('force', 1);
551 $controller->set('cid', $contactId);
552 $controller->set('memberId', $membershipId);
553 $controller->set('context', 'contribution');
554 $controller->process();
555 $controller->run();
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