From 5d30e259617e9d973b7a86f3961b7b4fe0ea5c26 Mon Sep 17 00:00:00 2001 From: Richard BAYET Date: Thu, 19 Dec 2024 18:08:58 +0100 Subject: [PATCH] [Analytics] Date picker vs switchers (store, customer group, company) --- .../Report/CustomerCompanySelector.php | 19 ++++++++++++++ .../Report/CustomerGroupSelector.php | 19 ++++++++++++++ .../report/customer_company_selector.phtml | 8 ++++-- .../report/customer_group_selector.phtml | 8 ++++-- .../report/date_range_switcher.phtml | 2 +- .../js/report/customer-company-selector.js | 25 ++++--------------- .../web/js/report/customer-group-selector.js | 25 ++++--------------- 7 files changed, 61 insertions(+), 45 deletions(-) diff --git a/src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerCompanySelector.php b/src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerCompanySelector.php index 8f88247bf..13a3fa46a 100644 --- a/src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerCompanySelector.php +++ b/src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerCompanySelector.php @@ -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. @@ -47,6 +48,11 @@ class CustomerCompanySelector extends Template */ protected $searchCriteriaBuilder; + /** + * @var ReportContext + */ + protected $reportContext; + /** * @var \Magento\Company\Api\CompanyRepositoryInterface|null */ @@ -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 */ @@ -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')) { @@ -117,4 +126,14 @@ public function getCompaniesList() return []; } + + /** + * Get customer company ID. + * + * @return mixed + */ + public function getCustomerCompanyId() + { + return $this->reportContext->getCustomerCompanyId(); + } } diff --git a/src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerGroupSelector.php b/src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerGroupSelector.php index 99170e56d..01612d896 100644 --- a/src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerGroupSelector.php +++ b/src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerGroupSelector.php @@ -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. @@ -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); } @@ -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(); + } } diff --git a/src/module-elasticsuite-analytics/view/adminhtml/templates/report/customer_company_selector.phtml b/src/module-elasticsuite-analytics/view/adminhtml/templates/report/customer_company_selector.phtml index b871b4230..fdd977b02 100644 --- a/src/module-elasticsuite-analytics/view/adminhtml/templates/report/customer_company_selector.phtml +++ b/src/module-elasticsuite-analytics/view/adminhtml/templates/report/customer_company_selector.phtml @@ -17,6 +17,8 @@ /** * @var Smile\ElasticsuiteAnalytics\Block\Adminhtml\Report\CustomerCompanySelector $block */ +$baseUrl = $block->getUrl('*/*/*', ['_current' => true, 'company_id' => '__company_id__']); +$companyId = $block->getCustomerCompanyId(); ?> isCompanyEnabled()): ?> getCompaniesList(); ?> @@ -25,7 +27,7 @@ @@ -33,7 +35,9 @@ diff --git a/src/module-elasticsuite-analytics/view/adminhtml/templates/report/customer_group_selector.phtml b/src/module-elasticsuite-analytics/view/adminhtml/templates/report/customer_group_selector.phtml index b004c3bc5..ded0655aa 100644 --- a/src/module-elasticsuite-analytics/view/adminhtml/templates/report/customer_group_selector.phtml +++ b/src/module-elasticsuite-analytics/view/adminhtml/templates/report/customer_group_selector.phtml @@ -18,13 +18,15 @@ * @var Smile\ElasticsuiteAnalytics\Block\Adminhtml\Report\CustomerGroupSelector $block */ $customerGroups = $block->getCustomerGroups(); +$customerGroupId = $block->getCurrentCustomerGroupId(); +$baseUrl = $block->getUrl('*/*/*', ['_current' => true, 'customer_group' => '__customer_group__']); ?>
@@ -32,7 +34,9 @@ $customerGroups = $block->getCustomerGroups(); diff --git a/src/module-elasticsuite-analytics/view/adminhtml/templates/report/date_range_switcher.phtml b/src/module-elasticsuite-analytics/view/adminhtml/templates/report/date_range_switcher.phtml index 5f0310592..e5bc62740 100644 --- a/src/module-elasticsuite-analytics/view/adminhtml/templates/report/date_range_switcher.phtml +++ b/src/module-elasticsuite-analytics/view/adminhtml/templates/report/date_range_switcher.phtml @@ -57,7 +57,7 @@ require(['jquery', 'mage/calendar', 'mage/adminhtml/tools'], function ($) { $('#getJsId('date-range-picker'); ?>').dateRange(getJsConfig(); ?>); $('#getJsId('date-range-picker', 'apply'); ?>').on('click', function() { - var url = "getUrl('*/*/*', ['from' => '__from__', 'to' => '__to__']); ?>" + let url = "getUrl('*/*/*', ['_current' => true, 'from' => '__from__', 'to' => '__to__']); ?>" .replace('__from__', Base64.encode($("#getJsId('date-range-picker', 'from'); ?>")[0].value)) .replace('__to__', Base64.encode($("#getJsId('date-range-picker', 'to'); ?>")[0].value)); window.location = url; diff --git a/src/module-elasticsuite-analytics/view/adminhtml/web/js/report/customer-company-selector.js b/src/module-elasticsuite-analytics/view/adminhtml/web/js/report/customer-company-selector.js index 40c5bb880..aba5e83ed 100644 --- a/src/module-elasticsuite-analytics/view/adminhtml/web/js/report/customer-company-selector.js +++ b/src/module-elasticsuite-analytics/view/adminhtml/web/js/report/customer-company-selector.js @@ -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); }); }; }); diff --git a/src/module-elasticsuite-analytics/view/adminhtml/web/js/report/customer-group-selector.js b/src/module-elasticsuite-analytics/view/adminhtml/web/js/report/customer-group-selector.js index 54089bc0e..b2b96548f 100644 --- a/src/module-elasticsuite-analytics/view/adminhtml/web/js/report/customer-group-selector.js +++ b/src/module-elasticsuite-analytics/view/adminhtml/web/js/report/customer-group-selector.js @@ -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); }); }; });