Merge pull request #15323 from elisseck/dev/core/1266
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / Delete.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
32 */
33
34 /**
35 * This class is to build the form for Deleting Group.
36 */
37 class CRM_Contribute_Form_ContributionPage_Delete extends CRM_Contribute_Form_ContributionPage {
38
39 /**
40 * Page title.
41 *
42 * @var string
43 */
44 protected $_title;
45
46 /**
47 * Check if there are any related contributions.
48 * @var bool
49 */
50 protected $_relatedContributions;
51
52 /**
53 * Set variables up before form is built.
54 */
55 public function preProcess() {
56 //Check if there are contributions related to Contribution Page
57
58 parent::preProcess();
59
60 //check for delete
61 if (!CRM_Core_Permission::checkActionPermission('CiviContribute', $this->_action)) {
62 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
63 }
64
65 $dao = new CRM_Contribute_DAO_Contribution();
66 $dao->contribution_page_id = $this->_id;
67
68 if ($dao->find(TRUE)) {
69 $this->_relatedContributions = TRUE;
70 $this->assign('relatedContributions', TRUE);
71 }
72 }
73
74 /**
75 * Build the form object.
76 */
77 public function buildQuickForm() {
78 $this->_title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'title');
79 $this->assign('title', $this->_title);
80
81 //if there are contributions related to Contribution Page
82 //then onle cancel button is displayed
83 $buttons = [];
84 if (!$this->_relatedContributions) {
85 $buttons[] = [
86 'type' => 'next',
87 'name' => ts('Delete Contribution Page'),
88 'isDefault' => TRUE,
89 ];
90 }
91
92 $buttons[] = [
93 'type' => 'cancel',
94 'name' => ts('Cancel'),
95 ];
96
97 $this->addButtons($buttons);
98 }
99
100 /**
101 * Process the form when submitted.
102 */
103 public function postProcess() {
104 $transaction = new CRM_Core_Transaction();
105
106 // first delete the join entries associated with this contribution page
107 $dao = new CRM_Core_DAO_UFJoin();
108
109 $params = [
110 'entity_table' => 'civicrm_contribution_page',
111 'entity_id' => $this->_id,
112 ];
113 $dao->copyValues($params);
114 $dao->delete();
115
116 //next delete the membership block fields
117 $dao = new CRM_Member_DAO_MembershipBlock();
118 $dao->entity_table = 'civicrm_contribution_page';
119 $dao->entity_id = $this->_id;
120 $dao->delete();
121
122 //next delete the pcp block fields
123 $dao = new CRM_PCP_DAO_PCPBlock();
124 $dao->entity_table = 'civicrm_contribution_page';
125 $dao->entity_id = $this->_id;
126 $dao->delete();
127
128 // need to delete premiums. CRM-4586
129 CRM_Contribute_BAO_Premium::deletePremium($this->_id);
130
131 // price set cleanup, CRM-5527
132 CRM_Price_BAO_PriceSet::removeFrom('civicrm_contribution_page', $this->_id);
133
134 // finally delete the contribution page
135 $dao = new CRM_Contribute_DAO_ContributionPage();
136 $dao->id = $this->_id;
137 $dao->delete();
138
139 $transaction->commit();
140
141 CRM_Core_Session::setStatus(ts("The contribution page '%1' has been deleted.", [1 => $this->_title]), ts('Deleted'), 'success');
142 }
143
144 }