This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Esun\AiMascotCampaign\Controller\Adminhtml\Participant;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;
class Index extends Action
{
public const ADMIN_RESOURCE = 'Esun_AiMascotCampaign::participants';
public function __construct(Context $context, private readonly PageFactory $pageFactory)
{
parent::__construct($context);
}
public function execute(): Page
{
$page = $this->pageFactory->create();
$page->setActiveMenu(self::ADMIN_RESOURCE);
$page->getConfig()->getTitle()->prepend(__('AI Mascot Participants'));
return $page;
}
}
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Esun\AiMascotCampaign\Controller\Register;
use Esun\AiMascotCampaign\Model\CampaignContext;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\Result\RedirectFactory;
class Start implements HttpGetActionInterface
{
public function __construct(
private readonly CampaignContext $campaignContext,
private readonly CustomerSession $customerSession,
private readonly RedirectFactory $redirectFactory
) {
}
public function execute(): Redirect
{
if ($this->customerSession->isLoggedIn()) {
return $this->redirectFactory->create()->setPath(
'customer/account',
['_secure' => true]
);
}
$this->campaignContext->start();
return $this->redirectFactory->create()->setPath(
'customer/account/create',
['_secure' => true]
);
}
}
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace Esun\AiMascotCampaign\Model;
use Magento\Customer\Model\Session as CustomerSession;
class CampaignContext
{
public const CAMPAIGN_CODE = 'ai_mascot_2026';
private const SESSION_KEY = 'esun_ai_mascot_campaign';
private const MAX_AGE_SECONDS = 7200;
public function __construct(private readonly CustomerSession $customerSession)
{
}
public function start(): void
{
$this->customerSession->setData(self::SESSION_KEY, [
'code' => self::CAMPAIGN_CODE,
'started_at' => time(),
]);
}
public function consume(): ?string
{
$data = $this->customerSession->getData(self::SESSION_KEY);
$this->customerSession->unsetData(self::SESSION_KEY);
if (!is_array($data)
|| ($data['code'] ?? null) !== self::CAMPAIGN_CODE
|| !isset($data['started_at'])
) {
return null;
}
$age = time() - (int)$data['started_at'];
if ($age < 0 || $age > self::MAX_AGE_SECONDS) {
return null;
}
return self::CAMPAIGN_CODE;
}
}
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Esun\AiMascotCampaign\Model;
use Magento\Framework\App\ResourceConnection;
class RegistrationRecorder
{
private const TABLE_NAME = 'esun_campaign_registration';
public function __construct(private readonly ResourceConnection $resourceConnection)
{
}
public function record(
string $campaignCode,
int $customerId,
string $email,
int $websiteId,
int $storeId
): void {
$normalizedEmail = strtolower(trim($email));
if ($customerId <= 0 || $normalizedEmail === '') {
return;
}
$connection = $this->resourceConnection->getConnection();
$connection->insertOnDuplicate(
$this->resourceConnection->getTableName(self::TABLE_NAME),
[
'campaign_code' => $campaignCode,
'customer_id' => $customerId,
'email' => $normalizedEmail,
'website_id' => $websiteId,
'store_id' => $storeId,
],
[]
);
}
}
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Esun\AiMascotCampaign\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class Registration extends AbstractDb
{
protected function _construct(): void
{
$this->_init('esun_campaign_registration', 'entity_id');
}
}
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace Esun\AiMascotCampaign\Observer;
use Esun\AiMascotCampaign\Model\CampaignContext;
use Esun\AiMascotCampaign\Model\RegistrationRecorder;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Psr\Log\LoggerInterface;
class RecordCampaignRegistration implements ObserverInterface
{
public function __construct(
private readonly CampaignContext $campaignContext,
private readonly RegistrationRecorder $registrationRecorder,
private readonly LoggerInterface $logger
) {
}
public function execute(Observer $observer): void
{
$campaignCode = $this->campaignContext->consume();
if ($campaignCode === null) {
return;
}
$customer = $observer->getEvent()->getCustomer();
$customerId = (int)($customer?->getId() ?? 0);
$email = (string)($customer?->getEmail() ?? '');
if ($customerId <= 0 || trim($email) === '') {
return;
}
try {
$this->registrationRecorder->record(
$campaignCode,
$customerId,
$email,
(int)$customer->getWebsiteId(),
(int)$customer->getStoreId()
);
} catch (\Throwable $exception) {
// Campaign tracking must not turn a successful account creation into an error page.
$this->logger->error('Unable to record AI Mascot campaign registration.', [
'campaign_code' => $campaignCode,
'customer_id' => $customerId,
'exception' => $exception,
]);
}
}
}
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Esun\AiMascotCampaign\Ui\DataProvider;
use Esun\AiMascotCampaign\Model\CampaignContext;
use Magento\Framework\Api\Search\SearchCriteria;
use Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider;
class ParticipantDataProvider extends DataProvider
{
private bool $campaignFilterAdded = false;
public function getSearchCriteria(): SearchCriteria
{
if (!$this->campaignFilterAdded) {
$this->addFilter(
$this->filterBuilder
->setField('campaign_code')
->setConditionType('eq')
->setValue(CampaignContext::CAMPAIGN_CODE)
->create()
);
$this->campaignFilterAdded = true;
}
return parent::getSearchCriteria();
}
}
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework/Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin">
<resource id="Magento_Customer::customer">
<resource id="Esun_AiMascotCampaign::participants"
title="AI Mascot Participants"
translate="title"
sortOrder="70"/>
</resource>
</resource>
</resources>
</acl>
</config>
@@ -0,0 +1,13 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Ui\Model\Export\MetadataProvider">
<arguments>
<argument name="data" xsi:type="array">
<item name="esun_ai_mascot_participant_listing" xsi:type="array">
<item name="registered_at" xsi:type="string">registered_at</item>
</item>
</argument>
</arguments>
</type>
</config>
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
<menu>
<add id="Esun_AiMascotCampaign::participants"
title="AI Mascot Participants"
translate="title"
module="Esun_AiMascotCampaign"
sortOrder="70"
parent="Magento_Customer::customer"
action="esun_ai_mascot/participant/index"
resource="Esun_AiMascotCampaign::participants"/>
</menu>
</config>
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework/App/etc/routes.xsd">
<router id="admin">
<route id="esun_ai_mascot" frontName="esun_ai_mascot">
<module name="Esun_AiMascotCampaign" before="Magento_Backend"/>
</route>
</router>
</config>
@@ -0,0 +1,36 @@
<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework/Setup/Declaration/Schema/etc/schema.xsd">
<table name="esun_campaign_registration" resource="default" engine="innodb"
comment="eSUN Campaign Registrations">
<column xsi:type="int" name="entity_id" unsigned="true" nullable="false" identity="true"
comment="Entity ID"/>
<column xsi:type="varchar" name="campaign_code" nullable="false" length="64"
comment="Campaign Code"/>
<column xsi:type="int" name="customer_id" unsigned="true" nullable="false"
comment="Customer ID"/>
<column xsi:type="varchar" name="email" nullable="false" length="255"
comment="Registration Email (Normalized)"/>
<column xsi:type="smallint" name="website_id" unsigned="true" nullable="false"
comment="Website ID"/>
<column xsi:type="smallint" name="store_id" unsigned="true" nullable="false"
comment="Store ID"/>
<column xsi:type="timestamp" name="registered_at" nullable="false" default="CURRENT_TIMESTAMP"
on_update="false" comment="Registered At"/>
<constraint xsi:type="primary" referenceId="PRIMARY">
<column name="entity_id"/>
</constraint>
<constraint xsi:type="unique" referenceId="ESUN_CAMPAIGN_REGISTRATION_CODE_CUSTOMER_UNQ">
<column name="campaign_code"/>
<column name="customer_id"/>
</constraint>
<constraint xsi:type="unique" referenceId="ESUN_CAMPAIGN_REGISTRATION_CODE_EMAIL_UNQ">
<column name="campaign_code"/>
<column name="email"/>
</constraint>
<index referenceId="ESUN_CAMPAIGN_REGISTRATION_REGISTERED_AT" indexType="btree">
<column name="registered_at"/>
</index>
</table>
</schema>
+18
View File
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
<arguments>
<argument name="collections" xsi:type="array">
<item name="esun_ai_mascot_participant_listing_data_source" xsi:type="string">EsunAiMascotParticipantGridCollection</item>
</argument>
</arguments>
</type>
<virtualType name="EsunAiMascotParticipantGridCollection"
type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
<arguments>
<argument name="mainTable" xsi:type="string">esun_campaign_registration</argument>
<argument name="resourceModel" xsi:type="string">Esun\AiMascotCampaign\Model\ResourceModel\Registration</argument>
</arguments>
</virtualType>
</config>
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework/Event/etc/events.xsd">
<event name="customer_register_success">
<observer name="esun_ai_mascot_record_campaign_registration"
instance="Esun\AiMascotCampaign\Observer\RecordCampaignRegistration"/>
</event>
</config>
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework/App/etc/routes.xsd">
<router id="standard">
<route id="ai_mascot" frontName="ai-mascot">
<module name="Esun_AiMascotCampaign"/>
</route>
</router>
</config>
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Esun_AiMascotCampaign">
<sequence>
<module name="Magento_Customer"/>
<module name="Magento_Store"/>
<module name="Magento_Search"/>
<module name="Magento_Ui"/>
</sequence>
</module>
</config>
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Esun_AiMascotCampaign',
__DIR__
);
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework/View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<uiComponent name="esun_ai_mascot_participant_listing"/>
</referenceContainer>
</body>
</page>
@@ -0,0 +1,130 @@
<?xml version="1.0"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="provider" xsi:type="string">esun_ai_mascot_participant_listing.esun_ai_mascot_participant_listing_data_source</item>
</item>
</argument>
<settings>
<spinner>esun_ai_mascot_participant_columns</spinner>
<deps>
<dep>esun_ai_mascot_participant_listing.esun_ai_mascot_participant_listing_data_source</dep>
</deps>
</settings>
<dataSource name="esun_ai_mascot_participant_listing_data_source"
component="Magento_Ui/js/grid/provider">
<settings>
<storageConfig>
<param name="indexField" xsi:type="string">entity_id</param>
</storageConfig>
<updateUrl path="mui/index/render"/>
</settings>
<aclResource>Esun_AiMascotCampaign::participants</aclResource>
<dataProvider class="Esun\AiMascotCampaign\Ui\DataProvider\ParticipantDataProvider"
name="esun_ai_mascot_participant_listing_data_source">
<settings>
<requestFieldName>id</requestFieldName>
<primaryFieldName>entity_id</primaryFieldName>
</settings>
</dataProvider>
</dataSource>
<listingToolbar name="listing_top">
<settings>
<sticky>true</sticky>
</settings>
<bookmark name="bookmarks"/>
<columnsControls name="columns_controls"/>
<exportButton name="export_button"/>
<filters name="listing_filters">
<settings>
<templates>
<filters>
<select>
<param name="template" xsi:type="string">ui/grid/filters/elements/ui-select</param>
<param name="component" xsi:type="string">Magento_Ui/js/form/element/ui-select</param>
</select>
</filters>
</templates>
</settings>
<filterSelect name="website_id" provider="${ $.parentName }" sortOrder="40">
<settings>
<captionValue>--</captionValue>
<options class="Magento\Search\Ui\Component\Listing\Column\Website\Options"/>
<label translate="true">Website</label>
<dataScope>website_id</dataScope>
<imports>
<link name="visible">ns = ${ $.ns }, index = ${ $.index }:visible</link>
</imports>
</settings>
</filterSelect>
<filterSelect name="store_id" provider="${ $.parentName }" sortOrder="50">
<settings>
<captionValue>--</captionValue>
<options class="Magento\Search\Ui\Component\Listing\Column\Store\Options"/>
<label translate="true">Store View</label>
<dataScope>store_id</dataScope>
<imports>
<link name="visible">ns = ${ $.ns }, index = ${ $.index }:visible</link>
</imports>
</settings>
</filterSelect>
</filters>
<paging name="listing_paging"/>
</listingToolbar>
<columns name="esun_ai_mascot_participant_columns">
<selectionsColumn name="ids" sortOrder="10">
<settings>
<indexField>entity_id</indexField>
</settings>
</selectionsColumn>
<column name="entity_id" sortOrder="20">
<settings>
<filter>textRange</filter>
<label translate="true">Record ID</label>
</settings>
</column>
<column name="customer_id" sortOrder="30">
<settings>
<filter>textRange</filter>
<label translate="true">Customer ID</label>
</settings>
</column>
<column name="email" sortOrder="40">
<settings>
<filter>text</filter>
<label translate="true">Email</label>
</settings>
</column>
<column name="website_id"
class="Magento\Search\Ui\Component\Listing\Column\Website"
sortOrder="50">
<settings>
<label translate="true">Website</label>
</settings>
</column>
<column name="store_id"
class="Magento\Search\Ui\Component\Listing\Column\StoreView"
sortOrder="60">
<settings>
<label translate="true">Store View</label>
</settings>
</column>
<column name="campaign_code" sortOrder="70">
<settings>
<filter>text</filter>
<label translate="true">Campaign</label>
</settings>
</column>
<column name="registered_at"
class="Magento\Ui\Component\Listing\Columns\Date"
sortOrder="80">
<settings>
<filter>dateRange</filter>
<dataType>date</dataType>
<label translate="true">Registered At</label>
<sorting>desc</sorting>
</settings>
</column>
</columns>
</listing>