3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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. Note that in edit/view mode
67 * the default values are retrieved from the database
72 public function setDefaultValues() {
76 $dao = new CRM_Contribute_DAO_PremiumsProduct();
77 $dao->id
= $this->_pid
;
79 $defaults['product_id'] = $dao->product_id
;
80 $defaults['financial_type_id'] = $dao->financial_type_id
;
81 $defaults['weight'] = $dao->weight
;
84 $dao = new CRM_Contribute_DAO_Product();
85 $dao->id
= key($this->_products
);
87 $defaults['financial_type_id'] = $dao->financial_type_id
;
89 if (!isset($defaults['weight']) ||
!($defaults['weight'])) {
90 $pageID = CRM_Utils_Request
::retrieve('id', 'Positive',
93 $dao = new CRM_Contribute_DAO_Premium();
94 $dao->entity_table
= 'civicrm_contribution_page';
95 $dao->entity_id
= $pageID;
97 $premiumID = $dao->id
;
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);
103 $defaults['weight'] = $dao->max_weight +
1;
109 * Build the form object.
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'
123 $dao = new CRM_Contribute_DAO_PremiumsProduct();
124 $dao->id
= $this->_pid
;
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);
130 $this->addButtons(array(
133 'name' => ts('Delete'),
134 'spacing' => ' ',
139 'name' => ts('Cancel'),
146 if ($this->_action
& CRM_Core_Action
::PREVIEW
) {
147 CRM_Contribute_BAO_Premium
::buildPremiumPreviewBlock($this, NULL, $this->_pid
);
148 $this->addButtons(array(
151 'name' => ts('Done with Preview'),
159 $session = CRM_Core_Session
::singleton();
160 $url = CRM_Utils_System
::url($urlParams, 'reset=1&action=update&id=' . $this->_id
);
161 $session->pushUserContext($url);
163 $this->add('select', 'product_id', ts('Select the Product') . ' ', $this->_products
, TRUE);
165 $this->addElement('text', 'weight', ts('Order'), CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_PremiumsProduct', 'weight'));
167 $financialType = CRM_Contribute_PseudoConstant
::financialType();
168 $premiumFinancialType = array();
169 CRM_Core_PseudoConstant
::populate(
170 $premiumFinancialType,
171 'CRM_Financial_DAO_EntityFinancialAccount',
173 $retrieve = 'entity_id',
175 'account_relationship = 8'
178 $costFinancialType = array();
179 CRM_Core_PseudoConstant
::populate(
181 'CRM_Financial_DAO_EntityFinancialAccount',
183 $retrieve = 'entity_id',
185 'account_relationship = 7'
187 $productFinancialType = array_intersect($costFinancialType, $premiumFinancialType);
188 foreach ($financialType as $key => $financialTypeName) {
189 if (!in_array($key, $productFinancialType)) {
190 unset($financialType[$key]);
193 if (count($financialType)) {
194 $this->assign('financialType', $financialType);
199 ts('Financial Type'),
200 array('' => ts('- select -')) +
$financialType
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
));
205 if ($this->_single
) {
206 $this->addButtons(array(
209 'name' => ts('Save'),
210 'spacing' => ' ',
215 'name' => ts('Cancel'),
221 parent
::buildQuickForm();
230 public function postProcess() {
231 // get the submitted form values.
232 $params = $this->controller
->exportValues($this->_name
);
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);
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
;
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);
253 $session = CRM_Core_Session
::singleton();
254 $url = CRM_Utils_System
::url($urlParams, 'reset=1&action=update&id=' . $this->_id
);
256 $params['id'] = $this->_pid
;
258 $dao = new CRM_Contribute_DAO_Premium();
259 $dao->entity_table
= 'civicrm_contribution_page';
260 $dao->entity_id
= $this->_id
;
262 $premiumID = $dao->id
;
263 $params['premiums_id'] = $premiumID;
267 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_PremiumsProduct', $this->_pid
, 'weight', 'id');
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);
274 $dao = new CRM_Contribute_DAO_PremiumsProduct();
275 $dao->copyValues($params);
277 CRM_Utils_System
::redirect($url);
282 * Return a descriptive name for the page, used in wizard header
286 public function getTitle() {
287 return ts('Add Premium to Contribution Page');