Skip to content

Commit c96f95c

Browse files
committed
[Feature] Moved import menu to main menu toolbar
1 parent 90b454c commit c96f95c

File tree

6 files changed

+126
-81
lines changed

6 files changed

+126
-81
lines changed

public/js/importPage.js

Lines changed: 92 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,109 @@ pimcore.registerNS("neusta_pimcore_import_export.plugin.page.import");
22

33
neusta_pimcore_import_export.plugin.page.import = Class.create({
44
initialize: function () {
5-
document.addEventListener(pimcore.events.prepareDocumentTreeContextMenu, this.onPrepareDocumentTreeContextMenu.bind(this));
5+
document.addEventListener(pimcore.events.preMenuBuild, this.preMenuBuild.bind(this));
66
},
77

8-
onPrepareDocumentTreeContextMenu: function (e) {
8+
preMenuBuild: function (e) {
99
let menu = e.detail.menu;
10-
let document = e.detail.document;
1110

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,
11+
menu.neusta_pimcore_import_export = {
12+
label: t('neusta_pimcore_import_export_import_menu_label'),
13+
iconCls: 'pimcore_icon_import', // Pimcore-Icon für Konsistenz
14+
priority: 50, // Position im Menü (höher = weiter oben)
15+
handler: this.openImportDialog.bind(this),
16+
noSubmenus: true // Kein Submenü, direkter Klick öffnet das Fenster
17+
};
18+
},
19+
20+
openImportDialog: function () {
21+
let uploadDialog = new Ext.Window({
22+
title: t('neusta_pimcore_import_export_import_dialog_title'),
23+
width: 600,
24+
layout: 'fit',
25+
modal: true,
26+
items: [
27+
new Ext.form.Panel({
28+
bodyPadding: 10,
2129
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'),
30+
{
31+
xtype: 'filefield',
32+
name: 'file',
33+
width: 450,
34+
fieldLabel: t('neusta_pimcore_import_export_import_dialog_file_label'),
35+
labelWidth: 100,
36+
allowBlank: false,
37+
buttonText: t('neusta_pimcore_import_export_import_dialog_file_button'),
38+
accept: '.yaml,.yml'
39+
},
40+
{
41+
xtype: 'checkbox',
42+
name: 'overwrite',
43+
fieldLabel: t('neusta_pimcore_import_export_import_dialog_overwrite_label'),
44+
}
45+
],
46+
buttons: [
47+
{
48+
text: 'Import',
49+
handler: function (btn) {
50+
let form = btn.up('form').getForm();
51+
if (!form.isValid()) {
52+
return;
3953
}
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-
}
4954

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-
params: {
58-
'csrfToken': parent.pimcore.settings["csrfToken"]
59-
},
60-
success: function (form, action) {
61-
let response = Ext.decode(action.response.responseText);
62-
pimcore.helpers.showNotification(t('neusta_pimcore_import_export_import_dialog_notification_success'), response.message, 'success');
63-
pimcore.globalmanager.get('layout_document_tree').tree.getStore().reload();
64-
uploadDialog.close();
65-
},
66-
failure: function (form, action) {
67-
let response = Ext.decode(action.response.responseText);
68-
pimcore.helpers.showNotification(t('neusta_pimcore_import_export_import_dialog_notification_error'), response.message || 'Import failed', 'error');
69-
}
55+
form.submit({
56+
url: Routing.generate('neusta_pimcore_import_export_page_import'),
57+
method: 'POST',
58+
waitMsg: t('neusta_pimcore_import_export_import_dialog_wait_message'),
59+
headers: {
60+
'X-Requested-With': 'XMLHttpRequest' // ✅ important for AJAX-Requests
61+
},
62+
params: {
63+
'csrfToken': parent.pimcore.settings["csrfToken"]
64+
},
65+
success: function (form, action) {
66+
let response = Ext.decode(action.response.responseText);
67+
// pimcore.helpers.showNotification(t('neusta_pimcore_import_export_import_dialog_notification_success'), response.message, 'success');
68+
let successDialog = new Ext.Window({
69+
title: t('neusta_pimcore_import_export_import_dialog_notification_success'),
70+
width: 300,
71+
height: 200,
72+
modal: true, // ✅ Modal = Benutzer muss interagieren
73+
layout: 'fit',
74+
items: [
75+
{
76+
xtype: 'panel',
77+
html: `${response.message}`,
78+
}
79+
],
80+
buttons: [
81+
{
82+
text: 'OK',
83+
handler: function () {
84+
successDialog.close();
85+
}
86+
}
87+
]
7088
});
89+
90+
successDialog.show();
91+
92+
pimcore.globalmanager.get('layout_document_tree').tree.getStore().reload();
93+
uploadDialog.close();
94+
},
95+
failure: function (form, action) {
96+
let response = Ext.decode(action.response.responseText);
97+
pimcore.helpers.showNotification(t('neusta_pimcore_import_export_import_dialog_notification_error'), response.message || 'Import failed', 'error');
7198
}
72-
}
73-
]
74-
})
99+
});
100+
}
101+
}
75102
]
76-
});
103+
})
104+
]
105+
});
77106

78-
uploadDialog.show();
79-
}
80-
}));
107+
uploadDialog.show();
81108
}
82109
});
83110

src/Controller/Admin/PageImportController.php

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public function __construct(
3030
) {
3131
$this->messagesMap = [
3232
self::ERR_NO_FILE_UPLOADED => 'No file uploaded',
33-
self::SUCCESS_DOCUMENT_REPLACEMENT => 'Documents replaced successfully',
34-
self::SUCCESS_WITHOUT_REPLACEMENT => 'Documents already exist and were not replaced',
35-
self::SUCCESS_NEW_DOCUMENT => 'New Documents imported successfully',
33+
self::SUCCESS_DOCUMENT_REPLACEMENT => 'replaced successfully',
34+
self::SUCCESS_WITHOUT_REPLACEMENT => 'not replaced',
35+
self::SUCCESS_NEW_DOCUMENT => 'imported successfully',
3636
];
3737
}
3838

@@ -53,12 +53,16 @@ public function import(Request $request): JsonResponse
5353
try {
5454
$pages = $this->pageImporter->parseYaml($file->getContent());
5555

56-
$resultMessage = 'Import Summary:' . \PHP_EOL;
57-
58-
foreach ($pages as $index => $page) {
56+
$results = [
57+
self::SUCCESS_DOCUMENT_REPLACEMENT => 0,
58+
self::SUCCESS_WITHOUT_REPLACEMENT => 0,
59+
self::SUCCESS_NEW_DOCUMENT => 0,
60+
];
61+
foreach ($pages as $page) {
5962
$resultCode = $this->replaceIfExists($page, $overwrite);
60-
$resultMessage = $this->appendMessage($index, $resultCode, $resultMessage);
63+
++$results[$resultCode];
6164
}
65+
$resultMessage = $this->appendMessage($results);
6266

6367
return new JsonResponse(['success' => true, 'message' => $resultMessage]);
6468
} catch (\Exception $e) {
@@ -84,11 +88,25 @@ protected function replaceIfExists(Page $page, bool $overwrite): int
8488
return self::SUCCESS_NEW_DOCUMENT;
8589
}
8690

87-
private function appendMessage(int|string $index, int $resultCode, string $resultMessage): string
91+
/**
92+
* @param array<int, int> $results
93+
*/
94+
private function appendMessage(array $results): string
8895
{
89-
$message = \sprintf('%d. %s', (int) $index + 1, $this->messagesMap[$resultCode]);
90-
$resultMessage .= $message . \PHP_EOL;
96+
$resultMessage = '';
97+
98+
foreach ($results as $resultCode => $result) {
99+
if ($result > 0) {
100+
if (1 === $result) {
101+
$start = 'One Document';
102+
} else {
103+
$start = \sprintf('%d Documents', $result);
104+
}
105+
$message = \sprintf('%s %s', $start, $this->messagesMap[$resultCode]);
106+
$resultMessage .= $message . '<br/><br/>';
107+
}
108+
}
91109

92-
return $resultMessage;
110+
return '<p style="padding: 20px;">' . $resultMessage . '</p>';
93111
}
94112
}

tests/Integration/Documents/Export/PageExporterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function testSinglePageExport(): void
3737
$page->setTitle('Titel meines Dokuments');
3838
$page->setController('Irgend/ein/Controller');
3939
$inputEditable = new Input();
40-
$inputEditable->setName('textEingabe');;
40+
$inputEditable->setName('textEingabe');
4141
$inputEditable->setDataFromResource('Texteingabe');
4242
$page->setEditables([$inputEditable]);
4343

tests/Unit/Controller/Admin/PageExportControllerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function testExportPage_regular_case(): void
4343
{
4444
$page = $this->prophesize(Page::class);
4545
$this->pageRepository->getById(17)->willReturn($page->reveal());
46-
$this->pageExporter->exportToYaml($page->reveal())->willReturn('TEST_YAML');
46+
$this->pageExporter->exportToYaml([$page->reveal()])->willReturn('TEST_YAML');
4747

4848
$response = $this->controller->exportPage($this->request);
4949

@@ -57,7 +57,7 @@ public function testExportPage_exceptional_case(): void
5757
{
5858
$page = $this->prophesize(Page::class);
5959
$this->pageRepository->getById(17)->willReturn($page->reveal());
60-
$this->pageExporter->exportToYaml($page->reveal())->willThrow(new \Exception('Problem'));
60+
$this->pageExporter->exportToYaml([$page->reveal()])->willThrow(new \Exception('Problem'));
6161

6262
$response = $this->controller->exportPage($this->request);
6363

translations/admin.de.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ neusta_pimcore_import_export_import_dialog_file_label: 'YAML Datei'
66
neusta_pimcore_import_export_import_dialog_file_button: 'Wähle YAML Datei...'
77
neusta_pimcore_import_export_import_dialog_overwrite_label: 'überschreibe, falls vorhanden'
88
neusta_pimcore_import_export_import_dialog_wait_message: 'Hochladen...'
9-
neusta_pimcore_import_export_import_dialog_notification_success: 'Import erfolgreich'
9+
neusta_pimcore_import_export_import_dialog_notification_success: 'Import Zusammenfassung'
1010
neusta_pimcore_import_export_import_dialog_notification_error: 'Import fehlgeschlagen'

translations/admin.en.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ neusta_pimcore_import_export_import_dialog_file_label: 'YAML File'
66
neusta_pimcore_import_export_import_dialog_file_button: 'Select Yaml file...'
77
neusta_pimcore_import_export_import_dialog_overwrite_label: 'overwrite if exists'
88
neusta_pimcore_import_export_import_dialog_wait_message: 'Uploading...'
9-
neusta_pimcore_import_export_import_dialog_notification_success: 'Import successful'
9+
neusta_pimcore_import_export_import_dialog_notification_success: 'Import Summary'
1010
neusta_pimcore_import_export_import_dialog_notification_error: 'Import failed'

0 commit comments

Comments
 (0)