Merge pull request #15902 from eileenmcnaughton/transaction_sillyness
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / AddProduct.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 * form to process actions fo adding product to contribution page
20 */
21 class CRM_Contribute_Form_ContributionPage_AddProduct extends CRM_Contribute_Form_ContributionPage {
22
23 protected $_products;
24
25 protected $_pid;
26
27 /**
28 * Pre process the form.
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 /**
50 * Set default values for the form.
51 *
52 * Note that in edit/view mode the default values are retrieved from the database.
53 */
54 public function setDefaultValues() {
55 $defaults = [];
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;
62 $defaults['financial_type_id'] = $dao->financial_type_id;
63 $defaults['weight'] = $dao->weight;
64 }
65 else {
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 );
75 $dao = new CRM_Contribute_DAO_Premium();
76 $dao->entity_table = 'civicrm_contribution_page';
77 $dao->entity_id = $pageID;
78 $dao->find(TRUE);
79 $premiumID = $dao->id;
80
81 $sql = 'SELECT max( weight ) as max_weight FROM civicrm_premiums_product WHERE premiums_id = %1';
82 $params = [1 => [$premiumID, 'Integer']];
83 $dao = CRM_Core_DAO::executeQuery($sql, $params);
84 $dao->fetch();
85 $defaults['weight'] = $dao->max_weight + 1;
86 }
87 return $defaults;
88 }
89
90 /**
91 * Build the form object.
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',
100 CRM_Core_DAO::$_nullObject, '', '', 'GET'
101 )
102 ) {
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
110 $this->addButtons([
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 ]);
122 return;
123 }
124
125 if ($this->_action & CRM_Core_Action::PREVIEW) {
126 CRM_Contribute_BAO_Premium::buildPremiumPreviewBlock($this, NULL, $this->_pid);
127 $this->addButtons([
128 [
129 'type' => 'next',
130 'name' => ts('Done with Preview'),
131 'isDefault' => TRUE,
132 ],
133 ]);
134 return;
135 }
136
137 $session = CRM_Core_Session::singleton();
138 $url = CRM_Utils_System::url($urlParams, 'reset=1&action=update&id=' . $this->_id);
139 $session->pushUserContext($url);
140
141 $this->add('select', 'product_id', ts('Select the Product') . ' ', $this->_products, TRUE);
142
143 $this->addElement('text', 'weight', ts('Order'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_PremiumsProduct', 'weight'));
144
145 $financialType = CRM_Contribute_PseudoConstant::financialType();
146 $premiumFinancialType = [];
147 CRM_Core_PseudoConstant::populate(
148 $premiumFinancialType,
149 'CRM_Financial_DAO_EntityFinancialAccount',
150 $all = TRUE,
151 $retrieve = 'entity_id',
152 $filter = NULL,
153 'account_relationship = 8'
154 );
155
156 $costFinancialType = [];
157 CRM_Core_PseudoConstant::populate(
158 $costFinancialType,
159 'CRM_Financial_DAO_EntityFinancialAccount',
160 $all = TRUE,
161 $retrieve = 'entity_id',
162 $filter = NULL,
163 'account_relationship = 7'
164 );
165 $productFinancialType = array_intersect($costFinancialType, $premiumFinancialType);
166 foreach ($financialType as $key => $financialTypeName) {
167 if (!in_array($key, $productFinancialType)) {
168 unset($financialType[$key]);
169 }
170 }
171 // Check permissioned financial types
172 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialType, CRM_Core_Action::ADD);
173 if (count($financialType)) {
174 $this->assign('financialType', $financialType);
175 }
176 $this->add(
177 'select',
178 'financial_type_id',
179 ts('Financial Type'),
180 ['' => ts('- select -')] + $financialType
181 );
182 $this->addRule('weight', ts('Please enter integer value for weight'), 'integer');
183 $session->pushUserContext(CRM_Utils_System::url($urlParams, 'action=update&reset=1&id=' . $this->_id));
184
185 if ($this->_single) {
186 $this->addButtons([
187 [
188 'type' => 'next',
189 'name' => ts('Save'),
190 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;',
191 'isDefault' => TRUE,
192 ],
193 [
194 'type' => 'cancel',
195 'name' => ts('Cancel'),
196 ],
197 ]);
198 }
199 else {
200 parent::buildQuickForm();
201 }
202 }
203
204 /**
205 * Process the form.
206 */
207 public function postProcess() {
208 // get the submitted form values.
209 $params = $this->controller->exportValues($this->_name);
210
211 $urlParams = 'civicrm/admin/contribute/premium';
212 if ($this->_action & CRM_Core_Action::PREVIEW) {
213 $url = CRM_Utils_System::url($urlParams, 'reset=1&action=update&id=' . $this->_id);
214 CRM_Utils_System::redirect($url);
215 return;
216 }
217
218 if ($this->_action & CRM_Core_Action::DELETE) {
219 $url = CRM_Utils_System::url($urlParams, 'reset=1&action=update&id=' . $this->_id);
220 $dao = new CRM_Contribute_DAO_PremiumsProduct();
221 $dao->id = $this->_pid;
222 $dao->delete();
223 CRM_Core_Session::setStatus(ts('Selected Premium Product has been removed from this Contribution Page.'), ts('Saved'), 'success');
224 CRM_Utils_System::redirect($url);
225 }
226 else {
227 $url = CRM_Utils_System::url($urlParams, 'reset=1&action=update&id=' . $this->_id);
228 if ($this->_pid) {
229 $params['id'] = $this->_pid;
230 }
231 $dao = new CRM_Contribute_DAO_Premium();
232 $dao->entity_table = 'civicrm_contribution_page';
233 $dao->entity_id = $this->_id;
234 $dao->find(TRUE);
235 $premiumID = $dao->id;
236 $params['premiums_id'] = $premiumID;
237
238 $oldWeight = NULL;
239 if ($this->_pid) {
240 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_PremiumsProduct', $this->_pid, 'weight', 'id');
241 }
242
243 // updateOtherWeights needs to filter on premiums_id
244 $filter = ['premiums_id' => $params['premiums_id']];
245 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Contribute_DAO_PremiumsProduct', $oldWeight, $params['weight'], $filter);
246
247 $dao = new CRM_Contribute_DAO_PremiumsProduct();
248 $dao->copyValues($params);
249 $dao->save();
250 CRM_Utils_System::redirect($url);
251 }
252 }
253
254 /**
255 * Return a descriptive name for the page, used in wizard header
256 *
257 * @return string
258 */
259 public function getTitle() {
260 return ts('Add Premium to Contribution Page');
261 }
262
263 }