3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * form to process actions fo adding product to contribution page
39 class CRM_Contribute_Form_ContributionPage_AddProduct
extends CRM_Contribute_Form_ContributionPage
{
46 * Function to pre process the form
52 public function preProcess() {
55 $this->_products
= CRM_Contribute_PseudoConstant
::products($this->_id
);
56 $this->_pid
= CRM_Utils_Request
::retrieve('pid', 'Positive',
61 $dao = new CRM_Contribute_DAO_PremiumsProduct();
62 $dao->id
= $this->_pid
;
64 $temp = CRM_Contribute_PseudoConstant
::products();
65 $this->_products
[$dao->product_id
] = $temp[$dao->product_id
];
68 //$this->_products = array_merge(array('' => '-- Select Product --') , $this->_products );
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
79 function setDefaultValues() {
83 $dao = new CRM_Contribute_DAO_PremiumsProduct();
84 $dao->id
= $this->_pid
;
86 $defaults['product_id'] = $dao->product_id
;
87 $defaults['financial_type_id'] = $dao->financial_type_id
;
88 $defaults['weight'] = $dao->weight
;
90 $dao = new CRM_Contribute_DAO_Product();
91 $dao->id
= key($this->_products
);
93 $defaults['financial_type_id'] = $dao->financial_type_id
;
95 if (!isset($defaults['weight']) ||
!($defaults['weight'])) {
96 $pageID = CRM_Utils_Request
::retrieve('id', 'Positive',
99 $dao = new CRM_Contribute_DAO_Premium();
100 $dao->entity_table
= 'civicrm_contribution_page';
101 $dao->entity_id
= $pageID;
103 $premiumID = $dao->id
;
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);
109 $defaults['weight'] = $dao->max_weight +
1;
115 * Function to actually build the form
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'
129 $dao = new CRM_Contribute_DAO_PremiumsProduct();
130 $dao->id
= $this->_pid
;
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);
136 $this->addButtons(array(
139 'name' => ts('Delete'),
140 'spacing' => ' ',
145 'name' => ts('Cancel'),
152 if ($this->_action
& CRM_Core_Action
::PREVIEW
) {
153 CRM_Contribute_BAO_Premium
::buildPremiumPreviewBlock($this, NULL, $this->_pid
);
154 $this->addButtons(array(
157 'name' => ts('Done with Preview'),
165 $session = CRM_Core_Session
::singleton();
166 $url = CRM_Utils_System
::url($urlParams, 'reset=1&action=update&id=' . $this->_id
);
167 $session->pushUserContext($url);
169 $this->add('select', 'product_id', ts('Select the Product') . ' ', $this->_products
, TRUE);
171 $this->addElement('text', 'weight', ts('Weight'), CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_PremiumsProduct', 'weight'));
173 $financialType = CRM_Contribute_PseudoConstant
::financialType( );
174 $premiumFinancialType = array();
175 CRM_Core_PseudoConstant
::populate(
176 $premiumFinancialType,
177 'CRM_Financial_DAO_EntityFinancialAccount',
179 $retrieve = 'entity_id',
181 'account_relationship = 8'
184 $costFinancialType = array();
185 CRM_Core_PseudoConstant
::populate(
187 'CRM_Financial_DAO_EntityFinancialAccount',
189 $retrieve = 'entity_id',
191 'account_relationship = 7'
193 $productFinancialType = array_intersect($costFinancialType, $premiumFinancialType);
194 foreach( $financialType as $key => $financialTypeName ){
195 if(!in_array( $key, $productFinancialType))
196 unset( $financialType[$key] );
198 if( count( $financialType ) ){
199 $this->assign( 'financialType', $financialType );
204 ts( 'Financial Type' ),
205 array(''=>ts('- select -')) +
$financialType
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
));
210 if ($this->_single
) {
211 $this->addButtons(array(
214 'name' => ts('Save'),
215 'spacing' => ' ',
220 'name' => ts('Cancel'),
226 parent
::buildQuickForm();
236 public function postProcess() {
237 // get the submitted form values.
238 $params = $this->controller
->exportValues($this->_name
);
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);
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
;
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);
259 $session = CRM_Core_Session
::singleton();
260 $url = CRM_Utils_System
::url($urlParams, 'reset=1&action=update&id=' . $this->_id
);
262 $params['id'] = $this->_pid
;
264 $dao = new CRM_Contribute_DAO_Premium();
265 $dao->entity_table
= 'civicrm_contribution_page';
266 $dao->entity_id
= $this->_id
;
268 $premiumID = $dao->id
;
269 $params['premiums_id'] = $premiumID;
273 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_PremiumsProduct', $this->_pid
, 'weight', 'id');
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);
280 $dao = new CRM_Contribute_DAO_PremiumsProduct();
281 $dao->copyValues($params);
283 CRM_Utils_System
::redirect($url);
288 * Return a descriptive name for the page, used in wizard header
293 public function getTitle() {
294 return ts('Add Premium to Contribution Page');