Merge pull request #21902 from eileenmcnaughton/tok
[civicrm-core.git] / CRM / Pledge / Page / Tab.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Pledge_Page_Tab extends CRM_Core_Page {
18 public $_permission = NULL;
19 public $_contactId = NULL;
20
21 /**
22 * called when action is browse.
23 */
24 public function browse() {
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');
30 $controller->set('limit', '25');
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);
37 $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('pledge', $this->_contactId);
38 // Refresh other tabs with related data
39 $this->ajaxResponse['updateTabs'] = [
40 '#tab_contribute' => CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId),
41 '#tab_activity' => CRM_Contact_BAO_Contact::getCountComponent('activity', $this->_contactId),
42 ];
43 }
44 }
45
46 /**
47 * called when action is view.
48 *
49 * @return null
50 */
51 public function view() {
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 /**
64 * called when action is update or new.
65 *
66 * @return null
67 */
68 public function edit() {
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
80 public function preProcess() {
81 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
82 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
83 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
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);
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 /**
106 * the main function that is called when the page loads, it decides the which action has to be taken for the page.
107 *
108 * @return null
109 */
110 public function run() {
111 $this->preProcess();
112
113 // check if we can process credit card registration
114 $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments());
115
116 self::setContext($this);
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
137 /**
138 * Get context.
139 *
140 * @param $form
141 */
142 public static function setContext(&$form) {
143 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $form, FALSE, 'search');
144
145 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $form);
146 // validate the qfKey
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',
172 "reset=1&force=1&cid={$form->_contactId}&selectedChild=pledge"
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',
182 "reset=1&force=1&cid={$form->_contactId}&selectedChild=activity"
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;
192 if ($form->_contactId) {
193 $cid = '&cid=' . $form->_contactId;
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 }
203
204 }