Skip to content
Snippets Groups Projects
profile.component.ts 38.5 KiB
Newer Older
import { Component, OnInit, NgZone, ViewChild, QueryList, ViewChildren, TemplateRef, ViewContainerRef } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { TranslateService } from '@ngx-translate/core';
import { NotificationService } from '@service/notification/notification.service';
import { HeaderService } from '@service/header.service';
import { debounceTime, switchMap, distinctUntilChanged, filter, tap } from 'rxjs/operators';
import { MatDialog, MatDialogRef } from '@angular/material/dialog';
import { MatExpansionPanel } from '@angular/material/expansion';
import { MatPaginator } from '@angular/material/paginator';
import { MatSidenav } from '@angular/material/sidenav';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
import { SelectionModel } from '@angular/cdk/collections';
import { FormControl, FormGroup, Validators, AbstractControl, ValidationErrors, ValidatorFn, FormBuilder } from '@angular/forms';
import { AppService } from '@service/app.service';
import { FunctionsService } from '@service/functions.service';
import { AuthService } from '@service/auth.service';
import { Router } from '@angular/router';
Damien's avatar
Damien committed

@Component({
    templateUrl: 'profile.component.html',
    styleUrls: ['profile.component.css']
Damien's avatar
Damien committed
})
export class ProfileComponent implements OnInit {
    highlightMe: boolean = false;
    histories: any[] = [];
    passwordModel: any = {
        currentPassword: '',
        newPassword: '',
        reNewPassword: '',
    firstFormGroup: FormGroup;
    ruleText: string = '';
    otherRuleText: string;
    validPassword: Boolean = false;
    matchPassword: Boolean = false;
    hidePassword: Boolean = true;
    passwordRules: any = {
        minLength: { enabled: false, value: 0 },
        complexityUpper: { enabled: false, value: 0 },
        complexityNumber: { enabled: false, value: 0 },
        complexitySpecial: { enabled: false, value: 0 },
        renewal: { enabled: false, value: 0 },
Alex ORLUC's avatar
Alex ORLUC committed
        historyLastUse: { enabled: false, value: 0 },
        base64: '',
        base64ForJs: '',
        name: '',
        type: '',
    mailSignatureModel: any = {
        selected: -1,
    userAbsenceModel: any[] = [];
    basketsToRedirect: string[] = [];

    showPassword: boolean = false;
    selectedSignature: number = -1;
    selectedSignatureLabel: string = '';
    loading: boolean = false;
    selectedIndex: number = 0;
    selectedIndexContactsGrp: number = 0;
Alex ORLUC's avatar
Alex ORLUC committed
    loadingSign: boolean = false;
    @ViewChild('snav2', { static: true }) sidenavRight: MatSidenav;
    @ViewChild('adminMenuTemplate', { static: true }) adminMenuTemplate: TemplateRef<any>;
    selectionBaskets = new SelectionModel<Element>(true, []);
    myBasketExpansionPanel: boolean = false;
    masterToggleBaskets(event: any) {
Alex ORLUC's avatar
Alex ORLUC committed
        if (event.checked) {
            this.user.baskets.forEach((basket: any) => {
Alex ORLUC's avatar
Alex ORLUC committed
                if (!basket.userToDisplay) {
                    this.selectionBaskets.select(basket);
                }
            });
        } else {
            this.selectionBaskets.clear();
        }
    }

    @ViewChildren(MatExpansionPanel) viewPanels: QueryList<MatExpansionPanel>;

Alex ORLUC's avatar
Alex ORLUC committed
    displayedColumnsGroupsList: string[] = ['label', 'description', 'nbContacts', 'public', 'actions'];
    @ViewChild('paginatorGroupsList', { static: false }) paginatorGroupsList: MatPaginator;
Alex ORLUC's avatar
Alex ORLUC committed
    @ViewChild('tableGroupsListSort', { static: false }) sortGroupsList: MatSort;
    applyFilterGroupsList(filterValue: string) {
        filterValue = filterValue.trim(); // Remove whitespace
        filterValue = filterValue.toLowerCase(); // MatTableDataSource defaults to lowercase matches
        this.dataSourceGroupsList.filter = filterValue;
    }

    // Group contacts List Autocomplete
    initAutoCompleteContact = true;

    searchTerm: FormControl = new FormControl();
    searchResult: any = [];
    displayedColumnsContactsListAutocomplete: string[] = ['select', 'contact', 'address'];
    dataSourceContactsListAutocomplete: any;
    @ViewChild('paginatorGroupsListAutocomplete', { static: false }) paginatorGroupsListAutocomplete: MatPaginator;
    selection = new SelectionModel<Element>(true, []);
    masterToggle(event: any) {
        if (event.checked) {
            this.dataSourceContactsListAutocomplete.data.forEach((row: any) => {
                if (!$('#check_' + row.id + '-input').is(':disabled')) {
                    this.selection.select(row.id);
    contactsListMode: boolean = false;
    contactsList: any[] = [];
    displayedColumnsContactsList: string[] = ['contact', 'address', 'actions'];
    dataSourceContactsList: any;
    @ViewChild('paginatorContactsList', { static: false }) paginatorContactsList: MatPaginator;
Alex ORLUC's avatar
Alex ORLUC committed
    @ViewChild('tableContactsListSort', { static: false }) sortContactsList: MatSort;
    applyFilterContactsList(filterValue: string) {
        filterValue = filterValue.trim(); // Remove whitespace
        filterValue = filterValue.toLowerCase(); // MatTableDataSource defaults to lowercase matches
        this.dataSourceContactsList.filter = filterValue;
    }
    displayedColumns = ['event_date', 'record_id', 'info'];
    @ViewChild('paginatorHistory', { static: false }) paginatorHistory: MatPaginator;
Alex ORLUC's avatar
Alex ORLUC committed
    @ViewChild('tableHistorySort', { static: false }) sortHistory: MatSort;
    applyFilter(filterValue: string) {
        filterValue = filterValue.trim(); // Remove whitespace
        filterValue = filterValue.toLowerCase(); // MatTableDataSource defaults to lowercase matches
        this.dataSource.filter = filterValue;
    }

        public translate: TranslateService,
Alex ORLUC's avatar
Alex ORLUC committed
        public http: HttpClient,
        private zone: NgZone,
        private notify: NotificationService,
        public dialog: MatDialog,
        private _formBuilder: FormBuilder,
        private authService: AuthService,
        private headerService: HeaderService,
        public appService: AppService,
Alex ORLUC's avatar
Alex ORLUC committed
        private viewContainerRef: ViewContainerRef,
        private functions: FunctionsService
        window['angularProfileComponent'] = {
            componentAfterUpload: (base64Content: any) => this.processAfterUpload(base64Content),
        this.searchTerm.valueChanges.pipe(
            debounceTime(500),
            filter(value => value.length > 2),
            distinctUntilChanged(),
            switchMap(data => this.http.get('../rest/autocomplete/contacts', { params: { 'search': data } }))
        ).subscribe((response: any) => {
            this.searchResult = response;
            this.dataSourceContactsListAutocomplete = new MatTableDataSource(this.searchResult);
            this.dataSourceContactsListAutocomplete.paginator = this.paginatorGroupsListAutocomplete;
            // this.dataSource.sort      = this.sortContactList;
        this.http.get('../rest/documentEditors').pipe(
            tap((data: any) => {
                this.editorsList = data;
            })
        ).subscribe();

        this.selectedIndex = event.index;
            if (!this.appService.getViewMode()) {
                this.sidenavRight.open();
            this.http.get('../rest/history/users/' + this.user.id)
Alex ORLUC's avatar
Alex ORLUC committed
                .subscribe((data: any) => {
                    this.histories = data.histories;
                    setTimeout(() => {
                        this.dataSource = new MatTableDataSource(this.histories);
Alex ORLUC's avatar
Alex ORLUC committed
                        this.dataSource.sortingDataAccessor = this.functions.listSortingDataAccessor;
Alex ORLUC's avatar
Alex ORLUC committed
                        this.dataSource.paginator = this.paginatorHistory;
                        this.dataSource.sort = this.sortHistory;
                    }, 0);
                }, (err) => {
                    this.notify.error(err.error.errors);
                });

        } else if (event.index == 1) {
            this.sidenavRight.close();
Alex ORLUC's avatar
Alex ORLUC committed
        } else if (!this.appService.getViewMode()) {
            this.sidenavRight.open();
        // LOAD EDITOR TINYMCE for MAIL SIGN
        tinymce.baseURL = '../node_modules/tinymce';
            selector: 'textarea#emailSignature',
            language: this.translate.instant('lang.langISO').replace('-', '_'),
            language_url: `../node_modules/tinymce-i18n/langs/${this.translate.instant('lang.langISO').replace('-', '_')}.js`,
                'maarch_b64image': '../src/frontend/plugins/tinymce/maarch_b64image/plugin.min.js'
            toolbar: 'undo | bold italic underline | alignleft aligncenter alignright | maarch_b64image | forecolor',
            theme_buttons1_add: 'fontselect,fontsizeselect',
            theme_buttons2_add_before: 'cut,copy,paste,pastetext,pasteword,separator,search,replace,separator',
            theme_buttons2_add: 'separator,insertdate,inserttime,preview,separator,forecolor,backcolor',
            theme_buttons3_add_before: 'tablecontrols,separator',
            theme_buttons3_add: 'separator,print,separator,ltr,rtl,separator,fullscreen,separator,insertlayer,moveforward,movebackward,absolut',
            theme_toolbar_align: 'left',
            theme_advanced_toolbar_location: 'top',
            theme_styles: 'Header 1=header1;Header 2=header2;Header 3=header3;Table Row=tableRow1'
        this.selectedIndexContactsGrp = 0;
        this.http.get('../rest/contactsGroups')
            .subscribe((data) => {
                this.contactsGroups = [];
Alex ORLUC's avatar
Alex ORLUC committed
                this.contactsGroup = { public: false, contacts: [] };
                let i = 0;
                data['contactsGroups'].forEach((ct: any) => {
                    if (ct.owner == this.headerService.user.id) {
                        ct.position = i;
                        this.contactsGroups.push(ct);
                        i++;
                    }
                });
                setTimeout(() => {
                    this.dataSourceGroupsList = new MatTableDataSource(this.contactsGroups);
                    this.dataSourceGroupsList.paginator = this.paginatorGroupsList;
                    this.dataSourceGroupsList.sort = this.sortGroupsList;
                }, 0);
            }, (err) => {
                this.notify.handleErrors(err);
        this.http.post('../rest/contactsGroups', this.contactsGroup)
            .subscribe((data: any) => {
                this.initGroupsContact();
                this.notify.success(this.translate.instant('lang.contactsGroupAdded'));
            }, (err) => {
                this.notify.error(err.error.errors);
            });
    }

    updateGroupSubmit() {
        this.http.put('../rest/contactsGroups/' + this.contactsGroup.id, this.contactsGroup)
                this.notify.success(this.translate.instant('lang.contactsGroupUpdated'));
Alex ORLUC's avatar
Alex ORLUC committed
                this.initGroupsContact();
            }, (err) => {
                this.notify.error(err.error.errors);
    }

    deleteContactsGroup(row: any) {
        var contactsGroup = this.contactsGroups[row];
        let r = confirm(this.translate.instant('lang.confirmAction') + ' ' + this.translate.instant('lang.delete') + ' « ' + contactsGroup.label + ' »');
            this.http.delete('../rest/contactsGroups/' + contactsGroup.id)
                    this.contactsListMode = false;
                    var lastElement = this.contactsGroups.length - 1;
                    this.contactsGroups[row] = this.contactsGroups[lastElement];
                    this.contactsGroups[row].position = row;
                    this.contactsGroups.splice(lastElement, 1);

                    this.dataSourceGroupsList = new MatTableDataSource(this.contactsGroups);
                    this.dataSourceGroupsList.paginator = this.paginatorGroupsList;
                    this.notify.success(this.translate.instant('lang.contactsGroupDeleted'));

                }, (err) => {
                    this.notify.error(err.error.errors);
                });
        }
    }

    loadContactsGroup(contactsGroup: any) {
        this.contactsListMode = true;

        this.http.get('../rest/contactsGroups/' + contactsGroup.id)
            .subscribe((data: any) => {
                this.contactsGroup = data.contactsGroup;
                setTimeout(() => {
                    this.dataSourceContactsList = new MatTableDataSource(this.contactsGroup.contacts);
                    this.dataSourceContactsList.paginator = this.paginatorContactsList;
                    this.dataSourceContactsList.sort = this.sortContactsList;
                    this.selectedIndexContactsGrp = 1;
        elem.textContent = this.translate.instant('lang.loading') + '...';
        this.http.post('../rest/contactsGroups/' + this.contactsGroup.id + '/contacts', { 'contacts': this.selection.selected })
                this.notify.success(this.translate.instant('lang.contactAdded'));
                elem.textContent = this.translate.instant('lang.add');
                this.contactsGroup = data.contactsGroup;
                setTimeout(() => {
                    this.dataSourceContactsList = new MatTableDataSource(this.contactsGroup.contacts);
                    this.dataSourceContactsList.paginator = this.paginatorContactsList;
                    this.dataSourceContactsList.sort = this.sortContactsList;
                }, 0);
            }, (err) => {
                this.notify.error(err.error.errors);
            });
    }

    preDelete(index: number) {
        let r = confirm(this.translate.instant('lang.reallyWantToDeleteContactFromGroup'));

        if (r) {
            this.removeContact(this.contactsGroup.contacts[index], index);
        }
    }

    removeContact(contact: any, row: any) {
        this.http.delete('../rest/contactsGroups/' + this.contactsGroup.id + '/contacts/' + contact['id'])
            .subscribe(() => {
                var lastElement = this.contactsGroup.contacts.length - 1;
                this.contactsGroup.contacts[row] = this.contactsGroup.contacts[lastElement];
                this.contactsGroup.contacts[row].position = row;
                this.contactsGroup.contacts.splice(lastElement, 1);

                this.dataSourceContactsList = new MatTableDataSource(this.contactsGroup.contacts);
                this.dataSourceContactsList.paginator = this.paginatorContactsList;
                this.dataSourceContactsList.sort = this.sortContactsList;
                this.notify.success(this.translate.instant('lang.contactDeletedFromGroup'));
            }, (err) => {
                this.notify.error(err.error.errors);
            });
    }

    launchLoading() {
        if (this.searchTerm.value.length > 2) {
            this.dataSourceContactsListAutocomplete = null;
            this.initAutoCompleteContact = false;
        }
    }

    isInGrp(contact: any): boolean {
        let isInGrp = false;
        this.contactsGroup.contacts.forEach((row: any) => {
            if (row.id == contact.id) {
    selectContact(id: any) {
        if (!$('#check_' + id + '-input').is(':disabled')) {
            this.selection.toggle(id);
    ngOnInit(): void {
        this.headerService.setHeader(this.translate.instant('lang.myProfile'));
        this.headerService.injectInSideBarLeft(this.adminMenuTemplate, this.viewContainerRef, 'adminMenu');
Damien's avatar
Damien committed
        this.loading = true;

        this.http.get('../rest/currentUser/profile')
                this.user.baskets.forEach((value: any, index: number) => {
                    this.user.baskets[index]['disabled'] = false;
                    this.user.redirectedBaskets.forEach((value2: any) => {
                        if (value.basket_id == value2.basket_id && value.basket_owner == value2.basket_owner) {
                            this.user.baskets[index]['disabled'] = true;
                        }
                    });
                });
    processAfterUpload(b64Content: any) {
        this.zone.run(() => this.resfreshUpload(b64Content));
        if (this.signatureModel.size <= 2000000) {
            this.signatureModel.base64 = b64Content.replace(/^data:.*?;base64,/, '');
            this.signatureModel.base64ForJs = b64Content;
        } else {
            this.signatureModel.name = '';
            this.signatureModel.size = 0;
            this.signatureModel.type = '';
            this.signatureModel.base64 = '';
            this.signatureModel.base64ForJs = '';
            this.notify.error('Taille maximum de fichier dépassée (2 MB)');
    displayPassword() {
        this.showPassword = !this.showPassword;
    }

    uploadSignatureTrigger(fileInput: any) {
        if (fileInput.target.files && fileInput.target.files[0]) {
            var reader = new FileReader();

            this.signatureModel.name = fileInput.target.files[0].name;
            this.signatureModel.size = fileInput.target.files[0].size;
            this.signatureModel.type = fileInput.target.files[0].type;
            if (this.signatureModel.label == '') {
                this.signatureModel.label = this.signatureModel.name;
            }

            reader.readAsDataURL(fileInput.target.files[0]);

                window['angularProfileComponent'].componentAfterUpload(value.target.result);
Alex ORLUC's avatar
Alex ORLUC committed
    dndUploadSignature(event: any) {
        if (event.mouseEvent.dataTransfer.files && event.mouseEvent.dataTransfer.files[0]) {
            var reader = new FileReader();

            this.signatureModel.name = event.mouseEvent.dataTransfer.files[0].name;
            this.signatureModel.size = event.mouseEvent.dataTransfer.files[0].size;
            this.signatureModel.type = event.mouseEvent.dataTransfer.files[0].type;
            if (this.signatureModel.label == '') {
                this.signatureModel.label = this.signatureModel.name;
            }

            reader.readAsDataURL(event.mouseEvent.dataTransfer.files[0]);

            reader.onload = (value: any) => {
                window['angularProfileComponent'].componentAfterUpload(value.target.result);
                this.submitSignature();
            };
        }
    }

    displaySignatureEditionForm(index: number) {
        this.selectedSignature = index;
        this.selectedSignatureLabel = this.user.signatures[index].signature_label;
    }

        this.mailSignatureModel.selected = i;
        tinymce.get('emailSignature').setContent(this.user.emailSignatures[i].html_body);
        this.mailSignatureModel.title = this.user.emailSignatures[i].title;
    resetEmailSignature() {
        this.mailSignatureModel.selected = -1;

        tinymce.get('emailSignature').setContent('');
        this.mailSignatureModel.title = '';
    addBasketRedirection(newUser: any) {
Alex ORLUC's avatar
Alex ORLUC committed
        let basketsRedirect: any[] = [];

        this.selectionBaskets.selected.forEach((elem: any) => {
            basketsRedirect.push(
                {
                    actual_user_id: newUser.serialId,
Alex ORLUC's avatar
Alex ORLUC committed
                    basket_id: elem.basket_id,
                    group_id: elem.groupSerialId,
                    originalOwner: null
                }
            )
        let r = confirm(this.translate.instant('lang.confirmAction') + ' ' + this.translate.instant('lang.redirectBasket'));
            this.http.post('../rest/users/' + this.user.id + '/redirectedBaskets', basketsRedirect)
                    this.user.baskets = data['baskets'];
                    this.user.redirectedBaskets = data['redirectedBaskets'];
                    this.selectionBaskets.clear();
                    this.notify.success(this.translate.instant('lang.basketUpdated'));
                    this.notify.error(err.error.errors);
Alex ORLUC's avatar
Alex ORLUC committed
    delBasketRedirection(basket: any, i: number) {
        let r = confirm(this.translate.instant('lang.confirmAction'));
            this.http.delete('../rest/users/' + this.user.id + '/redirectedBaskets?redirectedBasketIds[]=' + basket.id)
                .subscribe((data: any) => {
                    this.user.baskets = data['baskets'];
                    this.user.redirectedBaskets.splice(i, 1);
                    this.notify.success(this.translate.instant('lang.basketUpdated'));
                }, (err) => {
                    this.notify.error(err.error.errors);
                });
        }
    }

Alex ORLUC's avatar
Alex ORLUC committed
    delBasketAssignRedirection(basket: any, i: number) {
        let r = confirm(this.translate.instant('lang.confirmAction'));
            this.http.delete('../rest/users/' + this.user.id + '/redirectedBaskets?redirectedBasketIds[]=' + basket.id)
                .subscribe((data: any) => {
                    this.user.baskets = data['baskets'];
                    this.user.assignedBaskets.splice(i, 1);
                    this.notify.success(this.translate.instant('lang.basketUpdated'));
                }, (err) => {
                    this.notify.error(err.error.errors);
    reassignBasketRedirection(newUser: any, basket: any, i: number) {
        let r = confirm(this.translate.instant('lang.confirmAction') + ' ' + this.translate.instant('lang.redirectBasket'));
            this.http.post('../rest/users/' + this.user.id + '/redirectedBaskets', [
Alex ORLUC's avatar
Alex ORLUC committed
                {
                    'actual_user_id': newUser.serialId,
                    'basket_id': basket.basket_id,
                    'group_id': basket.group_id,
                    'originalOwner': basket.owner_user_id,
                .subscribe((data: any) => {
                    this.user.baskets = data['baskets'];
                    this.user.assignedBaskets.splice(i, 1);
                    this.notify.success(this.translate.instant('lang.basketUpdated'));
                }, (err) => {
                    this.notify.error(err.error.errors);
                });
        }
Damien's avatar
Damien committed
    updateBasketColor(i: number, y: number) {
        this.http.put('../rest/currentUser/groups/' + this.user.regroupedBaskets[i].groupSerialId + '/baskets/' + this.user.regroupedBaskets[i].baskets[y].basket_id, { 'color': this.user.regroupedBaskets[i].baskets[y].color })
Damien's avatar
Damien committed
            .subscribe((data: any) => {
                this.user.regroupedBaskets = data.userBaskets;
                this.notify.success(this.translate.instant('lang.modificationSaved'));
Damien's avatar
Damien committed
            }, (err) => {
                this.notify.error(err.error.errors);
Damien's avatar
Damien committed
    activateAbsence() {
        let r = confirm(this.translate.instant('lang.confirmToBeAbsent'));
            this.http.put('../rest/users/' + this.user.id + '/status', { 'status': 'ABS' })
                    this.authService.logout();
                    this.notify.error(err.error.errors);
        this.myBasketExpansionPanel = false;
        this.viewPanels.forEach(p => p.close());
        let r = confirm(this.translate.instant('lang.askRedirectBasketBeforeAbsence'));
            this.selectedIndex = 1;
            setTimeout(() => {
                this.myBasketExpansionPanel = true;
            }, 0);
        this.passwordModel.currentPassword = this.firstFormGroup.controls['currentPasswordCtrl'].value;
        this.passwordModel.newPassword = this.firstFormGroup.controls['newPasswordCtrl'].value;
        this.passwordModel.reNewPassword = this.firstFormGroup.controls['retypePasswordCtrl'].value;
        this.http.put('../rest/users/' + this.user.id + '/password', this.passwordModel)
                this.showPassword = false;
                this.passwordModel = {
                    currentPassword: '',
                    newPassword: '',
                    reNewPassword: '',
                this.notify.success(this.translate.instant('lang.passwordUpdated'));
                this.notify.error(err.error.errors);
    submitEmailSignature() {
        this.mailSignatureModel.htmlBody = tinymce.get('emailSignature').getContent();

        this.http.post('../rest/currentUser/emailSignature', this.mailSignatureModel)
                    this.notify.error(data.errors);
                    this.user.emailSignatures = data.emailSignatures;
                    tinymce.get('emailSignature').setContent('');
                    this.notify.success(this.translate.instant('lang.emailSignatureAdded'));
    updateEmailSignature() {
        this.mailSignatureModel.htmlBody = tinymce.get('emailSignature').getContent();
        var id = this.user.emailSignatures[this.mailSignatureModel.selected].id;
        this.http.put('../rest/currentUser/emailSignature/' + id, this.mailSignatureModel)
                    this.notify.error(data.errors);
                    this.user.emailSignatures[this.mailSignatureModel.selected].title = data.emailSignature.title;
                    this.user.emailSignatures[this.mailSignatureModel.selected].html_body = data.emailSignature.html_body;
                    this.notify.success(this.translate.instant('lang.emailSignatureUpdated'));
        let r = confirm(this.translate.instant('lang.confirmDeleteMailSignature'));
            var id = this.user.emailSignatures[this.mailSignatureModel.selected].id;
            this.http.delete('../rest/currentUser/emailSignature/' + id)
                    if (data.errors) {
                        this.notify.error(data.errors);
                    } else {
                        this.user.emailSignatures = data.emailSignatures;
                        tinymce.get('emailSignature').setContent('');
                        this.notify.success(this.translate.instant('lang.emailSignatureDeleted'));
    submitSignature() {
        this.http.post('../rest/users/' + this.user.id + '/signatures', this.signatureModel)
                this.user.signatures = data.signatures;
                    base64: '',
                    base64ForJs: '',
                    name: '',
                    type: '',
                this.notify.success(this.translate.instant('lang.signatureAdded'));
                this.notify.error(err.error.errors);
        this.http.put('../rest/users/' + this.user.id + '/signatures/' + signature.id, { 'label': signature.signature_label })
                this.notify.success(this.translate.instant('lang.signatureUpdated'));
                this.notify.error(err.error.errors);
    deleteSignature(id: number) {
        let r = confirm(this.translate.instant('lang.confirmDeleteSignature'));
            this.http.delete('../rest/users/' + this.user.id + '/signatures/' + id)
                    this.user.signatures = data.signatures;
                    this.notify.success(this.translate.instant('lang.signatureDeleted'));
                    this.notify.error(err.error.errors);
    onSubmit() {
        this.http.put('../rest/currentUser/profile', this.user)
            .subscribe(() => {
                this.notify.success(this.translate.instant('lang.modificationSaved'));
                this.headerService.user.firstname = this.user.firstname;
                this.headerService.user.lastname = this.user.lastname;
                this.notify.error(err.error.errors);
        this.http.put('../rest/currentUser/profile/preferences', { documentEdition: this.user.preferences.documentEdition })
                this.notify.success(this.translate.instant('lang.modificationSaved'));
                this.headerService.resfreshCurrentUser();
            }, (err) => {
                this.notify.error(err.error.errors);
            });
    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;
        };
    }

    changePasswd() {
        this.http.get('../rest/passwordRules')
Alex ORLUC's avatar
Alex ORLUC committed
                let valArr: ValidatorFn[] = [];
                let otherRuleTextArr: String[] = [];

                valArr.push(Validators.required);

                data.rules.forEach((rule: any) => {
                    if (rule.label == 'minLength') {
                        this.passwordRules.minLength.enabled = rule.enabled;
Alex ORLUC's avatar
Alex ORLUC committed
                        this.passwordRules.minLength.value = rule.value;
                            valArr.push(Validators.minLength(this.passwordRules.minLength.value));
                            ruleTextArr.push(rule.value + ' ' + this.translate.instant('lang.password' + rule.label));

                    } else if (rule.label == 'complexityUpper') {
                        this.passwordRules.complexityUpper.enabled = rule.enabled;
                        this.passwordRules.complexityUpper.value = rule.value;
                        if (rule.enabled) {
                            valArr.push(this.regexValidator(new RegExp('[A-Z]'), { 'complexityUpper': '' }));
                            ruleTextArr.push(this.translate.instant('lang.password' + rule.label));

                    } else if (rule.label == 'complexityNumber') {
                        this.passwordRules.complexityNumber.enabled = rule.enabled;
                        this.passwordRules.complexityNumber.value = rule.value;
                        if (rule.enabled) {
                            valArr.push(this.regexValidator(new RegExp('[0-9]'), { 'complexityNumber': '' }));
                            ruleTextArr.push(this.translate.instant('lang.password' + rule.label));

                    } else if (rule.label == 'complexitySpecial') {
                        this.passwordRules.complexitySpecial.enabled = rule.enabled;
Alex ORLUC's avatar
Alex ORLUC committed
                        this.passwordRules.complexitySpecial.value = rule.value;
                            valArr.push(this.regexValidator(new RegExp('[^A-Za-z0-9]'), { 'complexitySpecial': '' }));
                            ruleTextArr.push(this.translate.instant('lang.password' + rule.label));
                        }
                    } else if (rule.label == 'renewal') {
                        this.passwordRules.renewal.enabled = rule.enabled;
                        this.passwordRules.renewal.value = rule.value;
                            otherRuleTextArr.push(this.translate.instant('lang.password' + rule.label) + ' <b>' + rule.value + ' ' + this.translate.instant('lang.days') + '</b>. ' + this.translate.instant('lang.password2' + rule.label) + '.');
                    } else if (rule.label == 'historyLastUse') {
                        this.passwordRules.historyLastUse.enabled = rule.enabled;
                        this.passwordRules.historyLastUse.value = rule.value;
                        if (rule.enabled) {
                            otherRuleTextArr.push(this.translate.instant('lang.passwordhistoryLastUseDesc') + ' <b>' + rule.value + '</b> ' + this.translate.instant('lang.passwordhistoryLastUseDesc2') + '.');
                this.otherRuleText = otherRuleTextArr.join('<br/>');
                this.firstFormGroup.controls['newPasswordCtrl'].setValidators(valArr);
            }, (err) => {
                this.notify.error(err.error.errors);
            });

Alex ORLUC's avatar
Alex ORLUC committed
        this.firstFormGroup = this._formBuilder.group({
            newPasswordCtrl: [
                ''
            ],
            retypePasswordCtrl: [
                '',
                Validators.compose([Validators.required])
            ],
            currentPasswordCtrl: [
                '',
                Validators.compose([Validators.required])
            ]

        }, {
            validator: this.matchValidator
        });
        this.validPassword = false;
        this.firstFormGroup.controls['currentPasswordCtrl'].setErrors(null);
        this.firstFormGroup.controls['newPasswordCtrl'].setErrors(null);
        this.firstFormGroup.controls['retypePasswordCtrl'].setErrors(null);
        this.selectedIndex = 0;
    matchValidator(group: FormGroup) {

        if (group.controls['newPasswordCtrl'].value == group.controls['retypePasswordCtrl'].value) {
            return false;
        } else {
            group.controls['retypePasswordCtrl'].setErrors({ 'mismatch': true });
Alex ORLUC's avatar
Alex ORLUC committed
            return { 'mismatch': true };
        if (this.firstFormGroup.controls['newPasswordCtrl'].value != this.firstFormGroup.controls['retypePasswordCtrl'].value) {
Alex ORLUC's avatar
Alex ORLUC committed
            this.firstFormGroup.controls['retypePasswordCtrl'].setErrors({ 'mismatch': true });
        } else {
            this.firstFormGroup.controls['retypePasswordCtrl'].setErrors(null);
        }
        if (this.firstFormGroup.controls['newPasswordCtrl'].hasError('required')) {
            return this.translate.instant('lang.requiredField') + ' !';

        } else if (this.firstFormGroup.controls['newPasswordCtrl'].hasError('minlength') && this.passwordRules.minLength.enabled) {
            return this.passwordRules.minLength.value + ' ' + this.translate.instant('lang.passwordminLength') + ' !';

        } else if (this.firstFormGroup.controls['newPasswordCtrl'].errors != null && this.firstFormGroup.controls['newPasswordCtrl'].errors.complexityUpper !== undefined && this.passwordRules.complexityUpper.enabled) {
            return this.translate.instant('lang.passwordcomplexityUpper') + ' !';

        } else if (this.firstFormGroup.controls['newPasswordCtrl'].errors != null && this.firstFormGroup.controls['newPasswordCtrl'].errors.complexityNumber !== undefined && this.passwordRules.complexityNumber.enabled) {
            return this.translate.instant('lang.passwordcomplexityNumber') + ' !';

        } else if (this.firstFormGroup.controls['newPasswordCtrl'].errors != null && this.firstFormGroup.controls['newPasswordCtrl'].errors.complexitySpecial !== undefined && this.passwordRules.complexitySpecial.enabled) {
            return this.translate.instant('lang.passwordcomplexitySpecial') + ' !';
            this.firstFormGroup.controls['newPasswordCtrl'].setErrors(null);
Alex ORLUC's avatar
Alex ORLUC committed
    showActions(basket: any) {
        $('#' + basket.basket_id + '_' + basket.group_id).show();
Alex ORLUC's avatar
Alex ORLUC committed
    hideActions(basket: any) {
        $('#' + basket.basket_id + '_' + basket.group_id).hide();

    toggleAddGrp() {
        this.initGroupsContact();
        $('#contactsGroupFormUp').toggle();
        $('#contactsGroupList').toggle();
    }
    toggleAddContactGrp() {
        $('#contactsGroupFormAdd').toggle();
        // $('#contactsGroup').toggle();
Alex ORLUC's avatar
Alex ORLUC committed
    changeTabContactGrp(event: any) {
        this.selectedIndexContactsGrp = event;
        if (event == 0) {
            this.initGroupsContact();
        }
        this.http.put('../rest/users/' + this.user.id + '/externalSignatures', {})
            .subscribe((data: any) => {
                this.loadingSign = false;
                this.notify.success(this.translate.instant('lang.signsSynchronized'));
            }, (err) => {
                this.loadingSign = false;
                this.notify.handleErrors(err);