1
|
import { Component, ViewChild, OnInit, ElementRef } from '@angular/core';
|
2
|
import { ActivatedRoute, Router } from '@angular/router';
|
3
|
import { HelpContentService } from '../../services/help-content.service';
|
4
|
import { FormGroup } from '@angular/forms';
|
5
|
import { EntityFormComponent } from './entity-form.component';
|
6
|
import { CheckEntity, Entity } from '../../domain/entity';
|
7
|
import { Portal } from '../../domain/portal';
|
8
|
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';
|
9
|
import {Session} from '../../openaireLibrary/login/utils/helper.class';
|
10
|
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
|
11
|
import {HelperFunctions} from "../../openaireLibrary/utils/HelperFunctions.class";
|
12
|
import {UserManagementService} from '../../openaireLibrary/services/user-management.service';
|
13
|
import {Title} from '@angular/platform-browser';
|
14
|
import {ClearCacheService} from "../../openaireLibrary/services/clear-cache.service";
|
15
|
|
16
|
@Component({
|
17
|
selector: 'entities',
|
18
|
templateUrl: './entities.component.html',
|
19
|
})
|
20
|
|
21
|
export class EntitiesComponent implements OnInit {
|
22
|
|
23
|
// @ViewChild(ModalFormComponent)
|
24
|
// @ViewChild('saveModal')
|
25
|
// public modal:ModalFormComponent;
|
26
|
//
|
27
|
// @ViewChild('updateModal')
|
28
|
// public updateModal:ModalFormComponent;
|
29
|
@ViewChild('AlertModalSaveEntity') alertModalSaveEntity;
|
30
|
@ViewChild('AlertModalUpdateEntity') alertModalUpdateEntity;
|
31
|
@ViewChild('AlertModalDeleteEntities') alertModalDeleteEntities;
|
32
|
private selectedEntities: string[] = [];
|
33
|
|
34
|
@ViewChild(EntityFormComponent)
|
35
|
public formComponent: EntityFormComponent;
|
36
|
|
37
|
public checkboxes: CheckEntity[] = [];
|
38
|
|
39
|
public entities: Entity[] = [];
|
40
|
|
41
|
public formGroup: FormGroup;
|
42
|
|
43
|
private searchText: RegExp = new RegExp('');
|
44
|
public keyword = '';
|
45
|
|
46
|
public communities: Portal[] = [];
|
47
|
public selectedCommunityPid: string;
|
48
|
|
49
|
@ViewChild('AlertModalRelatedPages') alertModalRelatedPages;
|
50
|
|
51
|
public toggleIds: string[];
|
52
|
public toggleStatus: boolean;
|
53
|
public properties: EnvProperties = null;
|
54
|
|
55
|
public showLoading = true;
|
56
|
public errorMessage = '';
|
57
|
public updateErrorMessage = '';
|
58
|
public modalErrorMessage = '';
|
59
|
public isPortalAdministrator = null;
|
60
|
|
61
|
constructor(private element: ElementRef, private route: ActivatedRoute,
|
62
|
private _router: Router, private title: Title,
|
63
|
private _helpContentService: HelpContentService,
|
64
|
private userManagementService: UserManagementService,
|
65
|
private _clearCacheService: ClearCacheService) {}
|
66
|
|
67
|
ngOnInit() {
|
68
|
this.formGroup = this.formComponent.form;
|
69
|
this.route.data
|
70
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
71
|
this.properties = data.envSpecific;
|
72
|
this.title.setTitle('Administration Dashboard | Entities');
|
73
|
this.route.queryParams.subscribe(params => {
|
74
|
HelperFunctions.scroll();
|
75
|
this.userManagementService.getUserInfo().subscribe( user => {
|
76
|
this.selectedCommunityPid = params['communityId'];
|
77
|
this.applyCommunityFilter(this.selectedCommunityPid);
|
78
|
this.isPortalAdministrator = Session.isPortalAdministrator(user) && !this.selectedCommunityPid;
|
79
|
});
|
80
|
});
|
81
|
});
|
82
|
|
83
|
}
|
84
|
|
85
|
getEntities(community_pid: string) {
|
86
|
if (!Session.isLoggedIn()) {
|
87
|
this._router.navigate(['/user-info'],
|
88
|
{ queryParams: { 'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url} });
|
89
|
} else {
|
90
|
this.showLoading = true;
|
91
|
this.updateErrorMessage = '';
|
92
|
this.errorMessage = '';
|
93
|
if(community_pid) {
|
94
|
this._helpContentService.getCommunityEntities(community_pid, this.properties.adminToolsAPIURL).subscribe(
|
95
|
entities => {
|
96
|
this.entities = entities;
|
97
|
this.checkboxes = [];
|
98
|
|
99
|
let self = this;
|
100
|
entities.forEach(_ => {
|
101
|
self.checkboxes.push(<CheckEntity>{entity: _, checked: false});
|
102
|
});
|
103
|
|
104
|
this.showLoading = false;
|
105
|
},
|
106
|
error => this.handleError('System error retrieving entities', error));
|
107
|
} else {
|
108
|
this._helpContentService.getEntities(this.properties.adminToolsAPIURL).subscribe(
|
109
|
entities => {
|
110
|
this.entities = entities;
|
111
|
this.checkboxes = [];
|
112
|
|
113
|
let self = this;
|
114
|
entities.forEach(_ => {
|
115
|
self.checkboxes.push(<CheckEntity>{entity: _, checked: false});
|
116
|
});
|
117
|
this.showLoading = false;
|
118
|
},
|
119
|
error => this.handleError('System error retrieving community entities', error));
|
120
|
}
|
121
|
}
|
122
|
}
|
123
|
|
124
|
public toggleCheckBoxes(event) {
|
125
|
this.checkboxes.forEach(_ => _.checked = event.target.checked);
|
126
|
}
|
127
|
|
128
|
public applyCheck(flag: boolean) {
|
129
|
this.checkboxes.forEach(_ => _.checked = flag);
|
130
|
}
|
131
|
|
132
|
public getSelectedEntities(): string[] {
|
133
|
return this.checkboxes.filter(entity => entity.checked === true).map(checkedEntity => checkedEntity.entity).map(res => res._id);
|
134
|
}
|
135
|
|
136
|
private deleteEntitiesFromArray(ids: string[]): void {
|
137
|
for (let id of ids) {
|
138
|
const i = this.checkboxes.findIndex(_ => _.entity._id === id);
|
139
|
this.checkboxes.splice(i, 1);
|
140
|
}
|
141
|
}
|
142
|
|
143
|
public confirmDeleteEntity(id: string) {
|
144
|
// this.deleteConfirmationModal.ids = [id];
|
145
|
// this.deleteConfirmationModal.showModal();
|
146
|
this.selectedEntities = [id];
|
147
|
this.confirmDeleteEntitiesModalOpen();
|
148
|
}
|
149
|
|
150
|
public confirmDeleteSelectedEntities() {
|
151
|
// this.deleteConfirmationModal.ids = this.getSelectedEntities();
|
152
|
// this.deleteConfirmationModal.showModal();
|
153
|
this.selectedEntities = this.getSelectedEntities();
|
154
|
this.confirmDeleteEntitiesModalOpen();
|
155
|
}
|
156
|
|
157
|
private confirmDeleteEntitiesModalOpen() {
|
158
|
if (!Session.isLoggedIn()) {
|
159
|
this._router.navigate(['/user-info'],
|
160
|
{ queryParams: { 'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url} });
|
161
|
} else {
|
162
|
this.alertModalDeleteEntities.cancelButton = true;
|
163
|
this.alertModalDeleteEntities.okButton = true;
|
164
|
this.alertModalDeleteEntities.alertTitle = 'Delete Confirmation';
|
165
|
this.alertModalDeleteEntities.message = 'Are you sure you want to delete the selected entity(-ies)?';
|
166
|
this.alertModalDeleteEntities.okButtonText = 'Yes';
|
167
|
this.alertModalDeleteEntities.open();
|
168
|
}
|
169
|
}
|
170
|
|
171
|
public confirmedDeleteEntities(data: any) {
|
172
|
if (!Session.isLoggedIn()) {
|
173
|
this._router.navigate(['/user-info'],
|
174
|
{ queryParams: { 'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url} });
|
175
|
} else {
|
176
|
this.showLoading = true;
|
177
|
this.updateErrorMessage = '';
|
178
|
|
179
|
this._helpContentService.deleteEntities(this.selectedEntities, this.properties.adminToolsAPIURL).subscribe(
|
180
|
_ => {
|
181
|
this.deleteEntitiesFromArray(this.selectedEntities);
|
182
|
this.showLoading = false;
|
183
|
this._clearCacheService.clearCache("entities deleted");
|
184
|
},
|
185
|
error => this.handleUpdateError('System error deleting the selected entities', error)
|
186
|
);
|
187
|
}
|
188
|
}
|
189
|
|
190
|
public editEntity(i: number) {
|
191
|
const entity: Entity = this.checkboxes[i].entity;
|
192
|
this.formGroup.patchValue(entity);
|
193
|
// this.updateModal.showModal();
|
194
|
this.modalErrorMessage = '';
|
195
|
this.entitiesModalOpen(this.alertModalUpdateEntity, 'Update', 'Update Entity');
|
196
|
}
|
197
|
|
198
|
public newEntity() {
|
199
|
this.formComponent.reset();
|
200
|
this.modalErrorMessage = '';
|
201
|
this.entitiesModalOpen(this.alertModalSaveEntity, 'Save', 'Add a new Entity');
|
202
|
}
|
203
|
|
204
|
private entitiesModalOpen(modal: any, title: string, yesBtn: string) {
|
205
|
if (!Session.isLoggedIn()) {
|
206
|
this._router.navigate(['/user-info'],
|
207
|
{ queryParams: { 'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url} });
|
208
|
} else {
|
209
|
modal.cancelButton = true;
|
210
|
modal.okButton = true;
|
211
|
modal.alertTitle = title;
|
212
|
modal.okButtonText = yesBtn;
|
213
|
modal.open();
|
214
|
}
|
215
|
}
|
216
|
|
217
|
public entitySaveConfirmed(data: any) {
|
218
|
if (!Session.isLoggedIn()) {
|
219
|
this._router.navigate(['/user-info'],
|
220
|
{ queryParams: { 'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url} });
|
221
|
} else {
|
222
|
if (!this.formGroup.valid) {
|
223
|
this.entitiesModalOpen(this.alertModalSaveEntity, 'Save', 'Add a new Entity');
|
224
|
this.modalErrorMessage = 'Please fill in all required fields marked with *';
|
225
|
} else {
|
226
|
this.modalErrorMessage = '';
|
227
|
this._helpContentService.saveEntity(<Entity> this.formGroup.value, this.properties.adminToolsAPIURL).subscribe(
|
228
|
entity => {
|
229
|
this.entitySavedSuccessfully(entity);
|
230
|
this._clearCacheService.clearCache("entity saved");
|
231
|
},
|
232
|
error => this.handleUpdateError('System error creating entity', error)
|
233
|
);
|
234
|
}
|
235
|
}
|
236
|
}
|
237
|
|
238
|
public entityUpdateConfirmed(data: any) {
|
239
|
if (!Session.isLoggedIn()) {
|
240
|
this._router.navigate(['/user-info'],
|
241
|
{ queryParams: { 'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url} });
|
242
|
} else {
|
243
|
if(!this.formGroup.valid) {
|
244
|
this.entitiesModalOpen(this.alertModalUpdateEntity, 'Update', 'Update Entity');
|
245
|
this.modalErrorMessage = 'Please fill in all required fields marked with *';
|
246
|
} else {
|
247
|
this._helpContentService.updateEntity(
|
248
|
<Entity> this.formGroup.value, this.properties.adminToolsAPIURL).subscribe(
|
249
|
entity => {
|
250
|
this.entityUpdatedSuccessfully(entity);
|
251
|
this._clearCacheService.clearCache("entity updated");
|
252
|
},
|
253
|
error => this.handleUpdateError('System error updating entity', error)
|
254
|
);
|
255
|
}
|
256
|
}
|
257
|
}
|
258
|
|
259
|
public entitySavedSuccessfully(entity: Entity) {
|
260
|
this.checkboxes.push(<CheckEntity>{entity : entity, checked : false});
|
261
|
this.applyCheck(false);
|
262
|
}
|
263
|
|
264
|
public entityUpdatedSuccessfully(entity: Entity) {
|
265
|
this.checkboxes.find(checkItem => checkItem.entity._id === entity._id).entity = entity;
|
266
|
this.applyCheck(false);
|
267
|
}
|
268
|
|
269
|
public filterBySearch(text: string) {
|
270
|
this.searchText = new RegExp(text, 'i');
|
271
|
this.applyFilter();
|
272
|
}
|
273
|
|
274
|
public applyFilter() {
|
275
|
this.checkboxes = [];
|
276
|
this.entities.filter(item => this.filterEntities(item)).forEach(
|
277
|
_ => this.checkboxes.push(<CheckEntity>{entity: _, checked: false})
|
278
|
);
|
279
|
}
|
280
|
|
281
|
public filterEntities(entity: Entity): boolean {
|
282
|
const textFlag = this.searchText.toString() === '' || (entity.name).match(this.searchText) != null;
|
283
|
return textFlag;
|
284
|
}
|
285
|
|
286
|
handleError(message: string, error) {
|
287
|
this.errorMessage = message;
|
288
|
console.log('Server responded: ' + error);
|
289
|
|
290
|
this.showLoading = false;
|
291
|
}
|
292
|
|
293
|
handleUpdateError(message: string, error) {
|
294
|
if (error == null) {
|
295
|
this.formComponent.reset();
|
296
|
} else {
|
297
|
this.updateErrorMessage = message;
|
298
|
console.log('Server responded: ' + error);
|
299
|
}
|
300
|
|
301
|
this.showLoading = false;
|
302
|
}
|
303
|
|
304
|
public applyCommunityFilter(community_pid: string) {
|
305
|
this.getEntities(community_pid);
|
306
|
}
|
307
|
|
308
|
public toggleEntities(status: boolean, ids: string[]) {
|
309
|
// this.okModal.showModal();
|
310
|
this.toggleIds = ids;
|
311
|
this.toggleStatus = status;
|
312
|
this.confirmRelatedPagesModalOpen();
|
313
|
}
|
314
|
|
315
|
private confirmRelatedPagesModalOpen() {
|
316
|
if (!Session.isLoggedIn()) {
|
317
|
this._router.navigate(['/user-info'],
|
318
|
{ queryParams: { 'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url} });
|
319
|
} else {
|
320
|
this.alertModalRelatedPages.cancelButton = true;
|
321
|
this.alertModalRelatedPages.okButton = true;
|
322
|
this.alertModalRelatedPages.alertTitle = 'Warning';
|
323
|
this.alertModalRelatedPages.message = "This action will affect all search pages related to this entity! Pages' status will change to entity's status! Do you want to continue?";
|
324
|
this.alertModalRelatedPages.okButtonText = 'Yes';
|
325
|
this.alertModalRelatedPages.open();
|
326
|
}
|
327
|
}
|
328
|
|
329
|
public continueToggling(event: any) {
|
330
|
if (!Session.isLoggedIn()) {
|
331
|
this._router.navigate(['/user-info'],
|
332
|
{ queryParams: { 'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url} });
|
333
|
} else {
|
334
|
this.updateErrorMessage = '';
|
335
|
this._helpContentService.toggleEntities(
|
336
|
this.selectedCommunityPid, this.toggleIds, this.toggleStatus, this.properties.adminToolsAPIURL).subscribe(
|
337
|
() => {
|
338
|
for (let id of this.toggleIds) {
|
339
|
const i = this.checkboxes.findIndex(_ => _.entity._id === id);
|
340
|
this.checkboxes[i].entity.isEnabled = this.toggleStatus;
|
341
|
}
|
342
|
this.applyCheck(false);
|
343
|
this._clearCacheService.clearCache("entities toggled (status: "+this.toggleStatus+")");
|
344
|
},
|
345
|
error => this.handleUpdateError('System error changing the status of the selected entity(-ies)', error)
|
346
|
);
|
347
|
}
|
348
|
}
|
349
|
}
|