1
|
import {Component, OnInit, Input, ElementRef} from '@angular/core';
|
2
|
import {SimpleChanges, OnChanges} from '@angular/core';
|
3
|
import {FormGroup, FormArray, FormBuilder, Validators} from "@angular/forms";
|
4
|
import {ActivatedRoute, Router} from '@angular/router';
|
5
|
|
6
|
import {ConnectHelper} from '../../../openaireLibrary/connect/connectHelper';
|
7
|
|
8
|
import {Email} from '../../../openaireLibrary/utils/email/email';
|
9
|
import {Body} from '../../../openaireLibrary/utils/email/body';
|
10
|
|
11
|
import {EnvProperties} from '../../../openaireLibrary/utils/properties/env-properties';
|
12
|
import {EmailService} from '../../../openaireLibrary/utils/email/email.service';
|
13
|
import {CommunityService} from "../../../openaireLibrary/connect/community/community.service";
|
14
|
import {ErrorCodes} from '../../../openaireLibrary/utils/properties/errorCodes';
|
15
|
import {Session} from '../../../openaireLibrary/login/utils/helper.class';
|
16
|
|
17
|
@Component({
|
18
|
selector: 'invite',
|
19
|
templateUrl: './invite.component.html',
|
20
|
})
|
21
|
|
22
|
export class InviteComponent implements OnInit {
|
23
|
|
24
|
@Input() longView: boolean = true;
|
25
|
@Input() communityId = null;
|
26
|
|
27
|
private properties: EnvProperties = null;
|
28
|
|
29
|
public community = null;
|
30
|
//public showLoading: boolean = true;
|
31
|
public errorMessage: string = '';
|
32
|
public successfulSentMessage: string = '';
|
33
|
public inviteErrorMessage: string = '';
|
34
|
public missingCommunityId: string = '';
|
35
|
public showAddRecipientMessage: boolean = false;
|
36
|
public showAddNameMessage: boolean = false;
|
37
|
|
38
|
public email: Email;
|
39
|
public body: Body;
|
40
|
public recipients: string;
|
41
|
public fullname:string;
|
42
|
|
43
|
public areValid: boolean = true;
|
44
|
|
45
|
private ckeditorContent: string;
|
46
|
|
47
|
// public defaultBody = '<p>OpenAIRE invites you to subscribe in <a href="https://beta.egi.openaire.eu">_Community_name_</a> dashboard. </p><p>The community dashboard is part of the <a href="https://beta.egi.openaire.eu">OpenAIRE-Connect</a> project.</p>';
|
48
|
public defaultBody ='';
|
49
|
|
50
|
public communityIdParam = {};
|
51
|
public status: number = 1;
|
52
|
public errorCodes: ErrorCodes;
|
53
|
|
54
|
constructor (
|
55
|
private element: ElementRef,
|
56
|
private route: ActivatedRoute,
|
57
|
private _router: Router,
|
58
|
public _fb: FormBuilder,
|
59
|
private _emailService: EmailService,
|
60
|
private _communityService: CommunityService) {
|
61
|
|
62
|
this.route.queryParams.subscribe(
|
63
|
communityId => {
|
64
|
this.communityId = communityId['communityId'];
|
65
|
if(!this.communityId){
|
66
|
this.communityId = ConnectHelper.getCommunityFromDomain(document.location.hostname);
|
67
|
}
|
68
|
this.communityIdParam = (ConnectHelper.isProduction(document.location.hostname))?{}:{communityId:this.communityId};
|
69
|
});
|
70
|
this.errorCodes = new ErrorCodes();
|
71
|
this.status = this.errorCodes.LOADING;
|
72
|
}
|
73
|
|
74
|
|
75
|
public ngOnInit() {
|
76
|
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
|
77
|
this.properties = data.envSpecific;
|
78
|
this.errorMessage = "";
|
79
|
this.missingCommunityId = "";
|
80
|
this.status = this.errorCodes.LOADING;
|
81
|
if (this.communityId != null && this.communityId != '') {
|
82
|
this._communityService.getCommunity(this.properties, this.properties.communityAPI+this.communityId).subscribe (
|
83
|
community => {
|
84
|
this.community = community;
|
85
|
this.fullname = Session.getUserFullName();
|
86
|
console.log("Fullname from session " + Session.getUserFullName());
|
87
|
this.defaultBody = '<p>You are invited to subscribe to <a href="https://beta.' + this.communityId + '.openaire.eu/">' + this.community.title + '</a> dashboard.'
|
88
|
+ '<br>The purpose of this dashboard is to gather, link & monitor the research results related to your community.</p><p>The community dashboard is part of the <a href="https://connect.openaire.eu/">OpenAIRE-Connect</a> project and currently is in BETA version.</p>';
|
89
|
|
90
|
// TODO move the initialisation
|
91
|
this.body = {/*salutation: "Dear Sir/Madame,",*/ fromMessage: ", on behalf of ", fromName: this.fullname, paragraphs: this.defaultBody, /*closing: "Kind regards,",*/ signature: "OpenAIRE team", ps: ""};
|
92
|
this.email = {body: "", subject: "[OpenAIRE-Connect] " + community.title, recipients: []};
|
93
|
this.recipients = "";
|
94
|
|
95
|
this.status = this.errorCodes.DONE;
|
96
|
},
|
97
|
error => this.handleError(error)
|
98
|
);
|
99
|
} else {
|
100
|
this.status = this.errorCodes.DONE;
|
101
|
this.missingCommunityId = "There is no community selected!";
|
102
|
}
|
103
|
|
104
|
this.scroll();
|
105
|
});
|
106
|
}
|
107
|
|
108
|
public scroll() {
|
109
|
console.info("scroll into view");
|
110
|
if (typeof document !== 'undefined') {
|
111
|
this.element.nativeElement.scrollIntoView();
|
112
|
}
|
113
|
}
|
114
|
|
115
|
public invite() {
|
116
|
this.successfulSentMessage = "";
|
117
|
this.inviteErrorMessage = "";
|
118
|
this.status = this.errorCodes.LOADING;
|
119
|
if (this.recipients != "" && this.body.fromName != "") {
|
120
|
if (this.validateEmails()) {
|
121
|
this.composeEmail();
|
122
|
console.log(this.email.body);
|
123
|
|
124
|
this._emailService.sendEmail(this.properties.adminToolsAPIURL + "/sendMail/", this.email).subscribe(
|
125
|
res => {
|
126
|
console.log("The email has been sent successfully!");
|
127
|
this.status = this.errorCodes.DONE;
|
128
|
console.log("Emails Sent: "+res);
|
129
|
if(res > 1) {
|
130
|
this.successfulSentMessage = res + " emails sent successfully!";
|
131
|
} else {
|
132
|
this.successfulSentMessage = res + " email sent successfully!";
|
133
|
}
|
134
|
|
135
|
this.body = {/*salutation: "Dear Sir/Madame,",*/ fromMessage: ", on behalf of ", fromName: this.fullname, paragraphs: this.defaultBody, /*closing: "Kind regards,",*/ signature: "OpenAIRE team", ps: ""};
|
136
|
this.email = {body: "", subject: "[OpenAIRE-Connect] " + this.community.title, recipients: []};
|
137
|
this.recipients = "";
|
138
|
},
|
139
|
error => {
|
140
|
console.log(error);
|
141
|
this.status = this.errorCodes.DONE;
|
142
|
this.inviteErrorMessage = "There was an error sending emails. Please try again.";
|
143
|
}
|
144
|
);
|
145
|
} else {
|
146
|
this.scroll();
|
147
|
this.status = this.errorCodes.DONE;
|
148
|
}
|
149
|
} else {
|
150
|
this.showAddRecipientMessage = true;
|
151
|
this.scroll();
|
152
|
this.status = this.errorCodes.DONE;
|
153
|
}
|
154
|
}
|
155
|
|
156
|
public resetMessages() {
|
157
|
this.errorMessage = "";
|
158
|
this.successfulSentMessage = "";
|
159
|
this.inviteErrorMessage = "";
|
160
|
}
|
161
|
|
162
|
public validateEmails(): boolean {
|
163
|
if (this.parseEmails()) {
|
164
|
if (this.hasValidEmails()) {
|
165
|
return this.areValid;
|
166
|
}
|
167
|
}
|
168
|
this.areValid = false;
|
169
|
return this.areValid;
|
170
|
}
|
171
|
|
172
|
public parseEmails(): boolean {
|
173
|
let email = new Array<string>();
|
174
|
|
175
|
// remove spaces
|
176
|
this.recipients = this.recipients.replace(/\s/g, '');
|
177
|
|
178
|
// remove commas
|
179
|
email = this.recipients.split(",");
|
180
|
|
181
|
// remove empty fields
|
182
|
for (let i = 0; i < email.length; i++) {
|
183
|
if (!(email[i] == "")) {
|
184
|
this.email.recipients.push(email[i]);
|
185
|
}
|
186
|
}
|
187
|
console.log(this.email.recipients);
|
188
|
return true;
|
189
|
}
|
190
|
|
191
|
private hasValidEmails(): boolean {
|
192
|
let length = this.email.recipients.length;
|
193
|
|
194
|
for(let i = 0; i < length; i++) {
|
195
|
if (!this.emailValidator(this.email.recipients[i])){
|
196
|
// TODO remove console message after final testing
|
197
|
console.log("INVALID EMAIL");
|
198
|
return false;
|
199
|
}
|
200
|
}
|
201
|
// TODO remove console message after final testing
|
202
|
console.log("ALL EMAILS ARE VALID");
|
203
|
return true;
|
204
|
}
|
205
|
|
206
|
private emailValidator(email : any): boolean {
|
207
|
if (email.match("^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$"))
|
208
|
return true;
|
209
|
else
|
210
|
return false;
|
211
|
}
|
212
|
|
213
|
public composeEmail() {
|
214
|
// this.email.subject = "[OpenAIRE-Connect] Community_name";
|
215
|
this.email.body = this.formatEmailBody();
|
216
|
}
|
217
|
|
218
|
public formatEmailBody(): string {
|
219
|
let fromMessageAndName = "";
|
220
|
|
221
|
if (this.body.fromName != "") {
|
222
|
fromMessageAndName = "<span>" + this.body.fromMessage + this.body.fromName + "</span>";
|
223
|
}
|
224
|
|
225
|
let formattedEmail = "<div style='font-size:14px;'>" + /*+ this.body.salutation + "<br><br>" +*/
|
226
|
this.body.paragraphs +
|
227
|
"<p>" /*+ this.body.closing + "<br>"*/ + this.body.signature + fromMessageAndName + "<br>" +
|
228
|
"<a href='https://www.openaire.eu'>www.openaire.eu</a>" + "</p></div>";
|
229
|
|
230
|
|
231
|
return formattedEmail;
|
232
|
}
|
233
|
|
234
|
public handleError(error) {
|
235
|
if(error.status == '401') {
|
236
|
this.status = this.errorCodes.FORBIDDEN;
|
237
|
} else if(error.status == '403') {
|
238
|
this.status = this.errorCodes.FORBIDDEN;
|
239
|
} else if(error.status == '404') {
|
240
|
this.status = this.errorCodes.NOT_FOUND;
|
241
|
} else if(error.status == '500') {
|
242
|
this.status = this.errorCodes.ERROR;
|
243
|
} else {
|
244
|
this.status = this.errorCodes.NOT_AVAILABLE;
|
245
|
}
|
246
|
console.log('Server responded: ' + error);
|
247
|
}
|
248
|
allowEdit(){
|
249
|
var email = Session.getUserEmail();
|
250
|
var index =-1;
|
251
|
if(email && this.community != null && this.community.managers != null){
|
252
|
index = this.community.managers.indexOf(email);
|
253
|
}
|
254
|
|
255
|
return Session.isPortalAdministrator() || Session.isCommunityCurator()
|
256
|
}
|
257
|
}
|