Skip to content

Commit

Permalink
Merge pull request Smile-SA#3475 from rbayet/fix-analytics-context-sw…
Browse files Browse the repository at this point in the history
…itching

[Analytics] Date picker vs switchers (store, customer group, company)
  • Loading branch information
rbayet authored Dec 20, 2024
2 parents 690c5ea + 5d30e25 commit 21805db
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Magento\Framework\Module\Manager as ModuleManager;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Smile\ElasticsuiteAnalytics\Model\Report\Context as ReportContext;

/**
* Block used to display customer company selector in reports.
Expand Down Expand Up @@ -47,6 +48,11 @@ class CustomerCompanySelector extends Template
*/
protected $searchCriteriaBuilder;

/**
* @var ReportContext
*/
protected $reportContext;

/**
* @var \Magento\Company\Api\CompanyRepositoryInterface|null
*/
Expand All @@ -61,6 +67,7 @@ class CustomerCompanySelector extends Template
* @param ModuleManager $moduleManager Module manager.
* @param ScopeConfigInterface $scopeConfig Scope configuration.
* @param SearchCriteriaBuilder $searchCriteriaBuilder The search criteria builder.
* @param ReportContext $reportContext Report context.
* @param array $data Additional block data.
* @throws LocalizedException
*/
Expand All @@ -69,10 +76,12 @@ public function __construct(
ModuleManager $moduleManager,
ScopeConfigInterface $scopeConfig,
SearchCriteriaBuilder $searchCriteriaBuilder,
ReportContext $reportContext,
array $data = []
) {
$this->scopeConfig = $scopeConfig;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->reportContext = $reportContext;

// Check if Magento_Company module is enabled before attempting to load the repository.
if ($moduleManager->isEnabled('Magento_Company')) {
Expand Down Expand Up @@ -117,4 +126,14 @@ public function getCompaniesList()

return [];
}

/**
* Get customer company ID.
*
* @return mixed
*/
public function getCustomerCompanyId()
{
return $this->reportContext->getCustomerCompanyId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Magento\Framework\View\Element\Template;
use Magento\Customer\Model\ResourceModel\Group\CollectionFactory;
use Smile\ElasticsuiteAnalytics\Model\Report\Context as ReportContext;

/**
* Block used to display customer group selector in reports.
Expand All @@ -32,19 +33,27 @@ class CustomerGroupSelector extends Template
*/
protected $customerGroupCollectionFactory;

/**
* @var ReportContext
*/
protected $reportContext;

/**
* CustomerGroupSelector constructor.
*
* @param Template\Context $context The context of the template.
* @param CollectionFactory $customerGroupCollectionFactory Factory for creating customer group collection.
* @param ReportContext $reportContext Report context.
* @param array $data Additional block data.
*/
public function __construct(
Template\Context $context,
CollectionFactory $customerGroupCollectionFactory,
ReportContext $reportContext,
array $data = []
) {
$this->customerGroupCollectionFactory = $customerGroupCollectionFactory;
$this->reportContext = $reportContext;
parent::__construct($context, $data);
}

Expand All @@ -57,4 +66,14 @@ public function getCustomerGroups()
{
return $this->customerGroupCollectionFactory->create()->toOptionArray();
}

/**
* Get customer group ID.
*
* @return mixed
*/
public function getCurrentCustomerGroupId()
{
return $this->reportContext->getCustomerGroupId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
/**
* @var Smile\ElasticsuiteAnalytics\Block\Adminhtml\Report\CustomerCompanySelector $block
*/
$baseUrl = $block->getUrl('*/*/*', ['_current' => true, 'company_id' => '__company_id__']);
$companyId = $block->getCustomerCompanyId();
?>
<?php if ($block->isCompanyEnabled()): ?>
<?php $companies = $block->getCompaniesList(); ?>
Expand All @@ -25,15 +27,17 @@
<select id="company_id" name="company_id" class="admin__control-select">
<option value="all"><?= __('All Companies');?></option>
<?php foreach ($companies as $company): ?>
<option value="<?= $company->getId(); ?>"><?= $company->getCompanyName(); ?></option>
<option value="<?= $block->escapeHtmlAttr($company->getId()); ?>" <?php if ($companyId == $company->getId()): ?> selected="selected"<?php endif; ?>><?= $block->escapeHtml($company->getCompanyName()); ?></option>
<?php endforeach; ?>
</select>
</div>

<script type="text/x-magento-init">
{
"*": {
"customerCompanySelector": {}
"customerCompanySelector": {
"baseUrl": "<?= $block->escapeJs($block->escapeUrl($baseUrl)) ?>"
}
}
}
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,25 @@
* @var Smile\ElasticsuiteAnalytics\Block\Adminhtml\Report\CustomerGroupSelector $block
*/
$customerGroups = $block->getCustomerGroups();
$customerGroupId = $block->getCurrentCustomerGroupId();
$baseUrl = $block->getUrl('*/*/*', ['_current' => true, 'customer_group' => '__customer_group__']);
?>
<div class="customer-group-selector">
<label for="customer_group"><?= __('Customer Group:');?></label>
<select id="customer_group" name="customer_group" class="admin__control-select">
<option value="all"><?= __('All Customer Groups');?></option>
<?php foreach ($customerGroups as $group): ?>
<option value="<?php echo $group['value']; ?>"><?php echo $group['label']; ?></option>
<option value="<?= $block->escapeHtmlAttr($group['value']); ?>" <?php if ($customerGroupId == $group['value']): ?> selected="selected"<?php endif; ?>><?= $block->escapeHtml($group['label']); ?></option>
<?php endforeach; ?>
</select>
</div>

<script type="text/x-magento-init">
{
"*": {
"customerGroupSelector": {}
"customerGroupSelector": {
"baseUrl": "<?= $block->escapeJs($block->escapeUrl($baseUrl)) ?>"
}
}
}
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
require(['jquery', 'mage/calendar', 'mage/adminhtml/tools'], function ($) {
$('#<?= $block->getJsId('date-range-picker'); ?>').dateRange(<?= $block->getJsConfig(); ?>);
$('#<?= $block->getJsId('date-range-picker', 'apply'); ?>').on('click', function() {
var url = "<?= $block->getUrl('*/*/*', ['from' => '__from__', 'to' => '__to__']); ?>"
let url = "<?= $block->getUrl('*/*/*', ['_current' => true, 'from' => '__from__', 'to' => '__to__']); ?>"
.replace('__from__', Base64.encode($("#<?= $block->getJsId('date-range-picker', 'from'); ?>")[0].value))
.replace('__to__', Base64.encode($("#<?= $block->getJsId('date-range-picker', 'to'); ?>")[0].value));
window.location = url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,16 @@
*/

define('Smile_ElasticsuiteAnalytics/js/report/customer-company-selector', [
'jquery',
'mage/url'
], function($, urlBuilder) {
'jquery'
], function($) {
'use strict';

return function() {
// On document ready, set the selected value in the company dropdown.
$(document).ready(function() {
var urlParams = new URLSearchParams(window.location.search);
var selectedCompany = urlParams.get('company_id');

if (selectedCompany) {
$('#company_id').val(selectedCompany);
}
});

return function(config) {
// Handle the company dropdown value change.
$('#company_id').on('change', function() {
var selectedCompany = $(this).val();
var newUrl = new URL(window.location.href);

newUrl.searchParams.set('company_id', selectedCompany);
let selectedCompany = $(this).val();

// Redirect to the new URL with the company filter.
window.location.href = newUrl.href;
window.location = config.baseUrl.replace('__company_id__', selectedCompany);
});
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,16 @@
*/

define('Smile_ElasticsuiteAnalytics/js/report/customer-group-selector', [
'jquery',
'mage/url'
], function($, urlBuilder) {
'jquery'
], function($) {
'use strict';

return function() {
// !On document ready, set the selected value in the customer group dropdown.
$(document).ready(function() {
var urlParams = new URLSearchParams(window.location.search);
var selectedGroup = urlParams.get('customer_group');

if (selectedGroup) {
$('#customer_group').val(selectedGroup);
}
});

return function(config) {
// Handle the customer group dropdown value change.
$('#customer_group').on('change', function() {
var selectedGroup = $(this).val();
var newUrl = new URL(window.location.href);

newUrl.searchParams.set('customer_group', selectedGroup);
let selectedGroup = $(this).val();

// Redirect to the new URL with the customer group filter.
window.location.href = newUrl.href;
window.location = config.baseUrl.replace('__customer_group__', selectedGroup);
});
};
});
Expand Down

0 comments on commit 21805db

Please sign in to comment.