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