1 |
53971
|
sofia.balt
|
import {Component, OnInit, Input, ElementRef} from '@angular/core';
|
2 |
53695
|
sofia.balt
|
import {SimpleChanges, OnChanges} from '@angular/core';
|
3 |
|
|
import {FormGroup, FormArray, FormBuilder, Validators} from "@angular/forms";
|
4 |
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
5 |
|
|
|
6 |
53923
|
sofia.balt
|
import {ConnectHelper} from '../../../openaireLibrary/connect/connectHelper';
|
7 |
|
|
|
8 |
53695
|
sofia.balt
|
import {Email} from '../../../openaireLibrary/utils/email/email';
|
9 |
|
|
import {Body} from '../../../openaireLibrary/utils/email/body';
|
10 |
54442
|
sofia.balt
|
import {Validator} from '../../../openaireLibrary/utils/email/validator';
|
11 |
54443
|
sofia.balt
|
import {Composer} from '../../../openaireLibrary/utils/email/composer';
|
12 |
53695
|
sofia.balt
|
|
13 |
|
|
import {EnvProperties} from '../../../openaireLibrary/utils/properties/env-properties';
|
14 |
|
|
import {EmailService} from '../../../openaireLibrary/utils/email/email.service';
|
15 |
53923
|
sofia.balt
|
import {CommunityService} from "../../../openaireLibrary/connect/community/community.service";
|
16 |
53970
|
sofia.balt
|
import {ErrorCodes} from '../../../openaireLibrary/utils/properties/errorCodes';
|
17 |
54047
|
argiro.kok
|
import {Session} from '../../../openaireLibrary/login/utils/helper.class';
|
18 |
53695
|
sofia.balt
|
|
19 |
|
|
@Component({
|
20 |
|
|
selector: 'invite',
|
21 |
|
|
templateUrl: './invite.component.html',
|
22 |
|
|
})
|
23 |
|
|
|
24 |
|
|
export class InviteComponent implements OnInit {
|
25 |
|
|
|
26 |
|
|
@Input() longView: boolean = true;
|
27 |
53923
|
sofia.balt
|
@Input() communityId = null;
|
28 |
53695
|
sofia.balt
|
|
29 |
|
|
private properties: EnvProperties = null;
|
30 |
53923
|
sofia.balt
|
|
31 |
|
|
public community = null;
|
32 |
53970
|
sofia.balt
|
//public showLoading: boolean = true;
|
33 |
53923
|
sofia.balt
|
public errorMessage: string = '';
|
34 |
53970
|
sofia.balt
|
public successfulSentMessage: string = '';
|
35 |
|
|
public inviteErrorMessage: string = '';
|
36 |
|
|
public missingCommunityId: string = '';
|
37 |
|
|
public showAddRecipientMessage: boolean = false;
|
38 |
54063
|
sofia.balt
|
public showAddNameMessage: boolean = false;
|
39 |
53923
|
sofia.balt
|
|
40 |
53695
|
sofia.balt
|
public email: Email;
|
41 |
|
|
public body: Body;
|
42 |
53748
|
sofia.balt
|
public recipients: string;
|
43 |
54049
|
sofia.balt
|
public fullname:string;
|
44 |
53695
|
sofia.balt
|
|
45 |
53923
|
sofia.balt
|
public areValid: boolean = true;
|
46 |
|
|
|
47 |
53695
|
sofia.balt
|
private ckeditorContent: string;
|
48 |
|
|
|
49 |
54459
|
sofia.balt
|
// public defaultBody ='';
|
50 |
53695
|
sofia.balt
|
|
51 |
53966
|
sofia.balt
|
public communityIdParam = {};
|
52 |
53970
|
sofia.balt
|
public status: number = 1;
|
53 |
|
|
public errorCodes: ErrorCodes;
|
54 |
53966
|
sofia.balt
|
|
55 |
53695
|
sofia.balt
|
constructor (
|
56 |
53966
|
sofia.balt
|
private element: ElementRef,
|
57 |
53695
|
sofia.balt
|
private route: ActivatedRoute,
|
58 |
|
|
private _router: Router,
|
59 |
|
|
public _fb: FormBuilder,
|
60 |
53923
|
sofia.balt
|
private _emailService: EmailService,
|
61 |
|
|
private _communityService: CommunityService) {
|
62 |
53695
|
sofia.balt
|
|
63 |
53923
|
sofia.balt
|
this.route.queryParams.subscribe(
|
64 |
|
|
communityId => {
|
65 |
|
|
this.communityId = communityId['communityId'];
|
66 |
|
|
if(!this.communityId){
|
67 |
|
|
this.communityId = ConnectHelper.getCommunityFromDomain(document.location.hostname);
|
68 |
|
|
}
|
69 |
53966
|
sofia.balt
|
this.communityIdParam = (ConnectHelper.isProduction(document.location.hostname))?{}:{communityId:this.communityId};
|
70 |
53923
|
sofia.balt
|
});
|
71 |
53970
|
sofia.balt
|
this.errorCodes = new ErrorCodes();
|
72 |
|
|
this.status = this.errorCodes.LOADING;
|
73 |
53923
|
sofia.balt
|
}
|
74 |
53695
|
sofia.balt
|
|
75 |
53923
|
sofia.balt
|
|
76 |
53695
|
sofia.balt
|
public ngOnInit() {
|
77 |
|
|
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
|
78 |
|
|
this.properties = data.envSpecific;
|
79 |
53923
|
sofia.balt
|
this.errorMessage = "";
|
80 |
53970
|
sofia.balt
|
this.missingCommunityId = "";
|
81 |
|
|
this.status = this.errorCodes.LOADING;
|
82 |
53923
|
sofia.balt
|
if (this.communityId != null && this.communityId != '') {
|
83 |
|
|
this._communityService.getCommunity(this.properties, this.properties.communityAPI+this.communityId).subscribe (
|
84 |
|
|
community => {
|
85 |
|
|
this.community = community;
|
86 |
54049
|
sofia.balt
|
this.fullname = Session.getUserFullName();
|
87 |
|
|
console.log("Fullname from session " + Session.getUserFullName());
|
88 |
53923
|
sofia.balt
|
|
89 |
54459
|
sofia.balt
|
this.body = Composer.initializeInvitationsBody(this.communityId, this.community.title, this.fullname);
|
90 |
|
|
this.email = Composer.initializeInvitationsEmail(community.title);
|
91 |
54446
|
sofia.balt
|
this.recipients = "";
|
92 |
|
|
|
93 |
|
|
this.status = this.errorCodes.DONE;
|
94 |
53923
|
sofia.balt
|
},
|
95 |
53970
|
sofia.balt
|
error => this.handleError(error)
|
96 |
53923
|
sofia.balt
|
);
|
97 |
53970
|
sofia.balt
|
} else {
|
98 |
|
|
this.status = this.errorCodes.DONE;
|
99 |
|
|
this.missingCommunityId = "There is no community selected!";
|
100 |
53923
|
sofia.balt
|
}
|
101 |
53966
|
sofia.balt
|
|
102 |
|
|
this.scroll();
|
103 |
53695
|
sofia.balt
|
});
|
104 |
|
|
}
|
105 |
|
|
|
106 |
53966
|
sofia.balt
|
public scroll() {
|
107 |
|
|
console.info("scroll into view");
|
108 |
|
|
if (typeof document !== 'undefined') {
|
109 |
|
|
this.element.nativeElement.scrollIntoView();
|
110 |
|
|
}
|
111 |
|
|
}
|
112 |
|
|
|
113 |
53695
|
sofia.balt
|
public invite() {
|
114 |
53970
|
sofia.balt
|
this.successfulSentMessage = "";
|
115 |
|
|
this.inviteErrorMessage = "";
|
116 |
|
|
this.status = this.errorCodes.LOADING;
|
117 |
54063
|
sofia.balt
|
if (this.recipients != "" && this.body.fromName != "") {
|
118 |
53748
|
sofia.balt
|
if (this.validateEmails()) {
|
119 |
|
|
this.composeEmail();
|
120 |
|
|
console.log(this.email.body);
|
121 |
53695
|
sofia.balt
|
|
122 |
53971
|
sofia.balt
|
this._emailService.sendEmail(this.properties.adminToolsAPIURL + "/sendMail/", this.email).subscribe(
|
123 |
53923
|
sofia.balt
|
res => {
|
124 |
|
|
console.log("The email has been sent successfully!");
|
125 |
53970
|
sofia.balt
|
this.status = this.errorCodes.DONE;
|
126 |
|
|
console.log("Emails Sent: "+res);
|
127 |
|
|
if(res > 1) {
|
128 |
|
|
this.successfulSentMessage = res + " emails sent successfully!";
|
129 |
|
|
} else {
|
130 |
|
|
this.successfulSentMessage = res + " email sent successfully!";
|
131 |
|
|
}
|
132 |
|
|
|
133 |
54459
|
sofia.balt
|
this.body = Composer.initializeInvitationsBody(this.communityId, this.community.title, this.fullname);
|
134 |
|
|
this.email = Composer.initializeInvitationsEmail(this.community.title);
|
135 |
53970
|
sofia.balt
|
this.recipients = "";
|
136 |
53923
|
sofia.balt
|
},
|
137 |
|
|
error => {
|
138 |
|
|
console.log(error);
|
139 |
53970
|
sofia.balt
|
this.status = this.errorCodes.DONE;
|
140 |
|
|
this.inviteErrorMessage = "There was an error sending emails. Please try again.";
|
141 |
53923
|
sofia.balt
|
}
|
142 |
53748
|
sofia.balt
|
);
|
143 |
53970
|
sofia.balt
|
} else {
|
144 |
|
|
this.scroll();
|
145 |
|
|
this.status = this.errorCodes.DONE;
|
146 |
53748
|
sofia.balt
|
}
|
147 |
53970
|
sofia.balt
|
} else {
|
148 |
|
|
this.showAddRecipientMessage = true;
|
149 |
|
|
this.scroll();
|
150 |
|
|
this.status = this.errorCodes.DONE;
|
151 |
53695
|
sofia.balt
|
}
|
152 |
|
|
}
|
153 |
|
|
|
154 |
53970
|
sofia.balt
|
public resetMessages() {
|
155 |
|
|
this.errorMessage = "";
|
156 |
|
|
this.successfulSentMessage = "";
|
157 |
|
|
this.inviteErrorMessage = "";
|
158 |
|
|
}
|
159 |
|
|
|
160 |
53748
|
sofia.balt
|
public validateEmails(): boolean {
|
161 |
|
|
if (this.parseEmails()) {
|
162 |
54442
|
sofia.balt
|
if (Validator.hasValidEmails(this.email.recipients)) {
|
163 |
53923
|
sofia.balt
|
return this.areValid;
|
164 |
53748
|
sofia.balt
|
}
|
165 |
|
|
}
|
166 |
53923
|
sofia.balt
|
this.areValid = false;
|
167 |
|
|
return this.areValid;
|
168 |
53748
|
sofia.balt
|
}
|
169 |
|
|
|
170 |
|
|
public parseEmails(): boolean {
|
171 |
|
|
let email = new Array<string>();
|
172 |
|
|
|
173 |
|
|
// remove spaces
|
174 |
|
|
this.recipients = this.recipients.replace(/\s/g, '');
|
175 |
|
|
|
176 |
|
|
// remove commas
|
177 |
|
|
email = this.recipients.split(",");
|
178 |
|
|
|
179 |
|
|
// remove empty fields
|
180 |
|
|
for (let i = 0; i < email.length; i++) {
|
181 |
|
|
if (!(email[i] == "")) {
|
182 |
|
|
this.email.recipients.push(email[i]);
|
183 |
|
|
}
|
184 |
|
|
}
|
185 |
|
|
console.log(this.email.recipients);
|
186 |
|
|
return true;
|
187 |
|
|
}
|
188 |
|
|
|
189 |
53695
|
sofia.balt
|
public composeEmail() {
|
190 |
53926
|
sofia.balt
|
// this.email.subject = "[OpenAIRE-Connect] Community_name";
|
191 |
54443
|
sofia.balt
|
this.email.body = Composer.formatEmailBodyForInvitation(this.body);
|
192 |
53695
|
sofia.balt
|
}
|
193 |
|
|
|
194 |
53970
|
sofia.balt
|
public handleError(error) {
|
195 |
|
|
if(error.status == '401') {
|
196 |
|
|
this.status = this.errorCodes.FORBIDDEN;
|
197 |
|
|
} else if(error.status == '403') {
|
198 |
|
|
this.status = this.errorCodes.FORBIDDEN;
|
199 |
|
|
} else if(error.status == '404') {
|
200 |
|
|
this.status = this.errorCodes.NOT_FOUND;
|
201 |
|
|
} else if(error.status == '500') {
|
202 |
|
|
this.status = this.errorCodes.ERROR;
|
203 |
|
|
} else {
|
204 |
|
|
this.status = this.errorCodes.NOT_AVAILABLE;
|
205 |
|
|
}
|
206 |
53923
|
sofia.balt
|
console.log('Server responded: ' + error);
|
207 |
|
|
}
|
208 |
54047
|
argiro.kok
|
allowEdit(){
|
209 |
|
|
var email = Session.getUserEmail();
|
210 |
|
|
var index =-1;
|
211 |
|
|
if(email && this.community != null && this.community.managers != null){
|
212 |
|
|
index = this.community.managers.indexOf(email);
|
213 |
|
|
}
|
214 |
|
|
|
215 |
|
|
return Session.isPortalAdministrator() || Session.isCommunityCurator()
|
216 |
|
|
}
|
217 |
53695
|
sofia.balt
|
}
|