2019-01-22 15:24:02 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
|
2019-01-31 13:40:27 +01:00
|
|
|
import { ConfigService } from 'app/core/ui-services/config.service';
|
2019-01-22 15:24:02 +01:00
|
|
|
import { MotionPoll } from 'app/shared/models/motions/motion-poll';
|
2019-01-19 21:55:06 +01:00
|
|
|
import { MotionRepositoryService } from 'app/core/repositories/motions/motion-repository.service';
|
2019-01-31 13:40:27 +01:00
|
|
|
import { PdfDocumentService } from 'app/core/ui-services/pdf-document.service';
|
2019-04-29 16:24:59 +02:00
|
|
|
import { PollPdfService, AbstractPollData } from 'app/core/core-services/poll-pdf-service';
|
2019-01-31 13:40:27 +01:00
|
|
|
import { UserRepositoryService } from 'app/core/repositories/users/user-repository.service';
|
2019-01-22 15:24:02 +01:00
|
|
|
|
2019-01-31 08:20:15 +01:00
|
|
|
type BallotCountChoices = 'NUMBER_OF_DELEGATES' | 'NUMBER_OF_ALL_PARTICIPANTS' | 'CUSTOM_NUMBER';
|
|
|
|
|
2019-01-22 15:24:02 +01:00
|
|
|
/**
|
|
|
|
* Creates a pdf for a motion poll. Takes as input any motionPoll
|
|
|
|
* Provides the public method `printBallots(motionPoll)` which should be convenient to use.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* this.MotionPollPdfService.printBallos(this.poll);
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
2019-04-29 16:24:59 +02:00
|
|
|
export class MotionPollPdfService extends PollPdfService {
|
2019-01-22 15:24:02 +01:00
|
|
|
/**
|
|
|
|
* Constructor. Subscribes to configuration values
|
|
|
|
*
|
|
|
|
* @param translate handle translations
|
|
|
|
* @param motionRepo get parent motions
|
|
|
|
* @param configService Read config variables
|
|
|
|
* @param userRepo User repository for counting amount of ballots needed
|
|
|
|
* @param pdfService the pdf document creation service
|
|
|
|
*/
|
|
|
|
public constructor(
|
|
|
|
private translate: TranslateService,
|
|
|
|
private motionRepo: MotionRepositoryService,
|
2019-04-29 16:24:59 +02:00
|
|
|
configService: ConfigService,
|
|
|
|
userRepo: UserRepositoryService,
|
2019-01-22 15:24:02 +01:00
|
|
|
private pdfService: PdfDocumentService
|
|
|
|
) {
|
2019-04-29 16:24:59 +02:00
|
|
|
super(configService, userRepo);
|
2019-01-22 15:24:02 +01:00
|
|
|
this.configService
|
2019-01-31 08:20:15 +01:00
|
|
|
.get<number>('motions_pdf_ballot_papers_number')
|
|
|
|
.subscribe(count => (this.ballotCustomCount = count));
|
|
|
|
this.configService
|
|
|
|
.get<BallotCountChoices>('motions_pdf_ballot_papers_selection')
|
2019-01-22 15:24:02 +01:00
|
|
|
.subscribe(selection => (this.ballotCountSelection = selection));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Triggers a pdf creation for this poll's ballots.
|
|
|
|
* There will be 8 ballots per page.
|
|
|
|
* Each ballot will contain:
|
|
|
|
* - the event name and logo
|
|
|
|
* - a first, bold line with a title. Defaults to the label Motion, the identifier,
|
|
|
|
* and the current number of polls for this motion (if more than one)
|
|
|
|
* - a subtitle. A second, short (two lines, 90 characters) clarification for
|
|
|
|
* the ballot. Defaults to the beginning of the motion's title
|
|
|
|
* - the options 'yes', 'no', 'abstain' translated to the client's language.
|
|
|
|
*
|
|
|
|
* @param motionPoll: The poll this ballot refers to
|
|
|
|
* @param title (optional) a different title
|
|
|
|
* @param subtitle (optional) a different subtitle
|
|
|
|
*/
|
|
|
|
public printBallots(motionPoll: MotionPoll, title?: string, subtitle?: string): void {
|
|
|
|
const motion = this.motionRepo.getViewModel(motionPoll.motion_id);
|
|
|
|
const fileName = `${this.translate.instant('Motion')} - ${motion.identifier} - ${this.translate.instant(
|
|
|
|
'ballot-paper'
|
|
|
|
)}`;
|
|
|
|
if (!title) {
|
|
|
|
title = `${this.translate.instant('Motion')} - ${motion.identifier}`;
|
|
|
|
if (motion.motion.polls.length > 1) {
|
|
|
|
title += ` (${this.translate.instant('Vote')} ${motion.motion.polls.length})`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!subtitle) {
|
|
|
|
subtitle = motion.title;
|
|
|
|
}
|
|
|
|
if (subtitle.length > 90) {
|
|
|
|
subtitle = subtitle.substring(0, 90) + '...';
|
|
|
|
}
|
2019-04-29 16:24:59 +02:00
|
|
|
const rowsPerPage = 4;
|
|
|
|
this.pdfService.downloadWithBallotPaper(
|
|
|
|
this.getPages(rowsPerPage, { sheetend: 40, title: title, subtitle: subtitle }),
|
|
|
|
fileName,
|
|
|
|
this.logo
|
|
|
|
);
|
2019-01-22 15:24:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates one ballot in it's position on the page. Note that creating once
|
|
|
|
* and then pasting the result several times does not work
|
|
|
|
*
|
|
|
|
* @param title The identifier of the motion
|
|
|
|
* @param subtitle The actual motion title
|
|
|
|
*/
|
2019-04-29 16:24:59 +02:00
|
|
|
protected createBallot(data: AbstractPollData): any {
|
2019-01-22 15:24:02 +01:00
|
|
|
return {
|
|
|
|
stack: [
|
|
|
|
this.getHeader(),
|
2019-04-29 16:24:59 +02:00
|
|
|
this.getTitle(data.title),
|
|
|
|
this.getSubtitle(data.subtitle),
|
2019-01-22 15:24:02 +01:00
|
|
|
this.createBallotOption(this.translate.instant('Yes')),
|
|
|
|
this.createBallotOption(this.translate.instant('No')),
|
|
|
|
this.createBallotOption(this.translate.instant('Abstain'))
|
|
|
|
],
|
2019-04-29 16:24:59 +02:00
|
|
|
margin: [0, 0, 0, data.sheetend]
|
2019-01-22 15:24:02 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|