minor phpcs updates
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / AddProduct.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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
39 protected $_products;
40
41 protected $_pid;
42
43 /**
44 * Pre process the form.
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 /**
66 * Set default values for the form. Note that in edit/view mode
67 * the default values are retrieved from the database
68 *
69 *
70 * @return void
71 */
72 public function setDefaultValues() {
73 $defaults = array();
74
75 if ($this->_pid) {
76 $dao = new CRM_Contribute_DAO_PremiumsProduct();
77 $dao->id = $this->_pid;
78 $dao->find(TRUE);
79 $defaults['product_id'] = $dao->product_id;
80 $defaults['financial_type_id'] = $dao->financial_type_id;
81 $defaults['weight'] = $dao->weight;
82 }
83 else {
84 $dao = new CRM_Contribute_DAO_Product();
85 $dao->id = key($this->_products);
86 $dao->find(TRUE);
87 $defaults['financial_type_id'] = $dao->financial_type_id;
88 }
89 if (!isset($defaults['weight']) || !($defaults['weight'])) {
90 $pageID = CRM_Utils_Request::retrieve('id', 'Positive',
91 $this, FALSE, 0
92 );
93 $dao = new CRM_Contribute_DAO_Premium();
94 $dao->entity_table = 'civicrm_contribution_page';
95 $dao->entity_id = $pageID;
96 $dao->find(TRUE);
97 $premiumID = $dao->id;
98
99 $sql = 'SELECT max( weight ) as max_weight FROM civicrm_premiums_product WHERE premiums_id = %1';
100 $params = array(1 => array($premiumID, 'Integer'));
101 $dao = CRM_Core_DAO::executeQuery($sql, $params);
102 $dao->fetch();
103 $defaults['weight'] = $dao->max_weight + 1;
104 }
105 RETURN $defaults;
106 }
107
108 /**
109 * Build the form object.
110 *
111 * @return void
112 */
113 public function buildQuickForm() {
114 $urlParams = 'civicrm/admin/contribute/premium';
115 if ($this->_action & CRM_Core_Action::DELETE) {
116 $session = CRM_Core_Session::singleton();
117 $url = CRM_Utils_System::url($urlParams, 'reset=1&action=update&id=' . $this->_id);
118 $session->pushUserContext($url);
119 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean',
120 CRM_Core_DAO::$_nullObject, '', '', 'GET'
121 )
122 ) {
123 $dao = new CRM_Contribute_DAO_PremiumsProduct();
124 $dao->id = $this->_pid;
125 $dao->delete();
126 CRM_Core_Session::setStatus(ts('Selected Premium Product has been removed from this Contribution Page.'), ts('Saved'), 'success');
127 CRM_Utils_System::redirect($url);
128 }
129
130 $this->addButtons(array(
131 array(
132 'type' => 'next',
133 'name' => ts('Delete'),
134 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;',
135 'isDefault' => TRUE,
136 ),
137 array(
138 'type' => 'cancel',
139 'name' => ts('Cancel'),
140 ),
141 )
142 );
143 return;
144 }
145
146 if ($this->_action & CRM_Core_Action::PREVIEW) {
147 CRM_Contribute_BAO_Premium::buildPremiumPreviewBlock($this, NULL, $this->_pid);
148 $this->addButtons(array(
149 array(
150 'type' => 'next',
151 'name' => ts('Done with Preview'),
152 'isDefault' => TRUE,
153 ),
154 )
155 );
156 return;
157 }
158
159 $session = CRM_Core_Session::singleton();
160 $url = CRM_Utils_System::url($urlParams, 'reset=1&action=update&id=' . $this->_id);
161 $session->pushUserContext($url);
162
163 $this->add('select', 'product_id', ts('Select the Product') . ' ', $this->_products, TRUE);
164
165 $this->addElement('text', 'weight', ts('Order'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_PremiumsProduct', 'weight'));
166
167 $financialType = CRM_Contribute_PseudoConstant::financialType();
168 $premiumFinancialType = array();
169 CRM_Core_PseudoConstant::populate(
170 $premiumFinancialType,
171 'CRM_Financial_DAO_EntityFinancialAccount',
172 $all = TRUE,
173 $retrieve = 'entity_id',
174 $filter = NULL,
175 'account_relationship = 8'
176 );
177
178 $costFinancialType = array();
179 CRM_Core_PseudoConstant::populate(
180 $costFinancialType,
181 'CRM_Financial_DAO_EntityFinancialAccount',
182 $all = TRUE,
183 $retrieve = 'entity_id',
184 $filter = NULL,
185 'account_relationship = 7'
186 );
187 $productFinancialType = array_intersect($costFinancialType, $premiumFinancialType);
188 foreach ($financialType as $key => $financialTypeName) {
189 if (!in_array($key, $productFinancialType)) {
190 unset($financialType[$key]);
191 }
192 }
193 if (count($financialType)) {
194 $this->assign('financialType', $financialType);
195 }
196 $this->add(
197 'select',
198 'financial_type_id',
199 ts('Financial Type'),
200 array('' => ts('- select -')) + $financialType
201 );
202 $this->addRule('weight', ts('Please enter integer value for weight'), 'integer');
203 $session->pushUserContext(CRM_Utils_System::url($urlParams, 'action=update&reset=1&id=' . $this->_id));
204
205 if ($this->_single) {
206 $this->addButtons(array(
207 array(
208 'type' => 'next',
209 'name' => ts('Save'),
210 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;',
211 'isDefault' => TRUE,
212 ),
213 array(
214 'type' => 'cancel',
215 'name' => ts('Cancel'),
216 ),
217 )
218 );
219 }
220 else {
221 parent::buildQuickForm();
222 }
223 }
224
225 /**
226 * Process the form.
227 *
228 * @return void
229 */
230 public function postProcess() {
231 // get the submitted form values.
232 $params = $this->controller->exportValues($this->_name);
233
234 $urlParams = 'civicrm/admin/contribute/premium';
235 if ($this->_action & CRM_Core_Action::PREVIEW) {
236 $session = CRM_Core_Session::singleton();
237 $url = CRM_Utils_System::url($urlParams, 'reset=1&action=update&id=' . $this->_id);
238 $single = $session->get('singleForm');
239 CRM_Utils_System::redirect($url);
240 return;
241 }
242
243 if ($this->_action & CRM_Core_Action::DELETE) {
244 $session = CRM_Core_Session::singleton();
245 $url = CRM_Utils_System::url($urlParams, 'reset=1&action=update&id=' . $this->_id);
246 $dao = new CRM_Contribute_DAO_PremiumsProduct();
247 $dao->id = $this->_pid;
248 $dao->delete();
249 CRM_Core_Session::setStatus(ts('Selected Premium Product has been removed from this Contribution Page.'), ts('Saved'), 'success');
250 CRM_Utils_System::redirect($url);
251 }
252 else {
253 $session = CRM_Core_Session::singleton();
254 $url = CRM_Utils_System::url($urlParams, 'reset=1&action=update&id=' . $this->_id);
255 if ($this->_pid) {
256 $params['id'] = $this->_pid;
257 }
258 $dao = new CRM_Contribute_DAO_Premium();
259 $dao->entity_table = 'civicrm_contribution_page';
260 $dao->entity_id = $this->_id;
261 $dao->find(TRUE);
262 $premiumID = $dao->id;
263 $params['premiums_id'] = $premiumID;
264
265 $oldWeight = NULL;
266 if ($this->_pid) {
267 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_PremiumsProduct', $this->_pid, 'weight', 'id');
268 }
269
270 // updateOtherWeights needs to filter on premiums_id
271 $filter = array('premiums_id' => $params['premiums_id']);
272 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Contribute_DAO_PremiumsProduct', $oldWeight, $params['weight'], $filter);
273
274 $dao = new CRM_Contribute_DAO_PremiumsProduct();
275 $dao->copyValues($params);
276 $dao->save();
277 CRM_Utils_System::redirect($url);
278 }
279 }
280
281 /**
282 * Return a descriptive name for the page, used in wizard header
283 *
284 * @return string
285 */
286 public function getTitle() {
287 return ts('Add Premium to Contribution Page');
288 }
289
290 }