3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
35 * Form to process actions on Price Sets.
37 class CRM_Price_Form_Set
extends CRM_Core_Form
{
40 * The set id saved to the session for an update.
47 * Set variables up before form is built.
49 public function preProcess() {
51 $this->_sid
= $this->get('sid');
53 // setting title for html page
54 $title = ts('New Price Set');
56 $title = CRM_Price_BAO_PriceSet
::getTitle($this->_sid
);
58 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
59 $title = ts('Edit %1', array(1 => $title));
61 elseif ($this->_action
& CRM_Core_Action
::VIEW
) {
62 $title = ts('Preview %1', array(1 => $title));
64 CRM_Utils_System
::setTitle($title);
66 $url = CRM_Utils_System
::url('civicrm/admin/price', 'reset=1');
69 'title' => ts('Price Sets'),
73 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
79 * @param array $fields
80 * The input form values.
82 * The uploaded files if any.
83 * @param array $options
84 * Additional user data.
87 * true if no errors, else array of errors
89 public static function formRule($fields, $files, $options) {
91 $count = count(CRM_Utils_Array
::value('extends', $fields));
92 //price sets configured for membership
93 if ($count && array_key_exists(CRM_Core_Component
::getComponentID('CiviMember'), $fields['extends'])) {
95 $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).');
98 //checks the given price set doesnot start with digit
99 $title = $fields['title'];
100 // gives the ascii value
101 $asciiValue = ord($title{0});
102 if ($asciiValue >= 48 && $asciiValue <= 57) {
103 $errors['title'] = ts("Name cannot not start with a digit");
106 if (!empty($fields['extends'])
107 && (array_key_exists(CRM_Core_Component
::getComponentID('CiviEvent'), $fields['extends'])
108 ||
array_key_exists(CRM_Core_Component
::getComponentID('CiviMember'), $fields['extends']))
110 $isError = CRM_Financial_BAO_FinancialAccount
::validateFinancialType($fields['financial_type_id']);
112 $errors['financial_type_id'] = ts('Deferred revenue account is not configured for selected financial type. Please have an administrator set up the deferred revenue account at Administer > CiviContribute > Financial Accounts, then configure it for financial types at Administer > CiviContribution > Financial Types, Accounts');
115 return empty($errors) ?
TRUE : $errors;
119 * Build the form object.
121 public function buildQuickForm() {
122 $this->applyFilter('__ALL__', 'trim');
124 $this->assign('sid', $this->_sid
);
127 $this->add('text', 'title', ts('Set Name'), CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceSet', 'title'), TRUE);
128 $this->addRule('title', ts('Name already exists in Database.'),
129 'objectExists', array('CRM_Price_DAO_PriceSet', $this->_sid
, 'title')
132 $priceSetUsedTables = $extends = array();
133 if ($this->_action
== CRM_Core_Action
::UPDATE
&& $this->_sid
) {
134 $priceSetUsedTables = CRM_Price_BAO_PriceSet
::getUsedBy($this->_sid
, 'table');
137 $config = CRM_Core_Config
::singleton();
138 $showContribution = FALSE;
139 $enabledComponents = CRM_Core_Component
::getEnabledComponents();
141 foreach ($enabledComponents as $name => $compObj) {
144 $option = $this->createElement('checkbox', $compObj->componentID
, NULL, ts('Event'));
145 if (!empty($priceSetUsedTables)) {
146 foreach (array('civicrm_event', 'civicrm_participant') as $table) {
147 if (in_array($table, $priceSetUsedTables)) {
153 $extends[] = $option;
156 case 'CiviContribute':
157 $option = $this->createElement('checkbox', $compObj->componentID
, NULL, ts('Contribution'));
158 if (!empty($priceSetUsedTables)) {
159 foreach (array('civicrm_contribution', 'civicrm_contribution_page') as $table) {
160 if (in_array($table, $priceSetUsedTables)) {
166 $extends[] = $option;
170 $option = $this->createElement('checkbox', $compObj->componentID
, NULL, ts('Membership'));
171 if (!empty($priceSetUsedTables)) {
172 foreach (array('civicrm_membership', 'civicrm_contribution_page') as $table) {
173 if (in_array($table, $priceSetUsedTables)) {
179 $extends[] = $option;
184 if (CRM_Utils_System
::isNull($extends)) {
185 $this->assign('extends', FALSE);
188 $this->assign('extends', TRUE);
191 $this->addGroup($extends, 'extends', ts('Used For'), ' ', TRUE);
193 $this->addRule('extends', ts('%1 is a required field.', array(1 => ts('Used For'))), 'required');
196 $financialType = CRM_Financial_BAO_FinancialType
::getIncomeFinancialType();
198 foreach ($financialType as $finTypeId => $type) {
199 if (CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus()
200 && !CRM_Core_Permission
::check('add contributions of type ' . $type)
202 unset($financialType[$finTypeId]);
206 $this->add('select', 'financial_type_id',
207 ts('Default Financial Type'),
208 array('' => ts('- select -')) +
$financialType, 'required'
212 $this->add('textarea', 'help_pre', ts('Pre-form Help'),
213 CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceSet', 'help_pre')
215 $this->add('textarea', 'help_post', ts('Post-form Help'),
216 CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceSet', 'help_post')
219 // is this set active ?
220 $this->addElement('checkbox', 'is_active', ts('Is this Price Set active?'));
222 $this->addButtons(array(
225 'name' => ts('Save'),
226 'spacing' => ' ',
231 'name' => ts('Cancel'),
235 $this->addFormRule(array('CRM_Price_Form_Set', 'formRule'));
237 // views are implemented as frozen form
238 if ($this->_action
& CRM_Core_Action
::VIEW
) {
240 //$this->addElement('button', 'done', ts('Done'), array('onclick' => "location.href='civicrm/admin/price?reset=1&action=browse'"));
245 * Set default values for the form. Note that in edit/view mode.
247 * The default values are retrieved from the database.
250 * array of default values
252 public function setDefaultValues() {
253 $defaults = array('is_active' => TRUE);
255 $params = array('id' => $this->_sid
);
256 CRM_Price_BAO_PriceSet
::retrieve($params, $defaults);
257 $extends = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defaults['extends']);
258 unset($defaults['extends']);
259 foreach ($extends as $compId) {
260 $defaults['extends'][$compId] = 1;
270 public function postProcess() {
271 // get the submitted form values.
272 $params = $this->controller
->exportValues('Set');
273 $nameLength = CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceSet', 'name');
274 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
275 $params['financial_type_id'] = CRM_Utils_Array
::value('financial_type_id', $params, FALSE);
278 $extends = CRM_Utils_Array
::value('extends', $params);
279 if (is_array($extends)) {
280 foreach ($extends as $compId => $selected) {
282 $compIds[] = $compId;
286 $params['extends'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $compIds);
288 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
289 $params['id'] = $this->_sid
;
292 $params['name'] = CRM_Utils_String
::titleToVar($params['title'],
293 CRM_Utils_Array
::value('maxlength', $nameLength));
296 $set = CRM_Price_BAO_PriceSet
::create($params);
297 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
298 CRM_Core_Session
::setStatus(ts('The Set \'%1\' has been saved.', array(1 => $set->title
)), ts('Saved'), 'success');
301 // Jump directly to adding a field if popups are disabled
302 $action = CRM_Core_Resources
::singleton()->ajaxPopupsEnabled ?
'browse' : 'add';
303 $url = CRM_Utils_System
::url('civicrm/admin/price/field', array(
309 CRM_Core_Session
::setStatus(ts("Your Set '%1' has been added. You can add fields to this set now.",
310 array(1 => $set->title
)
311 ), ts('Saved'), 'success');
312 $session = CRM_Core_Session
::singleton();
313 $session->replaceUserContext($url);