Skip to content

Commit 3e15f80

Browse files
committed
update ux + mass export
1 parent a56b63b commit 3e15f80

File tree

11 files changed

+374
-75
lines changed

11 files changed

+374
-75
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Copyright © Ronangr1, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Ronangr1\CmsImportExport\Controller\Adminhtml\Block;
9+
10+
use Ronangr1\CmsImportExport\Controller\Adminhtml\MassExporter;
11+
12+
class MassExport extends MassExporter
13+
{
14+
protected string $type = "cms_block";
15+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Copyright © Ronangr1, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Ronangr1\CmsImportExport\Controller\Adminhtml\Export;
9+
10+
use Magento\Backend\App\Action;
11+
12+
class Download extends Action
13+
{
14+
public function execute()
15+
{
16+
$file = $this->getRequest()->getParam('pathToFile');
17+
if ($file) {
18+
$file = base64_decode($file);
19+
if (file_exists($file)) {
20+
$filename = basename($file);
21+
$this->_actionFlag->set('', 'no-dispatch', true);
22+
$this->getResponse()->setHttpResponseCode(200);
23+
$this->getResponse()->setHeader('Content-Type', 'application/zip');
24+
$this->getResponse()->setHeader('Content-Disposition', 'attachment; filename=' . $filename);
25+
$this->getResponse()->setBody(file_get_contents($file));
26+
unlink($file);
27+
}
28+
} else {
29+
$this->messageManager->addErrorMessage(__('File not found.'));
30+
return $this->_redirect('cms/**/*');
31+
}
32+
}
33+
}

Controller/Adminhtml/Exporter.php

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
namespace Ronangr1\CmsImportExport\Controller\Adminhtml;
99

1010
use Magento\Backend\App\Action;
11+
use Magento\Cms\Api\BlockRepositoryInterface;
12+
use Magento\Cms\Api\PageRepositoryInterface;
1113
use Magento\Framework\App\Filesystem\DirectoryList;
1214
use Magento\Framework\App\Response\Http\FileFactory;
1315
use Magento\Framework\Controller\Result\RedirectFactory;
16+
use Magento\Framework\Exception\LocalizedException;
17+
use Magento\Framework\UrlInterface;
1418
use Ronangr1\CmsImportExport\Api\ExporterInterface;
1519

1620
abstract 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
}

Controller/Adminhtml/Importer.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ abstract class Importer extends Action
1515
{
1616
public const ADMIN_RESOURCE = "Ronangr1_CmsImportExport::import";
1717

18-
public function __construct(
19-
Context $context,
20-
)
18+
public function execute()
2119
{
22-
parent::__construct($context);
20+
return $this->resultFactory->create(ResultFactory::TYPE_PAGE);
2321
}
2422

25-
public function execute()
23+
protected function _isAllowed(): bool
2624
{
27-
return $this->resultFactory->create(ResultFactory::TYPE_PAGE);
25+
return $this->_authorization->isAllowed(self::ADMIN_RESOURCE);
2826
}
2927
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © Ronangr1, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Ronangr1\CmsImportExport\Controller\Adminhtml;
9+
10+
use Magento\Framework\App\Filesystem\DirectoryList;
11+
12+
abstract class MassExporter extends Exporter
13+
{
14+
15+
public function execute()
16+
{
17+
$ids = $this->getRequest()->getParam("selected");
18+
if(!$ids) {
19+
$this->messageManager->addErrorMessage(__("You must select ids before exporting."));
20+
return $this->redirectFactory->create()->setPath("cms/*/index");
21+
}
22+
23+
$phrases = "";
24+
25+
foreach ($ids as $id) {
26+
$id = (int) $id;
27+
[$fileName, $content, $mime] = $this->exporter->export($id, $this->getEntityTYpe());
28+
29+
$this->fileFactory->create(
30+
$fileName, $content, DirectoryList::VAR_DIR, $mime
31+
);
32+
33+
$zipUrl = $this->getUrl(
34+
'cmsimportexport/export/download',
35+
[
36+
'pathToFile' => base64_encode($content["value"]),
37+
'_secure' => true
38+
]
39+
);
40+
41+
try {
42+
$cms = $this->getInstance()->getById($id);
43+
$phrases .= __(
44+
'The %3 "%2" has been exported with success! (<a href="%1" target="_blank">Download the archive</a>)<br/>',
45+
$zipUrl,
46+
$cms->getTitle(),
47+
$this->getEntityTitle()
48+
);
49+
} catch (\Exception $e) {
50+
$this->messageManager->addErrorMessage($e->getMessage());
51+
}
52+
}
53+
54+
if($phrases) {
55+
$this->messageManager->addComplexSuccessMessage("withHtml", [
56+
"html" => $phrases,
57+
"allowed_tags" => ["a", "br"],
58+
]);
59+
}
60+
61+
return $this->redirectFactory->create()->setPath("cms/*/index");
62+
}
63+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Copyright © Ronangr1, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Ronangr1\CmsImportExport\Controller\Adminhtml\Page;
9+
10+
use Ronangr1\CmsImportExport\Controller\Adminhtml\MassExporter;
11+
12+
class MassExport extends MassExporter
13+
{
14+
protected string $type = "cms_page";
15+
}

0 commit comments

Comments
 (0)