From 07fb4e216752f532cbd65b74f90b5f340ad17e98 Mon Sep 17 00:00:00 2001 From: GabrielMeyer Date: Fri, 12 Jul 2019 16:47:11 +0200 Subject: [PATCH] Exporting comments for XLSX --- .../src/app/shared/utils/reconvert-chars.ts | 18 +++++ .../motion-export-dialog.component.ts | 9 ++- .../motion-list/motion-list.component.ts | 2 +- .../services/motion-xlsx-export.service.ts | 78 ++++++++++++------- 4 files changed, 77 insertions(+), 30 deletions(-) create mode 100644 client/src/app/shared/utils/reconvert-chars.ts diff --git a/client/src/app/shared/utils/reconvert-chars.ts b/client/src/app/shared/utils/reconvert-chars.ts new file mode 100644 index 000000000..74ec7111f --- /dev/null +++ b/client/src/app/shared/utils/reconvert-chars.ts @@ -0,0 +1,18 @@ +/** + * This function converts german umlauts back. + * + * @param text + * + * @returns {string} The whole text with german umlauts. + */ +export function reconvertChars(text: string): string { + return text + .replace(/ä|ä/g, 'ä') + .replace(/Ä|Ä/g, 'Ä') + .replace(/ö|ö/g, 'ö') + .replace(/Ö|Ö/g, 'Ö') + .replace(/ü/g, 'ü') + .replace(/Ü/g, 'Ü') + .replace(/å|å/g, 'å') + .replace(/Å|Å/g, 'Å'); +} diff --git a/client/src/app/site/motions/modules/motion-list/components/motion-export-dialog/motion-export-dialog.component.ts b/client/src/app/site/motions/modules/motion-list/components/motion-export-dialog/motion-export-dialog.component.ts index f8c99b70a..1a9645460 100644 --- a/client/src/app/site/motions/modules/motion-list/components/motion-export-dialog/motion-export-dialog.component.ts +++ b/client/src/app/site/motions/modules/motion-list/components/motion-export-dialog/motion-export-dialog.component.ts @@ -148,10 +148,16 @@ export class MotionExportDialogComponent implements OnInit { this.enableControl('content'); } + // At the moment the csv can't export comments. + if (format === FileFormat.CSV) { + this.disableControl('comments'); + } else { + this.enableControl('comments'); + } + if (format === FileFormat.CSV || format === FileFormat.XLSX) { this.disableControl('lnMode'); this.disableControl('crMode'); - this.disableControl('comments'); this.disableControl('pdfOptions'); // remove the selection of "votingResult" @@ -168,7 +174,6 @@ export class MotionExportDialogComponent implements OnInit { if (format === FileFormat.PDF) { this.enableControl('lnMode'); this.enableControl('crMode'); - this.enableControl('comments'); this.enableControl('pdfOptions'); this.votingResultButton.disabled = false; } diff --git a/client/src/app/site/motions/modules/motion-list/components/motion-list/motion-list.component.ts b/client/src/app/site/motions/modules/motion-list/components/motion-list/motion-list.component.ts index c8802cbac..2f0ffc940 100644 --- a/client/src/app/site/motions/modules/motion-list/components/motion-list/motion-list.component.ts +++ b/client/src/app/site/motions/modules/motion-list/components/motion-list/motion-list.component.ts @@ -326,7 +326,7 @@ export class MotionListComponent extends BaseListViewComponent imple exportInfo.crMode ); } else if (exportInfo.format === FileFormat.XLSX) { - this.motionXlsxExport.exportMotionList(data, exportInfo.metaInfo); + this.motionXlsxExport.exportMotionList(data, exportInfo.metaInfo, exportInfo.comments); } } }); diff --git a/client/src/app/site/motions/services/motion-xlsx-export.service.ts b/client/src/app/site/motions/services/motion-xlsx-export.service.ts index 40e1fe798..78b2348f6 100644 --- a/client/src/app/site/motions/services/motion-xlsx-export.service.ts +++ b/client/src/app/site/motions/services/motion-xlsx-export.service.ts @@ -8,6 +8,9 @@ import { MotionRepositoryService } from 'app/core/repositories/motions/motion-re import { TranslateService } from '@ngx-translate/core'; import { ViewMotion } from '../models/view-motion'; import { XlsxExportServiceService, CellFillingDefinition } from 'app/core/ui-services/xlsx-export-service.service'; +import { MotionCommentSectionRepositoryService } from 'app/core/repositories/motions/motion-comment-section-repository.service'; +import { stripHtmlTags } from 'app/shared/utils/strip-html-tags'; +import { reconvertChars } from 'app/shared/utils/reconvert-chars'; /** * Service to export motion elements to XLSX @@ -64,7 +67,8 @@ export class MotionXlsxExportService { public constructor( private xlsx: XlsxExportServiceService, private translate: TranslateService, - private motionRepo: MotionRepositoryService + private motionRepo: MotionRepositoryService, + private commentRepo: MotionCommentSectionRepositoryService ) {} /** @@ -73,8 +77,9 @@ export class MotionXlsxExportService { * @param motions * @param contentToExport * @param infoToExport + * @param comments The ids of the comments, that will be exported, too. */ - public exportMotionList(motions: ViewMotion[], infoToExport: InfoToExport[]): void { + public exportMotionList(motions: ViewMotion[], infoToExport: InfoToExport[], comments: number[]): void { const workbook = new Workbook(); const properties = sortMotionPropertyList(['identifier', 'title'].concat(infoToExport)); @@ -97,15 +102,21 @@ export class MotionXlsxExportService { } }); - worksheet.columns = properties.map(property => { - const propertyHeader = - property === 'motion_block' - ? 'Motion block' - : property.charAt(0).toLocaleUpperCase() + property.slice(1); - return { - header: this.translate.instant(propertyHeader) - }; - }); + const columns = []; + columns.push( + ...properties.map(property => { + const propertyHeader = + property === 'motion_block' + ? 'Motion block' + : property.charAt(0).toLocaleUpperCase() + property.slice(1); + return { + header: this.translate.instant(propertyHeader) + }; + }) + ); + columns.push(...comments.map(commentId => ({ header: this.commentRepo.getViewModel(commentId).getTitle() }))); + + worksheet.columns = columns; worksheet.getRow(1).eachCell(cell => { cell.font = { @@ -118,23 +129,36 @@ export class MotionXlsxExportService { }); // map motion data to properties - const motionData = motions.map(motion => - properties.map(property => { - const motionProp = motion[property]; - if (motionProp) { - switch (property) { - case 'state': - return this.motionRepo.getExtendedStateLabel(motion); - case 'recommendation': - return this.motionRepo.getExtendedRecommendationLabel(motion); - default: - return this.translate.instant(motionProp.toString()); + const motionData = motions.map(motion => { + const data = []; + data.push( + ...properties.map(property => { + const motionProp = motion[property]; + if (motionProp) { + switch (property) { + case 'state': + return this.motionRepo.getExtendedStateLabel(motion); + case 'recommendation': + return this.motionRepo.getExtendedRecommendationLabel(motion); + default: + return this.translate.instant(motionProp.toString()); + } + } else { + return ''; } - } else { - return ''; - } - }) - ); + }) + ); + data.push( + ...comments.map(commentId => { + const section = this.commentRepo.getViewModel(commentId); + const motionComment = motion.getCommentForSection(section); + return motionComment && motionComment.comment + ? reconvertChars(stripHtmlTags(motionComment.comment)) + : ''; + }) + ); + return data; + }); // add to sheet for (let i = 0; i < motionData.length; i++) {