Merge pull request #18084 from civicrm/5.28
[civicrm-core.git] / CRM / Contribute / Form / Task / Delete.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
18 /**
19 * This class provides the functionality to delete a group of contributions.
20 *
21 * This class provides functionality for the actual deletion.
22 */
23 class CRM_Contribute_Form_Task_Delete extends CRM_Contribute_Form_Task {
24
25 /**
26 * Are we operating in "single mode", i.e. deleting one
27 * specific contribution?
28 *
29 * @var bool
30 */
31 protected $_single = FALSE;
32
33 /**
34 * Build all the data structures needed to build the form.
35 */
36 public function preProcess() {
37 //check for delete
38 if (!CRM_Core_Permission::checkActionPermission('CiviContribute', CRM_Core_Action::DELETE)) {
39 CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
40 }
41 parent::preProcess();
42 }
43
44 /**
45 * Build the form object.
46 */
47 public function buildQuickForm() {
48 $count = 0;
49 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) {
50 foreach ($this->_contributionIds as $key => $id) {
51 $finTypeID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $id, 'financial_type_id');
52 if (!CRM_Core_Permission::check('delete contributions of type ' . CRM_Contribute_PseudoConstant::financialType($finTypeID))) {
53 unset($this->_contributionIds[$key]);
54 $count++;
55 }
56 // Now check for lineItems
57 if ($lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($id)) {
58 foreach ($lineItems as $items) {
59 if (!CRM_Core_Permission::check('delete contributions of type ' . CRM_Contribute_PseudoConstant::financialType($items['financial_type_id']))) {
60 unset($this->_contributionIds[$key]);
61 $count++;
62 break;
63 }
64 }
65 }
66 }
67 }
68 if ($count && empty($this->_contributionIds)) {
69 CRM_Core_Session::setStatus(ts('1 contribution could not be deleted.', ['plural' => '%count contributions could not be deleted.', 'count' => $count]), ts('Error'), 'error');
70 $this->addButtons([
71 [
72 'type' => 'back',
73 'name' => ts('Cancel'),
74 ],
75 ]);
76 }
77 elseif ($count && !empty($this->_contributionIds)) {
78 CRM_Core_Session::setStatus(ts('1 contribution will not be deleted.', ['plural' => '%count contributions will not be deleted.', 'count' => $count]), ts('Warning'), 'warning');
79 $this->addDefaultButtons(ts('Delete Contributions'), 'done');
80 }
81 else {
82 $this->addDefaultButtons(ts('Delete Contributions'), 'done');
83 }
84 }
85
86 /**
87 * Process the form after the input has been submitted and validated.
88 */
89 public function postProcess() {
90 $deleted = $failed = 0;
91 foreach ($this->_contributionIds as $contributionId) {
92 if (CRM_Contribute_BAO_Contribution::deleteContribution($contributionId)) {
93 $deleted++;
94 }
95 else {
96 $failed++;
97 }
98 }
99
100 if ($deleted) {
101 $msg = ts('%count contribution deleted.', ['plural' => '%count contributions deleted.', 'count' => $deleted]);
102 CRM_Core_Session::setStatus($msg, ts('Removed'), 'success');
103 }
104
105 if ($failed) {
106 CRM_Core_Session::setStatus(ts('1 could not be deleted.', ['plural' => '%count could not be deleted.', 'count' => $failed]), ts('Error'), 'error');
107 }
108 }
109
110 }