Skip to content
Snippets Groups Projects
parameter-administration.component.ts 4.66 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { ChangeDetectorRef, Component, OnInit, ViewChild } from '@angular/core';
    
    import { MediaMatcher } from '@angular/cdk/layout';
    
    import { HttpClient } from '@angular/common/http';
    
    Pegane Nestor's avatar
    Pegane Nestor committed
    import { Router, ActivatedRoute } from '@angular/router';
    
    import { LANG } from '../../translate.component';
    import { NotificationService } from '../../notification.service';
    
    Florian Azizian's avatar
    Florian Azizian committed
    import { HeaderService }        from '../../../service/header.service';
    
    import { MatSidenav } from '@angular/material';
    
    Pegane Nestor's avatar
    Pegane Nestor committed
    
    
    declare function $j(selector: any): any;
    declare var angularGlobals: any;
    
    Pegane Nestor's avatar
    Pegane Nestor committed
    @Component({
    
        templateUrl: "parameter-administration.component.html",
    
        providers: [NotificationService]
    
    Pegane Nestor's avatar
    Pegane Nestor committed
    })
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    export class ParameterAdministrationComponent implements OnInit {
    
        @ViewChild('snav') public  sidenavLeft   : MatSidenav;
        @ViewChild('snav2') public sidenavRight  : MatSidenav;
    
        private _mobileQueryListener    : () => void;
        mobileQuery                     : MediaQueryList;
        
        coreUrl                         : string;
        lang                            : any       = LANG;
        loading                         : boolean   = false;
    
        parameter                       : any       = {};
        type                            : string;
        creationMode                    : boolean;
    
    Florian Azizian's avatar
    Florian Azizian committed
        constructor(changeDetectorRef: ChangeDetectorRef, media: MediaMatcher, public http: HttpClient, private route: ActivatedRoute, private router: Router, private notify: NotificationService, private headerService: HeaderService) {
    
            $j("link[href='merged_css.php']").remove();
            this.mobileQuery = media.matchMedia('(max-width: 768px)');
            this._mobileQueryListener = () => changeDetectorRef.detectChanges();
            this.mobileQuery.addListener(this._mobileQueryListener);
        }
    
        ngOnDestroy(): void {
            this.mobileQuery.removeListener(this._mobileQueryListener);
    
    Pegane Nestor's avatar
    Pegane Nestor committed
        ngOnInit(): void {
            this.coreUrl = angularGlobals.coreUrl;
    
            this.loading = true;
    
    
            this.route.params.subscribe((params) => {
    
                if (typeof params['id'] == "undefined") {
    
    Florian Azizian's avatar
    Florian Azizian committed
                    this.headerService.headerMessage = this.lang.parameterCreation;
    
                    window['MainHeaderComponent'].setSnav(this.sidenavLeft);
                    window['MainHeaderComponent'].setSnavRight(null);
    
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                    this.creationMode = true;
    
                    this.loading = false;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                } else {
    
                    window['MainHeaderComponent'].setSnav(this.sidenavLeft);
                    window['MainHeaderComponent'].setSnavRight(null);
    
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                    this.creationMode = false;
    
                    this.http.get(this.coreUrl + "rest/parameters/" + params['id'])
    
                        .subscribe((data: any) => {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                            this.parameter = data.parameter;
    
    Florian Azizian's avatar
    Florian Azizian committed
                            this.headerService.headerMessage = this.lang.parameterModification + " <small>" +  this.parameter.id + "</small>";
    
                            if (typeof (this.parameter.param_value_int) == "number") {
    
                                this.type = "int";
                            } else if (this.parameter.param_value_date) {
                                this.type = "date";
                            } else {
                                this.type = "string";
                            }
    
                            this.loading = false;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                        }, () => {
                            location.href = "index.php";
    
    Pegane Nestor's avatar
    Pegane Nestor committed
        }
    
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        onSubmit() {
    
            if (this.type == 'date') {
                this.parameter.param_value_int = null;
                this.parameter.param_value_string = null;
    
            else if (this.type == 'int') {
                this.parameter.param_value_date = null;
                this.parameter.param_value_string = null;
    
            else if (this.type == 'string') {
                this.parameter.param_value_date = null;
                this.parameter.param_value_int = null;
    
            if (this.creationMode == true) {
    
                this.http.post(this.coreUrl + 'rest/parameters', this.parameter)
    
                        this.router.navigate(['administration/parameters']);
                        this.notify.success(this.lang.parameterAdded);
    
                        this.notify.error(err.error.errors);
                    });
    
            } else if (this.creationMode == false) {
    
                this.http.put(this.coreUrl + 'rest/parameters/' + this.parameter.id, this.parameter)
    
                        this.router.navigate(['administration/parameters']);
                        this.notify.success(this.lang.parameterUpdated);
    
                        this.notify.error(err.error.errors);
                    });
    
    Pegane Nestor's avatar
    Pegane Nestor committed
        }
    }