3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * form to process actions fo adding product to contribution page
37 class CRM_Contribute_Form_ContributionPage_AddProduct
extends CRM_Contribute_Form_ContributionPage
{
44 * Pre process the form.
46 public function preProcess() {
49 $this->_products
= CRM_Contribute_PseudoConstant
::products($this->_id
);
50 $this->_pid
= CRM_Utils_Request
::retrieve('pid', 'Positive',
55 $dao = new CRM_Contribute_DAO_PremiumsProduct();
56 $dao->id
= $this->_pid
;
58 $temp = CRM_Contribute_PseudoConstant
::products();
59 $this->_products
[$dao->product_id
] = $temp[$dao->product_id
];
62 //$this->_products = array_merge(array('' => '-- Select Product --') , $this->_products );
66 * Set default values for the form.
68 * Note that in edit/view mode the default values are retrieved from the database.
70 public function setDefaultValues() {
74 $dao = new CRM_Contribute_DAO_PremiumsProduct();
75 $dao->id
= $this->_pid
;
77 $defaults['product_id'] = $dao->product_id
;
78 $defaults['financial_type_id'] = $dao->financial_type_id
;
79 $defaults['weight'] = $dao->weight
;
82 $dao = new CRM_Contribute_DAO_Product();
83 $dao->id
= key($this->_products
);
85 $defaults['financial_type_id'] = $dao->financial_type_id
;
87 if (!isset($defaults['weight']) ||
!($defaults['weight'])) {
88 $pageID = CRM_Utils_Request
::retrieve('id', 'Positive',
91 $dao = new CRM_Contribute_DAO_Premium();
92 $dao->entity_table
= 'civicrm_contribution_page';
93 $dao->entity_id
= $pageID;
95 $premiumID = $dao->id
;
97 $sql = 'SELECT max( weight ) as max_weight FROM civicrm_premiums_product WHERE premiums_id = %1';
98 $params = [1 => [$premiumID, 'Integer']];
99 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
101 $defaults['weight'] = $dao->max_weight +
1;
107 * Build the form object.
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'
119 $dao = new CRM_Contribute_DAO_PremiumsProduct();
120 $dao->id
= $this->_pid
;
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);
129 'name' => ts('Delete'),
130 'spacing' => ' ',
135 'name' => ts('Cancel'),
141 if ($this->_action
& CRM_Core_Action
::PREVIEW
) {
142 CRM_Contribute_BAO_Premium
::buildPremiumPreviewBlock($this, NULL, $this->_pid
);
146 'name' => ts('Done with Preview'),
153 $session = CRM_Core_Session
::singleton();
154 $url = CRM_Utils_System
::url($urlParams, 'reset=1&action=update&id=' . $this->_id
);
155 $session->pushUserContext($url);
157 $this->add('select', 'product_id', ts('Select the Product') . ' ', $this->_products
, TRUE);
159 $this->addElement('text', 'weight', ts('Order'), CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_PremiumsProduct', 'weight'));
161 $financialType = CRM_Contribute_PseudoConstant
::financialType();
162 $premiumFinancialType = [];
163 CRM_Core_PseudoConstant
::populate(
164 $premiumFinancialType,
165 'CRM_Financial_DAO_EntityFinancialAccount',
167 $retrieve = 'entity_id',
169 'account_relationship = 8'
172 $costFinancialType = [];
173 CRM_Core_PseudoConstant
::populate(
175 'CRM_Financial_DAO_EntityFinancialAccount',
177 $retrieve = 'entity_id',
179 'account_relationship = 7'
181 $productFinancialType = array_intersect($costFinancialType, $premiumFinancialType);
182 foreach ($financialType as $key => $financialTypeName) {
183 if (!in_array($key, $productFinancialType)) {
184 unset($financialType[$key]);
187 // Check permissioned financial types
188 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialType, CRM_Core_Action
::ADD
);
189 if (count($financialType)) {
190 $this->assign('financialType', $financialType);
195 ts('Financial Type'),
196 ['' => ts('- select -')] +
$financialType
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
));
201 if ($this->_single
) {
205 'name' => ts('Save'),
206 'spacing' => ' ',
211 'name' => ts('Cancel'),
216 parent
::buildQuickForm();
223 public function postProcess() {
224 // get the submitted form values.
225 $params = $this->controller
->exportValues($this->_name
);
227 $urlParams = 'civicrm/admin/contribute/premium';
228 if ($this->_action
& CRM_Core_Action
::PREVIEW
) {
229 $url = CRM_Utils_System
::url($urlParams, 'reset=1&action=update&id=' . $this->_id
);
230 CRM_Utils_System
::redirect($url);
234 if ($this->_action
& CRM_Core_Action
::DELETE
) {
235 $url = CRM_Utils_System
::url($urlParams, 'reset=1&action=update&id=' . $this->_id
);
236 $dao = new CRM_Contribute_DAO_PremiumsProduct();
237 $dao->id
= $this->_pid
;
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);
243 $url = CRM_Utils_System
::url($urlParams, 'reset=1&action=update&id=' . $this->_id
);
245 $params['id'] = $this->_pid
;
247 $dao = new CRM_Contribute_DAO_Premium();
248 $dao->entity_table
= 'civicrm_contribution_page';
249 $dao->entity_id
= $this->_id
;
251 $premiumID = $dao->id
;
252 $params['premiums_id'] = $premiumID;
256 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_PremiumsProduct', $this->_pid
, 'weight', 'id');
259 // updateOtherWeights needs to filter on premiums_id
260 $filter = ['premiums_id' => $params['premiums_id']];
261 $params['weight'] = CRM_Utils_Weight
::updateOtherWeights('CRM_Contribute_DAO_PremiumsProduct', $oldWeight, $params['weight'], $filter);
263 $dao = new CRM_Contribute_DAO_PremiumsProduct();
264 $dao->copyValues($params);
266 CRM_Utils_System
::redirect($url);
271 * Return a descriptive name for the page, used in wizard header
275 public function getTitle() {
276 return ts('Add Premium to Contribution Page');