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