2018-11-27 22:44:37 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
|
|
|
|
import { ViewMotion } from '../models/view-motion';
|
2019-01-31 13:40:27 +01:00
|
|
|
import { ChoiceService } from 'app/core/ui-services/choice.service';
|
|
|
|
import { PromptService } from 'app/core/ui-services/prompt.service';
|
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 { UserRepositoryService } from 'app/core/repositories/users/user-repository.service';
|
2019-01-19 21:55:06 +01:00
|
|
|
import { WorkflowRepositoryService } from 'app/core/repositories/motions/workflow-repository.service';
|
|
|
|
import { CategoryRepositoryService } from 'app/core/repositories/motions/category-repository.service';
|
2019-01-31 13:40:27 +01:00
|
|
|
import { TagRepositoryService } from 'app/core/repositories/tags/tag-repository.service';
|
|
|
|
import { HttpService } from 'app/core/core-services/http.service';
|
2019-02-01 13:56:08 +01:00
|
|
|
import { ItemRepositoryService } from 'app/core/repositories/agenda/item-repository.service';
|
|
|
|
import { Displayable } from 'app/site/base/displayable';
|
2018-11-30 10:09:49 +01:00
|
|
|
import { Identifiable } from 'app/shared/models/base/identifiable';
|
2019-01-19 21:55:06 +01:00
|
|
|
import { MotionBlockRepositoryService } from 'app/core/repositories/motions/motion-block-repository.service';
|
2019-02-04 15:41:01 +01:00
|
|
|
import { TreeService } from 'app/core/ui-services/tree.service';
|
2018-11-27 22:44:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Contains all multiselect actions for the motion list view.
|
|
|
|
*/
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class MotionMultiselectService {
|
|
|
|
/**
|
|
|
|
* Does nothing.
|
|
|
|
*
|
|
|
|
* @param repo MotionRepositoryService
|
|
|
|
* @param translate TranslateService
|
|
|
|
* @param promptService
|
|
|
|
* @param choiceService
|
|
|
|
* @param userRepo
|
|
|
|
* @param workflowRepo
|
|
|
|
* @param categoryRepo
|
|
|
|
* @param tagRepo
|
2018-12-18 17:06:17 +01:00
|
|
|
* @param agendaRepo
|
|
|
|
* @param motionBlockRepo
|
|
|
|
* @param httpService
|
2019-02-04 15:41:01 +01:00
|
|
|
* @param treeService
|
2018-11-27 22:44:37 +01:00
|
|
|
*/
|
|
|
|
public constructor(
|
|
|
|
private repo: MotionRepositoryService,
|
|
|
|
private translate: TranslateService,
|
|
|
|
private promptService: PromptService,
|
|
|
|
private choiceService: ChoiceService,
|
|
|
|
private userRepo: UserRepositoryService,
|
|
|
|
private workflowRepo: WorkflowRepositoryService,
|
|
|
|
private categoryRepo: CategoryRepositoryService,
|
2018-11-30 09:24:07 +01:00
|
|
|
private tagRepo: TagRepositoryService,
|
2019-02-01 13:56:08 +01:00
|
|
|
private agendaRepo: ItemRepositoryService,
|
2018-12-18 17:06:17 +01:00
|
|
|
private motionBlockRepo: MotionBlockRepositoryService,
|
2019-02-04 15:41:01 +01:00
|
|
|
private httpService: HttpService,
|
|
|
|
private treeService: TreeService
|
2018-11-27 22:44:37 +01:00
|
|
|
) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes the given motions. Asks for confirmation.
|
|
|
|
*
|
|
|
|
* @param motions The motions to delete
|
|
|
|
*/
|
|
|
|
public async delete(motions: ViewMotion[]): Promise<void> {
|
|
|
|
const content = this.translate.instant('This will delete all selected motions.');
|
|
|
|
if (await this.promptService.open('Are you sure?', content)) {
|
|
|
|
for (const motion of motions) {
|
|
|
|
await this.repo.delete(motion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 10:09:49 +01:00
|
|
|
/**
|
|
|
|
* Moves the related agenda items from the motions as childs under a selected (parent) agenda item.
|
|
|
|
*/
|
|
|
|
public async moveToItem(motions: ViewMotion[]): Promise<void> {
|
|
|
|
const title = this.translate.instant('This will move all selected motions as childs to:');
|
|
|
|
const choices: (Displayable & Identifiable)[] = this.agendaRepo.getViewModelList();
|
|
|
|
const selectedChoice = await this.choiceService.open(title, choices);
|
|
|
|
if (selectedChoice) {
|
|
|
|
const requestData = {
|
|
|
|
items: motions.map(motion => motion.agenda_item_id),
|
2018-12-18 17:06:17 +01:00
|
|
|
parent_id: selectedChoice.items as number
|
2018-11-30 10:09:49 +01:00
|
|
|
};
|
|
|
|
await this.httpService.post('/rest/agenda/item/assign', requestData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 22:44:37 +01:00
|
|
|
/**
|
|
|
|
* Opens a dialog and then sets the status for all motions.
|
|
|
|
*
|
|
|
|
* @param motions The motions to change
|
|
|
|
*/
|
2018-12-06 12:28:05 +01:00
|
|
|
public async setStateOfMultiple(motions: ViewMotion[]): Promise<void> {
|
2019-01-21 15:07:55 +01:00
|
|
|
const title = this.translate.instant('This will set the following state for all selected motions:');
|
2019-01-10 12:54:48 +01:00
|
|
|
const choices = this.workflowRepo.getWorkflowStatesForMotions(motions).map(workflowState => ({
|
|
|
|
id: workflowState.id,
|
|
|
|
label: workflowState.name
|
|
|
|
}));
|
2018-11-27 22:44:37 +01:00
|
|
|
const selectedChoice = await this.choiceService.open(title, choices);
|
|
|
|
if (selectedChoice) {
|
2019-01-18 18:25:02 +01:00
|
|
|
await this.repo.setMultiState(motions, selectedChoice.items as number);
|
2018-11-27 22:44:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens a dialog and sets the recommendation to the users choice for all selected motions.
|
|
|
|
*
|
|
|
|
* @param motions The motions to change
|
|
|
|
*/
|
|
|
|
public async setRecommendation(motions: ViewMotion[]): Promise<void> {
|
2019-01-21 15:07:55 +01:00
|
|
|
const title = this.translate.instant('This will set the following recommendation for all selected motions:');
|
2018-11-27 22:44:37 +01:00
|
|
|
const choices = this.workflowRepo
|
2018-12-18 17:06:17 +01:00
|
|
|
.getWorkflowStatesForMotions(motions)
|
2018-11-27 22:44:37 +01:00
|
|
|
.filter(workflowState => !!workflowState.recommendation_label)
|
|
|
|
.map(workflowState => ({
|
|
|
|
id: workflowState.id,
|
|
|
|
label: workflowState.recommendation_label
|
|
|
|
}));
|
2019-02-11 12:39:41 +01:00
|
|
|
const clearChoice = this.translate.instant('Delete recommendation');
|
2019-01-10 12:54:48 +01:00
|
|
|
const selectedChoice = await this.choiceService.open(title, choices, false, null, clearChoice);
|
2018-12-18 17:06:17 +01:00
|
|
|
if (selectedChoice) {
|
2018-11-30 09:24:07 +01:00
|
|
|
const requestData = motions.map(motion => ({
|
|
|
|
id: motion.id,
|
2019-01-10 12:54:48 +01:00
|
|
|
recommendation: selectedChoice.action ? 0 : (selectedChoice.items as number)
|
2018-11-30 09:24:07 +01:00
|
|
|
}));
|
2019-02-07 11:46:19 +01:00
|
|
|
await this.httpService.post('/rest/motions/motion/manage_multiple_recommendation/', {
|
2018-11-30 09:24:07 +01:00
|
|
|
motions: requestData
|
|
|
|
});
|
2018-11-27 22:44:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens a dialog and sets the category for all given motions.
|
|
|
|
*
|
|
|
|
* @param motions The motions to change
|
|
|
|
*/
|
|
|
|
public async setCategory(motions: ViewMotion[]): Promise<void> {
|
2019-01-21 15:07:55 +01:00
|
|
|
const title = this.translate.instant('This will set the following category for all selected motions:');
|
2019-02-11 12:39:41 +01:00
|
|
|
const clearChoice = this.translate.instant('No category');
|
2019-01-10 12:54:48 +01:00
|
|
|
const selectedChoice = await this.choiceService.open(
|
|
|
|
title,
|
2019-02-07 12:34:31 +01:00
|
|
|
this.categoryRepo.sortViewCategoriesByConfig(this.categoryRepo.getViewModelList()),
|
2019-01-10 12:54:48 +01:00
|
|
|
false,
|
|
|
|
null,
|
|
|
|
clearChoice
|
|
|
|
);
|
2018-11-27 22:44:37 +01:00
|
|
|
if (selectedChoice) {
|
|
|
|
for (const motion of motions) {
|
2018-12-18 17:06:17 +01:00
|
|
|
await this.repo.update(
|
2019-01-10 12:54:48 +01:00
|
|
|
{ category_id: selectedChoice.action ? 0 : (selectedChoice.items as number) },
|
|
|
|
motion
|
|
|
|
);
|
2018-11-27 22:44:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-12-18 17:06:17 +01:00
|
|
|
* Opens a dialog and adds or removes the selected submitters for all given motions.
|
2018-11-27 22:44:37 +01:00
|
|
|
*
|
2018-12-18 17:06:17 +01:00
|
|
|
* @param motions The motions to add/remove the sumbitters to
|
2018-11-27 22:44:37 +01:00
|
|
|
*/
|
2018-12-18 17:06:17 +01:00
|
|
|
public async changeSubmitters(motions: ViewMotion[]): Promise<void> {
|
2019-01-10 12:54:48 +01:00
|
|
|
const title = this.translate.instant(
|
|
|
|
'This will add or remove the following submitters for all selected motions:'
|
|
|
|
);
|
2019-02-11 12:39:41 +01:00
|
|
|
const choices = [this.translate.instant('Add'), this.translate.instant('Remove')];
|
2019-01-10 12:54:48 +01:00
|
|
|
const selectedChoice = await this.choiceService.open(title, this.userRepo.getViewModelList(), true, choices);
|
2018-12-18 17:06:17 +01:00
|
|
|
if (selectedChoice && selectedChoice.action === choices[0]) {
|
2018-11-30 09:24:07 +01:00
|
|
|
const requestData = motions.map(motion => {
|
2019-02-14 13:45:52 +01:00
|
|
|
let submitterIds = [...motion.sorted_submitters_id, ...(selectedChoice.items as number[])];
|
2018-11-30 09:24:07 +01:00
|
|
|
submitterIds = submitterIds.filter((id, index, self) => self.indexOf(id) === index); // remove duplicates
|
|
|
|
return {
|
|
|
|
id: motion.id,
|
|
|
|
submitters: submitterIds
|
|
|
|
};
|
|
|
|
});
|
2019-02-07 11:46:19 +01:00
|
|
|
await this.httpService.post('/rest/motions/motion/manage_multiple_submitters/', { motions: requestData });
|
2018-12-18 17:06:17 +01:00
|
|
|
} else if (selectedChoice && selectedChoice.action === choices[1]) {
|
2018-11-30 09:24:07 +01:00
|
|
|
const requestData = motions.map(motion => {
|
2018-12-18 17:06:17 +01:00
|
|
|
const submitterIdsToRemove = selectedChoice.items as number[];
|
2019-02-14 13:45:52 +01:00
|
|
|
const submitterIds = motion.sorted_submitters_id.filter(id => !submitterIdsToRemove.includes(id));
|
2018-11-30 09:24:07 +01:00
|
|
|
return {
|
|
|
|
id: motion.id,
|
|
|
|
submitters: submitterIds
|
|
|
|
};
|
|
|
|
});
|
2019-02-07 11:46:19 +01:00
|
|
|
await this.httpService.post('/rest/motions/motion/manage_multiple_submitters/', { motions: requestData });
|
2018-11-27 22:44:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-12-18 17:06:17 +01:00
|
|
|
* Opens a dialog and adds/removes the selected tags for all given motions.
|
2018-11-27 22:44:37 +01:00
|
|
|
*
|
|
|
|
* @param motions The motions to add the tags to
|
|
|
|
*/
|
2018-12-18 17:06:17 +01:00
|
|
|
public async changeTags(motions: ViewMotion[]): Promise<void> {
|
|
|
|
const title = this.translate.instant('This will add or remove the following tags for all selected motions:');
|
2019-02-11 12:39:41 +01:00
|
|
|
const choices = [
|
|
|
|
this.translate.instant('Add'),
|
|
|
|
this.translate.instant('Remove'),
|
|
|
|
this.translate.instant('Clear tags')
|
|
|
|
];
|
2019-01-10 12:54:48 +01:00
|
|
|
const selectedChoice = await this.choiceService.open(title, this.tagRepo.getViewModelList(), true, choices);
|
2018-12-18 17:06:17 +01:00
|
|
|
if (selectedChoice && selectedChoice.action === choices[0]) {
|
2018-11-30 09:24:07 +01:00
|
|
|
const requestData = motions.map(motion => {
|
2018-12-18 17:06:17 +01:00
|
|
|
let tagIds = [...motion.tags_id, ...(selectedChoice.items as number[])];
|
2018-11-30 09:24:07 +01:00
|
|
|
tagIds = tagIds.filter((id, index, self) => self.indexOf(id) === index); // remove duplicates
|
|
|
|
return {
|
|
|
|
id: motion.id,
|
|
|
|
tags: tagIds
|
|
|
|
};
|
|
|
|
});
|
2019-02-07 11:46:19 +01:00
|
|
|
await this.httpService.post('/rest/motions/motion/manage_multiple_tags/', { motions: requestData });
|
2018-12-18 17:06:17 +01:00
|
|
|
} else if (selectedChoice && selectedChoice.action === choices[1]) {
|
2018-11-30 09:24:07 +01:00
|
|
|
const requestData = motions.map(motion => {
|
2018-12-18 17:06:17 +01:00
|
|
|
const tagIdsToRemove = selectedChoice.items as number[];
|
2018-11-27 22:44:37 +01:00
|
|
|
const tagIds = motion.tags_id.filter(id => !tagIdsToRemove.includes(id));
|
2018-11-30 09:24:07 +01:00
|
|
|
return {
|
|
|
|
id: motion.id,
|
|
|
|
tags: tagIds
|
|
|
|
};
|
|
|
|
});
|
2019-02-07 11:46:19 +01:00
|
|
|
await this.httpService.post('/rest/motions/motion/manage_multiple_tags/', { motions: requestData });
|
2018-12-18 17:06:17 +01:00
|
|
|
} else if (selectedChoice && selectedChoice.action === choices[2]) {
|
|
|
|
const requestData = motions.map(motion => {
|
|
|
|
return {
|
|
|
|
id: motion.id,
|
|
|
|
tags: []
|
|
|
|
};
|
|
|
|
});
|
2019-02-07 11:46:19 +01:00
|
|
|
await this.httpService.post('/rest/motions/motion/manage_multiple_tags/', { motions: requestData });
|
2018-12-18 17:06:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens a dialog and changes the motionBlock for all given motions.
|
|
|
|
*
|
|
|
|
* @param motions The motions for which to change the motionBlock
|
|
|
|
*/
|
|
|
|
public async setMotionBlock(motions: ViewMotion[]): Promise<void> {
|
2019-01-21 15:07:55 +01:00
|
|
|
const title = this.translate.instant('This will set the following motion block for all selected motions:');
|
2018-12-18 17:06:17 +01:00
|
|
|
const clearChoice = 'Clear motion block';
|
2019-01-10 12:54:48 +01:00
|
|
|
const selectedChoice = await this.choiceService.open(
|
|
|
|
title,
|
|
|
|
this.motionBlockRepo.getViewModelList(),
|
|
|
|
false,
|
|
|
|
null,
|
|
|
|
clearChoice
|
|
|
|
);
|
2018-12-18 17:06:17 +01:00
|
|
|
if (selectedChoice) {
|
|
|
|
for (const motion of motions) {
|
2019-01-10 12:54:48 +01:00
|
|
|
const blockId = selectedChoice.action ? null : (selectedChoice.items as number);
|
|
|
|
await this.repo.update({ motion_block_id: blockId }, motion);
|
2018-12-18 17:06:17 +01:00
|
|
|
}
|
2018-11-27 22:44:37 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-04 15:41:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Triggers the selected motions to be moved in the call-list (sort_parent, weight)
|
|
|
|
* as children or as following after a selected motion.
|
|
|
|
*
|
|
|
|
* @param motions The motions to move
|
|
|
|
*/
|
|
|
|
public async bulkMoveItems(motions: ViewMotion[]): Promise<void> {
|
2019-02-11 12:39:41 +01:00
|
|
|
const title = this.translate.instant(
|
|
|
|
'This will move all selected motions under or after the following motion in the call list:'
|
|
|
|
);
|
|
|
|
const options = [this.translate.instant('Set as parent'), this.translate.instant('Insert after')];
|
2019-02-04 15:41:01 +01:00
|
|
|
const allMotions = this.repo.getViewModelList();
|
|
|
|
const tree = this.treeService.makeTree(allMotions, 'weight', 'sort_parent_id');
|
|
|
|
const itemsToMove = this.treeService.getTopItemsFromTree(tree, motions);
|
|
|
|
const selectableItems = this.treeService.getTreeWithoutSelection(tree, motions);
|
|
|
|
|
|
|
|
const selectedChoice = await this.choiceService.open(title, selectableItems, false, options);
|
|
|
|
if (selectedChoice) {
|
|
|
|
if (selectedChoice.action === options[0]) {
|
|
|
|
// set choice as parent
|
|
|
|
this.repo.sortMotionBranches(itemsToMove, selectedChoice.items as number);
|
|
|
|
} else if (selectedChoice.action === options[1]) {
|
|
|
|
// insert after chosen
|
|
|
|
const olderSibling = this.repo.getViewModel(selectedChoice.items as number);
|
|
|
|
const parentId = olderSibling ? olderSibling.sort_parent_id : null;
|
|
|
|
const siblings = this.repo.getViewModelList().filter(motion => motion.sort_parent_id === parentId);
|
|
|
|
const idx = siblings.findIndex(sib => sib.id === olderSibling.id);
|
|
|
|
const before = siblings.slice(0, idx + 1);
|
|
|
|
const after = siblings.slice(idx + 1);
|
|
|
|
const sum = [].concat(before, itemsToMove, after);
|
|
|
|
this.repo.sortMotionBranches(sum, parentId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-27 22:44:37 +01:00
|
|
|
}
|