Merge pull request #9174 from colemanw/CRM-19448
[civicrm-core.git] / CRM / Price / Form / Set.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 */
33
34/**
ad37ac8e 35 * Form to process actions on Price Sets.
6a488035
TO
36 */
37class CRM_Price_Form_Set extends CRM_Core_Form {
38
39 /**
fe482240 40 * The set id saved to the session for an update.
6a488035
TO
41 *
42 * @var int
6a488035
TO
43 */
44 protected $_sid;
45
46 /**
fe482240 47 * Set variables up before form is built.
6a488035
TO
48 */
49 public function preProcess() {
50 // current set id
51 $this->_sid = $this->get('sid');
52
53 // setting title for html page
54 $title = ts('New Price Set');
55 if ($this->_sid) {
9da8dc8c 56 $title = CRM_Price_BAO_PriceSet::getTitle($this->_sid);
6a488035
TO
57 }
58 if ($this->_action & CRM_Core_Action::UPDATE) {
59 $title = ts('Edit %1', array(1 => $title));
60 }
61 elseif ($this->_action & CRM_Core_Action::VIEW) {
62 $title = ts('Preview %1', array(1 => $title));
63 }
64 CRM_Utils_System::setTitle($title);
65
66 $url = CRM_Utils_System::url('civicrm/admin/price', 'reset=1');
353ffa53
TO
67 $breadCrumb = array(
68 array(
69 'title' => ts('Price Sets'),
6a488035 70 'url' => $url,
bed98343 71 ),
353ffa53 72 );
6a488035
TO
73 CRM_Utils_System::appendBreadCrumb($breadCrumb);
74 }
75
76 /**
fe482240 77 * Global form rule.
6a488035 78 *
414c1420
TO
79 * @param array $fields
80 * The input form values.
81 * @param array $files
82 * The uploaded files if any.
83 * @param array $options
84 * Additional user data.
6a488035 85 *
72b3a70c
CW
86 * @return bool|array
87 * true if no errors, else array of errors
6a488035 88 */
00be9182 89 public static function formRule($fields, $files, $options) {
6a488035
TO
90 $errors = array();
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'])) {
94 if ($count > 1) {
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).');
96 }
97 }
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) {
5ab78070 103 $errors['title'] = ts("Name cannot not start with a digit");
6a488035 104 }
e68e1c9a
PN
105 // CRM-16189
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']))
109 ) {
f7e2bf47
PN
110 try {
111 CRM_Financial_BAO_FinancialAccount::validateFinancialType($fields['financial_type_id']);
112 }
113 catch (CRM_Core_Exception $e) {
114 $errors['financial_type_id'] = $e->getMessage();
e68e1c9a
PN
115 }
116 }
6a488035
TO
117 return empty($errors) ? TRUE : $errors;
118 }
119
120 /**
fe482240 121 * Build the form object.
6a488035
TO
122 */
123 public function buildQuickForm() {
124 $this->applyFilter('__ALL__', 'trim');
125
126 $this->assign('sid', $this->_sid);
127
128 // title
9da8dc8c 129 $this->add('text', 'title', ts('Set Name'), CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'title'), TRUE);
6a488035 130 $this->addRule('title', ts('Name already exists in Database.'),
9da8dc8c 131 'objectExists', array('CRM_Price_DAO_PriceSet', $this->_sid, 'title')
6a488035
TO
132 );
133
134 $priceSetUsedTables = $extends = array();
135 if ($this->_action == CRM_Core_Action::UPDATE && $this->_sid) {
9da8dc8c 136 $priceSetUsedTables = CRM_Price_BAO_PriceSet::getUsedBy($this->_sid, 'table');
6a488035
TO
137 }
138
353ffa53 139 $config = CRM_Core_Config::singleton();
6a488035 140 $showContribution = FALSE;
6091117f 141 $enabledComponents = CRM_Core_Component::getEnabledComponents();
142
143 foreach ($enabledComponents as $name => $compObj) {
144 switch ($name) {
145 case 'CiviEvent':
146 $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Event'));
147 if (!empty($priceSetUsedTables)) {
148 foreach (array('civicrm_event', 'civicrm_participant') as $table) {
149 if (in_array($table, $priceSetUsedTables)) {
150 $option->freeze();
151 break;
152 }
153 }
154 }
155 $extends[] = $option;
156 break;
ba1dcfda 157
6091117f 158 case 'CiviContribute':
159 $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Contribution'));
160 if (!empty($priceSetUsedTables)) {
161 foreach (array('civicrm_contribution', 'civicrm_contribution_page') as $table) {
162 if (in_array($table, $priceSetUsedTables)) {
163 $option->freeze();
164 break;
165 }
166 }
167 }
168 $extends[] = $option;
169 break;
ba1dcfda 170
6091117f 171 case 'CiviMember':
172 $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Membership'));
173 if (!empty($priceSetUsedTables)) {
174 foreach (array('civicrm_membership', 'civicrm_contribution_page') as $table) {
175 if (in_array($table, $priceSetUsedTables)) {
176 $option->freeze();
177 break;
178 }
179 }
6a488035 180 }
6091117f 181 $extends[] = $option;
182 break;
6a488035 183 }
6a488035
TO
184 }
185
186 if (CRM_Utils_System::isNull($extends)) {
187 $this->assign('extends', FALSE);
188 }
189 else {
190 $this->assign('extends', TRUE);
191 }
192
193 $this->addGroup($extends, 'extends', ts('Used For'), '&nbsp;', TRUE);
194
195 $this->addRule('extends', ts('%1 is a required field.', array(1 => ts('Used For'))), 'required');
366fe2a3 196
6a488035
TO
197 // financial type
198 $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
366fe2a3 199
53df1142 200 foreach ($financialType as $finTypeId => $type) {
40c655aa 201 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()
66af7c48
PN
202 && !CRM_Core_Permission::check('add contributions of type ' . $type)
203 ) {
53df1142
E
204 unset($financialType[$finTypeId]);
205 }
206 }
207
6a488035
TO
208 $this->add('select', 'financial_type_id',
209 ts('Default Financial Type'),
353ffa53 210 array('' => ts('- select -')) + $financialType, 'required'
6a488035
TO
211 );
212
213 // help text
214 $this->add('textarea', 'help_pre', ts('Pre-form Help'),
9da8dc8c 215 CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'help_pre')
6a488035
TO
216 );
217 $this->add('textarea', 'help_post', ts('Post-form Help'),
9da8dc8c 218 CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'help_post')
6a488035
TO
219 );
220
221 // is this set active ?
222 $this->addElement('checkbox', 'is_active', ts('Is this Price Set active?'));
223
224 $this->addButtons(array(
c5c263ca
AH
225 array(
226 'type' => 'next',
227 'name' => ts('Save'),
228 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
229 'isDefault' => TRUE,
230 ),
231 array(
232 'type' => 'cancel',
233 'name' => ts('Cancel'),
234 ),
235 ));
6a488035
TO
236
237 $this->addFormRule(array('CRM_Price_Form_Set', 'formRule'));
238
239 // views are implemented as frozen form
240 if ($this->_action & CRM_Core_Action::VIEW) {
241 $this->freeze();
242 //$this->addElement('button', 'done', ts('Done'), array('onclick' => "location.href='civicrm/admin/price?reset=1&action=browse'"));
243 }
244 }
245
246 /**
ad37ac8e 247 * Set default values for the form. Note that in edit/view mode.
248 *
249 * The default values are retrieved from the database.
6a488035 250 *
a6c01b45
CW
251 * @return array
252 * array of default values
6a488035 253 */
00be9182 254 public function setDefaultValues() {
6a488035
TO
255 $defaults = array('is_active' => TRUE);
256 if ($this->_sid) {
257 $params = array('id' => $this->_sid);
9da8dc8c 258 CRM_Price_BAO_PriceSet::retrieve($params, $defaults);
6a488035
TO
259 $extends = explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['extends']);
260 unset($defaults['extends']);
9277d9e4
TO
261 foreach ($extends as $compId) {
262 $defaults['extends'][$compId] = 1;
ba1dcfda 263 }
6a488035
TO
264 }
265
266 return $defaults;
267 }
268
269 /**
fe482240 270 * Process the form.
6a488035
TO
271 */
272 public function postProcess() {
273 // get the submitted form values.
274 $params = $this->controller->exportValues('Set');
9da8dc8c 275 $nameLength = CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'name');
6a488035
TO
276 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
277 $params['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $params, FALSE);
278
279 $compIds = array();
280 $extends = CRM_Utils_Array::value('extends', $params);
281 if (is_array($extends)) {
4f99ca55
TO
282 foreach ($extends as $compId => $selected) {
283 if ($selected) {
284 $compIds[] = $compId;
285 }
ba1dcfda 286 }
6a488035
TO
287 }
288 $params['extends'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $compIds);
289
290 if ($this->_action & CRM_Core_Action::UPDATE) {
291 $params['id'] = $this->_sid;
292 }
0c86abc7 293 else {
294 $params['name'] = CRM_Utils_String::titleToVar($params['title'],
295 CRM_Utils_Array::value('maxlength', $nameLength));
296 }
6a488035 297
9da8dc8c 298 $set = CRM_Price_BAO_PriceSet::create($params);
6a488035
TO
299 if ($this->_action & CRM_Core_Action::UPDATE) {
300 CRM_Core_Session::setStatus(ts('The Set \'%1\' has been saved.', array(1 => $set->title)), ts('Saved'), 'success');
301 }
302 else {
704f21c0
CW
303 // Jump directly to adding a field if popups are disabled
304 $action = CRM_Core_Resources::singleton()->ajaxPopupsEnabled ? 'browse' : 'add';
353ffa53 305 $url = CRM_Utils_System::url('civicrm/admin/price/field', array(
c5c263ca
AH
306 'reset' => 1,
307 'action' => $action,
308 'sid' => $set->id,
309 'new' => 1,
310 ));
6a488035 311 CRM_Core_Session::setStatus(ts("Your Set '%1' has been added. You can add fields to this set now.",
353ffa53
TO
312 array(1 => $set->title)
313 ), ts('Saved'), 'success');
6a488035
TO
314 $session = CRM_Core_Session::singleton();
315 $session->replaceUserContext($url);
316 }
317 }
96025800 318
6a488035 319}