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