Newer
Older

Alex ORLUC
committed
import { Component, OnInit, ViewChild, ElementRef, EventEmitter, Output, Input, QueryList, ViewChildren } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { TranslateService } from '@ngx-translate/core';
import { AppService } from '../../../service/app.service';
import { FunctionsService } from '../../../service/functions.service';
import { Observable, of } from 'rxjs';
import { FormControl } from '@angular/forms';
import { startWith, map, tap, filter, exhaustMap, catchError, elementAt } from 'rxjs/operators';
import { LatinisePipe } from 'ngx-pipes';
import { MatExpansionPanel } from '@angular/material/expansion';
import { IndexingFieldsService } from '../../../service/indexing-fields.service';
import { ActivatedRoute } from '@angular/router';
import { MatDialog } from '@angular/material/dialog';
import { ConfirmComponent } from '../../../plugins/modal/confirm.component';
import { NotificationService } from '../../../service/notification/notification.service';
import { AddSearchTemplateModalComponent } from './search-template/search-template-modal.component';
import { DatePipe } from '@angular/common';

Alex ORLUC
committed
import { ContactAutocompleteComponent } from '@appRoot/contact/autocomplete/contact-autocomplete.component';
import { PluginSelectAutocompleteSearchComponent } from '@plugins/select-autocomplete-search/plugin-select-autocomplete-search.component';
import { FolderInputComponent } from '@appRoot/folder/indexing/folder-input.component';
import { TagInputComponent } from '@appRoot/tag/indexing/tag-input.component';
import { IssuingSiteInputComponent } from '@appRoot/administration/registered-mail/issuing-site/indexing/issuing-site-input.component';
@Component({
selector: 'app-criteria-tool',
templateUrl: 'criteria-tool.component.html',
styleUrls: ['criteria-tool.component.scss', '../../indexation/indexing-form/indexing-form.component.scss'],
providers: [DatePipe]
})
export class CriteriaToolComponent implements OnInit {
loading: boolean = true;
criteria: any = [];
searchTemplates: any;
currentCriteria: any = [];
filteredCriteria: Observable<string[]>;
searchTermControl = new FormControl();
searchCriteria = new FormControl();
hideCriteriaList: boolean = true;
infoFields: any = [
{
id: 1,
desc: 'lang.searchInAttachmentsInfo'
id: 2,
desc: 'lang.searchFulltextInfo'
id: 3,
desc: 'lang.manualSearchInfo'
},
];
@Input() searchTerm: string = 'Foo';
@Input() defaultCriteria: any = [];
@Output() searchUrlGenerated = new EventEmitter<any>();
@Output() loaded = new EventEmitter<any>();
@ViewChild('criteriaTool', { static: false }) criteriaTool: MatExpansionPanel;
@ViewChild('searchCriteriaInput', { static: false }) searchCriteriaInput: ElementRef;

Alex ORLUC
committed
@ViewChild('appFolderInput', { static: false }) appFolderInput: FolderInputComponent;
@ViewChild('appTagInput', { static: false }) appTagInput: TagInputComponent;
@ViewChild('appIssuingSiteInput', { static: false }) appIssuingSiteInput: IssuingSiteInputComponent;
@ViewChildren('appContactAutocomplete') appContactAutocomplete: QueryList<ContactAutocompleteComponent>;
@ViewChildren('pluginSelectAutocompleteSearch') pluginSelectAutocompleteSearch: QueryList<PluginSelectAutocompleteSearchComponent>;
private _activatedRoute: ActivatedRoute,
public translate: TranslateService,
public http: HttpClient,
public appService: AppService,
public functions: FunctionsService,
public indexingFields: IndexingFieldsService,
private dialog: MatDialog,
private notify: NotificationService,
private latinisePipe: LatinisePipe) {
_activatedRoute.queryParams.subscribe(
params => {
this.searchTerm = params.value;
}
);
}
async ngOnInit(): Promise<void> {
this.searchTermControl.setValue(this.searchTerm);
this.criteria = await this.indexingFields.getAllSearchFields();
this.criteria.forEach((element: any) => {
if (this.defaultCriteria.indexOf(element.identifier) > -1) {
element.control = new FormControl('');
this.addCriteria(element, false);
}
this.loaded.emit(true);
this.filteredCriteria = this.searchCriteria.valueChanges
.pipe(
startWith(''),
map(value => this._filter(value))
);
this.loading = false;
setTimeout(() => {
this.searchTermControl.valueChanges
.pipe(
startWith(''),
map(value => {
if (typeof value === 'string' && !this.functions.empty(value)) {
this.searchTerm = value;
}
})
).subscribe();
}, 500);
this.getSearchTemplates();
private _filter(value: string): string[] {
if (typeof value === 'string') {
const filterValue = this.latinisePipe.transform(value.toLowerCase());
return this.criteria.filter((option: any) => this.latinisePipe.transform(option['label'].toLowerCase()).includes(filterValue));
return this.criteria;
isCurrentCriteriaById(criteriaIds: string[]) {
return this.currentCriteria.filter((currCrit: any) => criteriaIds.indexOf(currCrit.identifier) > -1).length > 0;
}
isCurrentCriteriaByType(criteriaTypes: string[]) {
return this.currentCriteria.filter((currCrit: any) => criteriaTypes.indexOf(currCrit.type) > -1).length > 0;
async addCriteria(criteria: any, openPanel: boolean = true) {
if (this.functions.empty(criteria.control) || this.functions.empty(criteria.control.value)) {
criteria.control = criteria.type === 'date' || criteria.type === 'integer' ? new FormControl({}) : new FormControl('');
this.initField(criteria);
this.currentCriteria.push(criteria);
this.searchTermControl.setValue(this.searchTerm);
this.searchCriteria.reset();
// this.searchCriteriaInput.nativeElement.blur();
if (openPanel) {
setTimeout(() => {
this.criteriaTool.open();
}, 0);
}
}
initField(field: any) {
try {
const regex = /role_[.]*/g;
if (field.identifier.match(regex) !== null) {
this['set_role_field'](field);
} else {
this['set_' + field.identifier + '_field'](field);
}
} catch (error) {
// console.log(error);
}
}
removeCriteria(index: number) {
this.currentCriteria.splice(index, 1);
if (this.currentCriteria.length === 0) {
this.criteriaTool.close();
}
getFilterControl() {
return this.searchCriteria;
}
getCriterias() {
return this.criteria;
}
getFilteredCriterias() {
return this.filteredCriteria;
}
focusFilter() {
this.hideCriteriaList = false;
setTimeout(() => {
this.searchCriteriaInput.nativeElement.focus();
}, 100);
}
getCurrentCriteriaValues() {
const objCriteria = {};
if (!this.functions.empty(this.searchTermControl.value)) {
values: this.searchTermControl.value
};
}
this.currentCriteria.forEach((field: any) => {
if (field.type === 'date' || field.type === 'integer') {
if (!this.functions.empty(field.control.value.start) || !this.functions.empty(field.control.value.end)) {
objCriteria[field.identifier] = {
type : field.type,
values: {
start: !this.functions.empty(field.control.value.start) ? field.control.value.start : field.control.value.end,
end: !this.functions.empty(field.control.value.end) ? field.control.value.end : field.control.value.start
}
};
}
} else {
if (!this.functions.empty(field.control.value)) {
objCriteria[field.identifier] = {
type : field.type,
values: field.control.value
};
} else {
if (['recipients', 'senders'].indexOf(field.identifier) > -1 || field.type === 'contact') {
if (!this.functions.empty(this.appContactAutocomplete.toArray().filter((component: any) => component.id === field.identifier)[0].getInputValue())) {
objCriteria[field.identifier] = {
type : field.type,
values: [this.appContactAutocomplete.toArray().filter((component: any) => component.id === field.identifier)[0].getInputValue()]
};
}
});
this.searchUrlGenerated.emit(objCriteria);
toggleTool(state: boolean) {
if (state) {
this.criteriaTool.open();
} else {
this.criteriaTool.close();
}
}

Alex ORLUC
committed
getLabelValue(identifier: string, value: any) {
if (this.functions.empty(value)) {
return this.translate.instant('lang.undefined');
} else if (typeof value === 'object') {
if (Object.keys(value).indexOf('title') > -1) {
return value.title;
} else if (Object.keys(value).indexOf('label') > -1) {
return value.label;
} else {
return value;

Alex ORLUC
committed
getFormatLabel(identifier: string, value: any) {
if (this.criteria.filter((field: any) => field.identifier === identifier)[0].type === 'date') {
return `${this.datePipe.transform(value.start, 'dd/MM/y')} - ${this.datePipe.transform(value.end, 'dd/MM/y')}`;
} else if (this.criteria.filter((field: any) => field.identifier === identifier)[0].type === 'integer') {
return `${value.start} - ${value.end}`;

Alex ORLUC
committed
if (identifier === 'registeredMail_issuingSite') {
return this.appIssuingSiteInput.getSiteLabel(value);
} else {
return value;
}
getLabelValues(identifier: string, values: string[]) {
if (values.length === 0) {
return this.translate.instant('lang.undefined');
} else if (typeof values[0] === 'object') {
if (Object.keys(values[0]).indexOf('title') > -1) {
return values.map((item: any) => item.title);
} else if (Object.keys(values[0]).indexOf('label') > -1) {
return values.map((item: any) => item.label);
}
return values;
}
}
refreshCriteria(criteria: any) {
this.currentCriteria.forEach((field: any, index: number) => {
if (criteria[field.identifier] !== undefined) {
if (this.currentCriteria[index].type === 'date' && this.functions.empty(criteria[field.identifier].values)) {
field.control.setValue({
start: null,
end: null
});
} else {
field.control.setValue(criteria[field.identifier].values);
}
if (Object.keys(criteria)[0] === 'meta') {
this.searchTermControl.setValue(criteria['meta'].values);
}
this.getCurrentCriteriaValues();
}

Alex ORLUC
committed
searchInAttachments(identifier: string) {
return ['subject', 'chrono', 'fulltext'].indexOf(identifier) > -1;
}
displayInfoSearch(infoSearchNumber: number) {
if (infoSearchNumber === 1 && (this.isCurrentCriteriaById(['subject', 'chrono', 'fulltext']))) {
} else if (infoSearchNumber === 2 && this.isCurrentCriteriaById(['fulltext'])) {
return true;
} else if (infoSearchNumber === 3 && (this.isCurrentCriteriaById(['recipients', 'senders', 'registeredMail_recipient']) || this.isCurrentCriteriaByType(['contact']))) {
return true;
}
return false;

Alex ORLUC
committed
}
getBadgesInfoField(field: any) {
const badges = [];
if (['subject', 'chrono', 'fulltext'].indexOf(field.identifier) > -1) {
badges.push(1);
}
if (['fulltext'].indexOf(field.identifier) > -1) {
badges.push(2);
}
if (['recipients', 'senders', 'registeredMail_recipient'].indexOf(field.identifier) > -1 || field.type === 'contact') {
badges.push(3);
}
return badges;
}
set_meta_field(value: any) {
this.searchTermControl.setValue(value);
}
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
set_doctype_field(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,
};
}));
});
}
});
elem.values = arrValues;
elem.event = 'calcLimitDate';
resolve(true);
}),
catchError((err: any) => {
this.notify.handleSoftErrors(err);
return of(false);
})
).subscribe();
});
}
set_priority_field(elem: any) {
return new Promise((resolve, reject) => {
this.http.get(`../rest/priorities`).pipe(
tap((data: any) => {
elem.values = data.priorities;
resolve(true);
}),
catchError((err: any) => {
this.notify.handleSoftErrors(err);
return of(false);
})
).subscribe();
});
}

Alex ORLUC
committed
set_status_field(elem: any) {
return new Promise((resolve, reject) => {
this.http.get(`../rest/statuses`).pipe(
tap((data: any) => {
elem.values = data.statuses.map((val: any) => {
return {
id: val.identifier,
label: val.label_status
};
});
resolve(true);
}),
catchError((err: any) => {
this.notify.handleSoftErrors(err);
return of(false);

Alex ORLUC
committed
})
).subscribe();
});
}
set_category_field(elem: any) {
return new Promise((resolve, reject) => {
this.http.get(`../rest/categories`).pipe(
tap((data: any) => {
elem.values = data.categories.map((val: any) => {
return {
id: val.id,
label: val.label
};
});
resolve(true);
}),
catchError((err: any) => {
this.notify.handleSoftErrors(err);
return of(false);

Alex ORLUC
committed
})
).subscribe();
});
}
set_destination_field(elem: any) {
return new Promise((resolve, reject) => {
this.http.get(`../rest/indexingModels/entities`).pipe(
let title = '';
elem.values = elem.values.concat(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
};
}));
}),
catchError((err: any) => {
this.notify.handleSoftErrors(err);
return of(false);
})
).subscribe();
});
set_initiator_field(elem: any) {
return new Promise((resolve, reject) => {
this.http.get(`../rest/indexingModels/entities`).pipe(
tap((data: any) => {
let title = '';
elem.values = elem.values.concat(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
};
}));
}),
catchError((err: any) => {
this.notify.handleSoftErrors(err);
return of(false);
})
).subscribe();
});
}
set_registeredMail_issuingSite_field(elem: any) {
return new Promise((resolve, reject) => {
this.http.get(`../rest/registeredMail/sites`).pipe(
elem.values = data['sites'].map((item: any) => {
return {
id: item.id,
label: `${item.label} (${item.accountNumber})`
};
}),
catchError((err: any) => {
this.notify.handleSoftErrors(err);
return of(false);
});
}
set_role_field(elem: any) {
elem.type = 'selectAutocomplete';
elem.routeDatas = elem.identifier === 'role_dest' ? ['/rest/autocomplete/users?serial=serialId'] : ['/rest/autocomplete/users', '/rest/autocomplete/entities?serial=serialId'];
elem.extraModel = ['type'];
elem.returnValue = 'object';
getSearchTemplates() {
this.http.get(`../rest/searchTemplates`).pipe(
tap((data: any) => {
this.searchTemplates = data.searchTemplates;
})
).subscribe();
}
saveSearchTemplate() {
let query: any = [];
this.currentCriteria.forEach((field: any, index: number) => {
query.push({ 'identifier': field.identifier, 'values': field.control.value });
query.push({ 'identifier': 'searchTerm', 'values': this.searchTermControl.value });
const dialogRef = this.dialog.open(
AddSearchTemplateModalComponent,
{
panelClass: 'maarch-modal',
autoFocus: true,
disableClose: true,
data: {
searchTemplate: { query: query }
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
}
}
);
dialogRef.afterClosed().pipe(
filter((data: any) => data !== undefined),
tap((data) => {
this.searchTemplates.push(data.searchTemplate);
}),
catchError((err: any) => {
this.notify.handleErrors(err);
return of(false);
})
).subscribe();
}
deleteSearchTemplate(id: number, index: number) {
const dialogRef = this.dialog.open(
ConfirmComponent,
{
panelClass: 'maarch-modal',
autoFocus: false,
disableClose: true,
data: {
title: this.translate.instant('lang.delete'),
msg: this.translate.instant('lang.confirmAction')
}
}
);
dialogRef.afterClosed().pipe(
filter((data: string) => data === 'ok'),
exhaustMap(() => this.http.delete(`../rest/searchTemplates/${id}`)),
tap(() => {
this.searchTemplates.splice(index, 1);
this.notify.success(this.translate.instant('lang.searchTemplateDeleted'));
}),
catchError((err: any) => {
this.notify.handleErrors(err);
return of(false);
})
).subscribe();
}
selectSearchTemplate(searchTemplate: any, openPanel: boolean = true) {
this.currentCriteria = [];
this.criteria.forEach((element: any) => {
let index = searchTemplate.query.map((field: any) => field.identifier).indexOf(element.identifier);
if (index > -1) {
element.control = new FormControl({ value: searchTemplate.query[index].values, disabled: false });
element.control.value = searchTemplate.query[index].values;
console.log(element.control);
this.addCriteria(element, openPanel);
if (element.type === 'selectAutocomplete') {
setTimeout(() => {
console.log(element.identifier);
console.log(this.pluginSelectAutocompleteSearch.toArray());
this.pluginSelectAutocompleteSearch.toArray().filter((component: any) => component.id === element.identifier)[0].setDatas(element.control.value);
this.pluginSelectAutocompleteSearch.toArray().filter((component: any) => component.id === element.identifier)[0].resetACDatas();
}, 0);
}
let index = searchTemplate.query.map((field: any) => field.identifier).indexOf('searchTerm');
if (index > -1) {
this.searchTermControl.setValue(searchTemplate.query[index].values);
}