2018-11-23 13:42:44 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
|
2019-04-03 11:31:08 +02:00
|
|
|
import {
|
|
|
|
CsvExportService,
|
|
|
|
CsvColumnDefinitionProperty,
|
|
|
|
CsvColumnDefinitionMap
|
|
|
|
} from 'app/core/ui-services/csv-export.service';
|
2019-04-17 12:10:56 +02:00
|
|
|
import { motionImportExportHeaderOrder } from '../motion-import-export-order';
|
2019-04-03 11:31:08 +02:00
|
|
|
import { MotionRepositoryService } from 'app/core/repositories/motions/motion-repository.service';
|
2019-02-06 13:13:19 +01:00
|
|
|
import { ViewMotion } from '../models/view-motion';
|
2018-11-23 13:42:44 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Exports CSVs for motions. Collect all CSV types here to have them in one place.
|
|
|
|
*/
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class MotionCsvExportService {
|
|
|
|
/**
|
|
|
|
* Does nothing.
|
|
|
|
*
|
|
|
|
* @param csvExport CsvExportService
|
|
|
|
* @param translate TranslateService
|
|
|
|
*/
|
2019-04-03 11:31:08 +02:00
|
|
|
public constructor(
|
|
|
|
private csvExport: CsvExportService,
|
|
|
|
private translate: TranslateService,
|
|
|
|
private motionRepo: MotionRepositoryService
|
|
|
|
) {}
|
2018-11-23 13:42:44 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Export all motions as CSV
|
|
|
|
*
|
|
|
|
* @param motions Motions to export
|
2019-01-25 17:03:05 +01:00
|
|
|
* @param contentToExport content properties to export
|
2018-11-23 13:42:44 +01:00
|
|
|
*/
|
2019-04-17 12:10:56 +02:00
|
|
|
public exportMotionList(motions: ViewMotion[], contentToExport: string[]): void {
|
|
|
|
// reorders the exported properties according to motionImportExportHeaderOrder
|
|
|
|
const propertyList = motionImportExportHeaderOrder.filter(property => contentToExport.includes(property));
|
2019-04-03 11:31:08 +02:00
|
|
|
const exportProperties: (
|
|
|
|
| CsvColumnDefinitionProperty<ViewMotion>
|
|
|
|
| CsvColumnDefinitionMap<ViewMotion>)[] = propertyList.map(option => {
|
|
|
|
if (option === 'recommendation') {
|
|
|
|
return {
|
|
|
|
label: 'recommendation',
|
|
|
|
map: motion => this.motionRepo.getExtendedRecommendationLabel(motion)
|
|
|
|
};
|
|
|
|
} else if (option === 'state') {
|
|
|
|
return {
|
|
|
|
label: 'state',
|
|
|
|
map: motion => this.motionRepo.getExtendedStateLabel(motion)
|
|
|
|
};
|
2019-04-17 12:10:56 +02:00
|
|
|
} else if (option === 'motion_block') {
|
|
|
|
return {
|
|
|
|
label: 'Motion block',
|
|
|
|
map: motion => (motion.motion_block ? motion.motion_block.getTitle() : '')
|
|
|
|
};
|
2019-04-03 11:31:08 +02:00
|
|
|
} else {
|
|
|
|
return { property: option } as CsvColumnDefinitionProperty<ViewMotion>;
|
|
|
|
}
|
2019-01-25 17:03:05 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
this.csvExport.export(motions, exportProperties, this.translate.instant('Motions') + '.csv');
|
2018-11-23 13:42:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exports the call list.
|
|
|
|
*
|
|
|
|
* @param motions All motions in the CSV. They should be ordered by callListWeight correctly.
|
|
|
|
*/
|
|
|
|
public exportCallList(motions: ViewMotion[]): void {
|
|
|
|
this.csvExport.export(
|
|
|
|
motions,
|
|
|
|
[
|
2019-01-10 12:54:48 +01:00
|
|
|
{ label: 'Called', map: motion => (motion.sort_parent_id ? '' : motion.identifierOrTitle) },
|
|
|
|
{ label: 'Called with', map: motion => (!motion.sort_parent_id ? '' : motion.identifierOrTitle) },
|
2018-11-23 13:42:44 +01:00
|
|
|
{ label: 'submitters', map: motion => motion.submitters.map(s => s.short_name).join(',') },
|
|
|
|
{ property: 'title' },
|
2019-01-10 12:54:48 +01:00
|
|
|
{
|
|
|
|
label: 'recommendation',
|
2019-04-03 11:31:08 +02:00
|
|
|
map: motion => (motion.recommendation ? this.motionRepo.getExtendedRecommendationLabel(motion) : '')
|
2019-01-10 12:54:48 +01:00
|
|
|
},
|
2018-11-23 13:42:44 +01:00
|
|
|
{ property: 'motion_block', label: 'Motion block' }
|
|
|
|
],
|
|
|
|
this.translate.instant('Call list') + '.csv'
|
|
|
|
);
|
|
|
|
}
|
2018-12-04 19:31:24 +01:00
|
|
|
|
2019-04-17 12:10:56 +02:00
|
|
|
// TODO does not reflect updated export order. any more. Hard coded for now
|
2018-12-04 19:31:24 +01:00
|
|
|
public exportDummyMotion(): void {
|
2019-02-28 12:46:39 +01:00
|
|
|
const headerRow = ['Identifier', 'Title', 'Text', 'Reason', 'Submitters', 'Category', 'Origin', 'Motion block'];
|
2018-12-04 19:31:24 +01:00
|
|
|
const rows = [
|
2019-02-28 12:46:39 +01:00
|
|
|
['A1', 'Title 1', 'Text 1', 'Reason 1', 'Submitter A', 'Category A', 'Last Year Conference A', 'Block A'],
|
|
|
|
['B1', 'Title 2', 'Text 2', 'Reason 2', 'Submitter B', 'Category B', null, 'Block A'],
|
|
|
|
[null, 'Title 3', 'Text 3', null, null, null, null, null]
|
2018-12-04 19:31:24 +01:00
|
|
|
];
|
2019-02-28 12:46:39 +01:00
|
|
|
this.csvExport.dummyCSVExport(headerRow, rows, `${this.translate.instant('motions-example')}.csv`);
|
2018-12-04 19:31:24 +01:00
|
|
|
}
|
2018-11-23 13:42:44 +01:00
|
|
|
}
|