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