Skip to content

Commit 9c29780

Browse files
author
Rocket Web
authored
Merge branch 'main' into ICP-9_translate_block_id
2 parents 93861ba + 95cf57d commit 9c29780

File tree

5 files changed

+108
-15
lines changed

5 files changed

+108
-15
lines changed

Console/Command/DumpCmsData.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class DumpCmsData extends \Symfony\Component\Console\Command\Command
2525
private const INPUT_KEY_TYPE = 'type';
2626
private const INPUT_TYPE_VALUES = ['block', 'page', 'all'];
2727
private const INPUT_KEY_IDENTIFIER = 'identifier';
28+
private const INPUT_KEY_REMOVE_ALL = 'removeAll';
2829
private \RocketWeb\CmsImportExport\Model\Service\DumpCmsDataService $dumpCmsDataService;
2930

3031
public function __construct(
@@ -51,6 +52,12 @@ protected function configure()
5152
'i',
5253
InputOption::VALUE_OPTIONAL,
5354
'identifier to process (one or CSV list)'
55+
),
56+
new InputOption(
57+
self::INPUT_KEY_REMOVE_ALL,
58+
'r',
59+
InputOption::VALUE_NONE,
60+
'Flag to remove all existing data'
5461
)
5562
]);
5663
parent::configure();
@@ -59,6 +66,7 @@ protected function configure()
5966
protected function execute(InputInterface $input, OutputInterface $output): void
6067
{
6168
$type = $input->getOption(self::INPUT_KEY_TYPE);
69+
$removeAll = (bool)$input->getOption(self::INPUT_KEY_REMOVE_ALL);
6270
if ($type === null) {
6371
throw new \RuntimeException("Type ([-t|--type) is required");
6472
}
@@ -78,6 +86,6 @@ protected function execute(InputInterface $input, OutputInterface $output): void
7886
$identifiers = explode(',', $identifiers);
7987
}
8088

81-
$this->dumpCmsDataService->execute($types, $identifiers);
89+
$this->dumpCmsDataService->execute($types, $identifiers, $removeAll);
8290
}
8391
}

Console/Command/ImportCmsData.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class ImportCmsData extends \Symfony\Component\Console\Command\Command
2727
private const INPUT_KEY_TYPE = 'type';
2828
private const INPUT_TYPE_VALUES = ['block', 'page', 'all'];
2929
private const INPUT_KEY_IDENTIFIER = 'identifier';
30+
private const INPUT_KEY_IMPORT_ALL = 'importAll';
3031
private \RocketWeb\CmsImportExport\Model\Service\ImportCmsDataService $importCmsDataService;
3132

3233
public function __construct(
@@ -53,6 +54,12 @@ protected function configure()
5354
'i',
5455
InputOption::VALUE_OPTIONAL,
5556
'identifier to process (one or CSV list)'
57+
),
58+
new InputOption(
59+
self::INPUT_KEY_IMPORT_ALL,
60+
'a',
61+
InputOption::VALUE_NONE,
62+
'Flag to import all files'
5663
)
5764
]);
5865
parent::configure();
@@ -61,6 +68,7 @@ protected function configure()
6168
protected function execute(InputInterface $input, OutputInterface $output): void
6269
{
6370
$type = $input->getOption(self::INPUT_KEY_TYPE);
71+
$importAll = (bool)$input->getOption(self::INPUT_KEY_IMPORT_ALL);
6472
if ($type === null) {
6573
throw new \RuntimeException("Type ([-t|--type) is required");
6674
}
@@ -80,6 +88,6 @@ protected function execute(InputInterface $input, OutputInterface $output): void
8088
$identifiers = explode(',', $identifiers);
8189
}
8290

83-
$this->importCmsDataService->execute($types, $identifiers);
91+
$this->importCmsDataService->execute($types, $identifiers, $importAll);
8492
}
8593
}

Model/Service/DumpCmsDataService.php

Lines changed: 44 additions & 8 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
private array $blocksMapping = [];
3436

@@ -39,7 +41,8 @@ public function __construct(
3941
\Magento\Framework\Api\SearchCriteriaBuilder $criteriaBuilder,
4042
\Magento\Framework\Filesystem\DirectoryList $directoryList,
4143
\Magento\Framework\Filesystem $filesystem,
42-
\Magento\Framework\Serialize\SerializerInterface $serializer
44+
\Magento\Framework\Serialize\SerializerInterface $serializer,
45+
\Magento\Store\Model\StoreManagerInterface $storeManager
4346
) {
4447
$this->pageRepository = $pageRepository;
4548
$this->blockRepository = $blockRepository;
@@ -48,14 +51,15 @@ public function __construct(
4851
$this->filesystem = $filesystem;
4952
$this->serializer = $serializer;
5053
$this->categoryList = $categoryList;
54+
$this->storeManager = $storeManager;
5155
}
5256

53-
public function execute(array $types, ?array $identifiers)
57+
public function execute(array $types, ?array $identifiers, bool $removeAll)
5458
{
5559
$varDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
5660
$varPath = $this->directoryList->getPath(DirectoryList::VAR_DIR);
5761
$workingDirPath = $varPath . '/sync_cms_data';
58-
if ($varDirectory->isExist($workingDirPath)) {
62+
if ($varDirectory->isExist($workingDirPath) && $removeAll) {
5963
$varDirectory->delete($workingDirPath);
6064
}
6165

@@ -99,6 +103,28 @@ private function replaceBlockIds(string $content): string
99103
return $content;
100104
}
101105

106+
private function getStoreCodes($stores): array
107+
{
108+
$storeCodes = [];
109+
if (!$stores) {
110+
return [self::STORE_SCOPE_ALL];
111+
} else {
112+
foreach ($stores as $storeId) {
113+
if ($storeId == 0) {
114+
return [self::STORE_SCOPE_ALL];
115+
}
116+
try {
117+
$store = $this->storeManager->getStore($storeId);
118+
$storeCodes[] = $store->getCode();
119+
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
120+
echo $exception->getMessage() . "\n";
121+
}
122+
}
123+
}
124+
125+
return $storeCodes;
126+
}
127+
102128
private function dumpPages(string $path, WriteInterface $varDirectory, ?array $identifiers): void
103129
{
104130
$searchCriteria = $this->criteriaBuilder;
@@ -114,18 +140,24 @@ private function dumpPages(string $path, WriteInterface $varDirectory, ?array $i
114140
if (strpos($identifier, '.html') !== false) {
115141
$identifier = str_replace('.html', '_html', $identifier);
116142
}
117-
$htmlPath = $path . $identifier . '.html';
143+
144+
$storeCodes = $this->getStoreCodes($page->getStores());
145+
$htmlPath = $path . $identifier . '|' . implode('|', $storeCodes) . '.html';
118146
$pageContent = $this->replaceBlockIds($page->getContent());
119147
$this->write($varDirectory, $htmlPath, $pageContent);
120-
$jsonPath = $path . $identifier . '.json';
148+
$jsonPath = $path . $identifier . '|' . implode('|', $storeCodes) . '.json';
121149
$jsonContent = [
122150
'title' => $page->getTitle(),
123151
'is_active' => $page->isActive(),
124152
'page_layout' => $page->getPageLayout(),
125153
'identifier' => $page->getIdentifier(),
154+
'stores' => $storeCodes,
126155
'content_heading' => $page->getContentHeading(),
127156

128157
];
158+
if ($page->getIsTailwindcssJitEnabled() !== null) {
159+
$jsonContent['is_tailwindcss_jit_enabled'] = $page->getIsTailwindcssJitEnabled();
160+
}
129161
$this->write($varDirectory, $jsonPath, $this->serializer->serialize($jsonContent));
130162
}
131163
}
@@ -148,15 +180,19 @@ private function dumpBlocks(string $path, WriteInterface $varDirectory, ?array $
148180
continue;
149181
}
150182
$this->blockIdentifiers[$block->getId()] = $block->getIdentifier();
151-
$htmlPath = $path . trim($block->getIdentifier()) . '.html';
183+
$storeCodes = $this->getStoreCodes($block->getStores());
184+
$htmlPath = $path . trim($block->getIdentifier()) . '|' . implode('|', $storeCodes) . '.html';
152185
$this->write($varDirectory, $htmlPath, $block->getContent());
153-
$jsonPath = $path . trim($block->getIdentifier()) . '.json';
186+
$jsonPath = $path . trim($block->getIdentifier()) . '|' . implode('|', $storeCodes) . '.json';
154187
$jsonContent = [
155188
'title' => $block->getTitle(),
156189
'identifier' => $block->getIdentifier(),
157-
'stores' => [1],
190+
'stores' => $storeCodes,
158191
'is_active' => $block->isActive()
159192
];
193+
if ($block->getIsTailwindcssJitEnabled() !== null) {
194+
$jsonContent['is_tailwindcss_jit_enabled'] = $block->getIsTailwindcssJitEnabled();
195+
}
160196
$this->write($varDirectory, $jsonPath, $this->serializer->serialize($jsonContent));
161197
}
162198
}

Model/Service/ImportCmsDataService.php

Lines changed: 43 additions & 4 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,16 +48,21 @@ 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

50-
public function execute(array $types, ?array $identifiers)
54+
public function execute(array $types, ?array $identifiers, bool $importAll)
5155
{
5256
$workingDirPath = 'sync_cms_data';
5357

5458
if (!$this->directoryRead->isExist($this->varPath . $workingDirPath)) {
5559
throw new \Exception('The sync folder does not exists! Path: ' . $workingDirPath);
5660
}
5761

62+
if (!$identifiers && !$importAll) {
63+
throw new \Exception('If you want to import all entries at once, use --importAll flag');
64+
}
65+
5866
foreach ($types as $type) {
5967
$typeDirPath = $workingDirPath . sprintf('/cms/%ss/', $type);
6068
if (!$this->directoryRead->isExist($this->varPath . $typeDirPath)) {
@@ -69,6 +77,26 @@ public function execute(array $types, ?array $identifiers)
6977
}
7078
}
7179

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+
72100
private function importBlocks(string $dirPath, ?array $identifiers): void
73101
{
74102
$filePaths = $this->directoryRead->read($this->varPath . $dirPath);
@@ -79,6 +107,7 @@ private function importBlocks(string $dirPath, ?array $identifiers): void
79107
}
80108
$identifier = str_replace($dirPath, '', $filePath);
81109
$identifier = str_replace('.html', '', $identifier);
110+
$identifier = substr_replace($identifier, '', strpos($identifier, '|'));
82111
if ($identifiers !== null && !in_array($identifier, $identifiers)) {
83112
// If we have a list of items, we skip if its not in the list
84113
continue;
@@ -98,11 +127,15 @@ private function importBlocks(string $dirPath, ?array $identifiers): void
98127
'stores' => [1],
99128
'is_active' => $block->isActive()
100129
];*/
130+
$storeIds = $this->getStoreIds($jsonData['stores']);
101131
$block->setTitle($jsonData['title']);
102132
$block->setContent($content);
103133
$block->setIdentifier($jsonData['identifier']);
104-
$block->setStoreId($jsonData['stores'][0]);
105134
$block->setIsActive((bool)$jsonData['is_active']);
135+
$block->setStores($storeIds);
136+
if (isset($jsonData['is_tailwindcss_jit_enabled'])) {
137+
$block->setIsTailwindcssJitEnabled($jsonData['is_tailwindcss_jit_enabled']);
138+
}
106139

107140
try {
108141
$this->blockRepository->save($block);
@@ -121,8 +154,9 @@ private function importPages(string $dirPath, ?array $identifiers): void
121154
continue;
122155
}
123156
$identifier = str_replace($dirPath, '', $filePath);
124-
$identifier = str_replace('|', '/', $identifier);
125157
$identifier = str_replace('.html', '', $identifier);
158+
$identifier = substr_replace($identifier, '', strpos($identifier, '|'));
159+
$identifier = str_replace('|', '/', $identifier);
126160
$identifier = str_replace('_html', '.html', $identifier);
127161
if ($identifiers !== null && !in_array($identifier, $identifiers)) {
128162
// If we have a list of items, we skip if its not in the list
@@ -137,6 +171,7 @@ private function importPages(string $dirPath, ?array $identifiers): void
137171
$content = $this->directoryRead->readFile($filePath);
138172
$jsonData = $this->directoryRead->readFile(str_replace('.html', '.json', $filePath));
139173
$jsonData = $this->serializer->unserialize($jsonData);
174+
$storeIds = $this->getStoreIds($jsonData['stores']);
140175
/*$jsonContent = [
141176
'title' => $page->getTitle(),
142177
'is_active' => $page->isActive(),
@@ -150,6 +185,10 @@ private function importPages(string $dirPath, ?array $identifiers): void
150185
$page->setPageLayout($jsonData['page_layout']);
151186
$page->setContentHeading($jsonData['content_heading']);
152187
$page->setIsActive((bool)$jsonData['is_active']);
188+
$page->setStores($storeIds);
189+
if (isset($jsonData['is_tailwindcss_jit_enabled'])) {
190+
$page->setIsTailwindcssJitEnabled($jsonData['is_tailwindcss_jit_enabled']);
191+
}
153192

154193
try {
155194
$this->pageRepository->save($page);

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Dumps cms pages/blocks to var/sync_cms_data for further import
2828
Options:
2929
-t, --type=TYPE Which type are we dumping - block/page/all
3030
-i, --identifier[=IDENTIFIER] identifier to process (one or CSV list)
31+
-a, --importAll Flag to import all files
32+
-r, --removeAll Flag to remove all existing data
3133
```
3234

3335
As you can see from the options, we need to define:
@@ -86,4 +88,4 @@ With the combination of these two, we can **import**:
8688
- specific CMS block or blocks (using --type=block --identifier=who-are-we,homepage-carousel)
8789

8890
Once you execute the command, the content will be created/updated in Magento Admin.
89-
By executing `php bin/magento cache:flush` you should be able to see the updated CMS content on frontend also!
91+
By executing `php bin/magento cache:flush` you should be able to see the updated CMS content on frontend also!

0 commit comments

Comments
 (0)