88namespace Ronangr1 \CmsImportExport \Controller \Adminhtml ;
99
1010use Magento \Backend \App \Action ;
11+ use Magento \Cms \Api \BlockRepositoryInterface ;
12+ use Magento \Cms \Api \PageRepositoryInterface ;
1113use Magento \Framework \App \Filesystem \DirectoryList ;
1214use Magento \Framework \App \Response \Http \FileFactory ;
1315use Magento \Framework \Controller \Result \RedirectFactory ;
16+ use Magento \Framework \Exception \LocalizedException ;
17+ use Magento \Framework \UrlInterface ;
1418use Ronangr1 \CmsImportExport \Api \ExporterInterface ;
1519
1620abstract class Exporter extends Action
@@ -20,25 +24,92 @@ abstract class Exporter extends Action
2024 protected string $ type = "cms_default " ;
2125
2226 public function __construct (
23- Action \Context $ context ,
27+ Action \Context $ context ,
2428 protected readonly FileFactory $ fileFactory ,
2529 protected readonly ExporterInterface $ exporter ,
2630 protected readonly RedirectFactory $ redirectFactory ,
27- ) {
31+ protected readonly PageRepositoryInterface $ pageRepository ,
32+ protected readonly BlockRepositoryInterface $ blockRepository ,
33+ protected readonly UrlInterface $ url ,
34+ protected $ entityRepository = null ,
35+ )
36+ {
2837 parent ::__construct ($ context );
2938 }
3039
40+ /**
41+ * @throws \Exception
42+ */
3143 public function execute ()
3244 {
33- $ id = (int ) $ this ->getRequest ()->getParam ("id " );
34- if ($ id === 0 ) {
45+ $ id = (int )$ this ->getRequest ()->getParam ("id " );
46+ if ($ id === 0 ) {
3547 $ this ->messageManager ->addErrorMessage (__ ("You must save the entity before exporting. " ));
3648 return $ this ->redirectFactory ->create ()->setPath ("cms/*/new " );
3749 }
3850
39- [$ fileName , $ content , $ mime ] = $ this ->exporter ->export ($ id , $ this ->type );
40- return $ this ->fileFactory ->create (
51+ [$ fileName , $ content , $ mime ] = $ this ->exporter ->export ($ id , $ this ->getEntityType ());
52+
53+ $ this ->fileFactory ->create (
4154 $ fileName , $ content , DirectoryList::VAR_DIR , $ mime
4255 );
56+
57+ $ zipUrl = $ this ->url ->getUrl (
58+ 'cmsimportexport/export/download ' ,
59+ [
60+ 'pathToFile ' => base64_encode ($ content ["value " ]),
61+ '_secure ' => true
62+ ]
63+ );
64+
65+ $ phrases = "" ;
66+
67+ try {
68+ $ entity = $ this ->getInstance ()->getById ($ id );
69+ $ phrases .= __ (
70+ 'The %3 "%2" has been exported with success! (<a href="%1" target="_blank">Download the archive</a>)<br/> ' ,
71+ $ zipUrl ,
72+ $ entity ->getTitle (),
73+ $ this ->getEntityTitle ()
74+ );
75+ } catch (\Exception $ e ) {
76+ $ this ->messageManager ->addErrorMessage ($ e ->getMessage ());
77+ }
78+
79+ if ($ phrases ) {
80+ $ this ->messageManager ->addComplexSuccessMessage ("withHtml " , [
81+ "html " => $ phrases ,
82+ "allowed_tags " => ["a " , "br " ],
83+ ]);
84+ }
85+
86+ return $ this ->redirectFactory ->create ()->setPath ("cms/*/index " );
87+ }
88+
89+ protected function getEntityTitle (): string
90+ {
91+ return $ this ->type === "cms_page " ? "page " : "block " ;
92+ }
93+
94+ protected function getEntityType (): string
95+ {
96+ return $ this ->type ;
97+ }
98+
99+ protected function getInstance (): BlockRepositoryInterface |PageRepositoryInterface
100+ {
101+ if ($ this ->entityRepository === null ) {
102+ match ($ this ->getEntityTYpe ()) {
103+ "cms_page " => $ this ->entityRepository = $ this ->pageRepository ,
104+ "cms_block " => $ this ->entityRepository = $ this ->blockRepository ,
105+ default => throw new LocalizedException (__ ("Invalid entity type " )),
106+ };
107+ }
108+ return $ this ->entityRepository ;
109+ }
110+
111+ protected function _isAllowed (): bool
112+ {
113+ return $ this ->_authorization ->isAllowed (self ::ADMIN_RESOURCE );
43114 }
44115}
0 commit comments