This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,256 @@
<?php
namespace Stemcell\Customer\Ui\Component\DataProvider;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\Address;
use Magento\Customer\Model\AttributeMetadataResolver;
use Magento\Customer\Model\Customer;
use Magento\Customer\Model\CustomerFactory;
use Magento\Customer\Model\FileUploaderDataResolver;
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;
use Magento\Directory\Model\CountryFactory;
use Magento\Eav\Model\Config;
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
use Magento\Eav\Model\Entity\Type;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Session\SessionManagerInterface;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Ui\Component\Form\Element\Multiline;
class DataProviderWithDefaultAddresses extends Customer\DataProviderWithDefaultAddresses
{
/**
* @var array
*/
protected $loadedData = [];
/**
* @var SessionManagerInterface
*/
protected $session;
/**
* Customer fields that must be removed
*
* @var array
*/
protected static $forbiddenCustomerFields = [
'password_hash',
'rp_token',
];
/**
* Allow to manage attributes, even they are hidden on storefront
*
* @var bool
*/
protected $allowToShowHiddenAttributes;
/**
* @var CountryFactory
*/
protected $countryFactory;
/**
* @var FileUploaderDataResolver
*/
protected $fileUploaderDataResolver;
/**
* @var AttributeMetadataResolver
*/
protected $attributeMetadataResolver;
/**
* @var CustomerFactory
*/
protected $customerFactory;
/**
* @var ContextInterface
*/
protected $context;
/**
* @var CustomerRepositoryInterface
*/
protected $customerRepository;
/**
* @param string $name
* @param string $primaryFieldName
* @param string $requestFieldName
* @param CustomerCollectionFactory $customerCollectionFactory
* @param Config $eavConfig
* @param CountryFactory $countryFactory
* @param SessionManagerInterface $session
* @param FileUploaderDataResolver $fileUploaderDataResolver
* @param AttributeMetadataResolver $attributeMetadataResolver
* @param bool $allowToShowHiddenAttributes
* @param array $meta
* @param array $data
* @param CustomerFactory|null $customerFactory
* @param ContextInterface|null $context
* @param CustomerRepositoryInterface|null $customerRepository
* @throws LocalizedException
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
string $name,
string $primaryFieldName,
string $requestFieldName,
CustomerCollectionFactory $customerCollectionFactory,
Config $eavConfig,
CountryFactory $countryFactory,
SessionManagerInterface $session,
FileUploaderDataResolver $fileUploaderDataResolver,
AttributeMetadataResolver $attributeMetadataResolver,
$allowToShowHiddenAttributes = true,
array $meta = [],
array $data = [],
CustomerFactory $customerFactory = null,
?ContextInterface $context = null,
CustomerRepositoryInterface $customerRepository = null
) {
parent::__construct($name, $primaryFieldName, $requestFieldName, $customerCollectionFactory, $eavConfig, $countryFactory, $session, $fileUploaderDataResolver, $attributeMetadataResolver, $allowToShowHiddenAttributes, $meta, $data, $customerFactory, $context, $customerRepository);
$this->session = $session;
$this->allowToShowHiddenAttributes = $allowToShowHiddenAttributes;
$this->countryFactory = $countryFactory;
$this->fileUploaderDataResolver = $fileUploaderDataResolver;
$this->attributeMetadataResolver = $attributeMetadataResolver;
$this->context = $context ?? ObjectManager::getInstance()->get(ContextInterface::class);
$this->customerFactory = $customerFactory ?: ObjectManager::getInstance()->get(CustomerFactory::class);
$this->customerRepository = $customerRepository ??
ObjectManager::getInstance()->get(CustomerRepositoryInterface::class);
$this->meta['customer']['children'] = $this->getAttributesMeta(
$eavConfig->getEntityType('customer')
);
}
/**
* Get data
*
* @return array
*/
public function getData(): array
{
if (!empty($this->loadedData)) {
return $this->loadedData;
}
$items = $this->collection->getItems();
/** @var Customer $customer */
foreach ($items as $customer) {
if ($customer->getSalespersonsId()) {
$customer->setSalespersonsId(explode(',', $customer->getSalespersonsId()));
}
$result['customer'] = $customer->getData();
if ($customer->getSalespersonsId()) {
$result['customer']['extension_attributes']['salespersons_id'] = $customer->getSalespersonsId();
}
$this->fileUploaderDataResolver->overrideFileUploaderData($customer, $result['customer']);
$result['customer'] = array_diff_key(
$result['customer'],
array_flip(self::$forbiddenCustomerFields)
);
$this->prepareCustomAttributeValue($result['customer']);
$result['default_billing_address'] = $this->prepareDefaultAddress(
$customer->getDefaultBillingAddress()
);
$result['default_shipping_address'] = $this->prepareDefaultAddress(
$customer->getDefaultShippingAddress()
);
$result['customer_id'] = $customer->getId();
$this->loadedData[$customer->getId()] = $result;
}
$data = $this->session->getCustomerFormData();
if (!empty($data)) {
$customer = $this->customerFactory->create();
$this->fileUploaderDataResolver->overrideFileUploaderData($customer, $data['customer']);
$customerId = $data['customer']['entity_id'] ?? null;
$this->loadedData[$customerId] = $data;
$this->session->unsCustomerFormData();
}
return $this->loadedData;
}
/**
* Prepare default address data.
*
* @param Address|false $address
* @return array
*/
protected function prepareDefaultAddress($address): array
{
if (!$address) {
return [];
}
$addressData = $address->getData();
if (isset($addressData['street']) && !is_array($addressData['street'])) {
$addressData['street'] = explode("\n", $addressData['street']);
}
if (!empty($addressData['country_id'])) {
$addressData['country'] = $this->countryFactory->create()
->loadByCode($addressData['country_id'])
->getName();
}
$addressData['region'] = $address->getRegion();
return $addressData;
}
/***
* Prepare values for Custom Attributes.
*
* @param array $data
* @return void
*/
protected function prepareCustomAttributeValue(array &$data): void
{
foreach ($this->meta['customer']['children'] as $attributeName => $attributeMeta) {
if ($attributeMeta['arguments']['data']['config']['dataType'] === Multiline::NAME
&& isset($data[$attributeName])
&& !is_array($data[$attributeName])
) {
$data[$attributeName] = explode("\n", $data[$attributeName]);
}
}
}
/**
* Get attributes meta
*
* @param Type $entityType
* @return array
* @throws LocalizedException
*/
protected function getAttributesMeta(Type $entityType): array
{
$meta = [];
$attributes = $entityType->getAttributeCollection();
$customerId = $this->context->getRequestParam('id');
if ($customerId) {
$customer = $this->customerRepository->getById($customerId);
$attributes->setWebsite($customer->getWebsiteId());
}
/* @var AbstractAttribute $attribute */
foreach ($attributes as $attribute) {
$meta[$attribute->getAttributeCode()] = $this->attributeMetadataResolver->getAttributesMeta(
$attribute,
$entityType,
$this->allowToShowHiddenAttributes
);
}
$this->attributeMetadataResolver->processWebsiteMeta($meta);
return $meta;
}
}
@@ -0,0 +1,39 @@
<?php
namespace Stemcell\Customer\Ui\Component\DataProvider;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Session\SessionManagerInterface;
use Magento\Ui\DataProvider\AbstractDataProvider;
use Psr\Log\LoggerInterface;
use Stemcell\Customer\Model\ResourceModel\Lab\CollectionFactory;
class Lab extends AbstractDataProvider
{
protected SessionManagerInterface $session;
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
SessionManagerInterface $session,
CollectionFactory $collectionFactory,
array $meta = [],
array $data = []
) {
$this->collection = $collectionFactory->create();
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
$this->session = $session;
}
public function getData(): array
{
$data = [];
$salesperson = $this->session->getLab();
if (!empty($salesperson) && isset($salesperson['lab_id'])) {
$data[$salesperson['lab_id']]['lab'] = $salesperson;
}
$this->session->unsLab();
return $data;
}
}
@@ -0,0 +1,39 @@
<?php
namespace Stemcell\Customer\Ui\Component\DataProvider;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Session\SessionManagerInterface;
use Magento\Ui\DataProvider\AbstractDataProvider;
use Psr\Log\LoggerInterface;
use Stemcell\Customer\Model\ResourceModel\Salesperson\CollectionFactory;
class Salesperson extends AbstractDataProvider
{
protected SessionManagerInterface $session;
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
SessionManagerInterface $session,
CollectionFactory $collectionFactory,
array $meta = [],
array $data = []
) {
$this->collection = $collectionFactory->create();
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
$this->session = $session;
}
public function getData(): array
{
$data = [];
$salesperson = $this->session->getSalesperson();
if (!empty($salesperson) && isset($salesperson['salesperson_id'])) {
$data[$salesperson['salesperson_id']]['salesperson'] = $salesperson;
}
$this->session->unsSalesperson();
return $data;
}
}
@@ -0,0 +1,25 @@
<?php
namespace Stemcell\Customer\Ui\Component\DataProvider;
use Magento\Framework\Data\OptionSourceInterface;
use Stemcell\Customer\Model\ResourceModel\Salesperson\Collection;
class SalespersonOption implements OptionSourceInterface
{
private Collection $collection;
public function __construct(
Collection $collection
) {
$this->collection = $collection;
}
public function toOptionArray()
{
return $this->collection->addFieldToFilter('is_active', 1)
->addFieldToSelect('salesperson_id', 'value')
->addFieldToSelect('name', 'label')
->getData();
}
}
@@ -0,0 +1,39 @@
<?php
namespace Stemcell\Customer\Ui\Component\DataProvider;
use Magento\Framework\Data\OptionSourceInterface;
use Magento\Customer\Model\ResourceModel\Customer\Collection;
class VendorOption implements OptionSourceInterface
{
private Collection $collection;
/**
* @var array
*/
protected array $customerTree;
public function __construct(
Collection $collection
) {
$this->collection = $collection;
}
public function toOptionArray()
{
$collection = $this->collection->addFieldToSelect('firstname');
foreach ($collection as $customer) {
$customerId = $customer->getEntityId();
if (!isset($customerById[$customerId])) {
$customerById[$customerId] = [
'value' => $customerId
];
}
$customerById[$customerId]['label'] = $customer->getFirstname();
}
$this->customerTree = $customerById;
return $this->customerTree;
}
}
@@ -0,0 +1,69 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Stemcell\Customer\Ui\Component\Listing\Columns;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\UrlInterface;
/**
* Class ProductActions
*
* @api
* @since 100.0.2
*/
class Actions extends Column
{
/**
* @var UrlInterface
*/
protected $urlBuilder;
/**
* @param ContextInterface $context
* @param UiComponentFactory $uiComponentFactory
* @param UrlInterface $urlBuilder
* @param array $components
* @param array $data
*/
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
UrlInterface $urlBuilder,
array $components = [],
array $data = []
) {
$this->urlBuilder = $urlBuilder;
parent::__construct($context, $uiComponentFactory, $components, $data);
}
/**
* Prepare Data Source
*
* @param array $dataSource
* @return array
*/
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
$storeId = $this->context->getFilterParam('store_id');
foreach ($dataSource['data']['items'] as &$item) {
$item[$this->getData('name')]['edit'] = [
'href' => $this->urlBuilder->getUrl(
'stemcell_customer/salesperson/edit',
['id' => $item['salesperson_id'], 'store' => $storeId]
),
'label' => __('Edit'),
'hidden' => false,
];
}
}
return $dataSource;
}
}
@@ -0,0 +1,69 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Stemcell\Customer\Ui\Component\Listing\Columns;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\UrlInterface;
/**
* Class ProductActions
*
* @api
* @since 100.0.2
*/
class LabActions extends Column
{
/**
* @var UrlInterface
*/
protected $urlBuilder;
/**
* @param ContextInterface $context
* @param UiComponentFactory $uiComponentFactory
* @param UrlInterface $urlBuilder
* @param array $components
* @param array $data
*/
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
UrlInterface $urlBuilder,
array $components = [],
array $data = []
) {
$this->urlBuilder = $urlBuilder;
parent::__construct($context, $uiComponentFactory, $components, $data);
}
/**
* Prepare Data Source
*
* @param array $dataSource
* @return array
*/
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
$storeId = $this->context->getFilterParam('store_id');
foreach ($dataSource['data']['items'] as &$item) {
$item[$this->getData('name')]['edit'] = [
'href' => $this->urlBuilder->getUrl(
'stemcell_customer/lab/edit',
['id' => $item['lab_id'], 'store' => $storeId]
),
'label' => __('Edit'),
'hidden' => false,
];
}
}
return $dataSource;
}
}
@@ -0,0 +1,87 @@
<?php
namespace Stemcell\Customer\Ui\Component\Listing\Lab;
use Magento\Customer\Model\ResourceModel\Address\Grid\Collection as GridCollection;
use Magento\Framework\Api\Filter;
use Magento\Framework\App\ObjectManager;
use Magento\Ui\DataProvider\AbstractDataProvider;
use Psr\Log\LoggerInterface;
use Stemcell\Customer\Model\ResourceModel\Lab\Collection;
class DataProvider extends AbstractDataProvider
{
public function __construct(
$name, $primaryFieldName,
$requestFieldName,
Collection $collection,
array $meta = [],
array $data = []
) {
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
$this->collection = $collection;
}
public function getData()
{
$this->collection->addFieldToSelect([
'lab_id', 'lab_code', 'booking_account', 'pi_sap_id', 'is_active', 'created_at', 'updated_at'
])->getSelect()
->joinLeft('customer_entity', 'customer_entity.entity_id=main_table.customer_id', ['customer_firstname'=>'firstname'])
->joinLeft('salesperson', 'salesperson.salesperson_id=main_table.salesperson_id', ['salesperson_name'=>'salesperson.name'])
->query();
return $this->collection->toArray();
}
/**
* Add full text search filter to collection
*
* @param Filter $filter
* @return void
*/
public function addFilter(Filter $filter): void
{
/** @var GridCollection $collection */
$collection = $this->getCollection()->addFieldToSelect([
'*'
]);
$value = is_string($filter->getValue()) ? trim($filter->getValue()) : $filter->getValue();
if ($value !== null && $value !== '') {
if ($filter->getField() === 'customer_firstname') {
$collection->addFieldToFilter('customer_entity.firstname', ['like' => $value]);
// $collection->getSelect()->group('main_table.lab_id');
} elseif ($filter->getField() === 'salesperson_name') {
$collection->addFieldToFilter('salesperson.name', ['like' => $value]);
} else if ($filter->getField() === 'fulltext') {
$collection->getSelect()->where(
"main_table.lab_code LIKE '%$value%'
OR main_table.booking_account LIKE '%$value%'
OR main_table.pi_sap_id LIKE '%$value%'
OR customer_entity.firstname LIKE '%$value%'
OR salesperson.name LIKE '%$value%'"
);
// $collection->getSelect()->group('main_table.lab_id');
}
else {
$field = in_array($filter->getField(), [
'lab_id',
'lab_code',
'booking_account',
'pi_sap_id',
'is_active',
'created_at',
'updated_at',
], true) ? 'main_table.' . $filter->getField() : $filter->getField();
$collection->addFieldToFilter(
$field,
[$filter->getConditionType() => $value]
);
}
}
// $logger = ObjectManager::getInstance()->get(LoggerInterface::class);
// $logger->info('lab grid sql: '.$collection->getSelectSql(true));
}
}
@@ -0,0 +1,80 @@
<?php
namespace Stemcell\Customer\Ui\Component\Listing\Salesperson;
use Magento\Customer\Model\ResourceModel\Address\Grid\Collection as GridCollection;
use Magento\Framework\Api\Filter;
use Magento\Framework\App\ObjectManager;
use Magento\Ui\DataProvider\AbstractDataProvider;
use Psr\Log\LoggerInterface;
use Stemcell\Customer\Model\ResourceModel\Salesperson\Collection;
class DataProvider extends AbstractDataProvider
{
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
Collection $collection,
array $meta = [],
array $data = []
) {
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
$this->collection = $collection;
}
public function getData()
{
$this->collection->addFieldToSelect([
'salesperson_id', 'name', 'email', 'telephone', 'rsm_email', 'sisr_email',
'csr_email', 'is_active', 'created_at', 'updated_at'
])->getSelect()
->joinLeft('customer_entity', 'customer_entity.entity_id=main_table.customer_id', ['customer_id'=>'firstname'])
->query();
return $this->collection->toArray();
}
/**
* Add full text search filter to collection
*
* @param Filter $filter
* @return void
*/
public function addFilter(Filter $filter): void
{
/** @var GridCollection $collection */
$collection = $this->getCollection();
$value = is_string($filter->getValue()) ? trim($filter->getValue()) : $filter->getValue();
$hasValue = is_array($value) ? !empty($value) : $value !== null && $value !== '';
if ($hasValue) {
if ($filter->getField() === 'fulltext' && is_string($value)) {
$collection->getSelect()->where(
"main_table.name LIKE '%$value%'
OR main_table.telephone LIKE '%$value%'
OR main_table.email LIKE '%$value%'"
);
} else {
$field = in_array($filter->getField(), [
'salesperson_id',
'name',
'email',
'telephone',
'rsm_email',
'sisr_email',
'csr_email',
'is_active',
'created_at',
'updated_at',
], true) ? 'main_table.' . $filter->getField() : $filter->getField();
$collection->addFieldToFilter(
$field,
[$filter->getConditionType() => $value]
);
}
}
}
}