Fix button breakage on viewContribution
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / AddProduct.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
19 * form to process actions fo adding product to contribution page
20 */
21class CRM_Contribute_Form_ContributionPage_AddProduct extends CRM_Contribute_Form_ContributionPage {
22
fcc5922d 23 protected $_products;
6a488035 24
fcc5922d 25 protected $_pid;
6a488035
TO
26
27 /**
07f8d162 28 * Pre process the form.
6a488035
TO
29 */
30 public function preProcess() {
31 parent::preProcess();
32
33 $this->_products = CRM_Contribute_PseudoConstant::products($this->_id);
34 $this->_pid = CRM_Utils_Request::retrieve('pid', 'Positive',
35 $this, FALSE, 0
36 );
37
38 if ($this->_pid) {
39 $dao = new CRM_Contribute_DAO_PremiumsProduct();
40 $dao->id = $this->_pid;
41 $dao->find(TRUE);
42 $temp = CRM_Contribute_PseudoConstant::products();
43 $this->_products[$dao->product_id] = $temp[$dao->product_id];
44 }
45
46 //$this->_products = array_merge(array('' => '-- Select Product --') , $this->_products );
47 }
48
49 /**
95cdcc0f 50 * Set default values for the form.
6a488035 51 *
95cdcc0f 52 * Note that in edit/view mode the default values are retrieved from the database.
6a488035 53 */
00be9182 54 public function setDefaultValues() {
be2fb01f 55 $defaults = [];
6a488035
TO
56
57 if ($this->_pid) {
58 $dao = new CRM_Contribute_DAO_PremiumsProduct();
59 $dao->id = $this->_pid;
60 $dao->find(TRUE);
61 $defaults['product_id'] = $dao->product_id;
353ffa53 62 $defaults['financial_type_id'] = $dao->financial_type_id;
6a488035 63 $defaults['weight'] = $dao->weight;
0db6c3e1
TO
64 }
65 else {
6a488035
TO
66 $dao = new CRM_Contribute_DAO_Product();
67 $dao->id = key($this->_products);
68 $dao->find(TRUE);
69 $defaults['financial_type_id'] = $dao->financial_type_id;
70 }
71 if (!isset($defaults['weight']) || !($defaults['weight'])) {
72 $pageID = CRM_Utils_Request::retrieve('id', 'Positive',
73 $this, FALSE, 0
74 );
353ffa53 75 $dao = new CRM_Contribute_DAO_Premium();
6a488035 76 $dao->entity_table = 'civicrm_contribution_page';
353ffa53 77 $dao->entity_id = $pageID;
6a488035
TO
78 $dao->find(TRUE);
79 $premiumID = $dao->id;
80
353ffa53 81 $sql = 'SELECT max( weight ) as max_weight FROM civicrm_premiums_product WHERE premiums_id = %1';
be2fb01f 82 $params = [1 => [$premiumID, 'Integer']];
353ffa53 83 $dao = CRM_Core_DAO::executeQuery($sql, $params);
6a488035
TO
84 $dao->fetch();
85 $defaults['weight'] = $dao->max_weight + 1;
86 }
1330f57a 87 return $defaults;
6a488035
TO
88 }
89
90 /**
fe482240 91 * Build the form object.
6a488035
TO
92 */
93 public function buildQuickForm() {
94 $urlParams = 'civicrm/admin/contribute/premium';
95 if ($this->_action & CRM_Core_Action::DELETE) {
96 $session = CRM_Core_Session::singleton();
97 $url = CRM_Utils_System::url($urlParams, 'reset=1&action=update&id=' . $this->_id);
98 $session->pushUserContext($url);
99 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean',
353ffa53
TO
100 CRM_Core_DAO::$_nullObject, '', '', 'GET'
101 )
102 ) {
6a488035
TO
103 $dao = new CRM_Contribute_DAO_PremiumsProduct();
104 $dao->id = $this->_pid;
105 $dao->delete();
106 CRM_Core_Session::setStatus(ts('Selected Premium Product has been removed from this Contribution Page.'), ts('Saved'), 'success');
107 CRM_Utils_System::redirect($url);
108 }
109
be2fb01f 110 $this->addButtons([
1330f57a
SL
111 [
112 'type' => 'next',
113 'name' => ts('Delete'),
114 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;',
115 'isDefault' => TRUE,
116 ],
117 [
118 'type' => 'cancel',
119 'name' => ts('Cancel'),
120 ],
121 ]);
6a488035
TO
122 return;
123 }
124
125 if ($this->_action & CRM_Core_Action::PREVIEW) {
f93837a8
BT
126 $dao = new CRM_Contribute_DAO_PremiumsProduct();
127 $dao->id = $this->_pid;
128 $dao->find(TRUE);
129 $productID = $dao->product_id;
130
131 CRM_Contribute_BAO_Premium::buildPremiumPreviewBlock($this, $productID);
be2fb01f 132 $this->addButtons([
1330f57a
SL
133 [
134 'type' => 'next',
135 'name' => ts('Done with Preview'),
136 'isDefault' => TRUE,
137 ],
138 ]);
6a488035
TO
139 return;
140 }
141
142 $session = CRM_Core_Session::singleton();
143 $url = CRM_Utils_System::url($urlParams, 'reset=1&action=update&id=' . $this->_id);
144 $session->pushUserContext($url);
145
146 $this->add('select', 'product_id', ts('Select the Product') . ' ', $this->_products, TRUE);
665e5ec7 147
7ecddde4 148 $this->addElement('text', 'weight', ts('Order'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_PremiumsProduct', 'weight'));
665e5ec7 149
481a74f4 150 $financialType = CRM_Contribute_PseudoConstant::financialType();
be2fb01f 151 $premiumFinancialType = [];
6a488035
TO
152 CRM_Core_PseudoConstant::populate(
153 $premiumFinancialType,
154 'CRM_Financial_DAO_EntityFinancialAccount',
874c9be7 155 $all = TRUE,
665e5ec7 156 $retrieve = 'entity_id',
874c9be7 157 $filter = NULL,
665e5ec7 158 'account_relationship = 8'
6a488035 159 );
665e5ec7 160
be2fb01f 161 $costFinancialType = [];
6a488035
TO
162 CRM_Core_PseudoConstant::populate(
163 $costFinancialType,
164 'CRM_Financial_DAO_EntityFinancialAccount',
874c9be7 165 $all = TRUE,
665e5ec7 166 $retrieve = 'entity_id',
874c9be7 167 $filter = NULL,
665e5ec7 168 'account_relationship = 7'
6a488035
TO
169 );
170 $productFinancialType = array_intersect($costFinancialType, $premiumFinancialType);
481a74f4
TO
171 foreach ($financialType as $key => $financialTypeName) {
172 if (!in_array($key, $productFinancialType)) {
173 unset($financialType[$key]);
874c9be7 174 }
6a488035 175 }
84e489b7 176 // Check permissioned financial types
573fd305 177 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialType, CRM_Core_Action::ADD);
481a74f4
TO
178 if (count($financialType)) {
179 $this->assign('financialType', $financialType);
665e5ec7 180 }
6a488035 181 $this->add(
665e5ec7 182 'select',
183 'financial_type_id',
481a74f4 184 ts('Financial Type'),
be2fb01f 185 ['' => ts('- select -')] + $financialType
6a488035
TO
186 );
187 $this->addRule('weight', ts('Please enter integer value for weight'), 'integer');
188 $session->pushUserContext(CRM_Utils_System::url($urlParams, 'action=update&reset=1&id=' . $this->_id));
189
190 if ($this->_single) {
be2fb01f 191 $this->addButtons([
1330f57a
SL
192 [
193 'type' => 'next',
194 'name' => ts('Save'),
195 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;',
196 'isDefault' => TRUE,
197 ],
198 [
199 'type' => 'cancel',
200 'name' => ts('Cancel'),
201 ],
202 ]);
6a488035
TO
203 }
204 else {
205 parent::buildQuickForm();
206 }
207 }
208
209 /**
fe482240 210 * Process the form.
6a488035
TO
211 */
212 public function postProcess() {
213 // get the submitted form values.
214 $params = $this->controller->exportValues($this->_name);
215
216 $urlParams = 'civicrm/admin/contribute/premium';
217 if ($this->_action & CRM_Core_Action::PREVIEW) {
353ffa53 218 $url = CRM_Utils_System::url($urlParams, 'reset=1&action=update&id=' . $this->_id);
6a488035
TO
219 CRM_Utils_System::redirect($url);
220 return;
221 }
222
223 if ($this->_action & CRM_Core_Action::DELETE) {
353ffa53
TO
224 $url = CRM_Utils_System::url($urlParams, 'reset=1&action=update&id=' . $this->_id);
225 $dao = new CRM_Contribute_DAO_PremiumsProduct();
6a488035
TO
226 $dao->id = $this->_pid;
227 $dao->delete();
228 CRM_Core_Session::setStatus(ts('Selected Premium Product has been removed from this Contribution Page.'), ts('Saved'), 'success');
229 CRM_Utils_System::redirect($url);
230 }
231 else {
6a488035
TO
232 $url = CRM_Utils_System::url($urlParams, 'reset=1&action=update&id=' . $this->_id);
233 if ($this->_pid) {
234 $params['id'] = $this->_pid;
235 }
353ffa53 236 $dao = new CRM_Contribute_DAO_Premium();
6a488035 237 $dao->entity_table = 'civicrm_contribution_page';
353ffa53 238 $dao->entity_id = $this->_id;
6a488035
TO
239 $dao->find(TRUE);
240 $premiumID = $dao->id;
241 $params['premiums_id'] = $premiumID;
242
243 $oldWeight = NULL;
244 if ($this->_pid) {
245 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_PremiumsProduct', $this->_pid, 'weight', 'id');
246 }
665e5ec7 247
6a488035 248 // updateOtherWeights needs to filter on premiums_id
be2fb01f 249 $filter = ['premiums_id' => $params['premiums_id']];
6a488035
TO
250 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Contribute_DAO_PremiumsProduct', $oldWeight, $params['weight'], $filter);
251
252 $dao = new CRM_Contribute_DAO_PremiumsProduct();
253 $dao->copyValues($params);
254 $dao->save();
255 CRM_Utils_System::redirect($url);
256 }
257 }
258
259 /**
260 * Return a descriptive name for the page, used in wizard header
261 *
262 * @return string
6a488035
TO
263 */
264 public function getTitle() {
265 return ts('Add Premium to Contribution Page');
266 }
96025800 267
6a488035 268}