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