From c42a5c1f39b219eddbd98fa5853740536fec5fec Mon Sep 17 00:00:00 2001
From: Ron Lucke <lucke@elan-ev.de>
Date: Wed, 13 Apr 2022 15:53:06 +0000
Subject: [PATCH] fix #846

Closes #846
---
 .../courseware/CoursewareCourseManager.vue    |  2 +-
 resources/vue/mixins/courseware/export.js     | 32 +++++++++++++++++++
 resources/vue/mixins/courseware/import.js     | 31 +++++++++++++++---
 3 files changed, 59 insertions(+), 6 deletions(-)

diff --git a/resources/vue/components/courseware/CoursewareCourseManager.vue b/resources/vue/components/courseware/CoursewareCourseManager.vue
index 7999ca63e15..cd3f77421eb 100755
--- a/resources/vue/components/courseware/CoursewareCourseManager.vue
+++ b/resources/vue/components/courseware/CoursewareCourseManager.vue
@@ -232,7 +232,7 @@ export default {
 
     methods: {
         ...mapActions({
-            loadCoursewareStructure: 'loadCoursewareStructure',
+            loadCoursewareStructure: 'courseware-structure/load',
             createStructuralElement: 'createStructuralElement',
             updateStructuralElement: 'updateStructuralElement',
             deleteStructuralElement: 'deleteStructuralElement',
diff --git a/resources/vue/mixins/courseware/export.js b/resources/vue/mixins/courseware/export.js
index 2f46d7b08f1..1eab732a951 100755
--- a/resources/vue/mixins/courseware/export.js
+++ b/resources/vue/mixins/courseware/export.js
@@ -27,7 +27,13 @@ export default {
     },
 
     methods: {
+        initData() {
+            this.exportFiles = { json: [], download: [] };
+            this.elementCounter = 0;
+            this.exportElementCounter = 0;
+        },
         async sendExportZip(root_id = null, options) {
+            this.initData();
             let view = this;
             let zip = await this.createExportFile(root_id, options);
             this.setExportState(this.$gettext('Erstelle Zip-Archiv'));
@@ -123,6 +129,7 @@ export default {
                     root_element.children = children;
                 }
             }
+            root_element.imageId = await this.exportStructuralElementImage(root_element);
 
             delete root_element.relationships;
             delete root_element.links;
@@ -211,6 +218,9 @@ export default {
                         }
                     }
 
+                    // export file data (if any)
+                    content.imageId = await this.exportStructuralElementImage(element);
+
                     delete content.relationships;
                     content.children = new_childs;
 
@@ -221,6 +231,28 @@ export default {
             return children;
         },
 
+        async exportStructuralElementImage(element) {
+            let fileId = element.relationships.image?.data?.id;
+            if (fileId) {
+                await this.$store.dispatch('file-refs/loadById', {id: fileId});
+                let fileRef = this.$store.getters['file-refs/byId']({id: fileId});
+                
+                let fileRefData = {};
+                fileRefData.id = fileRef.id;
+                fileRefData.attributes = fileRef.attributes;
+                fileRefData.related_element_id = element.id;
+                fileRefData.folder = null;
+
+                this.exportFiles.json.push(fileRefData);
+                this.exportFiles.download[fileRef.id] = {
+                    folder: null,
+                    url: fileRef.meta['download-url']
+                };
+            }
+
+            return fileId;
+        },
+
         async exportContainer(container_ref) {
             // make a local copy of the container
             let container = { ...container_ref };
diff --git a/resources/vue/mixins/courseware/import.js b/resources/vue/mixins/courseware/import.js
index 39e49cb4a8e..bbafe25c03b 100755
--- a/resources/vue/mixins/courseware/import.js
+++ b/resources/vue/mixins/courseware/import.js
@@ -63,7 +63,7 @@ export default {
         async importStructuralElement(element, parent_id, files) {
             if (element.length) {
                 for (var i = 0; i < element.length; i++) {
-                    this.setImportStructuresState('Lege Seite an: ' + element[i].attributes.title);
+                    this.setImportStructuresState(this.$gettext('Lege Seite an:') + ' ' + element[i].attributes.title);
                     await this.createStructuralElement({
                         attributes: element[i].attributes,
                         parentId: parent_id,
@@ -72,6 +72,23 @@ export default {
                     this.importElementCounter++;
 
                     let new_element = this.$store.getters['courseware-structural-elements/lastCreated'];
+
+                    if (element[i].imageId) {
+                        let imageFile = files.find((file) => { return file.id === element[i].imageId});
+                        let zip_filedata = await this.zip.file(imageFile.id).async('blob');
+                        // create new blob with correct type
+                        let filedata = zip_filedata.slice(0, zip_filedata.size, imageFile.attributes['mime-type']);
+                        this.setImportStructuresState(this.$gettext('Lade Vorschaubild hoch'));
+                        this.uploadImageForStructuralElement({
+                            structuralElement: new_element,
+                            file: filedata,
+                        }).catch((error) => {
+                            console.error(error);
+                            this.currentImportErrors.push(this.$gettext('Fehler beim Hochladen des Vorschaubildes.'));
+                        });
+                    }
+
+
                     if (element[i].children?.length > 0) {
                         await this.importStructuralElement(element[i].children, new_element.id, files);
                     }
@@ -80,7 +97,7 @@ export default {
                         for (var j = 0; j < element[i].containers.length; j++) {
                             let container = element[i].containers[j];
                             // TODO: create element on server and fetch new id
-                            this.setImportStructuresState('Lege Abschnitt an: ' + container.attributes.title);
+                            this.setImportStructuresState(this.$gettext('Lege Abschnitt an:') + ' ' + container.attributes.title);
                             await this.createContainer({
                                 attributes: container.attributes,
                                 structuralElementId: new_element.id,
@@ -108,7 +125,7 @@ export default {
         },
 
         async importBlock(block, block_container, files) {
-            this.setImportStructuresState('Lege neuen Block an: ' + block.attributes.title);
+            this.setImportStructuresState(this.$gettext('Lege neuen Block an:') + ' ' + block.attributes.title);
             try {
                 await this.createBlockInContainer({
                     container: {type: block_container.type, id: block_container.id},
@@ -135,7 +152,7 @@ export default {
                     block.attributes.payload = JSON.parse(payload);
                 }
             }
-            this.setImportStructuresState('Aktualisiere neuen Block: ' + block.attributes.title);
+            this.setImportStructuresState(this.$gettext('Aktualisiere neuen Block:') + ' ' + block.attributes.title);
             await this.updateBlockInContainer({
                 attributes: block.attributes,
                 blockId: new_block.id,
@@ -168,7 +185,7 @@ export default {
             this.setImportFilesProgress(0);
             this.setImportFilesState('');
             let now = new Date();
-            this.setImportFilesState('Lege Import Ordner an...');
+            this.setImportFilesState(this.$gettext('Lege Import Ordner an...'));
             let main_folder = await this.createRootFolder({
                 context: this.context,
                 folder: {
@@ -185,6 +202,9 @@ export default {
             if (main_folder) {
                 for (var i = 0; i < files.length; i++) {
                     // if the subfolder with the referenced id does not exist yet, create it
+                    if (!files[i].folder) {
+                        continue;
+                    }
                     if (!folders[files[i].folder.id]) {
                         this.setImportFilesState(this.$gettext('Lege Ordner an') + ': ' + files[i].folder.name);
                         folders[files[i].folder.id] = await this.createFolder({
@@ -249,6 +269,7 @@ export default {
             'setImportStructuresState',
             'setImportStructuresProgress',
             'setImportErrors',
+            'uploadImageForStructuralElement'
         ]),
     },
     watch: {
-- 
GitLab