"src/git@labs.maarch.org:maarch/MaarchCourrier.git" did not exist on "229dcaaf876848b0615adc3988613c6bf5e06479"
Newer
Older
import { Component, OnInit, Input, ViewChild } from '@angular/core';

Alex ORLUC
committed
import { HttpClient } from '@angular/common/http';
import { LANG } from '../../translate.component';
import { NotificationService } from '../../notification.service';
import { HeaderService } from '../../../service/header.service';
import { MatDialog } from '@angular/material/dialog';
import { AppService } from '../../../service/app.service';

Alex ORLUC
committed
import { tap, catchError, finalize, exhaustMap, map, filter } from 'rxjs/operators';
import { of, forkJoin } from 'rxjs';

Alex ORLUC
committed
import { SortPipe } from '../../../plugins/sorting.pipe';
import { CdkDragDrop, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';
import { FormControl, Validators, FormGroup, ValidationErrors, ValidatorFn, AbstractControl } from '@angular/forms';
import { DiffusionsListComponent } from '../../diffusions/diffusions-list.component';
import { stringify } from '@angular/compiler/src/util';

Alex ORLUC
committed
@Component({
selector: 'app-indexing-form',
templateUrl: "indexing-form.component.html",
styleUrls: ['indexing-form.component.scss'],
providers: [NotificationService, AppService, SortPipe]
})
export class IndexingFormComponent implements OnInit {
lang: any = LANG;

Alex ORLUC
committed
@Input('indexingFormId') indexingFormId: number;
@Input('groupId') groupId: number;
@Input('admin') adminMode: boolean;
@ViewChild('appDiffusionsList', { static: false }) appDiffusionsList: DiffusionsListComponent;
fieldCategories: any[] = ['mail', 'contact', 'process', 'classifying'];

Alex ORLUC
committed
indexingModelsCore: any[] = [
{
identifier: 'doctype',

Alex ORLUC
committed
unit: 'mail',
type: 'select',
system: true,
mandatory: true,

Alex ORLUC
committed
values: []
},
{
identifier: 'subject',

Alex ORLUC
committed
unit: 'mail',
type: 'string',
system: true,
mandatory: true,

Alex ORLUC
committed
values: []
},
];
indexingModels_mail: any[] = [];
indexingModels_contact: any[] = [];
indexingModels_process: any[] = [];
indexingModels_classement: any[] = [];
indexingModels_mailClone: any[] = [];
indexingModels_contactClone: any[] = [];
indexingModels_processClone: any[] = [];
indexingModels_classementClone: any[] = [];
indexingModelsCustomFields: any[] = [];
availableFields: any[] = [
identifier: 'getRecipients',
type: 'autocomplete',

Alex ORLUC
committed
{
identifier: 'priority',
label: this.lang.priority,
type: 'select',

Alex ORLUC
committed
values: []
},
{
identifier: 'confidential',
label: this.lang.confidential,
type: 'radio',
values: ['Oui', 'Non']

Alex ORLUC
committed
},
{
identifier: 'initiator',
label: this.lang.initiatorEntityAlt,

Alex ORLUC
committed
type: 'select',

Alex ORLUC
committed
values: []
},
{
identifier: 'departureDate',
label: this.lang.departureDate,
type: 'date',
default_value: '',
values: []
},

Alex ORLUC
committed
{
identifier: 'processLimitDate',
label: this.lang.processLimitDate,
type: 'date',
default_value: '',

Alex ORLUC
committed
values: []
},
{
identifier: 'tags',
label: this.lang.tags,
type: 'autocomplete',
default_value: '',
values: ['/rest/autocomplete/tags', '/rest/tags']
},
{
identifier: 'senders',
label: this.lang.getSenders,
type: 'autocomplete',
default_value: '',
values: ['/rest/autocomplete/contacts']
},
{
identifier: 'destination',
label: this.lang.destination,
type: 'select',
default_value: '',
values: []
},
{
identifier: 'folders',
label: this.lang.folders,
type: 'autocomplete',
default_value: '',
values: ['/rest/autocomplete/folders', '/rest/folders']
},
{
identifier: 'documentDate',
label: this.lang.docDate,
unit: 'mail',
type: 'date',
default_value: '',
values: []
},
{
identifier: 'arrivalDate',
label: this.lang.arrivalDate,
unit: 'mail',
type: 'date',
default_value: '',
values: []
},

Alex ORLUC
committed
];

Alex ORLUC
committed
availableFieldsClone: any[] = [];

Alex ORLUC
committed

Alex ORLUC
committed
availableCustomFields: any[] = [];
availableCustomFieldsClone: any[] = null;

Alex ORLUC
committed
indexingFormGroup: FormGroup;
arrFormControl: any = {};
currentCategory: string = '';
currentPriorityColor: string = '';

Alex ORLUC
committed
constructor(
public http: HttpClient,
private notify: NotificationService,
public dialog: MatDialog,
private headerService: HeaderService,
public appService: AppService,
) {
}
ngOnInit(): void {
this.adminMode === undefined ? this.adminMode = false : this.adminMode = true;

Alex ORLUC
committed
this.availableFieldsClone = JSON.parse(JSON.stringify(this.availableFields));

Alex ORLUC
committed
this.fieldCategories.forEach(category => {
this['indexingModels_' + category] = [];
});
if (this.indexingFormId <= 0 || this.indexingFormId === undefined) {
this.http.get("../../rest/customFields").pipe(
tap((data: any) => {
this.availableCustomFields = data.customFields.map((info: any) => {
info.identifier = 'indexingCustomField_' + info.id;
info.system = false;
info.values = info.values.length > 0 ? info.values.map((custVal: any) => {
return {
id: custVal,
label: custVal
}
}) : info.values;

Alex ORLUC
committed
this.fieldCategories.forEach(element => {
this['indexingModels_' + element] = this.indexingModelsCore.filter((x: any, i: any, a: any) => x.unit === element);
this['indexingModels_' + element].forEach((field: any) => {
this.initElemForm();
}),
catchError((err: any) => {
this.notify.handleErrors(err);
return of(false);
})
).subscribe();
} else {
this.loadForm(this.indexingFormId);

Alex ORLUC
committed
}
drop(event: CdkDragDrop<string[]>) {
event.item.data.unit = event.container.id.split('_')[1];
if (event.previousContainer === event.container) {
moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
} else {
this.initValidator(event.item.data);

Alex ORLUC
committed
transferArrayItem(event.previousContainer.data,
event.container.data,
event.previousIndex,
event.currentIndex);
if (['destination', 'priority'].indexOf(event.item.data.identifier) > -1) {
this.initElemForm();
}

Alex ORLUC
committed
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
}
}
onSubmit() {
let arrIndexingModels: any[] = [];
this.fieldCategories.forEach(category => {
arrIndexingModels = arrIndexingModels.concat(this['indexingModels_' + category]);
});
}
removeItem(arrTarget: string, item: any, index: number) {
item.mandatory = false;
if (item.identifier.indexOf('indexingCustomField') > -1) {
this.availableCustomFields.push(item);
this[arrTarget].splice(index, 1);
} else {
this.availableFields.push(item);
this[arrTarget].splice(index, 1);
}
}
getDatas() {
let arrIndexingModels: any[] = [];
this.fieldCategories.forEach(category => {
arrIndexingModels = arrIndexingModels.concat(this['indexingModels_' + category]);
});
arrIndexingModels.forEach(element => {
if (element.today === true) {
element.default_value = '_TODAY';
} else {
element.default_value = this.arrFormControl[element.identifier].value;
}
if (element.identifier === "destination" && !this.adminMode) {
arrIndexingModels.push({
identifier : 'diffusionList',
default_value : this.arrFormControl['diffusionList'].value
});
}

Alex ORLUC
committed
return arrIndexingModels;
}
getCategory() {
return this.currentCategory;
}

Alex ORLUC
committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
getAvailableFields() {
return this.availableFields;
}
getAvailableCustomFields() {
return this.availableCustomFields;
}
isModified() {
let state = false;
let compare: string = '';
let compareClone: string = '';
this.fieldCategories.forEach(category => {
compare = JSON.stringify((this['indexingModels_' + category]));
compareClone = JSON.stringify((this['indexingModels_' + category + 'Clone']));
if (compare !== compareClone) {
state = true;
}
});
return state;
}
setModification() {
this.fieldCategories.forEach(element => {
this['indexingModels_' + element + 'Clone'] = JSON.parse(JSON.stringify(this['indexingModels_' + element]));
});
}
cancelModification() {
this.fieldCategories.forEach(element => {
this['indexingModels_' + element] = JSON.parse(JSON.stringify(this['indexingModels_' + element + 'Clone']));
});
}
initElemForm() {

Alex ORLUC
committed
this.loading = true;
const myObservable = of(42);
myObservable.pipe(
exhaustMap(() => this.initializeRoutes()),
tap((data) => {
if (!this.adminMode) {
this.arrFormControl['mailtracking'].setValue(false);
}
this.currentPriorityColor = '';
this.fieldCategories.forEach(element => {
this['indexingModels_' + element].forEach((elem: any) => {
elem.startDate = '';
elem.endDate = '_TODAY';

Alex ORLUC
committed
} else if (elem.identifier === 'destination') {
if (this.adminMode) {
let title = '';
elem.values = data.entities.map((entity: any) => {
title = entity.entity_label;
for (let index = 0; index < entity.level; index++) {
entity.entity_label = ' ' + entity.entity_label;
}
return {
id: entity.id,
title: title,
label: entity.entity_label,
disabled: false
}
});
} else {
let title = '';
let defaultVal = data.entities.filter((entity: any) => entity.enabled === true && entity.id === elem.default_value);

Alex ORLUC
committed
elem.default_value = defaultVal.length > 0 ? defaultVal[0].id : null;
this.arrFormControl[elem.identifier].setValue(defaultVal.length > 0 ? defaultVal[0].id : '');
elem.values = data.entities.map((entity: any) => {
title = entity.entity_label;
for (let index = 0; index < entity.level; index++) {
entity.entity_label = ' ' + entity.entity_label;
}
return {
id: entity.id,
title: title,
label: entity.entity_label,
disabled: !entity.enabled
}
});
elem.event = 'loadDiffusionList';

Alex ORLUC
committed
if (elem.default_value !== null && !this.adminMode) {
this.loadDiffusionList(elem, elem.default_value);
}
elem.allowedEntities = elem.values.filter((val: any) => val.disabled === false).map((entities: any) => entities.id);
}
} else if (elem.identifier === 'arrivalDate') {
elem.endDate = '_TODAY';
} else if (elem.identifier === 'initiator' && !this.adminMode) {
elem.values = this.headerService.user.entities.map((entity: any) => {
return {
id: entity.id,
label: entity.entity_label
}
});
} else if (elem.identifier === 'processLimitDate') {
elem.startDate = '_TODAY';
elem.endDate = '';

Alex ORLUC
committed
elem.event = 'setPriorityColorByLimitDate';
} else if (elem.identifier === 'departureDate') {
elem.startDate = '_TODAY';
elem.endDate = '';
elem.values = null;
} else if (elem.identifier === 'category_id') {
elem.values = data.categories;
} else if (elem.identifier === 'priority') {
elem.values = data.priorities;

Alex ORLUC
committed
elem.event = 'calcLimitDateByPriority';
if (elem.default_value !== null) {
this.calcLimitDateByPriority(elem, elem.default_value);
} else if (elem.identifier === 'doctype') {
let title = '';
let arrValues: any[] = [];
data.structure.forEach((doctype: any) => {
if (doctype['doctypes_second_level_id'] === undefined) {
arrValues.push({
id: doctype.doctypes_first_level_id,
label: doctype.doctypes_first_level_label,
title: doctype.doctypes_first_level_label,
disabled: true,
isTitle: true,
color: doctype.css_style
} else if (doctype['description'] === undefined) {
arrValues.push({
id: doctype.doctypes_second_level_id,
label: ' ' + doctype.doctypes_second_level_label,
title: doctype.doctypes_second_level_label,
disabled: true,
isTitle: true,
color: doctype.css_style
});
arrValues = arrValues.concat(data.structure.filter((info: any) => info.doctypes_second_level_id === doctype.doctypes_second_level_id && info.description !== undefined).map((info: any) => {
return {
id: info.type_id,
label: ' ' + info.description,
title: info.description,
disabled: false,
isTitle: false,
}));
}
});
elem.values = arrValues;
elem.event = 'calcLimitDate';
if (elem.default_value !== null && !this.adminMode) {
this.calcLimitDate(elem, elem.default_value);
}
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
});
});
}),
finalize(() => this.loading = false)
).subscribe();
}
initializeRoutes() {
let arrayRoutes: any = [];
let mergedRoutesDatas: any = {};
this.fieldCategories.forEach(element => {
this['indexingModels_' + element].forEach((elem: any) => {
if (elem.identifier === 'destination') {
if (this.adminMode) {
arrayRoutes.push(this.http.get('../../rest/indexingModels/entities'));
} else {
arrayRoutes.push(this.http.get('../../rest/indexing/' + this.groupId + '/entities'));
}
} else if (elem.identifier === 'category_id') {
arrayRoutes.push(this.http.get('../../rest/categories'));
} else if (elem.identifier === 'priority') {
arrayRoutes.push(this.http.get('../../rest/priorities'));
} else if (elem.identifier === 'doctype') {
arrayRoutes.push(this.http.get('../../rest/doctypes'));
}
return forkJoin(arrayRoutes).pipe(
map(data => {
let objectId = '';
let index = '';
for (var key in data) {
index = key;
objectId = Object.keys(data[key])[0];
mergedRoutesDatas[Object.keys(data[key])[0]] = data[index][objectId]
}
return mergedRoutesDatas;
})
)
createForm() {
this.indexingFormGroup = new FormGroup(this.arrFormControl);
}
loadForm(indexModelId: number) {
Object.keys(this.arrFormControl).forEach(element => {
delete this.arrFormControl[element];
});

Alex ORLUC
committed
this.availableFields = JSON.parse(JSON.stringify(this.availableFieldsClone));
if (!this.adminMode) {
this.arrFormControl['mailtracking'] = new FormControl({ value: '', disabled: this.adminMode ? true : false });
}
this.fieldCategories.forEach(category => {
this['indexingModels_' + category] = [];
});
let arrayRoutes: any = [];
let mergedRoutesDatas: any = {};
if (this.availableCustomFieldsClone === null) {
arrayRoutes.push(this.http.get("../../rest/customFields"));
} else {
this.availableCustomFields = JSON.parse(JSON.stringify(this.availableCustomFieldsClone));
}
arrayRoutes.push(this.http.get(`../../rest/indexingModels/${indexModelId}`));
forkJoin(arrayRoutes).pipe(
map(data => {
let objectId = '';
let index = '';
for (var key in data) {
index = key;
objectId = Object.keys(data[key])[0];
mergedRoutesDatas[Object.keys(data[key])[0]] = data[index][objectId]
}
return mergedRoutesDatas;
}),
tap((data: any) => {
if (data.customFields !== undefined) {
this.availableCustomFields = data.customFields.map((info: any) => {
info.identifier = 'indexingCustomField_' + info.id;
info.system = false;
info.values = info.values.length > 0 ? info.values.map((custVal: any) => {
return {
id: custVal,
label: custVal
}
}) : info.values;
return info;
});
this.availableCustomFieldsClone = JSON.parse(JSON.stringify(this.availableCustomFields));
}
this.currentCategory = data.indexingModel.category;
let fieldExist: boolean;
if (data.indexingModel.fields.length === 0) {
this.fieldCategories.forEach(element => {
this['indexingModels_' + element] = this.indexingModelsCore.filter((x: any, i: any, a: any) => x.unit === element);
this.indexingModelsCore.forEach(field => {
});
this.notify.error(this.lang.noFieldInModelMsg);
} else {
data.indexingModel.fields.forEach((field: any) => {
fieldExist = false;
field.system = false;
field.values = [];
let indexFound = this.availableFields.map(avField => avField.identifier).indexOf(field.identifier);
if (indexFound > -1) {
field.label = this.availableFields[indexFound].label;
field.values = this.availableFields[indexFound].values;
field.type = this.availableFields[indexFound].type;
this.availableFields.splice(indexFound, 1);
fieldExist = true;
}
indexFound = this.availableCustomFields.map(avField => avField.identifier).indexOf(field.identifier);
if (indexFound > -1) {
field.label = this.availableCustomFields[indexFound].label;
field.values = this.availableCustomFields[indexFound].values;
field.type = this.availableCustomFields[indexFound].type;
this.availableCustomFields.splice(indexFound, 1);
fieldExist = true;
}
indexFound = this.indexingModelsCore.map(info => info.identifier).indexOf(field.identifier);
if (indexFound > -1) {
field.label = this.indexingModelsCore[indexFound].label;
field.values = this.indexingModelsCore[indexFound].values;
field.type = this.indexingModelsCore[indexFound].type;
fieldExist = true;
field.system = true;
}
if (field.type === 'date' && field.default_value === '_TODAY') {
field.today = true;
field.default_value = new Date();
}
if (fieldExist) {
this['indexingModels_' + field.unit].push(field);
this.notify.error(this.lang.fieldNotExist + ': ' + field.identifier);
}
});
}
this.initElemForm();
this.createForm();
}),
catchError((err: any) => {
this.notify.handleErrors(err);
return of(false);
})
).subscribe();
this.arrFormControl[field.identifier] = new FormControl({ value: field.default_value, disabled: (field.today && this.adminMode) ? true : false });
if (field.type === 'integer') {
valArr.push(this.regexValidator(new RegExp('[+-]?([0-9]*[.])?[0-9]+'), { 'floatNumber': '' }));
}
if (field.mandatory && !this.adminMode) {
valArr.push(Validators.required);
}
this.arrFormControl[field.identifier].setValidators(valArr);
if (field.identifier === 'destination') {
let valArr: ValidatorFn[] = [];
valArr.push(Validators.required);
this.arrFormControl['diffusionList'] = new FormControl({ value: null, disabled: false });
this.arrFormControl['diffusionList'].setValidators(valArr);
this.arrFormControl['diffusionList'].setValue([]);
}
}
regexValidator(regex: RegExp, error: ValidationErrors): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } => {
if (!control.value) {
return null;
}
const valid = regex.test(control.value);
return valid ? null : error;
};
}
isValidForm() {
if (!this.indexingFormGroup.valid) {
Object.keys(this.indexingFormGroup.controls).forEach(key => {
const controlErrors: ValidationErrors = this.indexingFormGroup.get(key).errors;
if (controlErrors != null) {
this.indexingFormGroup.controls[key].markAsTouched();
/*Object.keys(controlErrors).forEach(keyError => {
console.log('Key control: ' + key + ', keyError: ' + keyError + ', err value: ', controlErrors[keyError]);
}
});
}
return this.indexingFormGroup.valid;
}
isEmptyField(field: any) {

Alex ORLUC
committed
if (this.arrFormControl[field.identifier].value === null) {
return true;
} else if (Array.isArray(this.arrFormControl[field.identifier].value)) {
if (this.arrFormControl[field.identifier].value.length > 0) {
return false;
} else {
return true;
}
} else if (String(this.arrFormControl[field.identifier].value) !== '') {
return false;
} else {
return true;
}
}
getMinDate(id: string) {
if (this.arrFormControl[id] !== undefined) {
return this.arrFormControl[id].value;
} else if (id === '_TODAY') {
return new Date();
} else {
return '';
}
}
getMaxDate(id: string) {
if (this.arrFormControl[id] !== undefined) {
return this.arrFormControl[id].value;
} else if (id === '_TODAY') {
return new Date();
} else {
return '';
}
}
toggleTodayDate(field: any) {
field.today = !field.today;
if (field.today) {
this.arrFormControl[field.identifier].disable();
this.arrFormControl[field.identifier].setValue(new Date());
} else {
this.arrFormControl[field.identifier].setValue('');
this.arrFormControl[field.identifier].enable();
}
}

Alex ORLUC
committed
toggleMailTracking() {
this.arrFormControl['mailtracking'].setValue(!this.arrFormControl['mailtracking'].value);
}
changeCategory(categoryId: string) {
this.currentCategory = categoryId;
}
changeDestination(entityIds: number[], allowedEntities: number[]) {
if (entityIds.indexOf(this.arrFormControl['destination'].value) === -1) {
this.arrFormControl['destination'].setValue(entityIds[0]);
}
}
launchEvent(value: any, field: any) {

Alex ORLUC
committed
if (field.event !== undefined && value !== null && !this.adminMode) {
this[field.event](field, value);
}
calcLimitDate(field: any, value: any) {

Alex ORLUC
committed
let limitDate: Date = null;
if (this.arrFormControl['processLimitDate'] !== undefined) {
this.http.get("../../rest/indexing/processLimitDate", { params: { "doctype": value } }).pipe(
tap((data: any) => {

Alex ORLUC
committed
limitDate = new Date(data.processLimitDate);
this.arrFormControl['processLimitDate'].setValue(limitDate);
}),
filter(() => this.arrFormControl['priority'] !== undefined),
exhaustMap(() => this.http.get('../../rest/indexing/priority', { params: { "processLimitDate": limitDate.toDateString() } })),

Alex ORLUC
committed
tap((data: any) => {
this.arrFormControl['priority'].setValue(data.priority);
this.setPriorityColor(null, data.priority);
}),
catchError((err: any) => {
this.notify.handleErrors(err);
return of(false);
})
).subscribe();
}
}
calcLimitDateByPriority(field: any, value: any) {
let limitDate: Date = null;

Alex ORLUC
committed
if (this.arrFormControl['processLimitDate'] !== undefined) {
this.http.get("../../rest/indexing/processLimitDate", { params: { "priority": value } }).pipe(
tap((data: any) => {
limitDate = new Date(data.processLimitDate);
this.arrFormControl['processLimitDate'].setValue(limitDate);

Alex ORLUC
committed
this.setPriorityColor(field, value);
}),
catchError((err: any) => {
this.notify.handleErrors(err);
return of(false);
})
).subscribe();

Alex ORLUC
committed
} else {
this.setPriorityColor(field, value);
setPriorityColor(field: any, value: any) {

Alex ORLUC
committed
if (field !== null) {
this.currentPriorityColor = field.values.filter((fieldVal: any) => fieldVal.id === value).map((fieldVal: any) => fieldVal.color)[0];
} else {
this.fieldCategories.forEach(element => {
if (this['indexingModels_' + element].filter((field: any) => field.identifier === 'priority').length > 0) {
this.currentPriorityColor = this['indexingModels_' + element].filter((field: any) => field.identifier === 'priority')[0].values.filter((fieldVal: any) => fieldVal.id === value).map((fieldVal: any) => fieldVal.color)[0]
}
});
}
}
setPriorityColorByLimitDate(field: any, value: any) {
const limitDate = new Date(value.value);
this.http.get("../../rest/indexing/priority", { params: { "processLimitDate": limitDate.toDateString() } }).pipe(

Alex ORLUC
committed
tap((data: any) => {
this.arrFormControl['priority'].setValue(data.priority);
this.setPriorityColor(null, data.priority);

Alex ORLUC
committed
}),
catchError((err: any) => {
this.notify.handleErrors(err);
return of(false);
})
).subscribe();

Alex ORLUC
committed
loadDiffusionList(field: any, value: any) {

Alex ORLUC
committed
setTimeout(() => {
this.appDiffusionsList.loadListModel(value);
}, 0);