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