3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * form to process actions on the field aspect of Price
21 class CRM_Price_Form_Field
extends CRM_Core_Form
{
24 * Constants for number of options for data types of multiple option.
26 const NUM_OPTION
= 15;
29 * The custom set id saved to the session for an update.
36 * The field id, used when editing the field
43 * The extended component Id.
47 protected $_extendComponentId;
50 * Variable is set if price set is used for membership.
53 protected $_useForMember;
56 * Set variables up before form is built.
58 public function preProcess() {
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]];
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);
71 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
73 $this->setPageTitle(ts('Price Field'));
77 * Set default values for the form.
80 * array of default values
82 public function setDefaultValues() {
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'];
91 // if text, retrieve price
92 if ($defaults['html_type'] == 'Text') {
93 $isActive = $defaults['is_active'];
94 $valueParams = ['price_field_id' => $this->_fid
];
96 CRM_Price_BAO_PriceFieldValue
::retrieve($valueParams, $defaults);
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;
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;
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;
119 $enabledComponents = CRM_Core_Component
::getEnabledComponents();
120 $eventComponentId = NULL;
121 if (array_key_exists('CiviEvent', $enabledComponents)) {
122 $eventComponentId = CRM_Core_Component
::getComponentID('CiviEvent');
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;
136 * Build the form object.
138 public function buildQuickForm() {
139 // lets trim all the whitespace
140 $this->applyFilter('__ALL__', 'trim');
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
);
148 $this->add('text', 'label', ts('Field Label'), CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceField', 'label'), TRUE);
151 $javascript = 'onchange="option_html_type(this.form)";';
153 $htmlTypes = CRM_Price_BAO_PriceField
::htmlTypes();
155 // Text box for Participant Count for a field
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)
163 unset($financialType[$finTypeId]);
166 if (count($financialType)) {
167 $this->assign('financialType', $financialType);
170 //Visibility Type Options
171 $visibilityType = CRM_Core_PseudoConstant
::visibility();
172 $this->assign('visibilityType', $visibilityType);
174 $enabledComponents = CRM_Core_Component
::getEnabledComponents();
175 $eventComponentId = $memberComponentId = NULL;
176 if (array_key_exists('CiviEvent', $enabledComponents)) {
177 $eventComponentId = CRM_Core_Component
::getComponentID('CiviEvent');
179 if (array_key_exists('CiviMember', $enabledComponents)) {
180 $memberComponentId = CRM_Core_Component
::getComponentID('CiviMember');
183 $attributes = CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceFieldValue');
185 $this->add('select', 'financial_type_id',
186 ts('Financial Type'),
187 [' ' => ts('- select -')] +
$financialType
190 $this->assign('useForMember', FALSE);
191 if (in_array($eventComponentId, $this->_extendComponentId
)) {
192 $this->add('text', 'count', ts('Participant Count'), $attributes['count']);
194 $this->addRule('count', ts('Participant Count should be a positive number'), 'positiveInteger');
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');
199 $this->assign('useForEvent', TRUE);
202 if (in_array($memberComponentId, $this->_extendComponentId
)) {
203 $this->_useForMember
= 1;
204 $this->assign('useForMember', $this->_useForMember
);
206 $this->assign('useForEvent', FALSE);
209 $sel = $this->add('select', 'html_type', ts('Input Field Type'),
210 $htmlTypes, TRUE, $javascript
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');
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');
222 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
223 $this->freeze('html_type');
226 // form fields of Custom Option rows
227 $_showHide = new CRM_Core_ShowHideBlocks('', '');
229 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
231 //the show hide blocks
232 $showBlocks = 'optionField_' . $i;
234 $_showHide->addHide($showBlocks);
235 if ($i == self
::NUM_OPTION
) {
236 $_showHide->addHide('additionalOption');
240 $_showHide->addShow($showBlocks);
243 $attributes['label']['size'] = 25;
244 $this->add('text', 'option_label[' . $i . ']', ts('Label'), $attributes['label']);
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');
253 'option_financial_type_id[' . $i . ']',
254 ts('Financial Type'),
255 ['' => ts('- select -')] +
$financialType
257 if (in_array($eventComponentId, $this->_extendComponentId
)) {
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');
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');
267 //$this->add('textArea', 'option_description['.$i.']', ts('Description'), array('rows' => 1, 'cols' => 40 ));
269 elseif (in_array($memberComponentId, $this->_extendComponentId
)) {
270 $membershipTypes = CRM_Member_PseudoConstant
::membershipType();
271 $js = ['onchange' => "calculateRowValues( $i );"];
273 $this->add('select', 'membership_type_id[' . $i . ']', ts('Membership Type'),
274 ['' => ' '] +
$membershipTypes, FALSE, $js
276 $this->add('text', 'membership_num_terms[' . $i . ']', ts('Number of Terms'), CRM_Utils_Array
::value('membership_num_terms', $attributes));
280 $this->add('number', 'option_weight[' . $i . ']', ts('Order'), $attributes['weight']);
283 $this->add('checkbox', 'option_status[' . $i . ']', ts('Active?'));
285 $this->add('select', 'option_visibility_id[' . $i . ']', ts('Visibility'), $visibilityType);
286 $defaultOption[$i] = $this->createElement('radio', NULL, NULL, NULL, $i);
288 //for checkbox handling of default option
289 $this->add('checkbox', "default_checkbox_option[$i]", NULL);
291 //default option selection
292 $this->addGroup($defaultOption, 'default_option');
293 $_showHide->addToTemplate();
295 // is_display_amounts
296 $this->add('checkbox', 'is_display_amounts', ts('Display Amount?'));
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');
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');
306 $this->add('textarea', 'help_pre', ts('Pre Field Help'),
307 CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceField', 'help_post')
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')
315 $this->add('datepicker', 'active_on', ts('Active On'), [], FALSE, ['time' => TRUE]);
318 $this->add('datepicker', 'expire_on', ts('Expire On'), [], FALSE, ['time' => TRUE]);
321 $this->add('checkbox', 'is_required', ts('Required?'));
324 $this->add('checkbox', 'is_active', ts('Active?'));
330 'name' => ts('Save'),
335 'name' => ts('Save and New'),
340 'name' => ts('Cancel'),
344 $this->add('select', 'visibility_id', ts('Visibility'), CRM_Core_PseudoConstant
::visibility());
346 // add a form rule to check default value
347 $this->addFormRule(['CRM_Price_Form_Field', 'formRule'], $this);
349 // if view mode pls freeze it with the done button.
350 if ($this->_action
& CRM_Core_Action
::VIEW
) {
352 $url = CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
);
353 $this->addElement('button',
356 ['onclick' => "location.href='$url'"]
362 * Global validation rules for the form.
364 * @param array $fields
365 * Posted values of the form.
368 * @param CRM_Core_Form $form
371 * if errors then list of errors to be posted back to the form,
374 public static function formRule($fields, $files, $form) {
376 // all option fields are of type "money"
379 /** Check the option values entered
380 * Appropriate values are required for the selected datatype
381 * Incomplete row checking is also required.
383 if (($form->_action
& CRM_Core_Action
::ADD ||
$form->_action
& CRM_Core_Action
::UPDATE
) &&
384 $fields['html_type'] == 'Text'
386 if ($fields['price'] == NULL) {
387 $errors['price'] = ts('Price is a required field');
389 if ($fields['financial_type_id'] == '') {
390 $errors['financial_type_id'] = ts('Financial Type is a required field');
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
;
400 if ($priceFieldLabel->find(TRUE) && $form->_fid
!= $priceFieldLabel->id
) {
405 $errors['label'] = ts('Name already exists in Database.');
408 if ((is_numeric(CRM_Utils_Array
::value('count', $fields)) &&
409 empty($fields['count'])
411 (CRM_Utils_Array
::value('html_type', $fields) == 'Text')
413 $errors['count'] = ts('Participant Count must be greater than zero.');
416 if ($form->_action
& CRM_Core_Action
::ADD
) {
417 if ($fields['html_type'] != 'Text') {
419 $publicOptionCount = $_flagOption = $_rowError = 0;
421 $_showHide = new CRM_Core_ShowHideBlocks('', '');
422 $visibilityOptions = CRM_Price_BAO_PriceFieldValue
::buildOptions('visibility_id', NULL, ['labelColumn' => 'name']);
424 for ($index = 1; $index <= self
::NUM_OPTION
; $index++
) {
426 $noLabel = $noAmount = $noWeight = 1;
427 if (!empty($fields['option_label'][$index])) {
429 $duplicateIndex = CRM_Utils_Array
::key($fields['option_label'][$index],
430 $fields['option_label']
433 if ((!($duplicateIndex === FALSE)) &&
434 (!($duplicateIndex == $index))
436 $errors["option_label[{$index}]"] = ts('Duplicate label value');
440 if ($form->_useForMember
) {
441 if (!empty($fields['membership_type_id'][$index])) {
442 $memTypesIDS[] = $fields['membership_type_id'][$index];
446 // allow for 0 value.
447 if (!empty($fields['option_amount'][$index]) ||
448 strlen($fields['option_amount'][$index]) > 0
453 if (!empty($fields['option_weight'][$index])) {
455 $duplicateIndex = CRM_Utils_Array
::key($fields['option_weight'][$index],
456 $fields['option_weight']
459 if ((!($duplicateIndex === FALSE)) &&
460 (!($duplicateIndex == $index))
462 $errors["option_weight[{$index}]"] = ts('Duplicate weight value');
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.');
469 if ($noLabel && !$noAmount) {
470 $errors["option_label[{$index}]"] = ts('Label cannot be empty.');
474 if (!$noLabel && $noAmount) {
475 $errors["option_amount[{$index}]"] = ts('Amount cannot be empty.');
479 if ($noLabel && $noAmount) {
483 elseif (!empty($fields['option_max_value'][$index]) &&
484 !empty($fields['option_count'][$index]) &&
485 ($fields['option_count'][$index] > $fields['option_max_value'][$index])
487 $errors["option_max_value[{$index}]"] = ts('Participant count can not be greater than max participants.');
491 $showBlocks = 'optionField_' . $index;
493 $_showHide->addShow($showBlocks);
497 if (!empty($_emptyRow)) {
498 $_showHide->addHide($showBlocks);
501 $_showHide->addShow($showBlocks);
503 if ($index == self
::NUM_OPTION
) {
504 $hideBlock = 'additionalOption';
505 $_showHide->addHide($hideBlock);
508 if (!empty($fields['option_visibility_id'][$index]) && (!$noLabel ||
!$noAmount)) {
509 if ($visibilityOptions[$fields['option_visibility_id'][$index]] == 'public') {
510 $publicOptionCount++
;
514 $_flagOption = $_emptyRow = 0;
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;
522 foreach ($memTypesIDS as $key => $val) {
523 $org = CRM_Member_BAO_MembershipType
::getMembershipTypeOrganization($val);
524 if (in_array($org[$val], $orgIds)) {
525 $foundDuplicate = TRUE;
528 $orgIds[$val] = $org[$val];
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.');
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;
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.');
555 $_showHide->addToTemplate();
557 if ($countemptyrows == 15) {
558 $errors['option_label[1]'] = $errors['option_amount[1]'] = ts('Label and value cannot be empty.');
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.');
571 if ($visibilityOptions[$fields['visibility_id']] == 'admin' && $publicOptionCount > 0) {
572 $errors['visibility_id'] = ts('Field with \'Admin\' visibility should only contain \'Admin\' options.');
574 for ($index = 1; $index <= self
::NUM_OPTION
; $index++
) {
576 $isOptionSet = !empty($fields['option_label'][$index]) ||
!empty($fields['option_amount'][$index]);
577 $currentOptionVisibility = $visibilityOptions[$fields['option_visibility_id'][$index]] ??
NULL;
579 if ($isOptionSet && $currentOptionVisibility == 'public') {
580 $errors["option_visibility_id[{$index}]"] = ts('\'Admin\' field should only have \'Admin\' visibility options.');
585 elseif (!empty($fields['max_value']) &&
586 !empty($fields['count']) &&
587 ($fields['count'] > $fields['max_value'])
589 $errors['max_value'] = ts('Participant count can not be greater than max participants.');
592 // do not process if no option rows were submitted
593 if (empty($fields['option_amount']) && empty($fields['option_label'])) {
597 if (empty($fields['option_name'])) {
598 $fields['option_amount'] = [];
601 if (empty($fields['option_label'])) {
602 $fields['option_label'] = [];
606 return empty($errors) ?
TRUE : $errors;
612 * @throws \CRM_Core_Exception
613 * @throws \CiviCRM_API3_Exception
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']);
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);
627 // need the FKEY - price set id
628 $params['price_set_id'] = $this->_sid
;
630 if ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD
)) {
631 $fieldValues = ['price_set_id' => $this->_sid
];
634 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid
, 'weight', 'id');
636 $params['weight'] = CRM_Utils_Weight
::updateOtherWeights('CRM_Price_DAO_PriceField', $oldWeight, $params['weight'], $fieldValues);
639 // make value <=> name consistency.
640 if (isset($params['option_name'])) {
641 $params['option_value'] = $params['option_name'];
643 $params['is_enter_qty'] = CRM_Utils_Array
::value('is_enter_qty', $params, FALSE);
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
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)];
661 $params['id'] = $this->_fid
;
664 $params['membership_num_terms'] = (!empty($params['membership_type_id'])) ? CRM_Utils_Array
::value('membership_num_terms', $params, 1) : NULL;
666 $priceField = CRM_Price_BAO_PriceField
::create($params);
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');
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
));
678 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
));