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
37 * form to process actions fo adding product to contribution page
39 class CRM_Contribute_Form_ContributionPage_AddProduct
extends CRM_Contribute_Form_ContributionPage
{
46 * Pre process the form.
51 public function preProcess() {
54 $this->_products
= CRM_Contribute_PseudoConstant
::products($this->_id
);
55 $this->_pid
= CRM_Utils_Request
::retrieve('pid', 'Positive',
60 $dao = new CRM_Contribute_DAO_PremiumsProduct();
61 $dao->id
= $this->_pid
;
63 $temp = CRM_Contribute_PseudoConstant
::products();
64 $this->_products
[$dao->product_id
] = $temp[$dao->product_id
];
67 //$this->_products = array_merge(array('' => '-- Select Product --') , $this->_products );
71 * Set default values for the form. Note that in edit/view mode
72 * the default values are retrieved from the database
77 public function setDefaultValues() {
81 $dao = new CRM_Contribute_DAO_PremiumsProduct();
82 $dao->id
= $this->_pid
;
84 $defaults['product_id'] = $dao->product_id
;
85 $defaults['financial_type_id'] = $dao->financial_type_id
;
86 $defaults['weight'] = $dao->weight
;
89 $dao = new CRM_Contribute_DAO_Product();
90 $dao->id
= key($this->_products
);
92 $defaults['financial_type_id'] = $dao->financial_type_id
;
94 if (!isset($defaults['weight']) ||
!($defaults['weight'])) {
95 $pageID = CRM_Utils_Request
::retrieve('id', 'Positive',
98 $dao = new CRM_Contribute_DAO_Premium();
99 $dao->entity_table
= 'civicrm_contribution_page';
100 $dao->entity_id
= $pageID;
102 $premiumID = $dao->id
;
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);
108 $defaults['weight'] = $dao->max_weight +
1;
114 * Build the form object.
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'
128 $dao = new CRM_Contribute_DAO_PremiumsProduct();
129 $dao->id
= $this->_pid
;
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);
135 $this->addButtons(array(
138 'name' => ts('Delete'),
139 'spacing' => ' ',
144 'name' => ts('Cancel'),
151 if ($this->_action
& CRM_Core_Action
::PREVIEW
) {
152 CRM_Contribute_BAO_Premium
::buildPremiumPreviewBlock($this, NULL, $this->_pid
);
153 $this->addButtons(array(
156 'name' => ts('Done with Preview'),
164 $session = CRM_Core_Session
::singleton();
165 $url = CRM_Utils_System
::url($urlParams, 'reset=1&action=update&id=' . $this->_id
);
166 $session->pushUserContext($url);
168 $this->add('select', 'product_id', ts('Select the Product') . ' ', $this->_products
, TRUE);
170 $this->addElement('text', 'weight', ts('Order'), CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_PremiumsProduct', 'weight'));
172 $financialType = CRM_Contribute_PseudoConstant
::financialType();
173 $premiumFinancialType = array();
174 CRM_Core_PseudoConstant
::populate(
175 $premiumFinancialType,
176 'CRM_Financial_DAO_EntityFinancialAccount',
178 $retrieve = 'entity_id',
180 'account_relationship = 8'
183 $costFinancialType = array();
184 CRM_Core_PseudoConstant
::populate(
186 'CRM_Financial_DAO_EntityFinancialAccount',
188 $retrieve = 'entity_id',
190 'account_relationship = 7'
192 $productFinancialType = array_intersect($costFinancialType, $premiumFinancialType);
193 foreach ($financialType as $key => $financialTypeName) {
194 if (!in_array($key, $productFinancialType)) {
195 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();
235 public function postProcess() {
236 // get the submitted form values.
237 $params = $this->controller
->exportValues($this->_name
);
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);
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
;
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);
258 $session = CRM_Core_Session
::singleton();
259 $url = CRM_Utils_System
::url($urlParams, 'reset=1&action=update&id=' . $this->_id
);
261 $params['id'] = $this->_pid
;
263 $dao = new CRM_Contribute_DAO_Premium();
264 $dao->entity_table
= 'civicrm_contribution_page';
265 $dao->entity_id
= $this->_id
;
267 $premiumID = $dao->id
;
268 $params['premiums_id'] = $premiumID;
272 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_PremiumsProduct', $this->_pid
, 'weight', 'id');
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);
279 $dao = new CRM_Contribute_DAO_PremiumsProduct();
280 $dao->copyValues($params);
282 CRM_Utils_System
::redirect($url);
287 * Return a descriptive name for the page, used in wizard header
291 public function getTitle() {
292 return ts('Add Premium to Contribution Page');