Newer
Older
import { Component, OnInit, Input, ViewChild, EventEmitter, Output } from '@angular/core';

Alex ORLUC
committed
import { HttpClient } from '@angular/common/http';
import { LANG } from '../../translate.component';
import { TranslateService } from '@ngx-translate/core';
import { NotificationService } from '../../../service/notification/notification.service';

Alex ORLUC
committed
import { HeaderService } from '../../../service/header.service';
import { MatDialog, MatDialogRef } from '@angular/material/dialog';

Alex ORLUC
committed
import { AppService } from '../../../service/app.service';
import { tap, catchError, exhaustMap, filter } from 'rxjs/operators';
import { of } 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 { FunctionsService } from '../../../service/functions.service';
import { ConfirmComponent } from '../../../plugins/modal/confirm.component';
import { IssuingSiteInputComponent } from '../../administration/registered-mail/issuing-site/indexing/issuing-site-input.component';

Alex ORLUC
committed
@Component({
selector: 'app-indexing-form',
templateUrl: "indexing-form.component.html",
styleUrls: ['indexing-form.component.scss'],

Alex ORLUC
committed
})
export class IndexingFormComponent implements OnInit {
lang: any = LANG;

Alex ORLUC
committed
@Input('indexingFormId') indexingFormId: number;

Alex ORLUC
committed
@Input('resId') resId: number = null;
@Input('groupId') groupId: number;
@Input('admin') adminMode: boolean;

Alex ORLUC
committed
@Input('canEdit') canEdit: boolean = true;
@Input('mode') mode: string = 'indexation';

Alex ORLUC
committed
@Input('hideDiffusionList') hideDiffusionList: boolean = false;
@Output() retrieveDocumentEvent = new EventEmitter<string>();
@Output() loadingFormEndEvent = new EventEmitter<string>();
@ViewChild('appDiffusionsList', { static: false }) appDiffusionsList: DiffusionsListComponent;
@ViewChild('appIssuingSiteInput', { static: false }) appIssuingSiteInput: IssuingSiteInputComponent;
fieldCategories: any[] = ['mail', 'contact', 'process', 'classifying'];

Alex ORLUC
committed
indexingModelsCore: any[] = [
{
identifier: 'doctype',
label: this.translate.instant('lang.doctype'),

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

Alex ORLUC
committed
values: []
},
{
identifier: 'subject',
label: this.translate.instant('lang.subject'),

Alex ORLUC
committed
unit: 'mail',
type: 'string',
system: true,
mandatory: true,
enabled: 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[] = [
label: this.translate.instant('lang.getRecipients'),
type: 'autocomplete',
default_value: null,
values: [],
enabled: true,

Alex ORLUC
committed
{
identifier: 'priority',
label: this.translate.instant('lang.priority'),

Alex ORLUC
committed
type: 'select',
default_value: null,
values: [],
enabled: true,

Alex ORLUC
committed
},
{

Alex ORLUC
committed
identifier: 'confidentiality',
label: this.translate.instant('lang.confidential'),

Alex ORLUC
committed
type: 'radio',
default_value: null,
values: [{ 'id': true, 'label': this.translate.instant('lang.yes') }, { 'id': false, 'label': this.translate.instant('lang.no') }],
enabled: true,

Alex ORLUC
committed
},
{
identifier: 'initiator',
label: this.translate.instant('lang.initiatorEntityAlt'),

Alex ORLUC
committed
type: 'select',
default_value: null,
values: [],
enabled: true,

Alex ORLUC
committed
},
label: this.translate.instant('lang.departureDate'),
default_value: null,
values: [],
enabled: true,

Alex ORLUC
committed
{
identifier: 'processLimitDate',
label: this.translate.instant('lang.processLimitDate'),

Alex ORLUC
committed
type: 'date',
default_value: null,
values: [],
enabled: true,
},
{
identifier: 'tags',
label: this.translate.instant('lang.tags'),
type: 'autocomplete',
default_value: null,
values: ['/rest/autocomplete/tags', '/rest/tags'],
enabled: true,
},
{
identifier: 'senders',
label: this.translate.instant('lang.getSenders'),
type: 'autocomplete',
default_value: null,
values: ['/rest/autocomplete/correspondents'],
enabled: true,
},
{
identifier: 'destination',
label: this.translate.instant('lang.destination'),
type: 'select',
default_value: null,
values: [],
enabled: true,
},
{
identifier: 'folders',
label: this.translate.instant('lang.folders'),
type: 'autocomplete',
default_value: null,
values: ['/rest/autocomplete/folders', '/rest/folders'],
enabled: true,
},
{
identifier: 'documentDate',
label: this.translate.instant('lang.docDate'),
unit: 'mail',
type: 'date',
default_value: null,
values: [],
enabled: true,
},
{
identifier: 'arrivalDate',
label: this.translate.instant('lang.arrivalDate'),
unit: 'mail',
type: 'date',
default_value: null,
values: [],
enabled: true,
label: this.translate.instant('lang.registeredMailType'),

Alex ORLUC
committed
values: [{ 'id': '2D', 'label': this.translate.instant('lang.registeredMail_2D') }, { 'id': '2C', 'label': this.translate.instant('lang.registeredMail_2C') }, { 'id': 'RW', 'label': this.translate.instant('lang.registeredMail_RW') }],
enabled: true,
},
{
identifier: 'registeredMail_issuingSite',
label: this.translate.instant('lang.issuingSite'),
type: 'issuingSite',
default_value: null,
values: [],
enabled: true,
},
{
identifier: 'registeredMail_number',
label: this.translate.instant('lang.registeredMailNumber'),
type: 'string',
default_value: null,
values: [],

Alex ORLUC
committed
enabled: false,
},
{
identifier: 'registeredMail_warranty',
label: this.translate.instant('lang.warrantyLevel'),
type: 'radio',
default_value: null,
values: [{ 'id': 'R1', 'label': 'R1' }, { 'id': 'R2', 'label': 'R2' }, { 'id': 'R3', 'label': 'R3' }],
enabled: true,
},
{
identifier: 'registeredMail_letter',
label: this.translate.instant('lang.letter'),
type: 'radio',
default_value: null,
values: [{ 'id': true, 'label': this.translate.instant('lang.yes') }, { 'id': false, 'label': this.translate.instant('lang.no') }],
{
identifier: 'Destinataire de recommandés',
label: this.translate.instant('Destinataire de recommandés'),
type: 'registeredMailDest',
default_value: null,
values: [],
enabled: true,
},

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
currentResourceValues: any = null;
Guillaume Heurtier
committed
dialogRef: MatDialogRef<any>;

Alex ORLUC
committed
constructor(
public translate: TranslateService,

Alex ORLUC
committed
public http: HttpClient,
private notify: NotificationService,
public dialog: MatDialog,
private headerService: HeaderService,
public appService: AppService,

Alex ORLUC
committed
) {
}
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) {
await this.initFields();
await this.initCustomFields();
this.initElemForm();
} else {
this.loadForm(this.indexingFormId);
}
}
initFields() {
return new Promise((resolve, reject) => {
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.initValidator(field);
});
});
resolve(true);
});
}
initCustomFields() {
return new Promise((resolve, reject) => {
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.enabled = true;
info.SQLMode = info.SQLMode;
info.default_value = ['integer', 'string', 'date'].indexOf(info.type) > -1 && !this.functions.empty(info.values) ? info.values[0].key : null;
info.values = info.values.length > 0 ? info.values.map((custVal: any) => {
return {
id: custVal.key,
label: custVal.label
}
}) : info.values;
this.availableCustomFieldsClone = JSON.parse(JSON.stringify(this.availableCustomFields));
resolve(true);
}),
catchError((err: any) => {
this.notify.handleErrors(err);
return of(false);
})
).subscribe();

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 {

Alex ORLUC
committed
const regex = /registeredMail_[.]*/g;
if (event.item.data.identifier.match(regex) !== null && event.previousContainer.id === 'fieldsList') {
this.transferRegisteredMailInput(event);

Alex ORLUC
committed
} else {
this.transferInput(event);
}
if (['destination', 'priority'].indexOf(event.item.data.identifier) > -1) {
this.initElemForm();
}

Alex ORLUC
committed
}
}

Alex ORLUC
committed
transferInput(event: CdkDragDrop<string[]>) {
this.initValidator(event.item.data);
transferArrayItem(event.previousContainer.data,
event.container.data,
event.previousIndex,
event.currentIndex);
}

Alex ORLUC
committed
onSubmit() {
let arrIndexingModels: any[] = [];
this.fieldCategories.forEach(category => {
arrIndexingModels = arrIndexingModels.concat(this['indexingModels_' + category]);
});
}
removeItem(arrTarget: string, item: any, index: number) {
this.dialogRef = this.dialog.open(ConfirmComponent, { panelClass: 'maarch-modal', autoFocus: false, disableClose: true, data: { title: this.translate.instant('lang.indexingModelModification'), msg: this.translate.instant('lang.updateIndexingFieldWarning') } });
Guillaume Heurtier
committed
this.dialogRef.afterClosed().pipe(
filter((data: string) => data === 'ok'),
tap(() => {
item.mandatory = false;
item.enabled = true;

Alex ORLUC
committed
if (item.identifier.indexOf('registeredMail_') > -1) {
this.removeRegisteredMailItems();
} else if (item.identifier.indexOf('indexingCustomField') > -1) {
Guillaume Heurtier
committed
this.availableCustomFields.push(item);
this[arrTarget].splice(index, 1);
} else {
this.availableFields.push(item);
this[arrTarget].splice(index, 1);
}
}),
catchError((err: any) => {
this.notify.handleErrors(err);
return of(false);
})
).subscribe();

Alex ORLUC
committed
}

Alex ORLUC
committed
getDatas(withDiffusionList = true) {

Alex ORLUC
committed
let arrIndexingModels: any[] = [];
this.fieldCategories.forEach(category => {
arrIndexingModels = arrIndexingModels.concat(this['indexingModels_' + category]);
});
arrIndexingModels.forEach(element => {
if (element.type === 'date' && !this.functions.empty(this.arrFormControl[element.identifier].value)) {
element.default_value = '_TODAY';
} else {
element.default_value = this.functions.formatDateObjectToDateString(this.arrFormControl[element.identifier].value, false);
if (element.identifier === 'processLimitDate') {
element.default_value = this.functions.formatDateObjectToDateString(this.arrFormControl[element.identifier].value, true);
element.default_value = this.functions.formatDateObjectToDateString(this.arrFormControl[element.identifier].value, false);
element.default_value = this.arrFormControl[element.identifier].value === '' ? null : this.arrFormControl[element.identifier].value;

Alex ORLUC
committed
if (element.identifier === "destination" && !this.adminMode && withDiffusionList) {
identifier: 'diffusionList',
default_value: this.arrFormControl['diffusionList'].value
if (!this.adminMode) {
arrIndexingModels.push({
identifier: 'modelId',
default_value: this.indexingFormId
if (this.mode === 'indexation') {
arrIndexingModels.push({
identifier: 'followed',
default_value: this.arrFormControl['mailtracking'].value
});
}
}

Alex ORLUC
committed
return arrIndexingModels;
}
return new Promise((resolve, reject) => {
if (this.isValidForm()) {
const formatdatas = this.formatDatas(this.getDatas());
this.http.put(`../rest/resources/${this.resId}`, formatdatas).pipe(
this.currentResourceValues = JSON.parse(JSON.stringify(this.getDatas(false)));
this.notify.success(this.translate.instant('lang.dataUpdated'));
resolve(true);
}),
catchError((err: any) => {
this.notify.handleErrors(err);
return of(false);
})
).subscribe();
return true;
} else {
this.notify.error(this.translate.instant('lang.mustFixErrors'));

Alex ORLUC
committed
}
formatDatas(datas: any) {
let formatData: any = {};
const regex = /indexingCustomField_[.]*/g;
formatData['customFields'] = {};
datas.forEach((element: any) => {
if (element.identifier.match(regex) !== null) {
formatData['customFields'][element.identifier.split('_')[1]] = element.default_value;
} else {
formatData[element.identifier] = element.default_value;
}
});
return formatData;
}
getCategory() {
return this.currentCategory;
}

Alex ORLUC
committed
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;
}

Alex ORLUC
committed
isResourceModified() {
if (this.loading || JSON.stringify(this.currentResourceValues) === JSON.stringify(this.getDatas(false))) {

Alex ORLUC
committed
return false;
} else {
return true;
}
}

Alex ORLUC
committed
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']));
});
}
setDocumentDateField(elem: any) {
elem.startDate = '';
elem.endDate = '_TODAY';
this.fieldCategories.forEach(element => {
if (this['indexingModels_' + element].filter((field: any) => field.identifier === 'arrivalDate').length > 0) {
elem.endDate = 'arrivalDate';
} else if (this['indexingModels_' + element].filter((field: any) => field.identifier === 'departureDate').length > 0) {
elem.endDate = 'departureDate';
}
});
}

Alex ORLUC
committed
let route = this.adminMode || this.mode !== 'indexation' ? `../rest/indexingModels/entities` : `../rest/indexing/groups/${this.groupId}/entities`;
return new Promise((resolve, reject) => {
this.http.get(route).pipe(
tap((data: any) => {
if (this.adminMode) {
let title = '';
elem.values = [
{
id: '#myPrimaryEntity',
title: this.translate.instant('lang.myPrimaryEntity'),
label: `<i class="fa fa-hashtag"></i> ${this.translate.instant('lang.myPrimaryEntity')}`,
disabled: false
}
];
elem.values = elem.values.concat(data.entities.map((entity: any) => {
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
if (elem.default_value === '#myPrimaryEntity') {
this.selfDest = this.currentCategory === 'outgoing';
elem.default_value = this.headerService.user.entities[0].id;
this.arrFormControl[elem.identifier].setValue(elem.default_value);
} else {
const defaultVal = data.entities.filter((entity: any) => entity.enabled === true && entity.id === elem.default_value);
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';
elem.allowedEntities = elem.values.filter((val: any) => val.disabled === false).map((entities: any) => entities.id);
}
resolve(true);
})
).subscribe();
});
setInitiatorField(elem: any) {
elem.values = this.headerService.user.entities.map((entity: any) => {
return {
id: entity.id,
label: entity.entity_label
setCategoryField(elem: any) {
return new Promise((resolve, reject) => {
this.http.get(`../rest/categories`).pipe(
tap((data: any) => {
elem.values = data.categories;
resolve(true);
})
).subscribe();
});
}
setPriorityField(elem: any) {
return new Promise((resolve, reject) => {
this.http.get(`../rest/priorities`).pipe(
tap((data: any) => {
elem.values = data.priorities;
elem.event = 'calcLimitDateByPriority';
if (elem.default_value !== null) {
this.calcLimitDateByPriority(elem, elem.default_value);
}
resolve(true);
})
).subscribe();
});
}
setDoctypeField(elem: any) {
return new Promise((resolve, reject) => {
this.http.get(`../rest/doctypes`).pipe(
tap((data: any) => {
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
});
data.structure.filter((info: any) => info.doctypes_first_level_id === doctype.doctypes_first_level_id && info.doctypes_second_level_id !== undefined && info.description === undefined).forEach((secondDoctype: any) => {
arrValues.push({
id: secondDoctype.doctypes_second_level_id,
label: ' ' + secondDoctype.doctypes_second_level_label,
title: secondDoctype.doctypes_second_level_label,
disabled: true,
isTitle: true,
color: secondDoctype.css_style
});
arrValues = arrValues.concat(data.structure.filter((infoDoctype: any) => infoDoctype.doctypes_second_level_id === secondDoctype.doctypes_second_level_id && infoDoctype.description !== undefined).map((infoType: any) => {
return {
id: infoType.type_id,
label: ' ' + infoType.description,
title: infoType.description,
disabled: false,
isTitle: false,
};
}));

Alex ORLUC
committed
}
});
elem.values = arrValues;
elem.event = 'calcLimitDate';
if (!this.functions.empty(elem.default_value) && !this.adminMode) {
this.calcLimitDate(elem, elem.default_value);
}
resolve(true);
})
).subscribe();
});
async initElemForm(saveResourceState: boolean = true) {
this.loading = true;
if (!this.adminMode) {
this.arrFormControl['mailtracking'].setValue(false);
}
this.currentPriorityColor = '';
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
await Promise.all(this.fieldCategories.map(async (element) => {
await Promise.all(this['indexingModels_' + element].map(async (elem: any) => {
if (elem.identifier === 'documentDate') {
this.setDocumentDateField(elem);
} else if (elem.identifier === 'destination') {
await this.setDestinationField(elem);
} else if (elem.identifier === 'arrivalDate') {
elem.startDate = 'documentDate';
elem.endDate = '_TODAY';
} else if (elem.identifier === 'initiator' && !this.adminMode) {
this.setInitiatorField(elem);
} else if (elem.identifier === 'processLimitDate') {
elem.startDate = '_TODAY';
elem.endDate = '';
elem.event = 'setPriorityColorByLimitDate';
} else if (elem.identifier === 'departureDate') {
elem.startDate = 'documentDate';
elem.endDate = '';
} else if (elem.identifier === 'folders') {
elem.values = null;
} else if (elem.identifier === 'category_id') {
await this.setCategoryField(elem);
} else if (elem.identifier === 'priority') {
await this.setPriorityField(elem);
} else if (elem.identifier === 'doctype') {
} else if (elem.identifier === 'registeredMail_type') {
elem.event = 'getIssuingSites';
// await this.setDoctypeField(elem);
await this.setResource(saveResourceState);
setResource(saveResourceState: boolean = true) {
return new Promise((resolve, reject) => {
this.http.get(`../rest/resources/${this.resId}`).pipe(
tap(async (data: any) => {
await Promise.all(this.fieldCategories.map(async (element: any) => {
// this.fieldCategories.forEach(async element => {
await Promise.all(this['indexingModels_' + element].map(async (elem: any) => {
// this['indexingModels_' + element].forEach((elem: any) => {
const customId: any = Object.keys(data.customFields).filter(index => index === elem.identifier.split('indexingCustomField_')[1])[0];
if (Object.keys(data).indexOf(elem.identifier) > -1 || customId !== undefined) {
let fieldValue: any = '';
if (customId !== undefined) {
fieldValue = data.customFields[customId];
} else {
fieldValue = data[elem.identifier];
}
if (elem.identifier === 'priority') {
this.setPriorityColor(null, fieldValue);
} else if (elem.identifier === 'processLimitDate' && !this.functions.empty(fieldValue)) {
elem.startDate = '';
} else if (elem.identifier === 'destination') {
if (this.mode === 'process') {
this.arrFormControl[elem.identifier].disable();
}
this.arrFormControl['diffusionList'].disable();
} else if (elem.identifier === 'initiator' && elem.values.filter((val: any) => val.id === fieldValue).length === 0 && !this.functions.empty(fieldValue)) {
await this.getCurrentInitiator(elem, fieldValue);
if (elem.type === 'date' && !this.functions.empty(fieldValue)) {
fieldValue = new Date(fieldValue);
}

Florian Azizian
committed
if (!this.functions.empty(fieldValue)) {
this.arrFormControl[elem.identifier].setValue(fieldValue);
}
} else if (!saveResourceState && elem.identifier === 'destination') {
this.arrFormControl[elem.identifier].disable();
this.arrFormControl[elem.identifier].setValidators([]);
this.arrFormControl['diffusionList'].disable();
if (!this.canEdit) {
this.arrFormControl[elem.identifier].disable();
}
this.arrFormControl['mailtracking'].setValue(data.followed);
if (saveResourceState) {
this.currentResourceValues = JSON.parse(JSON.stringify(this.getDatas(false)));
}
resolve(true);
}),
catchError((err: any) => {
this.notify.handleErrors(err);
return of(false);
})
).subscribe();
});
}
getCurrentInitiator(field: any, initiatorId: number) {
return new Promise((resolve, reject) => {
this.http.get(`../rest/entities/${initiatorId}`).pipe(
field.values.unshift({
id: data.id,
label: data.entity_label
});
})
).subscribe();
});
}
createForm() {
this.indexingFormGroup = new FormGroup(this.arrFormControl);
this.loadingFormEndEvent.emit();
Object.keys(this.arrFormControl).forEach(element => {
delete this.arrFormControl[element];
});

Alex ORLUC
committed
this.availableFields = JSON.parse(JSON.stringify(this.availableFieldsClone));
this.fieldCategories.forEach(category => {
this['indexingModels_' + category] = [];
});
if (this.availableCustomFieldsClone === null) {
this.availableCustomFields = JSON.parse(JSON.stringify(this.availableCustomFieldsClone));
async loadForm(indexModelId: number, saveResourceState: boolean = true) {
this.indexingFormId = indexModelId;
await this.resetForm();
if (!this.adminMode) {
this.arrFormControl['mailtracking'] = new FormControl({ value: '', disabled: this.adminMode ? true : false });
}
this.http.get(`../rest/indexingModels/${indexModelId}`).pipe(
this.indexingFormId = data.indexingModel.master !== null ? data.indexingModel.master : data.indexingModel.id;
this.currentCategory = data.indexingModel.category;
let fieldExist: boolean;
if (data.indexingModel.fields.length === 0) {
this.notify.error(this.translate.instant('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;

Florian Azizian
committed
field.default_value = !this.functions.empty(field.default_value) ? field.default_value : this.availableFields[indexFound].default_value;
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.default_value = !this.functions.empty(field.default_value) ? field.default_value : this.availableCustomFields[indexFound].default_value;
field.values = this.availableCustomFields[indexFound].values;
field.type = this.availableCustomFields[indexFound].type;
field.SQLMode = this.availableCustomFields[indexFound].SQLMode;
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;

Florian Azizian
committed
field.default_value = !this.functions.empty(field.default_value) ? field.default_value : this.indexingModelsCore[indexFound].default_value;
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();
}

Florian Azizian
committed
if (field.identifier == 'initiator' && this.mode == 'indexation' && this.functions.empty(field.default_value)) {
if (this.headerService.user.entities[0]) {
field.default_value = this.headerService.user.entities.filter((entity: any) => entity.primary_entity == 'Y')[0].id;
}

Florian Azizian
committed
}
if (fieldExist) {
this['indexingModels_' + field.unit].push(field);
this.notify.error(this.translate.instant('lang.fieldNotExist') + ': ' + field.identifier);
}
});
}
await this.initElemForm(saveResourceState);
this.createForm();
}),
catchError((err: any) => {
this.notify.handleErrors(err);
return of(false);
})
).subscribe();
enableField(field: any, enable: boolean) {
if (enable) {
if (!this.isAlwaysDisabledField(field)) {
this.arrFormControl[field.identifier].enable();
}
field.enabled = true;
} else {
this.arrFormControl[field.identifier].disable();
field.enabled = false;
}
}
isAlwaysDisabledField(field: any) {
if (this.adminMode && ((['integer', 'string', 'date'].indexOf(field.type) > -1 && !this.functions.empty(field.values)) || field.today)) {
return true;
}
return false;
}
const disabledState = !field.enabled || this.isAlwaysDisabledField(field);
if (!disabledState) {
field.enabled = true;
}
this.arrFormControl[field.identifier] = new FormControl({ value: field.default_value, disabled: disabledState });
if (field.type === 'integer') {
valArr.push(this.regexValidator(new RegExp('[+-]?([0-9]*[.])?[0-9]+'), { 'floatNumber': '' }));
} else if (field.type === 'date' && !this.functions.empty(field.default_value)) {
this.arrFormControl[field.identifier].setValue(new Date(field.default_value));
}
if (field.mandatory && !this.adminMode) {
valArr.push(Validators.required);
}
this.arrFormControl[field.identifier].setValidators(valArr);
if (field.identifier === 'destination') {
let valArr: ValidatorFn[] = [];
if (field.mandatory) {
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;
};