Skip to content

Commit 95cf57d

Browse files
author
Rocket Web
authored
Merge pull request #2 from rocketweb/ICP-3-stores_support
adding stores support [#3]
2 parents b6ffbff + cdaaaba commit 95cf57d

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

Model/Service/DumpCmsDataService.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222

2323
class DumpCmsDataService
2424
{
25+
public const STORE_SCOPE_ALL = '_all_';
2526
private \Magento\Cms\Api\PageRepositoryInterface $pageRepository;
2627
private \Magento\Cms\Api\BlockRepositoryInterface $blockRepository;
2728
private \Magento\Framework\Api\SearchCriteriaBuilder $criteriaBuilder;
2829
private \Magento\Framework\Filesystem\DirectoryList $directoryList;
2930
private \Magento\Framework\Filesystem $filesystem;
3031
private \Magento\Framework\Serialize\SerializerInterface $serializer;
3132
private \Magento\Catalog\Model\CategoryList $categoryList;
33+
private \Magento\Store\Model\StoreManagerInterface $storeManager;
3234
private array $blockIdentifiers = [];
3335

3436
public function __construct(
@@ -38,7 +40,8 @@ public function __construct(
3840
\Magento\Framework\Api\SearchCriteriaBuilder $criteriaBuilder,
3941
\Magento\Framework\Filesystem\DirectoryList $directoryList,
4042
\Magento\Framework\Filesystem $filesystem,
41-
\Magento\Framework\Serialize\SerializerInterface $serializer
43+
\Magento\Framework\Serialize\SerializerInterface $serializer,
44+
\Magento\Store\Model\StoreManagerInterface $storeManager
4245
) {
4346
$this->pageRepository = $pageRepository;
4447
$this->blockRepository = $blockRepository;
@@ -47,6 +50,7 @@ public function __construct(
4750
$this->filesystem = $filesystem;
4851
$this->serializer = $serializer;
4952
$this->categoryList = $categoryList;
53+
$this->storeManager = $storeManager;
5054
}
5155

5256
public function execute(array $types, ?array $identifiers, bool $removeAll)
@@ -76,6 +80,28 @@ private function write(WriteInterface $writeDirectory, string $filePath, string
7680
$stream->close();
7781
}
7882

83+
private function getStoreCodes($stores): array
84+
{
85+
$storeCodes = [];
86+
if (!$stores) {
87+
return [self::STORE_SCOPE_ALL];
88+
} else {
89+
foreach ($stores as $storeId) {
90+
if ($storeId == 0) {
91+
return [self::STORE_SCOPE_ALL];
92+
}
93+
try {
94+
$store = $this->storeManager->getStore($storeId);
95+
$storeCodes[] = $store->getCode();
96+
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
97+
echo $exception->getMessage() . "\n";
98+
}
99+
}
100+
}
101+
102+
return $storeCodes;
103+
}
104+
79105
private function dumpPages(string $path, WriteInterface $varDirectory, ?array $identifiers): void
80106
{
81107
$searchCriteria = $this->criteriaBuilder;
@@ -91,14 +117,16 @@ private function dumpPages(string $path, WriteInterface $varDirectory, ?array $i
91117
if (strpos($identifier, '.html') !== false) {
92118
$identifier = str_replace('.html', '_html', $identifier);
93119
}
94-
$htmlPath = $path . $identifier . '.html';
120+
$storeCodes = $this->getStoreCodes($page->getStores());
121+
$htmlPath = $path . $identifier . '|' . implode('|', $storeCodes) . '.html';
95122
$this->write($varDirectory, $htmlPath, $page->getContent());
96-
$jsonPath = $path . $identifier . '.json';
123+
$jsonPath = $path . $identifier . '|' . implode('|', $storeCodes) . '.json';
97124
$jsonContent = [
98125
'title' => $page->getTitle(),
99126
'is_active' => $page->isActive(),
100127
'page_layout' => $page->getPageLayout(),
101128
'identifier' => $page->getIdentifier(),
129+
'stores' => $storeCodes,
102130
'content_heading' => $page->getContentHeading(),
103131

104132
];
@@ -127,13 +155,14 @@ private function dumpBlocks(string $path, WriteInterface $varDirectory, ?array $
127155
continue;
128156
}
129157
$this->blockIdentifiers[$block->getId()] = $block->getIdentifier();
130-
$htmlPath = $path . trim($block->getIdentifier()) . '.html';
158+
$storeCodes = $this->getStoreCodes($block->getStores());
159+
$htmlPath = $path . trim($block->getIdentifier()) . '|' . implode('|', $storeCodes) . '.html';
131160
$this->write($varDirectory, $htmlPath, $block->getContent());
132-
$jsonPath = $path . trim($block->getIdentifier()) . '.json';
161+
$jsonPath = $path . trim($block->getIdentifier()) . '|' . implode('|', $storeCodes) . '.json';
133162
$jsonContent = [
134163
'title' => $block->getTitle(),
135164
'identifier' => $block->getIdentifier(),
136-
'stores' => [1],
165+
'stores' => $storeCodes,
137166
'is_active' => $block->isActive()
138167
];
139168
if ($block->getIsTailwindcssJitEnabled() !== null) {

Model/Service/ImportCmsDataService.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121

2222
class ImportCmsDataService
2323
{
24+
private const STORE_SCOPE_ADMIN = 'admin';
2425
private \Magento\Cms\Api\PageRepositoryInterface $pageRepository;
2526
private \Magento\Cms\Api\BlockRepositoryInterface $blockRepository;
2627
private \Magento\Framework\Serialize\SerializerInterface $serializer;
2728
private \Magento\Framework\Filesystem\Directory\ReadInterface $directoryRead;
2829
private \Magento\Cms\Api\Data\BlockInterfaceFactory $blockFactory;
2930
private \Magento\Cms\Api\Data\PageInterfaceFactory $pageFactory;
31+
private \Magento\Store\Api\StoreRepositoryInterface $storeRepository;
3032
private string $varPath;
3133

3234
public function __construct(
@@ -36,7 +38,8 @@ public function __construct(
3638
\Magento\Cms\Api\Data\BlockInterfaceFactory $blockFactory,
3739
\Magento\Framework\Filesystem\DirectoryList $directoryList,
3840
\Magento\Framework\Filesystem $filesystem,
39-
\Magento\Framework\Serialize\SerializerInterface $serializer
41+
\Magento\Framework\Serialize\SerializerInterface $serializer,
42+
\Magento\Store\Api\StoreRepositoryInterface $storeRepository
4043
) {
4144
$this->pageRepository = $pageRepository;
4245
$this->blockRepository = $blockRepository;
@@ -45,6 +48,7 @@ public function __construct(
4548
$this->varPath = $directoryList->getPath(DirectoryList::VAR_DIR) . '/';
4649
$this->blockFactory = $blockFactory;
4750
$this->pageFactory = $pageFactory;
51+
$this->storeRepository = $storeRepository;
4852
}
4953

5054
public function execute(array $types, ?array $identifiers, bool $importAll)
@@ -73,6 +77,26 @@ public function execute(array $types, ?array $identifiers, bool $importAll)
7377
}
7478
}
7579

80+
private function getStoreIds($storeCodes): array
81+
{
82+
$storeIds = [];
83+
if (is_array($storeCodes) && count($storeCodes)) {
84+
foreach ($storeCodes as $storeCode) {
85+
try {
86+
if ($storeCode === DumpCmsDataService::STORE_SCOPE_ALL) {
87+
$storeCode = self::STORE_SCOPE_ADMIN;
88+
}
89+
$store = $this->storeRepository->get($storeCode);
90+
$storeIds[] = $store->getId();
91+
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
92+
echo $exception->getMessage() . "\n";
93+
}
94+
}
95+
}
96+
97+
return $storeIds;
98+
}
99+
76100
private function importBlocks(string $dirPath, ?array $identifiers): void
77101
{
78102
$filePaths = $this->directoryRead->read($this->varPath . $dirPath);
@@ -83,6 +107,7 @@ private function importBlocks(string $dirPath, ?array $identifiers): void
83107
}
84108
$identifier = str_replace($dirPath, '', $filePath);
85109
$identifier = str_replace('.html', '', $identifier);
110+
$identifier = substr_replace($identifier, '', strpos($identifier, '|'));
86111
if ($identifiers !== null && !in_array($identifier, $identifiers)) {
87112
// If we have a list of items, we skip if its not in the list
88113
continue;
@@ -102,11 +127,12 @@ private function importBlocks(string $dirPath, ?array $identifiers): void
102127
'stores' => [1],
103128
'is_active' => $block->isActive()
104129
];*/
130+
$storeIds = $this->getStoreIds($jsonData['stores']);
105131
$block->setTitle($jsonData['title']);
106132
$block->setContent($content);
107133
$block->setIdentifier($jsonData['identifier']);
108-
$block->setStoreId($jsonData['stores'][0]);
109134
$block->setIsActive((bool)$jsonData['is_active']);
135+
$block->setStores($storeIds);
110136
if (isset($jsonData['is_tailwindcss_jit_enabled'])) {
111137
$block->setIsTailwindcssJitEnabled($jsonData['is_tailwindcss_jit_enabled']);
112138
}
@@ -128,8 +154,9 @@ private function importPages(string $dirPath, ?array $identifiers): void
128154
continue;
129155
}
130156
$identifier = str_replace($dirPath, '', $filePath);
131-
$identifier = str_replace('|', '/', $identifier);
132157
$identifier = str_replace('.html', '', $identifier);
158+
$identifier = substr_replace($identifier, '', strpos($identifier, '|'));
159+
$identifier = str_replace('|', '/', $identifier);
133160
$identifier = str_replace('_html', '.html', $identifier);
134161
if ($identifiers !== null && !in_array($identifier, $identifiers)) {
135162
// If we have a list of items, we skip if its not in the list
@@ -144,6 +171,7 @@ private function importPages(string $dirPath, ?array $identifiers): void
144171
$content = $this->directoryRead->readFile($filePath);
145172
$jsonData = $this->directoryRead->readFile(str_replace('.html', '.json', $filePath));
146173
$jsonData = $this->serializer->unserialize($jsonData);
174+
$storeIds = $this->getStoreIds($jsonData['stores']);
147175
/*$jsonContent = [
148176
'title' => $page->getTitle(),
149177
'is_active' => $page->isActive(),
@@ -157,6 +185,7 @@ private function importPages(string $dirPath, ?array $identifiers): void
157185
$page->setPageLayout($jsonData['page_layout']);
158186
$page->setContentHeading($jsonData['content_heading']);
159187
$page->setIsActive((bool)$jsonData['is_active']);
188+
$page->setStores($storeIds);
160189
if (isset($jsonData['is_tailwindcss_jit_enabled'])) {
161190
$page->setIsTailwindcssJitEnabled($jsonData['is_tailwindcss_jit_enabled']);
162191
}

0 commit comments

Comments
 (0)