Skip to content
Snippets Groups Projects
notification-administration.component.ts 5.04 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Component, OnInit } from '@angular/core';
    import { HttpClient } from '@angular/common/http';
    import { LANG } from '../translate.component';
    import { ActivatedRoute, Router } from '@angular/router';
    
    import { NotificationService } from '../notification.service';
    
    
    declare function $j(selector: any) : any;
    
    declare var angularGlobals : any;
    
    @Component({
        templateUrl : angularGlobals["notification-administrationView"],
    
        styleUrls   : ['../../node_modules/bootstrap/dist/css/bootstrap.min.css'],
        providers   : [NotificationService]
    
    })
    export class NotificationAdministrationComponent implements OnInit {
    
        coreUrl             : string;
    
    Damien's avatar
    Damien committed
    
        creationMode        : boolean;
        notification        : any       = {
            diffusionType_label  : null
        };
        loading              : boolean   = false;
        lang                 : any       = LANG;
    
        constructor(public http: HttpClient, private route: ActivatedRoute, private router: Router, private notify: NotificationService) {
    
    Damien's avatar
    Damien committed
        updateBreadcrumb(applicationName: string){
            if ($j('#ariane')[0]) {
                $j('#ariane')[0].innerHTML = "<a href='index.php?reinit=true'>" + applicationName + "</a> > <a onclick='location.hash = \"/administration\"' style='cursor: pointer'>Administration</a> > <a onclick='location.hash = \"/administration/notifications\"' style='cursor: pointer'>notifications</a>";
            }
        }
    
    
    Damien's avatar
    Damien committed
            this.updateBreadcrumb(angularGlobals.applicationName);
    
            this.coreUrl = angularGlobals.coreUrl;
    
    Damien's avatar
    Damien committed
    
    
            this.route.params.subscribe(params => {
    
    Damien's avatar
    Damien committed
                if (typeof params['identifier'] == "undefined") {
    
                    this.http.get(this.coreUrl + 'rest/administration/notifications/new')
                        .subscribe((data : any) => {
                            this.notification = data.notification;
                                        
                            this.loading = false;
                        }, (err) => {
    
    Damien's avatar
    Damien committed
                            this.notify.error(err.error.errors);
    
    Damien's avatar
    Damien committed
                } else {
    
                    this.creationMode = false;
                    this.http.get(this.coreUrl + 'rest/administration/notifications/' + params['identifier'])
    
    Damien's avatar
    Damien committed
                        .subscribe((data : any) => {
    
    Damien's avatar
    Damien committed
                            this.notification = data.notification;
                            this.loading = false;
                        }, (err) => {
                            this.notify.error(err.error.errors);
                        });
    
    Damien's avatar
    Damien committed
        selectAll(event: any) {
    
            var target = event.target.getAttribute("data-target");
            $j('#' + target + ' option').prop('selected', true);
            $j('#' + target).trigger('chosen:updated');
        }
    
    
    Damien's avatar
    Damien committed
        unselectAll(event: any) {
    
            var target = event.target.getAttribute("data-target");
            $j('#' + target + ' option').prop('selected', false);
            $j('#' + target).trigger('chosen:updated');
        }
    
        onSubmit() {
    
    Damien's avatar
    Damien committed
            if ($j("#groupslist").val()) {
                this.notification.diffusion_properties = $j("#groupslist").val();
            } else if($j("#entitieslist").val()) {
                this.notification.diffusion_properties = $j("#entitieslist").val();
            } else if($j("#statuseslist").val()) {
                this.notification.diffusion_properties = $j("#statuseslist").val();
            } else if($j("#userslist").val()) {
                this.notification.diffusion_properties = $j("#userslist").val();
    
    Damien's avatar
    Damien committed
            if ($j("#joinDocJd").val() == null) {
    
                this.notification.attachfor_properties = '';
    
    Damien's avatar
    Damien committed
            } else if ($j("#groupslistJd").val()) {
                this.notification.attachfor_properties = $j("#groupslistJd").val();
            } else if($j("#entitieslistJd").val()) {
                this.notification.attachfor_properties = $j("#entitieslistJd").val();
            } else if($j("#statuseslistJd").val()) {
                this.notification.attachfor_properties = $j("#statuseslistJd").val();
            } else if($j("#userslistJd").val()) {
                this.notification.attachfor_properties = $j("#userslistJd").val();
    
    Damien's avatar
    Damien committed
    
    
            if (this.creationMode) {
                this.http.post(this.coreUrl + 'rest/notifications', this.notification)
    
    Damien's avatar
    Damien committed
                    .subscribe((data : any) => {
                        this.router.navigate(['/administration/notifications']);
                        this.notify.success(this.lang.newNotificationAdded+' « '+this.notification.notification_id+' »');
                    },(err) => {
                        this.notify.error(err.error.errors);
                    });
            } else {
    
                this.http.put(this.coreUrl + 'rest/notifications/' + this.notification.notification_sid, this.notification)
    
    Damien's avatar
    Damien committed
                    .subscribe((data : any) => {
                        this.router.navigate(['/administration/notifications']);
                        this.notify.success(this.lang.notificationUpdated+' « '+this.notification.notification_id+' »');
                    },(err) => {
                        this.notify.error(err.error.errors);
                    });