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 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 $enabledComponents = CRM_Core_Component
::getEnabledComponents();
139 foreach ($enabledComponents as $name => $compObj) {
142 $option = $this->createElement('checkbox', $compObj->componentID
, NULL, ts('Event'));
143 if (!empty($priceSetUsedTables)) {
144 foreach (array('civicrm_event', 'civicrm_participant') as $table) {
145 if (in_array($table, $priceSetUsedTables)) {
151 $extends[] = $option;
153 case 'CiviContribute':
154 $option = $this->createElement('checkbox', $compObj->componentID
, NULL, ts('Contribution'));
155 if (!empty($priceSetUsedTables)) {
156 foreach (array('civicrm_contribution', 'civicrm_contribution_page') as $table) {
157 if (in_array($table, $priceSetUsedTables)) {
163 $extends[] = $option;
166 $option = $this->createElement('checkbox', $compObj->componentID
, NULL, ts('Membership'));
167 if (!empty($priceSetUsedTables)) {
168 foreach (array('civicrm_membership', 'civicrm_contribution_page') as $table) {
169 if (in_array($table, $priceSetUsedTables)) {
175 $extends[] = $option;
180 if (CRM_Utils_System
::isNull($extends)) {
181 $this->assign('extends', FALSE);
184 $this->assign('extends', TRUE);
187 $this->addGroup($extends, 'extends', ts('Used For'), ' ', TRUE);
189 $this->addRule('extends', ts('%1 is a required field.', array(1 => ts('Used For'))), 'required');
192 $financialType = CRM_Financial_BAO_FinancialType
::getIncomeFinancialType();
194 $this->add('select', 'financial_type_id',
195 ts('Default Financial Type'),
196 array('' => ts('- select -')) +
$financialType, 'required'
200 $this->add('textarea', 'help_pre', ts('Pre-form Help'),
201 CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceSet', 'help_pre')
203 $this->add('textarea', 'help_post', ts('Post-form Help'),
204 CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceSet', 'help_post')
207 // is this set active ?
208 $this->addElement('checkbox', 'is_active', ts('Is this Price Set active?'));
210 $this->addButtons(array(
213 'name' => ts('Save'),
214 'spacing' => ' ',
219 'name' => ts('Cancel'),
224 $this->addFormRule(array('CRM_Price_Form_Set', 'formRule'));
226 // views are implemented as frozen form
227 if ($this->_action
& CRM_Core_Action
::VIEW
) {
229 //$this->addElement('button', 'done', ts('Done'), array('onclick' => "location.href='civicrm/admin/price?reset=1&action=browse'"));
234 * This function sets the default values for the form. Note that in edit/view mode
235 * the default values are retrieved from the database
239 * @return array array of default values
242 function setDefaultValues() {
243 $defaults = array('is_active' => TRUE);
245 $params = array('id' => $this->_sid
);
246 CRM_Price_BAO_PriceSet
::retrieve($params, $defaults);
247 $extends = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defaults['extends']);
248 unset($defaults['extends']);
249 foreach ($extends as $compId) $defaults['extends'][$compId] = 1;
263 public function postProcess() {
264 // get the submitted form values.
265 $params = $this->controller
->exportValues('Set');
266 $nameLength = CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceSet', 'name');
267 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
268 $params['financial_type_id'] = CRM_Utils_Array
::value('financial_type_id', $params, FALSE);
271 $extends = CRM_Utils_Array
::value('extends', $params);
272 if (is_array($extends)) {
273 foreach ($extends as $compId => $selected) if ($selected) { $compIds[] = $compId; }
275 $params['extends'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $compIds);
277 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
278 $params['id'] = $this->_sid
;
281 $params['name'] = CRM_Utils_String
::titleToVar($params['title'],
282 CRM_Utils_Array
::value('maxlength', $nameLength));
285 $set = CRM_Price_BAO_PriceSet
::create($params);
286 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
287 CRM_Core_Session
::setStatus(ts('The Set \'%1\' has been saved.', array(1 => $set->title
)), ts('Saved'), 'success');
290 $url = CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=add&sid=' . $set->id
);
291 CRM_Core_Session
::setStatus(ts("Your Set '%1' has been added. You can add fields to this set now.",
292 array(1 => $set->title
)
293 ), ts('Saved'), 'success');
294 $session = CRM_Core_Session
::singleton();
295 $session->replaceUserContext($url);