3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
37 * form to process actions on Price Sets
39 class CRM_Price_Form_Set
extends CRM_Core_Form
{
42 * the set id saved to the session for an update
50 * Function to set variables up before form is built
57 public function preProcess() {
59 $this->_sid
= $this->get('sid');
61 // setting title for html page
62 $title = ts('New Price Set');
64 $title = CRM_Price_BAO_PriceSet
::getTitle($this->_sid
);
66 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
67 $title = ts('Edit %1', array(1 => $title));
69 elseif ($this->_action
& CRM_Core_Action
::VIEW
) {
70 $title = ts('Preview %1', array(1 => $title));
72 CRM_Utils_System
::setTitle($title);
74 $url = CRM_Utils_System
::url('civicrm/admin/price', 'reset=1');
75 $breadCrumb = array(array('title' => ts('Price Sets'),
78 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
84 * @param array $fields the input form values
85 * @param array $files the uploaded files if any
86 * @param array $options additional user data
88 * @return true if no errors, else array of errors
92 static function formRule($fields, $files, $options) {
94 $count = count(CRM_Utils_Array
::value('extends', $fields));
95 //price sets configured for membership
96 if ($count && array_key_exists(CRM_Core_Component
::getComponentID('CiviMember'), $fields['extends'])) {
98 $errors['extends'] = ts('If you plan on using this price set for membership signup and renewal, you can not also use it for Events or Contributions. However, a membership price set may include additional fields for non-membership options that require an additional fee (e.g. magazine subscription).');
101 //checks the given price set doesnot start with digit
102 $title = $fields['title'];
103 // gives the ascii value
104 $asciiValue = ord($title{0});
105 if ($asciiValue >= 48 && $asciiValue <= 57) {
106 $errors['title'] = ts("Set's Name should not start with digit");
108 return empty($errors) ?
TRUE : $errors;
112 * Function to actually build the form
119 public function buildQuickForm() {
120 $this->applyFilter('__ALL__', 'trim');
122 $this->assign('sid', $this->_sid
);
125 $this->add('text', 'title', ts('Set Name'), CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceSet', 'title'), TRUE);
126 $this->addRule('title', ts('Name already exists in Database.'),
127 'objectExists', array('CRM_Price_DAO_PriceSet', $this->_sid
, 'title')
130 $priceSetUsedTables = $extends = array();
131 if ($this->_action
== CRM_Core_Action
::UPDATE
&& $this->_sid
) {
132 $priceSetUsedTables = CRM_Price_BAO_PriceSet
::getUsedBy($this->_sid
, 'table');
135 $config = CRM_Core_Config
::singleton();
136 $showContribution = FALSE;
137 $components = array('CiviEvent' => array('title' => ts('Event'),
138 'extend' => CRM_Core_Component
::getComponentID('CiviEvent'),
141 'civicrm_participant',
144 'CiviContribute' => array('title' => ts('Contribution'),
145 'extend' => CRM_Core_Component
::getComponentID('CiviContribute'),
147 'civicrm_contribution',
148 'civicrm_contribution_page',
151 'CiviMember' => array('title' => ts('Membership'),
152 'extend' => CRM_Core_Component
::getComponentID('CiviMember'),
154 'civicrm_membership',
155 'civicrm_contribution_page',
159 foreach ($components as $compName => $compValues) {
160 // take only enabled components.
161 if (!in_array($compName, $config->enableComponents
)) {
164 $option = $this->createElement('checkbox', $compValues['extend'], NULL, $compValues['title']);
166 //if price set is used than freeze it.
167 if (!empty($priceSetUsedTables)) {
168 foreach ($compValues['tables'] as $table) {
170 if (in_array($table, $priceSetUsedTables)) {
176 $extends[] = $option;
179 if (CRM_Utils_System
::isNull($extends)) {
180 $this->assign('extends', FALSE);
183 $this->assign('extends', TRUE);
186 $this->addGroup($extends, 'extends', ts('Used For'), ' ', TRUE);
188 $this->addRule('extends', ts('%1 is a required field.', array(1 => ts('Used For'))), 'required');
191 $financialType = CRM_Financial_BAO_FinancialType
::getIncomeFinancialType();
193 $this->add('select', 'financial_type_id',
194 ts('Default Financial Type'),
195 array('' => ts('- select -')) +
$financialType, 'required'
199 $this->add('textarea', 'help_pre', ts('Pre-form Help'),
200 CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceSet', 'help_pre')
202 $this->add('textarea', 'help_post', ts('Post-form Help'),
203 CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceSet', 'help_post')
206 // is this set active ?
207 $this->addElement('checkbox', 'is_active', ts('Is this Price Set active?'));
209 $this->addButtons(array(
212 'name' => ts('Save'),
213 'spacing' => ' ',
218 'name' => ts('Cancel'),
223 $this->addFormRule(array('CRM_Price_Form_Set', 'formRule'));
225 // views are implemented as frozen form
226 if ($this->_action
& CRM_Core_Action
::VIEW
) {
228 //$this->addElement('button', 'done', ts('Done'), array('onclick' => "location.href='civicrm/admin/price?reset=1&action=browse'"));
233 * This function sets the default values for the form. Note that in edit/view mode
234 * the default values are retrieved from the database
238 * @return array array of default values
241 function setDefaultValues() {
242 $defaults = array('is_active' => TRUE);
244 $params = array('id' => $this->_sid
);
245 CRM_Price_BAO_PriceSet
::retrieve($params, $defaults);
246 $extends = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defaults['extends']);
247 unset($defaults['extends']);
248 foreach ($extends as $compId) $defaults['extends'][$compId] = 1;
262 public function postProcess() {
263 // get the submitted form values.
264 $params = $this->controller
->exportValues('Set');
265 $nameLength = CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceSet', 'name');
266 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
267 $params['financial_type_id'] = CRM_Utils_Array
::value('financial_type_id', $params, FALSE);
270 $extends = CRM_Utils_Array
::value('extends', $params);
271 if (is_array($extends)) {
272 foreach ($extends as $compId => $selected) if ($selected) { $compIds[] = $compId; }
274 $params['extends'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $compIds);
276 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
277 $params['id'] = $this->_sid
;
280 $params['name'] = CRM_Utils_String
::titleToVar($params['title'],
281 CRM_Utils_Array
::value('maxlength', $nameLength));
284 $set = CRM_Price_BAO_PriceSet
::create($params);
285 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
286 CRM_Core_Session
::setStatus(ts('The Set \'%1\' has been saved.', array(1 => $set->title
)), ts('Saved'), 'success');
289 $url = CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=add&sid=' . $set->id
);
290 CRM_Core_Session
::setStatus(ts("Your Set '%1' has been added. You can add fields to this set now.",
291 array(1 => $set->title
)
292 ), ts('Saved'), 'success');
293 $session = CRM_Core_Session
::singleton();
294 $session->replaceUserContext($url);