Skip to content

Commit

Permalink
Merge pull request #29248 from eileenmcnaughton/split
Browse files Browse the repository at this point in the history
Unshare another function to permit code cleanup
  • Loading branch information
eileenmcnaughton authored Feb 5, 2024
2 parents 41a6aa7 + a4bb381 commit 4e8f1d9
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
78 changes: 77 additions & 1 deletion CRM/Event/Form/ParticipantFeeSelection.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ private function formatFieldsForOptionFull(): void {
}

//get the current price event price set options count.
$currentOptionsCount = CRM_Event_Form_Registration::getPriceSetOptionCount($form);
$currentOptionsCount = $this->getPriceOptionCount();
$recordedOptionsCount = CRM_Event_BAO_Participant::priceSetOptionsCount($form->_eventId, []);
$optionFullTotalAmount = 0;
$currentParticipantNo = (int) substr($form->_name, 12);
Expand Down Expand Up @@ -804,4 +804,80 @@ private function initializeEventFee(): void {
}
}

/**
* Calculate total count for each price set options.
*
* - currently selected by user.
*
* @return array
* array of each option w/ count total.
*/
private function getPriceOptionCount() {
$form = $this;
$params = $form->get('params');
$priceSet = $form->get('priceSet');
$priceSetId = $form->get('priceSetId');

$optionsCount = [];
if (!$priceSetId ||
!is_array($priceSet) ||
empty($priceSet) ||
!is_array($params) ||
empty($params)
) {
return $optionsCount;
}

$priceSetFields = $priceMaxFieldDetails = [];
// @todo - replace this line with if ($this->getOrder()->isUseParticipantCount()) {
// (pending https://github.com/civicrm/civicrm-core/pull/29249 being merged)
if (!empty($priceSet['optionsCountTotal'])) {
$priceSetFields = $priceSet['optionsCountDetails']['fields'];
}

if (!empty($priceSet['optionsMaxValueTotal'])) {
$priceMaxFieldDetails = $priceSet['optionsMaxValueDetails']['fields'];
}

$addParticipantNum = substr($form->_name, 12);
foreach ($params as $pCnt => $values) {
if ($values == 'skip' ||
$pCnt === $addParticipantNum
) {
continue;
}

foreach ($values as $valKey => $value) {
if (strpos($valKey, 'price_') === FALSE) {
continue;
}

$priceFieldId = substr($valKey, 6);
if (!$priceFieldId ||
!is_array($value) ||
!(array_key_exists($priceFieldId, $priceSetFields) || array_key_exists($priceFieldId, $priceMaxFieldDetails))
) {
continue;
}

foreach ($value as $optId => $optVal) {
if (($priceSet['fields'][$priceFieldId]['html_type'] ?? NULL) === 'Text') {
$currentCount = $optVal;
}
else {
$currentCount = 1;
}

if (isset($priceSetFields[$priceFieldId]) && isset($priceSetFields[$priceFieldId]['options'][$optId])) {
$currentCount = $priceSetFields[$priceFieldId]['options'][$optId] * $optVal;
}

$optionsCount[$optId] = $currentCount + ($optionsCount[$optId] ?? 0);
}
}
}

return $optionsCount;
}

}
10 changes: 4 additions & 6 deletions CRM/Event/Form/Registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -1049,13 +1049,11 @@ public static function formatPriceSetParams(&$form, $params) {
*
* - currently selected by user.
*
* @param CRM_Core_Form $form
* Form object.
*
* @return array
* array of each option w/ count total.
*/
public static function getPriceSetOptionCount(&$form) {
protected function getPriceSetOptionCount() {
$form = $this;
$params = $form->get('params');
$priceSet = $form->get('priceSet');
$priceSetId = $form->get('priceSetId');
Expand Down Expand Up @@ -1307,7 +1305,7 @@ protected function getAvailableSpaces(): int {
*
* @return array
*/
protected static function validatePriceSet(array $params, $feeBlock, $priceSetId, $priceSetDetails) {
protected function validatePriceSet(array $params, $feeBlock, $priceSetId, $priceSetDetails) {
$errors = [];
$hasOptMaxValue = FALSE;
if (!is_array($params) || empty($params)) {
Expand Down Expand Up @@ -1958,7 +1956,7 @@ protected function formatPriceFieldsForFull(&$feeBlock): void {
}

//get the current price event price set options count.
$currentOptionsCount = self::getPriceSetOptionCount($form);
$currentOptionsCount = $form->getPriceSetOptionCount();
$recordedOptionsCount = CRM_Event_BAO_Participant::priceSetOptionsCount($form->_eventId, $skipParticipants);

$currentParticipantNo = (int) substr($form->_name, 12);
Expand Down

0 comments on commit 4e8f1d9

Please sign in to comment.