init
This commit is contained in:
@@ -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