dev/core#560 Convert final CRM_Core_Error::fatal to statusBounces or Exceptions
[civicrm-core.git] / CRM / Price / Form / Option.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 * $Id$
17 *
18 */
19
20/**
21 * form to process actions on the field aspect of Custom
22 */
23class CRM_Price_Form_Option extends CRM_Core_Form {
24
25 /**
fe482240 26 * The price field id saved to the session for an update.
6a488035
TO
27 *
28 * @var int
6a488035
TO
29 */
30 protected $_fid;
31
32 /**
100fef9d 33 * Option value id, used when editing the Option
6a488035
TO
34 *
35 * @var int
6a488035
TO
36 */
37 protected $_oid;
38
062913c2
SL
39 /**
40 * Array of Money fields
41 *
42 * @var array
43 */
44 protected $_moneyFields = ['amount', 'non_deductible_amount'];
45
6a488035 46 /**
fe482240 47 * Set variables up before form is built.
6a488035 48 *
6a488035 49 * @return void
6a488035
TO
50 */
51 public function preProcess() {
e5e8ab99 52 $this->setPageTitle(ts('Price Option'));
6a488035
TO
53 $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive',
54 $this
55 );
56 $this->_oid = CRM_Utils_Request::retrieve('oid', 'Positive',
57 $this
58 );
59 }
60
61 /**
c490a46a 62 * Set default values for the form. Note that in edit/view mode
6a488035
TO
63 * the default values are retrieved from the database
64 *
bed98343 65 * @return array|void array of default values
6a488035 66 */
00be9182 67 public function setDefaultValues() {
a4c1913d 68 if ($this->_action == CRM_Core_Action::DELETE) {
bed98343 69 return NULL;
a4c1913d 70 }
be2fb01f 71 $defaults = [];
6a488035
TO
72
73 if (isset($this->_oid)) {
be2fb01f 74 $params = ['id' => $this->_oid];
6a488035 75
9da8dc8c 76 CRM_Price_BAO_PriceFieldValue::retrieve($params, $defaults);
6a488035
TO
77
78 // fix the display of the monetary value, CRM-4038
062913c2
SL
79 foreach ($this->_moneyFields as $field) {
80 $defaults[$field] = CRM_Utils_Money::format(CRM_Utils_Array::value($field, $defaults), NULL, '%a');
81 }
6a488035
TO
82 }
83
84 $memberComponentId = CRM_Core_Component::getComponentID('CiviMember');
9da8dc8c 85 $extendComponentId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'extends', 'id');
6a488035
TO
86
87 if (!isset($defaults['membership_num_terms']) && $memberComponentId == $extendComponentId) {
88 $defaults['membership_num_terms'] = 1;
89 }
90 // set financial type used for price set to set default for new option
91 if (!$this->_oid) {
fe7f4414 92 $defaults['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'financial_type_id', 'id');
6a488035
TO
93 }
94 if (!isset($defaults['weight']) || !$defaults['weight']) {
be2fb01f 95 $fieldValues = ['price_field_id' => $this->_fid];
9da8dc8c 96 $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Price_DAO_PriceFieldValue', $fieldValues);
6a488035
TO
97 $defaults['is_active'] = 1;
98 }
99
100 return $defaults;
101 }
102
103 /**
fe482240 104 * Build the form object.
6a488035 105 *
6a488035 106 * @return void
6a488035
TO
107 */
108 public function buildQuickForm() {
e3ca0d21
E
109 if ($this->_action == CRM_Core_Action::UPDATE) {
110 $finTypeId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $this->_oid, 'financial_type_id');
23dba589 111 if (!CRM_Financial_BAO_FinancialType::checkPermissionToEditFinancialType($finTypeId)) {
79e11805 112 CRM_Core_Error::statusBounce(ts("You do not have permission to access this page"));
e3ca0d21
E
113 }
114 }
6a488035 115 if ($this->_action == CRM_Core_Action::DELETE) {
be2fb01f
CW
116 $this->addButtons([
117 [
c5c263ca
AH
118 'type' => 'next',
119 'name' => ts('Delete'),
be2fb01f
CW
120 ],
121 [
c5c263ca
AH
122 'type' => 'cancel',
123 'name' => ts('Cancel'),
be2fb01f
CW
124 ],
125 ]);
bed98343 126 return NULL;
6a488035
TO
127 }
128 else {
353ffa53 129 $attributes = CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceFieldValue');
6a488035
TO
130 // lets trim all the whitespace
131 $this->applyFilter('__ALL__', 'trim');
132
133 // hidden Option Id for validation use
134 $this->add('hidden', 'optionId', $this->_oid);
135
94166e28
CW
136 // Needed for i18n dialog
137 $this->assign('optionId', $this->_oid);
138
6a488035
TO
139 //hidden field ID for validation use
140 $this->add('hidden', 'fieldId', $this->_fid);
141
142 // label
143 $this->add('text', 'label', ts('Option Label'), NULL, TRUE);
144 $memberComponentId = CRM_Core_Component::getComponentID('CiviMember');
145 if ($this->_action == CRM_Core_Action::UPDATE) {
146 $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this);
147 }
148 elseif ($this->_action == CRM_Core_Action::ADD ||
149 $this->_action == CRM_Core_Action::VIEW
150 ) {
9da8dc8c 151 $this->_sid = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid, 'price_set_id', 'id');
6a488035 152 }
ba1dcfda 153 $this->isEvent = FALSE;
9da8dc8c 154 $extendComponentId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'extends', 'id');
6a488035
TO
155 $this->assign('showMember', FALSE);
156 if ($memberComponentId == $extendComponentId) {
157 $this->assign('showMember', TRUE);
158 $membershipTypes = CRM_Member_PseudoConstant::membershipType();
be2fb01f 159 $this->add('select', 'membership_type_id', ts('Membership Type'), [
c5c263ca 160 '' => ' ',
be2fb01f
CW
161 ] + $membershipTypes, FALSE,
162 ['onClick' => "calculateRowValues( );"]);
d70ada2a 163 $this->add('number', 'membership_num_terms', ts('Number of Terms'), $attributes['membership_num_terms']);
6a488035
TO
164 }
165 else {
166 $allComponents = explode(CRM_Core_DAO::VALUE_SEPARATOR, $extendComponentId);
167 $eventComponentId = CRM_Core_Component::getComponentID('CiviEvent');
168 if (in_array($eventComponentId, $allComponents)) {
169 $this->isEvent = TRUE;
170 // count
d70ada2a 171 $this->add('number', 'count', ts('Participant Count'));
6a488035 172 $this->addRule('count', ts('Please enter a valid Max Participants.'), 'positiveInteger');
366fe2a3 173
d70ada2a 174 $this->add('number', 'max_value', ts('Max Participants'));
6a488035
TO
175 $this->addRule('max_value', ts('Please enter a valid Max Participants.'), 'positiveInteger');
176 }
366fe2a3 177
6a488035
TO
178 }
179 //Financial Type
180 $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
366fe2a3 181
6a488035
TO
182 if (count($financialType)) {
183 $this->assign('financialType', $financialType);
184 }
185 $this->add(
366fe2a3 186 'select',
187 'financial_type_id',
188 ts('Financial Type'),
be2fb01f 189 ['' => ts('- select -')] + $financialType,
ba1dcfda 190 TRUE
6a488035 191 );
366fe2a3 192
9da8dc8c 193 //CRM_Core_DAO::getFieldValue( 'CRM_Price_DAO_PriceField', $this->_fid, 'weight', 'id' );
6a488035
TO
194 // FIX ME: duplicate rule?
195 /*
e70a7fc0
TO
196 $this->addRule( 'label',
197 ts('Duplicate option label.'),
198 'optionExists',
199 array( 'CRM_Core_DAO_OptionValue', $this->_oid, $this->_ogId, 'label' ) );
200 */
6a488035 201
6a488035
TO
202 // value
203 $this->add('text', 'amount', ts('Option Amount'), NULL, TRUE);
204
205 // the above value is used directly by QF, so the value has to be have a rule
206 // please check with Lobo before u comment this
207 $this->registerRule('amount', 'callback', 'money', 'CRM_Utils_Rule');
208 $this->addRule('amount', ts('Please enter a monetary value for this field.'), 'money');
209
5afce5ad 210 $this->add('text', 'non_deductible_amount', ts('Non-deductible Amount'), NULL);
211 $this->registerRule('non_deductible_amount', 'callback', 'money', 'CRM_Utils_Rule');
212 $this->addRule('non_deductible_amount', ts('Please enter a monetary value for this field.'), 'money');
213
6a488035 214 $this->add('textarea', 'description', ts('Description'));
08b8b2d9 215 $this->add('textarea', 'help_pre', ts('Pre Option Help'));
216 $this->add('textarea', 'help_post', ts('Post Option Help'));
6a488035
TO
217
218 // weight
f20d2b0d 219 $this->add('number', 'weight', ts('Order'), NULL, TRUE);
6a488035
TO
220 $this->addRule('weight', ts('is a numeric field'), 'numeric');
221
222 // is active ?
223 $this->add('checkbox', 'is_active', ts('Active?'));
224
2db35bf6
AF
225 // is public?
226 $this->add('select', 'visibility_id', ts('Visibility'), CRM_Core_PseudoConstant::visibility());
227
6a488035
TO
228 //is default
229 $this->add('checkbox', 'is_default', ts('Default'));
230
231 if ($this->_fid) {
232 //hide the default checkbox option for text field
9da8dc8c 233 $htmlType = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid, 'html_type');
6a488035
TO
234 $this->assign('hideDefaultOption', FALSE);
235 if ($htmlType == 'Text') {
236 $this->assign('hideDefaultOption', TRUE);
237 }
238 }
239 // add buttons
be2fb01f
CW
240 $this->addButtons([
241 [
c5c263ca
AH
242 'type' => 'next',
243 'name' => ts('Save'),
be2fb01f
CW
244 ],
245 [
c5c263ca
AH
246 'type' => 'cancel',
247 'name' => ts('Cancel'),
be2fb01f
CW
248 ],
249 ]);
6a488035
TO
250
251 // if view mode pls freeze it with the done button.
252 if ($this->_action & CRM_Core_Action::VIEW) {
253 $this->freeze();
be2fb01f
CW
254 $this->addButtons([
255 [
c5c263ca
AH
256 'type' => 'cancel',
257 'name' => ts('Done'),
258 'isDefault' => TRUE,
be2fb01f
CW
259 ],
260 ]);
6a488035
TO
261 }
262 }
263
be2fb01f 264 $this->addFormRule(['CRM_Price_Form_Option', 'formRule'], $this);
6a488035
TO
265 }
266
267 /**
fe482240 268 * Global validation rules for the form.
6a488035 269 *
414c1420
TO
270 * @param array $fields
271 * Posted values of the form.
77b97be7
EM
272 *
273 * @param $files
c490a46a 274 * @param CRM_Core_Form $form
6a488035 275 *
a6c01b45
CW
276 * @return array
277 * if errors then list of errors to be posted back to the form,
6a488035 278 * true otherwise
6a488035 279 */
00be9182 280 public static function formRule($fields, $files, $form) {
be2fb01f 281 $errors = [];
8cc574cf 282 if (!empty($fields['count']) && !empty($fields['max_value']) &&
6a488035
TO
283 $fields['count'] > $fields['max_value']
284 ) {
285 $errors['count'] = ts('Participant count can not be greater than max participants.');
286 }
a13c171d
CR
287
288 $priceField = CRM_Price_BAO_PriceField::findById($fields['fieldId']);
39868387 289 $visibilityOptions = CRM_Core_PseudoConstant::get('CRM_Price_BAO_PriceFieldValue', 'visibility_id', ['labelColumn' => 'name']);
a13c171d
CR
290
291 $publicCount = 0;
292 $options = CRM_Price_BAO_PriceField::getOptions($priceField->id);
293 foreach ($options as $currentOption) {
294 if ($fields['optionId'] == $currentOption['id'] && $visibilityOptions[$fields['visibility_id']] == 'public') {
295 $publicCount++;
296 }
297 elseif ($fields['optionId'] != $currentOption['id'] && $visibilityOptions[$currentOption['visibility_id']] == 'public') {
298 $publicCount++;
299 }
300 }
c8ac8c5b 301 if ($visibilityOptions[$priceField->visibility_id] == 'public' && $publicCount == 0 && $visibilityOptions[$fields['visibility_id']] == 'admin') {
a13c171d
CR
302 $errors['visibility_id'] = ts('All other options for this \'Public\' field have \'Admin\' visibility. There should at least be one \'Public\' option, or make the field \'Admin\' only.');
303 }
c8ac8c5b 304 elseif ($visibilityOptions[$priceField->visibility_id] == 'admin' && $visibilityOptions[$fields['visibility_id']] == 'public') {
a13c171d
CR
305 $errors['visibility_id'] = ts('You must choose \'Admin\' visibility for this price option, as it belongs to a field with \'Admin\' visibility.');
306 }
307
6a488035
TO
308 return empty($errors) ? TRUE : $errors;
309 }
310
311 /**
fe482240 312 * Process the form.
6a488035 313 *
6a488035 314 * @return void
6a488035
TO
315 */
316 public function postProcess() {
317 if ($this->_action == CRM_Core_Action::DELETE) {
be2fb01f 318 $fieldValues = ['price_field_id' => $this->_fid];
353ffa53
TO
319 $wt = CRM_Utils_Weight::delWeight('CRM_Price_DAO_PriceFieldValue', $this->_oid, $fieldValues);
320 $label = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue',
6a488035
TO
321 $this->_oid,
322 'label', 'id'
323 );
324
9da8dc8c 325 if (CRM_Price_BAO_PriceFieldValue::del($this->_oid)) {
be2fb01f 326 CRM_Core_Session::setStatus(ts('%1 option has been deleted.', [1 => $label]), ts('Record Deleted'), 'success');
6a488035 327 }
bed98343 328 return NULL;
6a488035
TO
329 }
330 else {
be2fb01f 331 $params = $ids = [];
353ffa53 332 $params = $this->controller->exportValues('Option');
9da8dc8c 333 $fieldLabel = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid, 'label');
6a488035 334
062913c2
SL
335 foreach ($this->_moneyFields as $field) {
336 $params[$field] = CRM_Utils_Rule::cleanMoney(trim($params[$field]));
337 }
6a488035
TO
338 $params['price_field_id'] = $this->_fid;
339 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
ff33ddc8 340 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
2db35bf6 341 $params['visibility_id'] = CRM_Utils_Array::value('visibility_id', $params, FALSE);
be2fb01f 342 $ids = [];
6a488035 343 if ($this->_oid) {
72b84ded 344 $params['id'] = $this->_oid;
6a488035 345 }
9da8dc8c 346 $optionValue = CRM_Price_BAO_PriceFieldValue::create($params, $ids);
6a488035 347
be2fb01f 348 CRM_Core_Session::setStatus(ts("The option '%1' has been saved.", [1 => $params['label']]), ts('Value Saved'), 'success');
6a488035
TO
349 }
350 }
96025800 351
6a488035 352}