Skip to content

Commit

Permalink
PSHEASSN-517: Handle price feild label in Event Info page
Browse files Browse the repository at this point in the history
  • Loading branch information
ahed-compucorp committed Apr 15, 2021
1 parent d3f6bc5 commit 8da8e62
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions CRM/MembersOnlyEvent/Hook/PageRun/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use CRM_MembersOnlyEvent_Hook_PageRun_Base as PageRunBase;
use CRM_MembersOnlyEvent_BAO_MembersOnlyEvent as MembersOnlyEvent;
use CRM_MembersOnlyEvent_Service_MembersOnlyEventAccess as MembersOnlyEventAccessService;
use CRM_MembersOnlyEvent_BAO_MembersOnlyEventPriceFieldValue as MembersOnlyEventPriceFieldValue;

/**
* Class for Event Info PageRun Hook
Expand Down Expand Up @@ -43,6 +44,69 @@ protected function shouldHandle($pageName, &$page) {
*/
protected function pageRun(&$page) {
$this->showSessionMessageWhenRegisteringAnotherParticipant();
$this->handlePriceFieldValue();
}

/**
* Handles price field value for Event Info page.
*/
private function handlePriceFieldValue() {
$isEventForMembersOnly = $this->membersOnlyEventAccessService->getMembersOnlyEvent();

if (!$isEventForMembersOnly) {
return;
}

$membersOnlyEventID = $this->membersOnlyEventAccessService->getMembersOnlyEvent()->id;
$nonMemberPriceFieldValueIDs = MembersOnlyEventPriceFieldValue::getNonMemberPriceFieldValueIDs($membersOnlyEventID);
$hasMembership = $this->membersOnlyEventAccessService->hasMembership();

$priceFieldValueLabelsByIDs = [];
$priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $isEventForMembersOnly->event_id, NULL);

// Get only the values of the valid price fields
$priceFieldValues = civicrm_api3('PriceFieldValue', 'get', [
'sequential' => 1,
'price_field_id.price_set_id' => $priceSetId,
'return' => ['id', 'label'],
])['values'];

foreach ($priceFieldValues as $priceFieldValue) {
$priceFieldValueIDsByLabels[$priceFieldValue['label']] = $priceFieldValue['id'];
}

$_template = CRM_Core_Smarty::singleton();
$priceFieldValueToBeRemoved = [];
foreach ($_template->_tpl_vars['feeBlock']['label'] as $key => $label) {
$priceFieldValueID = $priceFieldValueIDsByLabels[$label] ?? NULL;
if (!$priceFieldValueID) {
// Skip price field label
continue;
}

$isNonMemberPriceFieldValue = in_array($priceFieldValueID, $nonMemberPriceFieldValueIDs);

if (!$hasMembership && !$isNonMemberPriceFieldValue) {
$priceFieldValueToBeRemoved[] = $key;
}

if ($hasMembership && $isNonMemberPriceFieldValue) {
$priceFieldValueToBeRemoved[] = $key;
}
}

foreach ($priceFieldValueToBeRemoved as $key) {
unset($_template->_tpl_vars['feeBlock']['value'][$key]);
unset($_template->_tpl_vars['feeBlock']['label'][$key]);
unset($_template->_tpl_vars['feeBlock']['lClass'][$key]);
unset($_template->_tpl_vars['feeBlock']['isDisplayAmount'][$key]);
}

// Change array key to start from 1 instead of 0
foreach (['value', 'label', 'lClass', 'isDisplayAmount'] as $key) {
array_unshift($_template->_tpl_vars['feeBlock'][$key], "");
unset($_template->_tpl_vars['feeBlock'][$key][0]);
}
}

/**
Expand Down

0 comments on commit 8da8e62

Please sign in to comment.