forked from Smile-SA/elasticsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Smile-SA#3459 from Smile-SA/feat-vahonc-3340-elast…
…icsuite-analytics-tracker-add-company_id-and-customer_group_id-in-tracking-data [Analytics][Tracker] Feature Smile-SA#3340, add company_id and customer_group_id in tracking data
- Loading branch information
Showing
18 changed files
with
888 additions
and
21 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerCompanySelector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer | ||
* versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteAnalytics | ||
* @author Vadym Honcharuk <vahonc@smile.fr> | ||
* @copyright 2024 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
namespace Smile\ElasticsuiteAnalytics\Block\Adminhtml\Report; | ||
|
||
use Magento\Framework\Api\SearchCriteriaBuilder; | ||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Framework\App\ObjectManager; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\Module\Manager as ModuleManager; | ||
use Magento\Framework\View\Element\Template; | ||
use Magento\Framework\View\Element\Template\Context; | ||
|
||
/** | ||
* Block used to display customer company selector in reports. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteAnalytics | ||
* @author Vadym Honcharuk <vahonc@smile.fr> | ||
*/ | ||
class CustomerCompanySelector extends Template | ||
{ | ||
/** | ||
* Company status configuration path. | ||
* | ||
* @var string | ||
*/ | ||
const CONFIG_IS_B2B_COMPANY_ACTIVE_XPATH = 'btob/website_configuration/company_active'; | ||
|
||
/** | ||
* @var ScopeConfigInterface | ||
*/ | ||
protected $scopeConfig; | ||
|
||
/** | ||
* @var SearchCriteriaBuilder | ||
*/ | ||
protected $searchCriteriaBuilder; | ||
|
||
/** | ||
* @var \Magento\Company\Api\CompanyRepositoryInterface|null | ||
*/ | ||
private $companyRepository = null; | ||
|
||
/** | ||
* CustomerCompanySelector constructor. | ||
* | ||
* @SuppressWarnings(PHPMD.ElseExpression) | ||
* | ||
* @param Context $context The template context. | ||
* @param ModuleManager $moduleManager Module manager. | ||
* @param ScopeConfigInterface $scopeConfig Scope configuration. | ||
* @param SearchCriteriaBuilder $searchCriteriaBuilder The search criteria builder. | ||
* @param array $data Additional block data. | ||
* @throws LocalizedException | ||
*/ | ||
public function __construct( | ||
Context $context, | ||
ModuleManager $moduleManager, | ||
ScopeConfigInterface $scopeConfig, | ||
SearchCriteriaBuilder $searchCriteriaBuilder, | ||
array $data = [] | ||
) { | ||
$this->scopeConfig = $scopeConfig; | ||
$this->searchCriteriaBuilder = $searchCriteriaBuilder; | ||
|
||
// Check if Magento_Company module is enabled before attempting to load the repository. | ||
if ($moduleManager->isEnabled('Magento_Company')) { | ||
if (interface_exists('\Magento\Company\Api\CompanyRepositoryInterface')) { | ||
$this->companyRepository = ObjectManager::getInstance()->get( | ||
\Magento\Company\Api\CompanyRepositoryInterface::class | ||
); | ||
} else { | ||
throw new LocalizedException(__('CompanyRepositoryInterface is not available.')); | ||
} | ||
} | ||
|
||
parent::__construct($context, $data); | ||
} | ||
|
||
/** | ||
* Check if the Company feature is enabled. | ||
* | ||
* @return bool | ||
*/ | ||
public function isCompanyEnabled() | ||
{ | ||
return $this->scopeConfig->isSetFlag( | ||
self::CONFIG_IS_B2B_COMPANY_ACTIVE_XPATH, | ||
\Magento\Store\Model\ScopeInterface::SCOPE_STORE | ||
); | ||
} | ||
|
||
/** | ||
* Get the list of companies if the Company feature is enabled. | ||
* | ||
* @return CompanyInterface[]|array | ||
* @throws LocalizedException | ||
*/ | ||
public function getCompaniesList() | ||
{ | ||
if ($this->isCompanyEnabled()) { | ||
$searchCriteria = $this->searchCriteriaBuilder->create(); | ||
|
||
return $this->companyRepository->getList($searchCriteria)->getItems(); // Fetch company list. | ||
} | ||
|
||
return []; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerGroupSelector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer | ||
* versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteAnalytics | ||
* @author Vadym Honcharuk <vahonc@smile.fr> | ||
* @copyright 2024 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
namespace Smile\ElasticsuiteAnalytics\Block\Adminhtml\Report; | ||
|
||
use Magento\Framework\View\Element\Template; | ||
use Magento\Customer\Model\ResourceModel\Group\CollectionFactory; | ||
|
||
/** | ||
* Block used to display customer group selector in reports. | ||
* | ||
* @SuppressWarnings(PHPMD.CamelCasePropertyName) | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteAnalytics | ||
* @author Vadym Honcharuk <vahonc@smile.fr> | ||
*/ | ||
class CustomerGroupSelector extends Template | ||
{ | ||
/** | ||
* @var CollectionFactory | ||
*/ | ||
protected $customerGroupCollectionFactory; | ||
|
||
/** | ||
* CustomerGroupSelector constructor. | ||
* | ||
* @param Template\Context $context The context of the template. | ||
* @param CollectionFactory $customerGroupCollectionFactory Factory for creating customer group collection. | ||
* @param array $data Additional block data. | ||
*/ | ||
public function __construct( | ||
Template\Context $context, | ||
CollectionFactory $customerGroupCollectionFactory, | ||
array $data = [] | ||
) { | ||
$this->customerGroupCollectionFactory = $customerGroupCollectionFactory; | ||
parent::__construct($context, $data); | ||
} | ||
|
||
/** | ||
* Get customer groups in an option array format. | ||
* | ||
* @return array | ||
*/ | ||
public function getCustomerGroups() | ||
{ | ||
return $this->customerGroupCollectionFactory->create()->toOptionArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...module-elasticsuite-analytics/Model/Report/Event/CustomerCompanyIdFilterQueryProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer | ||
* versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteAnalytics | ||
* @author Vadym Honcharuk <vahonc@smile.fr> | ||
* @copyright 2024 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
namespace Smile\ElasticsuiteAnalytics\Model\Report\Event; | ||
|
||
use Smile\ElasticsuiteAnalytics\Model\Report\QueryProviderInterface; | ||
use Smile\ElasticsuiteCore\Search\Request\QueryInterface; | ||
use Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory; | ||
use Smile\ElasticsuiteAnalytics\Model\Report\Context; | ||
|
||
/** | ||
* Customer company id filter query provider. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteAnalytics | ||
*/ | ||
class CustomerCompanyIdFilterQueryProvider implements QueryProviderInterface | ||
{ | ||
/** | ||
* @var QueryFactory | ||
*/ | ||
private $queryFactory; | ||
|
||
/** | ||
* @var Context | ||
*/ | ||
private $context; | ||
|
||
/** | ||
* CustomerCompanyIdFilterQueryProvider constructor. | ||
* | ||
* @param QueryFactory $queryFactory Query factory. | ||
* @param Context $context Report context. | ||
*/ | ||
public function __construct(QueryFactory $queryFactory, Context $context) | ||
{ | ||
$this->queryFactory = $queryFactory; | ||
$this->context = $context; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getQuery() | ||
{ | ||
// Get customer company ID from the context. | ||
$customerCompanyId = $this->context->getCustomerCompanyId(); | ||
|
||
// Check if customer company ID is set and not 'all'. | ||
if ($customerCompanyId !== 'all' && $customerCompanyId !== null) { | ||
// Return a TERM query for the customer company ID. | ||
return $this->queryFactory->create( | ||
QueryInterface::TYPE_TERM, | ||
[ | ||
'field' => 'customer.company_id', | ||
'value' => (int) $customerCompanyId, | ||
] | ||
); | ||
} | ||
|
||
// If 'all' is selected or no customer company ID is set, return null (no filtering). | ||
return null; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/module-elasticsuite-analytics/Model/Report/Event/CustomerGroupIdFilterQueryProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer | ||
* versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteAnalytics | ||
* @author Vadym Honcharuk <vahonc@smile.fr> | ||
* @copyright 2024 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
namespace Smile\ElasticsuiteAnalytics\Model\Report\Event; | ||
|
||
use Smile\ElasticsuiteAnalytics\Model\Report\QueryProviderInterface; | ||
use Smile\ElasticsuiteCore\Search\Request\QueryInterface; | ||
use Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory; | ||
use Smile\ElasticsuiteAnalytics\Model\Report\Context; | ||
|
||
/** | ||
* Customer group id filter query provider. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteAnalytics | ||
*/ | ||
class CustomerGroupIdFilterQueryProvider implements QueryProviderInterface | ||
{ | ||
/** | ||
* @var QueryFactory | ||
*/ | ||
private $queryFactory; | ||
|
||
/** | ||
* @var Context | ||
*/ | ||
private $context; | ||
|
||
/** | ||
* CustomerGroupIdFilterQueryProvider constructor. | ||
* | ||
* @param QueryFactory $queryFactory Query factory. | ||
* @param Context $context Report context. | ||
*/ | ||
public function __construct(QueryFactory $queryFactory, Context $context) | ||
{ | ||
$this->queryFactory = $queryFactory; | ||
$this->context = $context; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getQuery() | ||
{ | ||
// Get customer group ID from the context. | ||
$customerGroupId = $this->context->getCustomerGroupId(); | ||
|
||
// Check if customer group ID is set and not 'all'. | ||
if ($customerGroupId !== 'all' && $customerGroupId !== null) { | ||
// Return a TERM query for the customer group ID. | ||
return $this->queryFactory->create( | ||
QueryInterface::TYPE_TERM, | ||
[ | ||
'field' => 'customer.group_id', | ||
'value' => (int) $customerGroupId, | ||
] | ||
); | ||
} | ||
|
||
// If 'all' is selected or no customer group ID is set, return null (no filtering). | ||
return null; | ||
} | ||
} |
Oops, something went wrong.