Merge pull request #18479 from eileenmcnaughton/just_pay
[civicrm-core.git] / CRM / Pledge / Page / Tab.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Pledge_Page_Tab extends CRM_Core_Page {
18 public $_permission = NULL;
19 public $_contactId = NULL;
20
21 /**
fe482240 22 * called when action is browse.
6a488035 23 */
00be9182 24 public function browse() {
6a488035
TO
25 $controller = new CRM_Core_Controller_Simple('CRM_Pledge_Form_Search', ts('Pledges'), $this->_action);
26 $controller->setEmbedded(TRUE);
27 $controller->reset();
28 $controller->set('cid', $this->_contactId);
29 $controller->set('context', 'pledge');
3bba4de0 30 $controller->set('limit', '25');
6a488035
TO
31 $controller->process();
32 $controller->run();
33
34 if ($this->_contactId) {
35 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
36 $this->assign('displayName', $displayName);
4e8065a9 37 $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('pledge', $this->_contactId);
3dcb6fea 38 // Refresh other tabs with related data
be2fb01f 39 $this->ajaxResponse['updateTabs'] = [
0faf81a7 40 '#tab_contribute' => CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId),
3dcb6fea 41 '#tab_activity' => CRM_Contact_BAO_Contact::getCountComponent('activity', $this->_contactId),
be2fb01f 42 ];
6a488035
TO
43 }
44 }
45
46 /**
fe482240 47 * called when action is view.
6a488035 48 *
76e7a76c 49 * @return null
6a488035 50 */
00be9182 51 public function view() {
6a488035
TO
52 $controller = new CRM_Core_Controller_Simple('CRM_Pledge_Form_PledgeView',
53 'View Pledge',
54 $this->_action
55 );
56 $controller->setEmbedded(TRUE);
57 $controller->set('id', $this->_id);
58 $controller->set('cid', $this->_contactId);
59
60 return $controller->run();
61 }
62
63 /**
fe482240 64 * called when action is update or new.
6a488035 65 *
76e7a76c 66 * @return null
6a488035 67 */
00be9182 68 public function edit() {
6a488035
TO
69 $controller = new CRM_Core_Controller_Simple('CRM_Pledge_Form_Pledge',
70 'Create Pledge',
71 $this->_action
72 );
73 $controller->setEmbedded(TRUE);
74 $controller->set('id', $this->_id);
75 $controller->set('cid', $this->_contactId);
76
77 return $controller->run();
78 }
79
00be9182 80 public function preProcess() {
edc80cda 81 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
6a488035 82 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
353ffa53 83 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
6a488035
TO
84
85 if ($context == 'standalone') {
86 $this->_action = CRM_Core_Action::ADD;
87 }
88 else {
89 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
90 $this->assign('contactId', $this->_contactId);
91
92 // check logged in url permission
93 CRM_Contact_Page_View::checkUserPermission($this);
6a488035
TO
94 }
95
96 $this->assign('action', $this->_action);
97
98 if ($this->_permission == CRM_Core_Permission::EDIT && !CRM_Core_Permission::check('edit pledges')) {
99 // demote to view since user does not have edit pledge rights
100 $this->_permission = CRM_Core_Permission::VIEW;
101 $this->assign('permission', 'view');
102 }
103 }
104
105 /**
dc195289 106 * the main function that is called when the page loads, it decides the which action has to be taken for the page.
6a488035 107 *
76e7a76c 108 * @return null
6a488035 109 */
00be9182 110 public function run() {
6a488035
TO
111 $this->preProcess();
112
113 // check if we can process credit card registration
9be1374d 114 $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments());
6a488035 115
341c6436 116 self::setContext($this);
6a488035
TO
117
118 if ($this->_action & CRM_Core_Action::VIEW) {
119 $this->view();
120 }
121 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
122 $this->edit();
123 }
124 elseif ($this->_action & CRM_Core_Action::DETACH) {
125 CRM_Pledge_BAO_Pledge::cancel($this->_id);
126 $session = CRM_Core_Session::singleton();
127 $session->setStatus(ts('Pledge has been Cancelled and all scheduled (not completed) payments have been cancelled.<br />'));
128 CRM_Utils_System::redirect($session->popUserContext());
129 }
130 else {
131 $this->browse();
132 }
133
134 return parent::run();
135 }
136
2e2605fe
EM
137 /**
138 * Get context.
139 *
140 * @param $form
141 */
96c00f73 142 public static function setContext(&$form) {
edc80cda 143 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $form, FALSE, 'search');
6a488035 144
341c6436 145 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $form);
cc28438b 146 // validate the qfKey
6a488035
TO
147 if (!CRM_Utils_Rule::qfKey($qfKey)) {
148 $qfKey = NULL;
149 }
150
151 switch ($context) {
152 case 'dashboard':
153 case 'pledgeDashboard':
154 $url = CRM_Utils_System::url('civicrm/pledge', 'reset=1');
155 break;
156
157 case 'search':
158 $urlParams = 'force=1';
159 if ($qfKey) {
160 $urlParams .= "&qfKey=$qfKey";
161 }
162
163 $url = CRM_Utils_System::url('civicrm/pledge/search', $urlParams);
164 break;
165
166 case 'user':
167 $url = CRM_Utils_System::url('civicrm/user', 'reset=1');
168 break;
169
170 case 'pledge':
171 $url = CRM_Utils_System::url('civicrm/contact/view',
96c00f73 172 "reset=1&force=1&cid={$form->_contactId}&selectedChild=pledge"
6a488035
TO
173 );
174 break;
175
176 case 'home':
177 $url = CRM_Utils_System::url('civicrm/dashboard', 'force=1');
178 break;
179
180 case 'activity':
181 $url = CRM_Utils_System::url('civicrm/contact/view',
96c00f73 182 "reset=1&force=1&cid={$form->_contactId}&selectedChild=activity"
6a488035
TO
183 );
184 break;
185
186 case 'standalone':
187 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
188 break;
189
190 default:
191 $cid = NULL;
341c6436 192 if ($form->_contactId) {
193 $cid = '&cid=' . $form->_contactId;
6a488035
TO
194 }
195 $url = CRM_Utils_System::url('civicrm/pledge/search',
196 'force=1' . $cid
197 );
198 break;
199 }
200 $session = CRM_Core_Session::singleton();
201 $session->pushUserContext($url);
202 }
96025800 203
6a488035 204}