Merge pull request #17163 from jitendrapurohit/core-1731
[civicrm-core.git] / CRM / Price / Form / Field.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * form to process actions on the field aspect of Price
20 */
21 class CRM_Price_Form_Field extends CRM_Core_Form {
22
23 /**
24 * Constants for number of options for data types of multiple option.
25 */
26 const NUM_OPTION = 15;
27
28 /**
29 * The custom set id saved to the session for an update.
30 *
31 * @var int
32 */
33 protected $_sid;
34
35 /**
36 * The field id, used when editing the field
37 *
38 * @var int
39 */
40 protected $_fid;
41
42 /**
43 * The extended component Id.
44 *
45 * @var array
46 */
47 protected $_extendComponentId;
48
49 /**
50 * Variable is set if price set is used for membership.
51 * @var bool
52 */
53 protected $_useForMember;
54
55 /**
56 * Set variables up before form is built.
57 */
58 public function preProcess() {
59
60 $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this, FALSE, NULL, 'REQUEST');
61 $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive', $this, FALSE, NULL, 'REQUEST');
62 $url = CRM_Utils_System::url('civicrm/admin/price/field', "reset=1&action=browse&sid={$this->_sid}");
63 $breadCrumb = [['title' => ts('Price Set Fields'), 'url' => $url]];
64
65 $this->_extendComponentId = [];
66 $extendComponentId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'extends', 'id');
67 if ($extendComponentId) {
68 $this->_extendComponentId = explode(CRM_Core_DAO::VALUE_SEPARATOR, $extendComponentId);
69 }
70
71 CRM_Utils_System::appendBreadCrumb($breadCrumb);
72
73 $this->setPageTitle(ts('Price Field'));
74 }
75
76 /**
77 * Set default values for the form.
78 *
79 * @return array
80 * array of default values
81 */
82 public function setDefaultValues() {
83 $defaults = [];
84 // is it an edit operation ?
85 if (isset($this->_fid)) {
86 $params = ['id' => $this->_fid];
87 $this->assign('fid', $this->_fid);
88 CRM_Price_BAO_PriceField::retrieve($params, $defaults);
89 $this->_sid = $defaults['price_set_id'];
90
91 // if text, retrieve price
92 if ($defaults['html_type'] == 'Text') {
93 $isActive = $defaults['is_active'];
94 $valueParams = ['price_field_id' => $this->_fid];
95
96 CRM_Price_BAO_PriceFieldValue::retrieve($valueParams, $defaults);
97
98 // fix the display of the monetary value, CRM-4038
99 $defaults['price'] = CRM_Utils_Money::format($defaults['amount'], NULL, '%a');
100 $defaults['is_active'] = $isActive;
101 }
102
103 }
104 else {
105 $defaults['is_active'] = 1;
106 for ($i = 1; $i <= self::NUM_OPTION; $i++) {
107 $defaults['option_status[' . $i . ']'] = 1;
108 $defaults['option_weight[' . $i . ']'] = $i;
109 $defaults['option_visibility_id[' . $i . ']'] = 1;
110 }
111 }
112
113 if ($this->_action & CRM_Core_Action::ADD) {
114 $fieldValues = ['price_set_id' => $this->_sid];
115 $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Price_DAO_PriceField', $fieldValues);
116 $defaults['options_per_line'] = 1;
117 $defaults['is_display_amounts'] = 1;
118 }
119 $enabledComponents = CRM_Core_Component::getEnabledComponents();
120 $eventComponentId = NULL;
121 if (array_key_exists('CiviEvent', $enabledComponents)) {
122 $eventComponentId = CRM_Core_Component::getComponentID('CiviEvent');
123 }
124
125 if (isset($this->_sid) && $this->_action == CRM_Core_Action::ADD) {
126 $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'financial_type_id');
127 $defaults['financial_type_id'] = $financialTypeId;
128 for ($i = 1; $i <= self::NUM_OPTION; $i++) {
129 $defaults['option_financial_type_id[' . $i . ']'] = $financialTypeId;
130 }
131 }
132 return $defaults;
133 }
134
135 /**
136 * Build the form object.
137 */
138 public function buildQuickForm() {
139 // lets trim all the whitespace
140 $this->applyFilter('__ALL__', 'trim');
141
142 // add a hidden field to remember the price set id
143 // this get around the browser tab issue
144 $this->add('hidden', 'sid', $this->_sid);
145 $this->add('hidden', 'fid', $this->_fid);
146
147 // label
148 $this->add('text', 'label', ts('Field Label'), CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceField', 'label'), TRUE);
149
150 // html_type
151 $javascript = 'onchange="option_html_type(this.form)";';
152
153 $htmlTypes = CRM_Price_BAO_PriceField::htmlTypes();
154
155 // Text box for Participant Count for a field
156
157 // Financial Type
158 $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
159 foreach ($financialType as $finTypeId => $type) {
160 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()
161 && !CRM_Core_Permission::check('add contributions of type ' . $type)
162 ) {
163 unset($financialType[$finTypeId]);
164 }
165 }
166 if (count($financialType)) {
167 $this->assign('financialType', $financialType);
168 }
169
170 //Visibility Type Options
171 $visibilityType = CRM_Core_PseudoConstant::visibility();
172 $this->assign('visibilityType', $visibilityType);
173
174 $enabledComponents = CRM_Core_Component::getEnabledComponents();
175 $eventComponentId = $memberComponentId = NULL;
176 if (array_key_exists('CiviEvent', $enabledComponents)) {
177 $eventComponentId = CRM_Core_Component::getComponentID('CiviEvent');
178 }
179 if (array_key_exists('CiviMember', $enabledComponents)) {
180 $memberComponentId = CRM_Core_Component::getComponentID('CiviMember');
181 }
182
183 $attributes = CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceFieldValue');
184
185 $this->add('select', 'financial_type_id',
186 ts('Financial Type'),
187 [' ' => ts('- select -')] + $financialType
188 );
189
190 $this->assign('useForMember', FALSE);
191 if (in_array($eventComponentId, $this->_extendComponentId)) {
192 $this->add('text', 'count', ts('Participant Count'), $attributes['count']);
193
194 $this->addRule('count', ts('Participant Count should be a positive number'), 'positiveInteger');
195
196 $this->add('text', 'max_value', ts('Max Participants'), $attributes['max_value']);
197 $this->addRule('max_value', ts('Please enter a valid Max Participants.'), 'positiveInteger');
198
199 $this->assign('useForEvent', TRUE);
200 }
201 else {
202 if (in_array($memberComponentId, $this->_extendComponentId)) {
203 $this->_useForMember = 1;
204 $this->assign('useForMember', $this->_useForMember);
205 }
206 $this->assign('useForEvent', FALSE);
207 }
208
209 $sel = $this->add('select', 'html_type', ts('Input Field Type'),
210 $htmlTypes, TRUE, $javascript
211 );
212
213 // price (for text inputs)
214 $this->add('text', 'price', ts('Price'));
215 $this->registerRule('price', 'callback', 'money', 'CRM_Utils_Rule');
216 $this->addRule('price', ts('must be a monetary value'), 'money');
217
218 $this->add('text', 'non_deductible_amount', ts('Non-deductible Amount'), NULL);
219 $this->registerRule('non_deductible_amount', 'callback', 'money', 'CRM_Utils_Rule');
220 $this->addRule('non_deductible_amount', ts('Please enter a monetary value for this field.'), 'money');
221
222 if ($this->_action == CRM_Core_Action::UPDATE) {
223 $this->freeze('html_type');
224 }
225
226 // form fields of Custom Option rows
227 $_showHide = new CRM_Core_ShowHideBlocks('', '');
228
229 for ($i = 1; $i <= self::NUM_OPTION; $i++) {
230
231 //the show hide blocks
232 $showBlocks = 'optionField_' . $i;
233 if ($i > 2) {
234 $_showHide->addHide($showBlocks);
235 if ($i == self::NUM_OPTION) {
236 $_showHide->addHide('additionalOption');
237 }
238 }
239 else {
240 $_showHide->addShow($showBlocks);
241 }
242 // label
243 $attributes['label']['size'] = 25;
244 $this->add('text', 'option_label[' . $i . ']', ts('Label'), $attributes['label']);
245
246 // amount
247 $this->add('text', 'option_amount[' . $i . ']', ts('Amount'), $attributes['amount']);
248 $this->addRule('option_amount[' . $i . ']', ts('Please enter a valid amount for this field.'), 'money');
249
250 //Financial Type
251 $this->add(
252 'select',
253 'option_financial_type_id[' . $i . ']',
254 ts('Financial Type'),
255 ['' => ts('- select -')] + $financialType
256 );
257 if (in_array($eventComponentId, $this->_extendComponentId)) {
258 // count
259 $this->add('text', 'option_count[' . $i . ']', ts('Participant Count'), $attributes['count']);
260 $this->addRule('option_count[' . $i . ']', ts('Please enter a valid Participants Count.'), 'positiveInteger');
261
262 // max_value
263 $this->add('text', 'option_max_value[' . $i . ']', ts('Max Participants'), $attributes['max_value']);
264 $this->addRule('option_max_value[' . $i . ']', ts('Please enter a valid Max Participants.'), 'positiveInteger');
265
266 // description
267 //$this->add('textArea', 'option_description['.$i.']', ts('Description'), array('rows' => 1, 'cols' => 40 ));
268 }
269 elseif (in_array($memberComponentId, $this->_extendComponentId)) {
270 $membershipTypes = CRM_Member_PseudoConstant::membershipType();
271 $js = ['onchange' => "calculateRowValues( $i );"];
272
273 $this->add('select', 'membership_type_id[' . $i . ']', ts('Membership Type'),
274 ['' => ' '] + $membershipTypes, FALSE, $js
275 );
276 $this->add('text', 'membership_num_terms[' . $i . ']', ts('Number of Terms'), CRM_Utils_Array::value('membership_num_terms', $attributes));
277 }
278
279 // weight
280 $this->add('number', 'option_weight[' . $i . ']', ts('Order'), $attributes['weight']);
281
282 // is active ?
283 $this->add('checkbox', 'option_status[' . $i . ']', ts('Active?'));
284
285 $this->add('select', 'option_visibility_id[' . $i . ']', ts('Visibility'), $visibilityType);
286 $defaultOption[$i] = $this->createElement('radio', NULL, NULL, NULL, $i);
287
288 //for checkbox handling of default option
289 $this->add('checkbox', "default_checkbox_option[$i]", NULL);
290 }
291 //default option selection
292 $this->addGroup($defaultOption, 'default_option');
293 $_showHide->addToTemplate();
294
295 // is_display_amounts
296 $this->add('checkbox', 'is_display_amounts', ts('Display Amount?'));
297
298 // weight
299 $this->add('number', 'weight', ts('Order'), CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceField', 'weight'), TRUE);
300 $this->addRule('weight', ts('is a numeric field'), 'numeric');
301
302 // checkbox / radio options per line
303 $this->add('text', 'options_per_line', ts('Options Per Line'));
304 $this->addRule('options_per_line', ts('must be a numeric value'), 'numeric');
305
306 $this->add('textarea', 'help_pre', ts('Pre Field Help'),
307 CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceField', 'help_post')
308 );
309
310 // help post, mask, attributes, javascript ?
311 $this->add('textarea', 'help_post', ts('Post Field Help'),
312 CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceField', 'help_post')
313 );
314
315 $this->add('datepicker', 'active_on', ts('Active On'), [], FALSE, ['time' => TRUE]);
316
317 // expire_on
318 $this->add('datepicker', 'expire_on', ts('Expire On'), [], FALSE, ['time' => TRUE]);
319
320 // is required ?
321 $this->add('checkbox', 'is_required', ts('Required?'));
322
323 // is active ?
324 $this->add('checkbox', 'is_active', ts('Active?'));
325
326 // add buttons
327 $this->addButtons([
328 [
329 'type' => 'next',
330 'name' => ts('Save'),
331 'isDefault' => TRUE,
332 ],
333 [
334 'type' => 'next',
335 'name' => ts('Save and New'),
336 'subName' => 'new',
337 ],
338 [
339 'type' => 'cancel',
340 'name' => ts('Cancel'),
341 ],
342 ]);
343 // is public?
344 $this->add('select', 'visibility_id', ts('Visibility'), CRM_Core_PseudoConstant::visibility());
345
346 // add a form rule to check default value
347 $this->addFormRule(['CRM_Price_Form_Field', 'formRule'], $this);
348
349 // if view mode pls freeze it with the done button.
350 if ($this->_action & CRM_Core_Action::VIEW) {
351 $this->freeze();
352 $url = CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid);
353 $this->addElement('button',
354 'done',
355 ts('Done'),
356 ['onclick' => "location.href='$url'"]
357 );
358 }
359 }
360
361 /**
362 * Global validation rules for the form.
363 *
364 * @param array $fields
365 * Posted values of the form.
366 *
367 * @param $files
368 * @param CRM_Core_Form $form
369 *
370 * @return array
371 * if errors then list of errors to be posted back to the form,
372 * true otherwise
373 */
374 public static function formRule($fields, $files, $form) {
375
376 // all option fields are of type "money"
377 $errors = [];
378
379 /** Check the option values entered
380 * Appropriate values are required for the selected datatype
381 * Incomplete row checking is also required.
382 */
383 if (($form->_action & CRM_Core_Action::ADD || $form->_action & CRM_Core_Action::UPDATE) &&
384 $fields['html_type'] == 'Text'
385 ) {
386 if ($fields['price'] == NULL) {
387 $errors['price'] = ts('Price is a required field');
388 }
389 if ($fields['financial_type_id'] == '') {
390 $errors['financial_type_id'] = ts('Financial Type is a required field');
391 }
392 }
393
394 //avoid the same price field label in Within PriceSet
395 $priceFieldLabel = new CRM_Price_DAO_PriceField();
396 $priceFieldLabel->label = $fields['label'];
397 $priceFieldLabel->price_set_id = $form->_sid;
398
399 $dupeLabel = FALSE;
400 if ($priceFieldLabel->find(TRUE) && $form->_fid != $priceFieldLabel->id) {
401 $dupeLabel = TRUE;
402 }
403
404 if ($dupeLabel) {
405 $errors['label'] = ts('Name already exists in Database.');
406 }
407
408 if ((is_numeric(CRM_Utils_Array::value('count', $fields)) &&
409 empty($fields['count'])
410 ) &&
411 (CRM_Utils_Array::value('html_type', $fields) == 'Text')
412 ) {
413 $errors['count'] = ts('Participant Count must be greater than zero.');
414 }
415
416 if ($form->_action & CRM_Core_Action::ADD) {
417 if ($fields['html_type'] != 'Text') {
418 $countemptyrows = 0;
419 $publicOptionCount = $_flagOption = $_rowError = 0;
420
421 $_showHide = new CRM_Core_ShowHideBlocks('', '');
422 $visibilityOptions = CRM_Price_BAO_PriceFieldValue::buildOptions('visibility_id', 'validate');
423
424 for ($index = 1; $index <= self::NUM_OPTION; $index++) {
425
426 $noLabel = $noAmount = $noWeight = 1;
427 if (!empty($fields['option_label'][$index])) {
428 $noLabel = 0;
429 $duplicateIndex = CRM_Utils_Array::key($fields['option_label'][$index],
430 $fields['option_label']
431 );
432
433 if ((!($duplicateIndex === FALSE)) &&
434 (!($duplicateIndex == $index))
435 ) {
436 $errors["option_label[{$index}]"] = ts('Duplicate label value');
437 $_flagOption = 1;
438 }
439 }
440 if ($form->_useForMember) {
441 if (!empty($fields['membership_type_id'][$index])) {
442 $memTypesIDS[] = $fields['membership_type_id'][$index];
443 }
444 }
445
446 // allow for 0 value.
447 if (!empty($fields['option_amount'][$index]) ||
448 strlen($fields['option_amount'][$index]) > 0
449 ) {
450 $noAmount = 0;
451 }
452
453 if (!empty($fields['option_weight'][$index])) {
454 $noWeight = 0;
455 $duplicateIndex = CRM_Utils_Array::key($fields['option_weight'][$index],
456 $fields['option_weight']
457 );
458
459 if ((!($duplicateIndex === FALSE)) &&
460 (!($duplicateIndex == $index))
461 ) {
462 $errors["option_weight[{$index}]"] = ts('Duplicate weight value');
463 $_flagOption = 1;
464 }
465 }
466 if (!$noLabel && !$noAmount && !empty($fields['option_financial_type_id']) && $fields['option_financial_type_id'][$index] == '' && $fields['html_type'] != 'Text') {
467 $errors["option_financial_type_id[{$index}]"] = ts('Financial Type is a Required field.');
468 }
469 if ($noLabel && !$noAmount) {
470 $errors["option_label[{$index}]"] = ts('Label cannot be empty.');
471 $_flagOption = 1;
472 }
473
474 if (!$noLabel && $noAmount) {
475 $errors["option_amount[{$index}]"] = ts('Amount cannot be empty.');
476 $_flagOption = 1;
477 }
478
479 if ($noLabel && $noAmount) {
480 $countemptyrows++;
481 $_emptyRow = 1;
482 }
483 elseif (!empty($fields['option_max_value'][$index]) &&
484 !empty($fields['option_count'][$index]) &&
485 ($fields['option_count'][$index] > $fields['option_max_value'][$index])
486 ) {
487 $errors["option_max_value[{$index}]"] = ts('Participant count can not be greater than max participants.');
488 $_flagOption = 1;
489 }
490
491 $showBlocks = 'optionField_' . $index;
492 if ($_flagOption) {
493 $_showHide->addShow($showBlocks);
494 $_rowError = 1;
495 }
496
497 if (!empty($_emptyRow)) {
498 $_showHide->addHide($showBlocks);
499 }
500 else {
501 $_showHide->addShow($showBlocks);
502 }
503 if ($index == self::NUM_OPTION) {
504 $hideBlock = 'additionalOption';
505 $_showHide->addHide($hideBlock);
506 }
507
508 if (!empty($fields['option_visibility_id'][$index]) && (!$noLabel || !$noAmount)) {
509 if ($visibilityOptions[$fields['option_visibility_id'][$index]] == 'public') {
510 $publicOptionCount++;
511 }
512 }
513
514 $_flagOption = $_emptyRow = 0;
515 }
516
517 if (!empty($memTypesIDS)) {
518 // check for checkboxes allowing user to select multiple memberships from same membership organization
519 if ($fields['html_type'] == 'CheckBox') {
520 $foundDuplicate = FALSE;
521 $orgIds = [];
522 foreach ($memTypesIDS as $key => $val) {
523 $org = CRM_Member_BAO_MembershipType::getMembershipTypeOrganization($val);
524 if (in_array($org[$val], $orgIds)) {
525 $foundDuplicate = TRUE;
526 break;
527 }
528 $orgIds[$val] = $org[$val];
529
530 }
531 if ($foundDuplicate) {
532 $errors['_qf_default'] = ts('You have selected multiple memberships for the same organization or entity. Please review your selections and choose only one membership per entity.');
533 }
534 }
535
536 // CRM-10390 - Only one price field in a set can include auto-renew membership options
537 $foundAutorenew = FALSE;
538 foreach ($memTypesIDS as $key => $val) {
539 // see if any price field option values in this price field are for memberships with autorenew
540 $memTypeDetails = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($val);
541 if (!empty($memTypeDetails['auto_renew'])) {
542 $foundAutorenew = TRUE;
543 break;
544 }
545 }
546
547 if ($foundAutorenew) {
548 // if so, check for other fields in this price set which also have auto-renew membership options
549 $otherFieldAutorenew = CRM_Price_BAO_PriceSet::checkAutoRenewForPriceSet($form->_sid);
550 if ($otherFieldAutorenew) {
551 $errors['_qf_default'] = ts('You can include auto-renew membership choices for only one price field in a price set. Another field in this set already contains one or more auto-renew membership options.');
552 }
553 }
554 }
555 $_showHide->addToTemplate();
556
557 if ($countemptyrows == 15) {
558 $errors['option_label[1]'] = $errors['option_amount[1]'] = ts('Label and value cannot be empty.');
559 $_flagOption = 1;
560 }
561
562 if ($visibilityOptions[$fields['visibility_id']] == 'public' && $publicOptionCount == 0) {
563 $errors['visibility_id'] = ts('You have selected to make this field public but have not enabled any public price options. Please update your selections to include a public price option, or make this field admin visibility only.');
564 for ($index = 1; $index <= self::NUM_OPTION; $index++) {
565 if (!empty($fields['option_label'][$index]) || !empty($fields['option_amount'][$index])) {
566 $errors["option_visibility_id[{$index}]"] = ts('Public field should at least have one public option.');
567 }
568 }
569 }
570
571 if ($visibilityOptions[$fields['visibility_id']] == 'admin' && $publicOptionCount > 0) {
572 $errors['visibility_id'] = ts('Field with \'Admin\' visibility should only contain \'Admin\' options.');
573
574 for ($index = 1; $index <= self::NUM_OPTION; $index++) {
575
576 $isOptionSet = !empty($fields['option_label'][$index]) || !empty($fields['option_amount'][$index]);
577 $currentOptionVisibility = $visibilityOptions[$fields['option_visibility_id'][$index]] ?? NULL;
578
579 if ($isOptionSet && $currentOptionVisibility == 'public') {
580 $errors["option_visibility_id[{$index}]"] = ts('\'Admin\' field should only have \'Admin\' visibility options.');
581 }
582 }
583 }
584 }
585 elseif (!empty($fields['max_value']) &&
586 !empty($fields['count']) &&
587 ($fields['count'] > $fields['max_value'])
588 ) {
589 $errors['max_value'] = ts('Participant count can not be greater than max participants.');
590 }
591
592 // do not process if no option rows were submitted
593 if (empty($fields['option_amount']) && empty($fields['option_label'])) {
594 return TRUE;
595 }
596
597 if (empty($fields['option_name'])) {
598 $fields['option_amount'] = [];
599 }
600
601 if (empty($fields['option_label'])) {
602 $fields['option_label'] = [];
603 }
604 }
605
606 return empty($errors) ? TRUE : $errors;
607 }
608
609 /**
610 * Process the form.
611 *
612 * @throws \CRM_Core_Exception
613 * @throws \CiviCRM_API3_Exception
614 */
615 public function postProcess() {
616 // store the submitted values in an array
617 $params = $this->controller->exportValues('Field');
618 $params['price'] = CRM_Utils_Rule::cleanMoney($params['price']);
619
620 $params['is_display_amounts'] = CRM_Utils_Array::value('is_display_amounts', $params, FALSE);
621 $params['is_required'] = CRM_Utils_Array::value('is_required', $params, FALSE);
622 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
623 $params['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $params, FALSE);
624 $params['visibility_id'] = CRM_Utils_Array::value('visibility_id', $params, FALSE);
625 $params['count'] = CRM_Utils_Array::value('count', $params, FALSE);
626
627 // need the FKEY - price set id
628 $params['price_set_id'] = $this->_sid;
629
630 if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) {
631 $fieldValues = ['price_set_id' => $this->_sid];
632 $oldWeight = NULL;
633 if ($this->_fid) {
634 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid, 'weight', 'id');
635 }
636 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Price_DAO_PriceField', $oldWeight, $params['weight'], $fieldValues);
637 }
638
639 // make value <=> name consistency.
640 if (isset($params['option_name'])) {
641 $params['option_value'] = $params['option_name'];
642 }
643 $params['is_enter_qty'] = CRM_Utils_Array::value('is_enter_qty', $params, FALSE);
644
645 if ($params['html_type'] === 'Text') {
646 // if html type is Text, force is_enter_qty on
647 $params['is_enter_qty'] = 1;
648 // modify params values as per the option group and option
649 // value
650 $params['option_amount'] = [1 => $params['price']];
651 $params['option_label'] = [1 => $params['label']];
652 $params['option_count'] = [1 => $params['count']];
653 $params['option_max_value'] = [1 => CRM_Utils_Array::value('max_value', $params)];
654 //$params['option_description'] = array( 1 => $params['description'] );
655 $params['option_weight'] = [1 => $params['weight']];
656 $params['option_financial_type_id'] = [1 => $params['financial_type_id']];
657 $params['option_visibility_id'] = [1 => CRM_Utils_Array::value('visibility_id', $params)];
658 }
659
660 if ($this->_fid) {
661 $params['id'] = $this->_fid;
662 }
663
664 $params['membership_num_terms'] = (!empty($params['membership_type_id'])) ? CRM_Utils_Array::value('membership_num_terms', $params, 1) : NULL;
665
666 $priceField = CRM_Price_BAO_PriceField::create($params);
667
668 if (!is_a($priceField, 'CRM_Core_Error')) {
669 CRM_Core_Session::setStatus(ts('Price Field \'%1\' has been saved.', [1 => $priceField->label]), ts('Saved'), 'success');
670 }
671 $buttonName = $this->controller->getButtonName();
672 $session = CRM_Core_Session::singleton();
673 if ($buttonName == $this->getButtonName('next', 'new')) {
674 CRM_Core_Session::setStatus(ts(' You can add another price set field.'), '', 'info');
675 $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=add&sid=' . $this->_sid));
676 }
677 else {
678 $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid));
679 }
680 }
681
682 }