Skip to content

Commit 1b5487a

Browse files
mike4gitlukadschaaknsd0sneumannjdreesen
authored
[Feature] Add import context menu for re-importing YAML files (#6)
* [Feature] Add import context menu for re-importing YAML files * [Chore] update changelog * Update src/Controller/Admin/PageImportController.php Co-authored-by: Jacob Dreesen <jacob@hdreesen.de> * Update src/Controller/Admin/PageImportController.php Co-authored-by: Jacob Dreesen <jacob@hdreesen.de> * Update public/js/importPage.js Co-authored-by: Jacob Dreesen <jacob@hdreesen.de> * Update README.md Co-authored-by: Luka Dschaak <l.dschaak@neusta.de> * task: pipeline fix * Update src/Controller/Admin/PageImportController.php * task: dialog box customized --------- Co-authored-by: Luka Dschaak <l.dschaak@neusta.de> Co-authored-by: Sandra Neumann <s.neumann@neusta.de> Co-authored-by: nsd0sneumann <95299884+nsd0sneumann@users.noreply.github.com> Co-authored-by: Jacob Dreesen <jacob@hdreesen.de>
1 parent 5d0f2e1 commit 1b5487a

File tree

11 files changed

+221
-5
lines changed

11 files changed

+221
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Changelog
22

3-
## 1.0
3+
## 1.0.0
44

5-
- feature:
5+
- feature: Initial release
6+
- feature: Export Pimcore pages to YAML files
7+
- feature: Import Pimcore pages from YAML files

README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,50 @@
1818

1919
## Usage
2020

21-
TODO
21+
After enabling the bundle you should see a new menu item in the context menu of Pimcore Admin Backend - Section Documents:
22+
23+
![context_menu_import_export.png](docs/images/context_menu_import_export.png)
24+
25+
(german translation)
26+
27+
### Page Export
28+
The selected page will be exported into YAML format:
29+
```yaml
30+
page:
31+
id: 123
32+
parentId: 1
33+
type: page
34+
published: true
35+
path: /
36+
language: de
37+
navigation_name: my-site
38+
navigation_title: 'My Site'
39+
key: my-site
40+
title: 'My Site'
41+
controller: 'App\DefaultController::indexAction'
42+
editables:
43+
main:
44+
type: areablock
45+
name: main
46+
data: [{ key: '1', type: text-editor, hidden: false }]
47+
...
48+
```
49+
50+
In the same way you can re-import your yaml file again by selecting: `Import from YAML` in the context menu.
2251

2352
## Configuration
2453

25-
TODO
54+
### Page Import
55+
To use the Page Importer, the CSRF protection for the PageImportController route must be avoided.
56+
57+
To do this, create a file `config/packages/pimcore_admin.yaml` and add the following content:
58+
59+
```yaml
60+
pimcore_admin:
61+
csrf_protection:
62+
excluded_routes:
63+
- neusta_pimcore_import_export_page_import
64+
```
2665

2766
## Contribution
2867

config/pimcore/routing.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ neusta_pimcore_import_export_page_export:
33
type: attribute
44
options:
55
expose: true
6+
7+
neusta_pimcore_import_export_page_import:
8+
resource: '../../src/Controller/Admin/PageImportController.php'
9+
type: attribute
10+
options:
11+
expose: true

config/services.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ services:
1313
public: true
1414
tags: [ 'controller.service_arguments' ]
1515

16+
Neusta\Pimcore\ImportExportBundle\Controller\Admin\PageImportController:
17+
public: true
18+
tags: [ 'controller.service_arguments' ]
19+
1620
###########################################################
1721
# Import Populator (YamlExportPage -> Page)
1822
###########################################################
3.07 KB
Loading

public/js/exportPage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ neusta_pimcore_import_export.plugin.page.export = Class.create({
1111
// Export page into yaml file
1212
menu.add("-");
1313
menu.add(new Ext.menu.Item({
14-
text: 'Export to YAML',
14+
text: t('neusta_pimcore_import_export_export_menu_label'),
1515
iconCls: "pimcore_icon_export",
1616
handler: function () {
1717
pimcore.helpers.download(Routing.generate('neusta_pimcore_import_export_page_export', {page_id: document.data.id}));

public/js/importPage.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
pimcore.registerNS("neusta_pimcore_import_export.plugin.page.import");
2+
3+
neusta_pimcore_import_export.plugin.page.import = Class.create({
4+
initialize: function () {
5+
document.addEventListener(pimcore.events.prepareDocumentTreeContextMenu, this.onPrepareDocumentTreeContextMenu.bind(this));
6+
},
7+
8+
onPrepareDocumentTreeContextMenu: function (e) {
9+
let menu = e.detail.menu;
10+
let document = e.detail.document;
11+
12+
menu.add(new Ext.menu.Item({
13+
text: t('neusta_pimcore_import_export_import_menu_label'),
14+
iconCls: "pimcore_icon_import",
15+
handler: function () {
16+
let uploadDialog = new Ext.Window({
17+
title: t('neusta_pimcore_import_export_import_dialog_title'),
18+
width: 600,
19+
layout: 'fit',
20+
modal: true,
21+
items: [
22+
new Ext.form.Panel({
23+
bodyPadding: 10,
24+
items: [
25+
{
26+
xtype: 'filefield',
27+
name: 'file',
28+
width: 450,
29+
fieldLabel: t('neusta_pimcore_import_export_import_dialog_file_label'),
30+
labelWidth: 100,
31+
allowBlank: false,
32+
buttonText: t('neusta_pimcore_import_export_import_dialog_file_button'),
33+
accept: '.yaml,.yml'
34+
},
35+
{
36+
xtype: 'checkbox',
37+
name: 'overwrite',
38+
fieldLabel: t('neusta_pimcore_import_export_import_dialog_overwrite_label'),
39+
}
40+
],
41+
buttons: [
42+
{
43+
text: 'Import',
44+
handler: function (btn) {
45+
let form = btn.up('form').getForm();
46+
if (!form.isValid()) {
47+
return;
48+
}
49+
50+
form.submit({
51+
url: Routing.generate('neusta_pimcore_import_export_page_import'),
52+
method: 'POST',
53+
waitMsg: t('neusta_pimcore_import_export_import_dialog_wait_message'),
54+
headers: {
55+
'X-Requested-With': 'XMLHttpRequest' // ✅ important for AJAX-Requests
56+
},
57+
success: function (form, action) {
58+
let response = Ext.decode(action.response.responseText);
59+
pimcore.helpers.showNotification(t('neusta_pimcore_import_export_import_dialog_notification_success'), response.message, 'success');
60+
pimcore.globalmanager.get('layout_document_tree').tree.getStore().reload();
61+
uploadDialog.close();
62+
},
63+
failure: function (form, action) {
64+
let response = Ext.decode(action.response.responseText);
65+
pimcore.helpers.showNotification(t('neusta_pimcore_import_export_import_dialog_notification_error'), response.message || 'Import failed', 'error');
66+
}
67+
});
68+
}
69+
}
70+
]
71+
})
72+
]
73+
});
74+
75+
uploadDialog.show();
76+
}
77+
}));
78+
}
79+
});
80+
81+
var pimcorePluginPageImport = new neusta_pimcore_import_export.plugin.page.import();
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Neusta\Pimcore\ImportExportBundle\Controller\Admin;
6+
7+
use Neusta\Pimcore\ImportExportBundle\Documents\Import\PageImporter;
8+
use Neusta\Pimcore\ImportExportBundle\Toolbox\Repository\PageRepository;
9+
use Pimcore\Model\Document\Page;
10+
use Symfony\Component\HttpFoundation\File\UploadedFile;
11+
use Symfony\Component\HttpFoundation\JsonResponse;
12+
use Symfony\Component\HttpFoundation\Request;
13+
use Symfony\Component\Routing\Annotation\Route;
14+
15+
final class PageImportController
16+
{
17+
public function __construct(
18+
private PageImporter $pageImporter,
19+
private PageRepository $pageRepository,
20+
) {
21+
}
22+
23+
#[Route(
24+
'/admin/neusta/import-export/page/import',
25+
name: 'neusta_pimcore_import_export_page_import',
26+
methods: ['POST']
27+
)]
28+
public function import(Request $request): JsonResponse
29+
{
30+
$file = $request->files->get('file');
31+
if (!$file instanceof UploadedFile) {
32+
return new JsonResponse(['success' => false, 'message' => 'No file uploaded'], 400);
33+
}
34+
35+
$overwrite = $request->request->getBoolean('overwrite');
36+
37+
try {
38+
$page = $this->pageImporter->parseYaml($file->getContent());
39+
40+
$message = $this->replaceIfExists($page, $overwrite);
41+
42+
return new JsonResponse(['success' => true, 'message' => $message]);
43+
} catch (\Exception $e) {
44+
return new JsonResponse(['success' => false, 'message' => $e->getMessage()], 500);
45+
}
46+
}
47+
48+
protected function replaceIfExists(Page $page, bool $overwrite): string
49+
{
50+
$oldPage = $this->pageRepository->getByPath('/' . $page->getFullPath());
51+
if (null !== $oldPage) {
52+
if ($overwrite) {
53+
$oldPage->delete();
54+
$page->save();
55+
56+
return 'Document replaced successfully';
57+
}
58+
59+
return 'Document already exists and was not replaced';
60+
}
61+
$page->save();
62+
63+
return 'New Document imported successfully';
64+
}
65+
}

src/EventListener/PimcoreAdminListener.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public function addJSFiles(PathsEvent $event): void
1010
{
1111
$event->addPaths([
1212
'/bundles/neustapimcoreimportexport/js/exportPage.js',
13+
'/bundles/neustapimcoreimportexport/js/importPage.js',
1314
]);
1415
}
1516
}

translations/admin.de.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
neusta_pimcore_import_export_export_menu_label: 'Exportiere in YAML'
2+
neusta_pimcore_import_export_import_dialog_title: 'Importiere Seite aus YAML'
3+
neusta_pimcore_import_export_import_menu_label: 'Importiere aus YAML'
4+
neusta_pimcore_import_export_import_dialog_file_label: 'YAML Datei'
5+
neusta_pimcore_import_export_import_dialog_file_button: 'Wähle YAML Datei...'
6+
neusta_pimcore_import_export_import_dialog_overwrite_label: 'überschreibe, falls vorhanden'
7+
neusta_pimcore_import_export_import_dialog_wait_message: 'Hochladen...'
8+
neusta_pimcore_import_export_import_dialog_notification_success: 'Import erfolgreich'
9+
neusta_pimcore_import_export_import_dialog_notification_error: 'Import fehlgeschlagen'

0 commit comments

Comments
 (0)