Metadata fix - phone_type_id, location_type_id, gender_id
[civicrm-core.git] / CRM / Contribute / 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_Contribute_Page_Tab extends CRM_Core_Page {
18
19 /**
20 * The permission we have on this contact
21 *
22 * @var string
23 */
24 public $_permission = NULL;
25
26 /**
27 * The contact ID for the contributions we are acting on
28 * @var int
29 */
30 public $_contactId = NULL;
31
32 /**
33 * The recurring contribution ID (if any)
34 * @var int
35 */
36 public $_crid = NULL;
37
38 /**
39 * This method returns the links that are given for recur search row.
40 * currently the links added for each row are:
41 * - View
42 * - Edit
43 * - Cancel
44 *
45 * @param bool $recurID
46 * @param string $context
47 *
48 * @return array
49 */
50 public static function recurLinks($recurID = FALSE, $context = 'contribution') {
51 $links = [
52 CRM_Core_Action::VIEW => [
53 'name' => ts('View'),
54 'title' => ts('View Recurring Payment'),
55 'url' => 'civicrm/contact/view/contributionrecur',
56 'qs' => "reset=1&id=%%crid%%&cid=%%cid%%&context={$context}",
57 ],
58 CRM_Core_Action::UPDATE => [
59 'name' => ts('Edit'),
60 'title' => ts('Edit Recurring Payment'),
61 'url' => 'civicrm/contribute/updaterecur',
62 'qs' => "reset=1&action=update&crid=%%crid%%&cid=%%cid%%&context={$context}",
63 ],
64 CRM_Core_Action::DISABLE => [
65 'name' => ts('Cancel'),
66 'title' => ts('Cancel'),
67 'ref' => 'crm-enable-disable',
68 ],
69 ];
70
71 if ($recurID) {
72 $paymentProcessorObj = CRM_Contribute_BAO_ContributionRecur::getPaymentProcessorObject($recurID);
73 if ($paymentProcessorObj) {
74 if ($paymentProcessorObj->supports('cancelRecurring')) {
75 unset($links[CRM_Core_Action::DISABLE]['extra'], $links[CRM_Core_Action::DISABLE]['ref']);
76 $links[CRM_Core_Action::DISABLE]['url'] = "civicrm/contribute/unsubscribe";
77 $links[CRM_Core_Action::DISABLE]['qs'] = "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}";
78 }
79
80 if ($paymentProcessorObj->supports('UpdateSubscriptionBillingInfo')) {
81 $links[CRM_Core_Action::RENEW] = [
82 'name' => ts('Change Billing Details'),
83 'title' => ts('Change Billing Details'),
84 'url' => 'civicrm/contribute/updatebilling',
85 'qs' => "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}",
86 ];
87 }
88
89 if (!$paymentProcessorObj->supports('ChangeSubscriptionAmount') && !$paymentProcessorObj->supports('EditRecurringContribution')) {
90 unset($links[CRM_Core_Action::UPDATE]);
91 }
92 }
93 else {
94 unset($links[CRM_Core_Action::DISABLE]);
95 unset($links[CRM_Core_Action::UPDATE]);
96 }
97 }
98
99 return $links;
100 }
101
102 /**
103 * called when action is browse.
104 *
105 */
106 public function browse() {
107 // add annual contribution
108 $annual = [];
109 list($annual['count'],
110 $annual['amount'],
111 $annual['avg']
112 ) = CRM_Contribute_BAO_Contribution::annual($this->_contactId);
113 $this->assign('annual', $annual);
114
115 $controller = new CRM_Core_Controller_Simple(
116 'CRM_Contribute_Form_Search',
117 ts('Contributions'),
118 $this->_action,
119 FALSE, FALSE, TRUE
120 );
121 $controller->setEmbedded(TRUE);
122 $controller->reset();
123 $controller->set('cid', $this->_contactId);
124 $controller->set('crid', $this->_crid);
125 $controller->set('context', 'contribution');
126 $controller->set('limit', 50);
127 $controller->process();
128 $controller->run();
129
130 // add recurring block
131 $this->addRecurringContributionsBlock();
132
133 // enable/disable soft credit records for test contribution
134 $isTest = 0;
135 if (CRM_Utils_Request::retrieve('isTest', 'Positive', $this)) {
136 $isTest = 1;
137 }
138 $this->assign('isTest', $isTest);
139
140 $softCreditList = CRM_Contribute_BAO_ContributionSoft::getSoftContributionList($this->_contactId, NULL, $isTest);
141
142 if (!empty($softCreditList)) {
143 $softCreditTotals = [];
144
145 list($softCreditTotals['count'],
146 $softCreditTotals['cancel']['count'],
147 $softCreditTotals['amount'],
148 $softCreditTotals['avg'],
149 // to get cancel amount
150 $softCreditTotals['cancel']['amount']
151 ) = CRM_Contribute_BAO_ContributionSoft::getSoftContributionTotals($this->_contactId, $isTest);
152
153 $this->assign('softCredit', TRUE);
154 $this->assign('softCreditRows', $softCreditList);
155 $this->assign('softCreditTotals', $softCreditTotals);
156 }
157
158 if ($this->_contactId) {
159 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
160 $this->assign('displayName', $displayName);
161 $tabCount = CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId);
162 $this->assign('tabCount', $tabCount);
163 $this->ajaxResponse['tabCount'] = $tabCount;
164 }
165 }
166
167 /**
168 * Get all the recurring contribution information and assign to the template
169 */
170 private function addRecurringContributionsBlock() {
171 list($activeContributions, $activeContributionsCount) = $this->getActiveRecurringContributions();
172 list($inactiveRecurringContributions, $inactiveContributionsCount) = $this->getInactiveRecurringContributions();
173
174 if (!empty($activeContributions) || !empty($inactiveRecurringContributions)) {
175 // assign vars to templates
176 $this->assign('action', $this->_action);
177 $this->assign('activeRecurRows', $activeContributions);
178 $this->assign('contributionRecurCount', $activeContributionsCount + $inactiveContributionsCount);
179 $this->assign('inactiveRecurRows', $inactiveRecurringContributions);
180 $this->assign('recur', TRUE);
181 }
182 }
183
184 /**
185 * Loads active recurring contributions for the current contact and formats
186 * them to be used on the form.
187 *
188 * @return array
189 */
190 private function getActiveRecurringContributions() {
191 try {
192 $contributionRecurResult = civicrm_api3('ContributionRecur', 'get', [
193 'contact_id' => $this->_contactId,
194 'contribution_status_id' => ['NOT IN' => CRM_Contribute_BAO_ContributionRecur::getInactiveStatuses()],
195 'options' => ['limit' => 0, 'sort' => 'is_test, start_date DESC'],
196 ]);
197 $recurContributions = $contributionRecurResult['values'] ?? NULL;
198 }
199 catch (Exception $e) {
200 $recurContributions = [];
201 }
202
203 return $this->buildRecurringContributionsArray($recurContributions);
204 }
205
206 /**
207 * Loads inactive recurring contributions for the current contact and formats
208 * them to be used on the form.
209 *
210 * @return array
211 */
212 private function getInactiveRecurringContributions() {
213 try {
214 $contributionRecurResult = civicrm_api3('ContributionRecur', 'get', [
215 'contact_id' => $this->_contactId,
216 'contribution_status_id' => ['IN' => CRM_Contribute_BAO_ContributionRecur::getInactiveStatuses()],
217 'options' => ['limit' => 0, 'sort' => 'is_test, start_date DESC'],
218 ]);
219 $recurContributions = $contributionRecurResult['values'] ?? NULL;
220 }
221 catch (Exception $e) {
222 $recurContributions = NULL;
223 }
224
225 return $this->buildRecurringContributionsArray($recurContributions);
226 }
227
228 /**
229 * @param $recurContributions
230 *
231 * @return array
232 */
233 private function buildRecurringContributionsArray($recurContributions) {
234 $liveRecurringContributionCount = 0;
235 foreach ($recurContributions as $recurId => $recurDetail) {
236 // Is recurring contribution active?
237 $recurContributions[$recurId]['is_active'] = !in_array(CRM_Contribute_PseudoConstant::contributionStatus($recurDetail['contribution_status_id'], 'name'), CRM_Contribute_BAO_ContributionRecur::getInactiveStatuses());
238 if ($recurContributions[$recurId]['is_active']) {
239 $actionMask = array_sum(array_keys(self::recurLinks($recurId)));
240 }
241 else {
242 $actionMask = CRM_Core_Action::mask([CRM_Core_Permission::VIEW]);
243 }
244
245 if (empty($recurDetail['is_test'])) {
246 $liveRecurringContributionCount++;
247 }
248
249 // Get the name of the payment processor
250 if (!empty($recurDetail['payment_processor_id'])) {
251 $recurContributions[$recurId]['payment_processor'] = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessorName($recurDetail['payment_processor_id']);
252 }
253 // Get the label for the contribution status
254 if (!empty($recurDetail['contribution_status_id'])) {
255 $recurContributions[$recurId]['contribution_status'] = CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_ContributionRecur', 'contribution_status_id', $recurDetail['contribution_status_id']);
256 }
257
258 $recurContributions[$recurId]['action'] = CRM_Core_Action::formLink(self::recurLinks($recurId), $actionMask,
259 [
260 'cid' => $this->_contactId,
261 'crid' => $recurId,
262 'cxt' => 'contribution',
263 ],
264 ts('more'),
265 FALSE,
266 'contribution.selector.recurring',
267 'Contribution',
268 $recurId
269 );
270 }
271
272 return [$recurContributions, $liveRecurringContributionCount];
273 }
274
275 /**
276 * called when action is view.
277 *
278 * @return mixed
279 */
280 public function view() {
281 $controller = new CRM_Core_Controller_Simple(
282 'CRM_Contribute_Form_ContributionView',
283 ts('View Contribution'),
284 $this->_action
285 );
286 $controller->setEmbedded(TRUE);
287 $controller->set('id', $this->_id);
288 $controller->set('cid', $this->_contactId);
289
290 return $controller->run();
291 }
292
293 /**
294 * called when action is update or new.
295 *
296 * @return mixed
297 * @throws \CRM_Core_Exception
298 * @throws \Exception
299 */
300 public function edit() {
301 // set https for offline cc transaction
302 $mode = CRM_Utils_Request::retrieve('mode', 'Alphanumeric', $this);
303 if ($mode == 'test' || $mode == 'live') {
304 CRM_Utils_System::redirectToSSL();
305 }
306
307 $controller = new CRM_Core_Controller_Simple(
308 'CRM_Contribute_Form_Contribution',
309 'Create Contribution',
310 $this->_action
311 );
312 $controller->setEmbedded(TRUE);
313 $controller->set('id', $this->_id);
314 $controller->set('cid', $this->_contactId);
315
316 return $controller->run();
317 }
318
319 /**
320 * @throws \CRM_Core_Exception
321 * @throws \CiviCRM_API3_Exception
322 */
323 public function preProcess() {
324 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
325 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
326 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
327
328 if ($context == 'standalone') {
329 $this->_action = CRM_Core_Action::ADD;
330 }
331 else {
332 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, empty($this->_id));
333 if (empty($this->_contactId)) {
334 $this->_contactId = civicrm_api3('contribution', 'getvalue', [
335 'id' => $this->_id,
336 'return' => 'contact_id',
337 ]);
338 }
339 $this->assign('contactId', $this->_contactId);
340
341 // check logged in url permission
342 CRM_Contact_Page_View::checkUserPermission($this);
343 }
344 $this->assign('action', $this->_action);
345
346 if ($this->_permission == CRM_Core_Permission::EDIT && !CRM_Core_Permission::check('edit contributions')) {
347 // demote to view since user does not have edit contrib rights
348 $this->_permission = CRM_Core_Permission::VIEW;
349 $this->assign('permission', 'view');
350 }
351 }
352
353 /**
354 * the main function that is called when the page
355 * loads, it decides the which action has to be taken for the page.
356 *
357 * @throws \CRM_Core_Exception
358 * @throws \CiviCRM_API3_Exception
359 */
360 public function run() {
361 $this->preProcess();
362
363 // check if we can process credit card contribs
364 $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments());
365
366 $this->setContext();
367
368 if ($this->_action & CRM_Core_Action::VIEW) {
369 $this->view();
370 }
371 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
372 $this->edit();
373 }
374 else {
375 $this->browse();
376 }
377
378 parent::run();
379 }
380
381 /**
382 * @throws \CRM_Core_Exception
383 */
384 public function setContext() {
385 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
386 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric',
387 $this, FALSE, 'search'
388 );
389 $compContext = CRM_Utils_Request::retrieve('compContext', 'String', $this);
390
391 $searchContext = CRM_Utils_Request::retrieve('searchContext', 'String', $this);
392
393 //swap the context.
394 if ($context == 'search' && $compContext) {
395 $context = $compContext;
396 }
397 else {
398 $compContext = NULL;
399 }
400
401 // make sure we dont get tricked with a bad key
402 // so check format
403 if (!CRM_Core_Key::valid($qfKey)) {
404 $qfKey = NULL;
405 }
406
407 switch ($context) {
408 case 'user':
409 $url = CRM_Utils_System::url('civicrm/user', 'reset=1');
410 break;
411
412 case 'dashboard':
413 $url = CRM_Utils_System::url('civicrm/contribute',
414 'reset=1'
415 );
416 break;
417
418 case 'pledgeDashboard':
419 $url = CRM_Utils_System::url('civicrm/pledge',
420 'reset=1'
421 );
422 break;
423
424 case 'contribution':
425 $url = CRM_Utils_System::url('civicrm/contact/view',
426 "reset=1&force=1&cid={$this->_contactId}&selectedChild=contribute"
427 );
428 break;
429
430 case 'search':
431 case 'advanced':
432 $extraParams = "force=1";
433 if ($qfKey) {
434 $extraParams .= "&qfKey=$qfKey";
435 }
436
437 $this->assign('searchKey', $qfKey);
438 if ($context == 'advanced') {
439 $url = CRM_Utils_System::url('civicrm/contact/search/advanced', $extraParams);
440 }
441 elseif ($searchContext) {
442 $url = CRM_Utils_System::url("civicrm/$searchContext/search", $extraParams);
443 }
444 else {
445 $url = CRM_Utils_System::url('civicrm/contribute/search', $extraParams);
446 }
447 break;
448
449 case 'home':
450 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
451 break;
452
453 case 'activity':
454 $url = CRM_Utils_System::url('civicrm/contact/view',
455 "reset=1&force=1&cid={$this->_contactId}&selectedChild=activity"
456 );
457 break;
458
459 case 'member':
460 case 'membership':
461 $componentId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
462 $componentAction = CRM_Utils_Request::retrieve('compAction', 'Integer', $this);
463
464 $context = 'membership';
465 $searchKey = NULL;
466 if ($compContext) {
467 $context = 'search';
468 if ($qfKey) {
469 $searchKey = "&key=$qfKey";
470 }
471 $compContext = "&compContext={$compContext}";
472 }
473 if ($componentAction & CRM_Core_Action::VIEW) {
474 $action = 'view';
475 }
476 else {
477 $action = 'update';
478 }
479 $url = CRM_Utils_System::url('civicrm/contact/view/membership',
480 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=member{$searchKey}{$compContext}"
481 );
482 break;
483
484 case 'participant':
485 $componentId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
486 $componentAction = CRM_Utils_Request::retrieve('compAction', 'Integer', $this);
487
488 $context = 'participant';
489 $searchKey = NULL;
490 if ($compContext) {
491 $context = 'search';
492 if ($qfKey) {
493 $searchKey = "&key=$qfKey";
494 }
495 $compContext = "&compContext={$compContext}";
496 }
497 if ($componentAction == CRM_Core_Action::VIEW) {
498 $action = 'view';
499 }
500 else {
501 $action = 'update';
502 }
503 $url = CRM_Utils_System::url('civicrm/contact/view/participant',
504 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=event{$searchKey}{$compContext}"
505 );
506 break;
507
508 case 'pledge':
509 $url = CRM_Utils_System::url('civicrm/contact/view',
510 "reset=1&force=1&cid={$this->_contactId}&selectedChild=pledge"
511 );
512 break;
513
514 case 'standalone':
515 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
516 break;
517
518 case 'fulltext':
519 $keyName = '&qfKey';
520 $urlParams = 'force=1';
521 $urlString = 'civicrm/contact/search/custom';
522 if ($this->_action == CRM_Core_Action::UPDATE) {
523 if ($this->_contactId) {
524 $urlParams .= '&cid=' . $this->_contactId;
525 }
526 $keyName = '&key';
527 $urlParams .= '&context=fulltext&action=view';
528 $urlString = 'civicrm/contact/view/contribution';
529 }
530 if ($qfKey) {
531 $urlParams .= "$keyName=$qfKey";
532 }
533 $this->assign('searchKey', $qfKey);
534 $url = CRM_Utils_System::url($urlString, $urlParams);
535 break;
536
537 default:
538 $cid = NULL;
539 if ($this->_contactId) {
540 $cid = '&cid=' . $this->_contactId;
541 }
542 $url = CRM_Utils_System::url('civicrm/contribute/search',
543 'reset=1&force=1' . $cid
544 );
545 break;
546 }
547
548 $session = CRM_Core_Session::singleton();
549 $session->pushUserContext($url);
550 }
551
552 }