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