2121
2222class 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 );
0 commit comments