init
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Wiki\Model;
|
||||
|
||||
use Magento\Framework\Model\AbstractModel;
|
||||
use Esun\Wiki\Model\ResourceModel\Category as CategoryResource;
|
||||
|
||||
class Category extends AbstractModel
|
||||
{
|
||||
public function isClickable(): bool
|
||||
{
|
||||
$value = $this->getData('is_clickable');
|
||||
|
||||
return $value === null || $value === '' || (int) $value !== 0;
|
||||
}
|
||||
|
||||
public function isOpenByDefault(): bool
|
||||
{
|
||||
return (int) $this->getData('is_default_open') === 1;
|
||||
}
|
||||
|
||||
protected function _construct(): void
|
||||
{
|
||||
$this->_init(CategoryResource::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Wiki\Model\Config\Backend;
|
||||
|
||||
use Magento\Config\Model\Config\Backend\Image;
|
||||
|
||||
class BannerImage extends Image
|
||||
{
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
protected function _getAllowedExtensions()
|
||||
{
|
||||
return ['jpg', 'jpeg', 'gif', 'png', 'webp'];
|
||||
}
|
||||
|
||||
public function afterSave()
|
||||
{
|
||||
$result = parent::afterSave();
|
||||
$this->cleanPageCaches();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function afterDelete()
|
||||
{
|
||||
$result = parent::afterDelete();
|
||||
$this->cleanPageCaches();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function cleanPageCaches(): void
|
||||
{
|
||||
$this->cacheTypeList->cleanType('block_html');
|
||||
$this->cacheTypeList->cleanType('full_page');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Wiki\Model;
|
||||
|
||||
use Esun\Wiki\Model\ResourceModel\Homepage as HomepageResource;
|
||||
use Magento\Framework\DataObject\IdentityInterface;
|
||||
|
||||
class Homepage extends Category implements IdentityInterface
|
||||
{
|
||||
public const CACHE_TAG = 'esun_wiki_homepage';
|
||||
|
||||
protected $_cacheTag = self::CACHE_TAG;
|
||||
|
||||
protected $_eventPrefix = self::CACHE_TAG;
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getIdentities(): array
|
||||
{
|
||||
return $this->getId() ? [self::CACHE_TAG . '_' . $this->getId()] : [];
|
||||
}
|
||||
|
||||
protected function _construct(): void
|
||||
{
|
||||
$this->_init(HomepageResource::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Wiki\Model;
|
||||
|
||||
class PrintingParameterClassifier
|
||||
{
|
||||
private const BASE_OPTIONS = [
|
||||
'pla' => 'PLA Base',
|
||||
'petg' => 'PET(G) Base',
|
||||
'abs' => 'ABS Base',
|
||||
'asa' => 'ASA Base',
|
||||
'pa' => 'PA Base',
|
||||
'pc' => 'PC Base',
|
||||
'tpu' => 'TPU Base',
|
||||
'peba' => 'PEBA Base',
|
||||
'other' => 'Other',
|
||||
];
|
||||
|
||||
private const APPLICATION_OPTIONS = [
|
||||
'plus' => 'Plus Series',
|
||||
'basic' => 'Basic Series',
|
||||
'high_speed' => 'High Speed Series',
|
||||
'flexible' => 'Flexible Series',
|
||||
'high_elasticity' => 'High Elasticity Series',
|
||||
'recycled' => 'Recycled Series',
|
||||
'matte' => 'Matte Series',
|
||||
'silk' => 'Silk Series',
|
||||
'luminous' => 'Luminous Series',
|
||||
'sparkle' => 'Sparkle Series',
|
||||
'bionic' => 'Bionic Series',
|
||||
'color_change' => 'Color Change Series',
|
||||
'transparent' => 'Transparent Series',
|
||||
'carbon_fiber' => 'Carbon Fiber',
|
||||
'glass_fiber' => 'Glass Fiber',
|
||||
'lightweight' => 'Lightweight',
|
||||
'antistatic' => 'Antistatic',
|
||||
'conductive' => 'Conductive',
|
||||
'heat_resistant' => 'Heat Resistant',
|
||||
'flame_retardant' => 'Flame Retardant',
|
||||
'support' => 'Support Material',
|
||||
'general_engineering' => 'General Engineering',
|
||||
'special_engineering' => 'Special Engineering',
|
||||
'other' => 'Other Series',
|
||||
];
|
||||
|
||||
public function cleanName(string $name): string
|
||||
{
|
||||
$name = (string) preg_replace('/[\x{00A0}\x{2000}-\x{200B}\x{202F}\x{205F}\x{3000}]/u', ' ', $name);
|
||||
return trim((string) preg_replace('/\s+/u', ' ', $name));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{base: string, applications: string[]}
|
||||
*/
|
||||
public function classify(string $rawName): array
|
||||
{
|
||||
$name = $this->cleanName($rawName);
|
||||
$base = $this->detectBase($name);
|
||||
$applications = [];
|
||||
|
||||
$this->addWhen($applications, 'plus', str_contains($name, '+'));
|
||||
$this->addWhen($applications, 'basic', (bool) preg_match('/Basic|PLA-SS|PLA-Lite/i', $name));
|
||||
$this->addWhen($applications, 'high_speed', (bool) preg_match('/(?:^|[-+ ])(?:HS|HF)(?:$|[-+ (])/i', $name));
|
||||
$this->addWhen($applications, 'flexible', in_array($base, ['tpu', 'peba'], true)
|
||||
|| (bool) preg_match('/TPE|Flex/i', $name));
|
||||
$this->addWhen($applications, 'high_elasticity', $base === 'peba'
|
||||
|| (bool) preg_match('/^4D(?:-|$)/i', $name));
|
||||
$this->addWhen($applications, 'recycled', (bool) preg_match('/Revive/i', $name));
|
||||
$this->addWhen($applications, 'matte', (bool) preg_match('/Matte|^Mate$/i', $name));
|
||||
$this->addWhen($applications, 'silk', (bool) preg_match('/Silk/i', $name));
|
||||
$this->addWhen($applications, 'luminous', (bool) preg_match('/Luminous/i', $name));
|
||||
$this->addWhen($applications, 'sparkle', (bool) preg_match('/Twinkling|Stars/i', $name));
|
||||
$this->addWhen($applications, 'bionic', (bool) preg_match('/Rock|Marble|Wood|Coffee|Nature/i', $name));
|
||||
$this->addWhen($applications, 'color_change', (bool) preg_match('/Color Change|Chameleon/i', $name));
|
||||
$this->addWhen($applications, 'transparent', (bool) preg_match('/Clear|Transparent/i', $name));
|
||||
$this->addWhen($applications, 'carbon_fiber', (bool) preg_match('/(?:^|[-+])CF(?:$|[-+ ])/i', $name));
|
||||
$this->addWhen($applications, 'glass_fiber', (bool) preg_match('/(?:^|[-+])(?:GF|GK)(?:$|[-+ ])/i', $name));
|
||||
$this->addWhen($applications, 'lightweight', (bool) preg_match('/(?:^|-)LW(?:$|\()/i', $name));
|
||||
$this->addWhen($applications, 'antistatic', (bool) preg_match('/(?:^|-)ESD(?:$|-)/i', $name));
|
||||
$this->addWhen($applications, 'conductive', (bool) preg_match('/Conduct/i', $name));
|
||||
$this->addWhen($applications, 'heat_resistant', (bool) preg_match('/(?:^|-)HT(?:$|-)|PEEK|PPS/i', $name));
|
||||
$this->addWhen($applications, 'flame_retardant', (bool) preg_match('/(?:^|-)FR(?:$|-)/i', $name));
|
||||
$this->addWhen($applications, 'support', (bool) preg_match('/PVA|HIPS|^Clean$/i', $name));
|
||||
$this->addWhen($applications, 'general_engineering', in_array($base, ['petg', 'abs', 'asa', 'pa', 'pc'], true));
|
||||
$this->addWhen($applications, 'special_engineering', (bool) preg_match('/PEEK|PPS/i', $name));
|
||||
|
||||
if (!$applications) {
|
||||
$applications[] = 'other';
|
||||
}
|
||||
|
||||
return ['base' => $base, 'applications' => $applications];
|
||||
}
|
||||
|
||||
public function getBaseOptions(): array
|
||||
{
|
||||
return self::BASE_OPTIONS;
|
||||
}
|
||||
|
||||
public function getApplicationOptions(): array
|
||||
{
|
||||
return self::APPLICATION_OPTIONS;
|
||||
}
|
||||
|
||||
private function detectBase(string $name): string
|
||||
{
|
||||
if (preg_match('/PLA/i', $name)) {
|
||||
return 'pla';
|
||||
}
|
||||
if (preg_match('/PETG|(?:^|[^A-Z])PET(?:[^A-Z]|$)/i', $name)) {
|
||||
return 'petg';
|
||||
}
|
||||
if (preg_match('/ABS/i', $name)) {
|
||||
return 'abs';
|
||||
}
|
||||
if (preg_match('/ASA/i', $name)) {
|
||||
return 'asa';
|
||||
}
|
||||
if (preg_match('/(?:^|[^A-Z])PA(?:12|HT)?(?:[^A-Z]|$)/i', $name)) {
|
||||
return 'pa';
|
||||
}
|
||||
if (preg_match('/(?:^|[^A-Z])PC(?:[^A-Z]|$)/i', $name)) {
|
||||
return 'pc';
|
||||
}
|
||||
if (preg_match('/TPU/i', $name)) {
|
||||
return 'tpu';
|
||||
}
|
||||
if (preg_match('/PEBA/i', $name)) {
|
||||
return 'peba';
|
||||
}
|
||||
|
||||
return 'other';
|
||||
}
|
||||
|
||||
private function addWhen(array &$applications, string $value, bool $condition): void
|
||||
{
|
||||
if ($condition) {
|
||||
$applications[] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Wiki\Model\ResourceModel;
|
||||
|
||||
use Magento\Framework\Model\AbstractModel;
|
||||
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
|
||||
class Category extends AbstractDb
|
||||
{
|
||||
protected function _construct(): void
|
||||
{
|
||||
$this->_init('esun_wiki_category', 'category_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int[]
|
||||
*/
|
||||
public function lookupStoreIds(int $categoryId): array
|
||||
{
|
||||
$select = $this->getConnection()->select()
|
||||
->from($this->getTable('esun_wiki_category_store'), 'store_id')
|
||||
->where('category_id = ?', $categoryId);
|
||||
|
||||
return array_map('intval', $this->getConnection()->fetchCol($select));
|
||||
}
|
||||
|
||||
public function clearOtherDownloadCenters(int $categoryId): void
|
||||
{
|
||||
$this->getConnection()->update(
|
||||
$this->getMainTable(),
|
||||
['show_download_center' => 0],
|
||||
[
|
||||
'category_id != ?' => $categoryId,
|
||||
'show_download_center = ?' => 1,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function clearOtherParameterComparisons(int $categoryId): void
|
||||
{
|
||||
$this->getConnection()->update(
|
||||
$this->getMainTable(),
|
||||
['show_parameter_comparison' => 0],
|
||||
[
|
||||
'category_id != ?' => $categoryId,
|
||||
'show_parameter_comparison = ?' => 1,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
protected function _afterLoad(AbstractModel $object): self
|
||||
{
|
||||
if ($object->getId()) {
|
||||
$object->setData('store_ids', $this->lookupStoreIds((int) $object->getId()));
|
||||
}
|
||||
|
||||
parent::_afterLoad($object);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _afterSave(AbstractModel $object): self
|
||||
{
|
||||
if ($object->hasData('store_ids')) {
|
||||
$this->saveStoreIds((int) $object->getId(), (array) $object->getData('store_ids'));
|
||||
}
|
||||
|
||||
parent::_afterSave($object);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int[] $storeIds
|
||||
*/
|
||||
private function saveStoreIds(int $categoryId, array $storeIds): void
|
||||
{
|
||||
$storeIds = array_values(array_unique(array_map('intval', $storeIds)));
|
||||
$table = $this->getTable('esun_wiki_category_store');
|
||||
$connection = $this->getConnection();
|
||||
$oldStoreIds = $this->lookupStoreIds($categoryId);
|
||||
|
||||
$delete = array_diff($oldStoreIds, $storeIds);
|
||||
if ($delete) {
|
||||
$connection->delete($table, [
|
||||
'category_id = ?' => $categoryId,
|
||||
'store_id IN (?)' => $delete,
|
||||
]);
|
||||
}
|
||||
|
||||
$insert = array_diff($storeIds, $oldStoreIds);
|
||||
if ($insert) {
|
||||
$rows = [];
|
||||
foreach ($insert as $storeId) {
|
||||
$rows[] = ['category_id' => $categoryId, 'store_id' => (int) $storeId];
|
||||
}
|
||||
$connection->insertMultiple($table, $rows);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Wiki\Model\ResourceModel\Category;
|
||||
|
||||
use Esun\Wiki\Model\Category;
|
||||
use Esun\Wiki\Model\ResourceModel\Category as CategoryResource;
|
||||
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
|
||||
|
||||
class Collection extends AbstractCollection
|
||||
{
|
||||
private bool $storeFilterApplied = false;
|
||||
|
||||
protected function _construct(): void
|
||||
{
|
||||
$this->_init(Category::class, CategoryResource::class);
|
||||
$this->_map['fields']['category_id'] = 'main_table.category_id';
|
||||
}
|
||||
|
||||
public function addActiveFilter(): self
|
||||
{
|
||||
return $this->addFieldToFilter('is_active', 1);
|
||||
}
|
||||
|
||||
public function addTreeOrder(): self
|
||||
{
|
||||
return $this->setOrder('parent_id', 'ASC')
|
||||
->setOrder('sort_order', 'ASC')
|
||||
->setOrder('category_id', 'ASC');
|
||||
}
|
||||
|
||||
public function addStoreFilter(int $storeId, bool $withDefaultStore = true): self
|
||||
{
|
||||
if ($this->storeFilterApplied) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$storeIds = $withDefaultStore && $storeId !== 0 ? [0, $storeId] : [$storeId];
|
||||
$this->getSelect()
|
||||
->join(
|
||||
['wiki_store' => $this->getTable('esun_wiki_category_store')],
|
||||
'main_table.category_id = wiki_store.category_id',
|
||||
[]
|
||||
)
|
||||
->where('wiki_store.store_id IN (?)', $storeIds)
|
||||
->distinct(true);
|
||||
$this->storeFilterApplied = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Wiki\Model\ResourceModel;
|
||||
|
||||
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
|
||||
class Homepage extends AbstractDb
|
||||
{
|
||||
protected function _construct(): void
|
||||
{
|
||||
$this->_init('esun_wiki_homepage', 'homepage_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Wiki\Model\ResourceModel\Homepage;
|
||||
|
||||
use Esun\Wiki\Model\Homepage;
|
||||
use Esun\Wiki\Model\ResourceModel\Homepage as HomepageResource;
|
||||
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
|
||||
|
||||
class Collection extends AbstractCollection
|
||||
{
|
||||
protected function _construct(): void
|
||||
{
|
||||
$this->_init(Homepage::class, HomepageResource::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user