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