1
|
import {Component, ViewChild} from "@angular/core";
|
2
|
import {SearchUtilsClass} from "../../openaireLibrary/searchPages/searchUtils/searchUtils.class";
|
3
|
import {ErrorMessagesComponent} from "../../openaireLibrary/utils/errorMessages.component";
|
4
|
import {ErrorCodes} from "../../openaireLibrary/utils/properties/errorCodes";
|
5
|
import {EnvProperties} from "../../openaireLibrary/utils/properties/env-properties";
|
6
|
import {SearchPageComponent} from "../../openaireLibrary/searchPages/searchUtils/searchPage.component";
|
7
|
import {ActivatedRoute} from "@angular/router";
|
8
|
import {AdvancedField, Filter, Value} from "../../openaireLibrary/searchPages/searchUtils/searchHelperClasses.class";
|
9
|
import {SearchFields} from "../../openaireLibrary/utils/properties/searchFields";
|
10
|
import {CommunitiesService} from "../../openaireLibrary/connect/communities/communities.service";
|
11
|
import {Session, User} from "../../openaireLibrary/login/utils/helper.class";
|
12
|
import {CommunityInfo} from "../../openaireLibrary/connect/community/communityInfo";
|
13
|
import {SubscribeService} from "../../openaireLibrary/utils/subscribe/subscribe.service";
|
14
|
import {StringUtils} from "../../openaireLibrary/utils/string-utils.class";
|
15
|
import {HelperFunctions} from "../../openaireLibrary/utils/HelperFunctions.class";
|
16
|
import {UserManagementService} from "../../openaireLibrary/services/user-management.service";
|
17
|
import {Breadcrumb} from "../../openaireLibrary/utils/breadcrumbs/breadcrumbs.component";
|
18
|
import {NewSearchPageComponent} from "../../openaireLibrary/searchPages/searchUtils/newSearchPage.component";
|
19
|
|
20
|
@Component({
|
21
|
selector: 'search-communities',
|
22
|
template: `
|
23
|
<new-search-page pageTitle="OpenAIRE-Connect | Search Communities"
|
24
|
[hasPrefix]=false [piwikSiteId]="piwikSiteId"
|
25
|
searchFormClass="communitiesSearchForm"
|
26
|
[formPlaceholderText]="'Search for Research Gateways…'"
|
27
|
type="communities" entityType="community" [filters]="filters"
|
28
|
[results]="results" [searchUtils]="searchUtils"
|
29
|
[showResultCount]=true
|
30
|
[disableForms]="disableForms"
|
31
|
[lastIndex]=false [sort]=true
|
32
|
[simpleView]="true"
|
33
|
[fieldIds]="fieldIds" [fieldIdsMap]="fieldIdsMap" [selectedFields]="selectedFields"
|
34
|
[simpleSearchLink]="searchLink" [entitiesSelection]="false" [showBreadcrumb]="true"
|
35
|
>
|
36
|
</new-search-page>
|
37
|
<!-- [baseUrl]="baseUrl"-->
|
38
|
<!-- [breadcrumbs]="breadcrumbs"
|
39
|
[showType]="showType"-->
|
40
|
`
|
41
|
})
|
42
|
export class SearchCommunitiesComponent {
|
43
|
public piwikSiteId = null;
|
44
|
private errorCodes: ErrorCodes;
|
45
|
private errorMessages: ErrorMessagesComponent;
|
46
|
public results: CommunityInfo[] = [];
|
47
|
public totalResults: CommunityInfo[] = [];
|
48
|
public sub: any; public subResults: any;
|
49
|
public filters = [];
|
50
|
public searchFields:SearchFields = new SearchFields();
|
51
|
public searchUtils:SearchUtilsClass = new SearchUtilsClass();
|
52
|
public disableForms: boolean = false;
|
53
|
public baseUrl: string = null;
|
54
|
public fieldIds: string[] = ["q"];
|
55
|
public refineFields: string[] = this.searchFields.COMMUNITIES_SEARCH_FIELDS;
|
56
|
public fieldIdsMap = {["q"]:{name:"All fields", type:"keyword", param:"q", operator: "op", equalityOperator: "=", filterType: null}};
|
57
|
public selectedFields: AdvancedField[] = [new AdvancedField(this.fieldIds[0], "", this.fieldIdsMap[this.fieldIds[0]].name, this.fieldIdsMap[this.fieldIds[0]].type,"", "and")];
|
58
|
public searchLink;
|
59
|
public showType = false;
|
60
|
public breadcrumbs: Breadcrumb[] = [{name: 'home', route: '/'}, {name: 'communities'}];
|
61
|
properties:EnvProperties;
|
62
|
@ViewChild (NewSearchPageComponent) searchPage : NewSearchPageComponent ;
|
63
|
private user: User;
|
64
|
|
65
|
constructor (private route: ActivatedRoute,
|
66
|
private _communitiesService: CommunitiesService,
|
67
|
private _subscribeService: SubscribeService,
|
68
|
private userManagementService: UserManagementService) {
|
69
|
this.errorCodes = new ErrorCodes();
|
70
|
this.errorMessages = new ErrorMessagesComponent();
|
71
|
this.searchUtils.status = this.errorCodes.LOADING;
|
72
|
}
|
73
|
|
74
|
public ngOnInit() {
|
75
|
this.route.data
|
76
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
77
|
this.properties = data.envSpecific;
|
78
|
this.piwikSiteId = this.properties.piwikSiteId;
|
79
|
this.baseUrl = data.envSpecific.searchLinkToCommunities;
|
80
|
});
|
81
|
this.sub = this.route.queryParams.subscribe(params => {
|
82
|
this.searchPage.resultsPerPage = 10;
|
83
|
this.searchUtils.keyword = (params['fv0']?params['fv0']:'');
|
84
|
this.searchUtils.page = (params['page'] === undefined) ? 1: + params['page'];
|
85
|
this.searchUtils.sortBy = (params['sortBy'] === undefined)? '' : params['sortBy'];
|
86
|
this.searchUtils.size = (params['size'] === undefined) ? this.searchPage.resultsPerPage: +params['size'];
|
87
|
this.searchUtils.baseUrl = this.baseUrl;
|
88
|
this.searchPage.searchUtils = this.searchUtils;
|
89
|
if(this.searchUtils.size != 5 && this.searchUtils.size != 10 && this.searchUtils.size != 20 && this.searchUtils.size != 50) {
|
90
|
this.searchUtils.size = this.searchPage.resultsPerPage;
|
91
|
}
|
92
|
if(this.searchUtils.sortBy && this.searchUtils.sortBy != "creationdate,descending" && this.searchUtils.sortBy != "creationdate,ascending") {
|
93
|
this.searchUtils.sortBy = "";
|
94
|
}
|
95
|
this.searchPage.refineFields = this.refineFields;
|
96
|
this.searchLink = this.properties.searchLinkToCommunities;
|
97
|
this.selectedFields[0] = new AdvancedField(this.fieldIds[0], "", this.fieldIdsMap[this.fieldIds[0]].name, this.fieldIdsMap[this.fieldIds[0]].type,this.searchUtils.keyword, "and");
|
98
|
this.searchPage.prepareSearchPage(this.fieldIds, this.selectedFields, this.refineFields,[], this.fieldIdsMap,null,params, "community", null);
|
99
|
|
100
|
let queryParams = params;//this.searchPage.getQueryParamsFromUrl(params);
|
101
|
if(typeof document !== 'undefined') {
|
102
|
this.userManagementService.getUserInfo().subscribe(user => {
|
103
|
this.user = user;
|
104
|
this.initCommunities(queryParams);
|
105
|
});
|
106
|
} else {
|
107
|
this.initCommunities(queryParams);
|
108
|
}
|
109
|
});
|
110
|
}
|
111
|
|
112
|
public ngOnDestroy() {
|
113
|
if(this.sub){
|
114
|
this.sub.unsubscribe();
|
115
|
}
|
116
|
if(this.subResults){
|
117
|
this.subResults.unsubscribe();
|
118
|
}
|
119
|
}
|
120
|
|
121
|
/**
|
122
|
* Initialize communities from Communities APIs
|
123
|
*
|
124
|
* @param params
|
125
|
*/
|
126
|
private initCommunities(params) {
|
127
|
this.subResults = this._communitiesService.getCommunitiesState().subscribe(
|
128
|
data => {
|
129
|
if(!data){
|
130
|
return;
|
131
|
}
|
132
|
for(let i = 0; i < data.length; i++) {
|
133
|
this.totalResults[i] = data[i];
|
134
|
this.totalResults[i].isManager = false;
|
135
|
this.totalResults[i].isSubscribed = false;
|
136
|
if(this.user) {
|
137
|
this.totalResults[i].isManager = this.isCommunityManager(this.totalResults[i]);
|
138
|
}
|
139
|
}
|
140
|
if(this.user) {
|
141
|
this._subscribeService.getCommunitiesSubscribedTo(this.properties/*, this.user.email*/).subscribe(
|
142
|
res => {
|
143
|
for (let i = 0; i < this.totalResults.length; i++) {
|
144
|
this.totalResults[i].isSubscribed = (res.indexOf(this.totalResults[i].communityId) != -1);
|
145
|
}
|
146
|
this._getResults(params);
|
147
|
}
|
148
|
);
|
149
|
}
|
150
|
else {
|
151
|
this._getResults(params);
|
152
|
}
|
153
|
},
|
154
|
err => {
|
155
|
this.handleError('Error getting communities', err);
|
156
|
this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
|
157
|
this.disableForms = false;
|
158
|
HelperFunctions.scroll();
|
159
|
}
|
160
|
);
|
161
|
}
|
162
|
|
163
|
|
164
|
/**
|
165
|
* Get all communities from Communities API and apply permission access validator,
|
166
|
* keyword searching, filter, paging and sorting.
|
167
|
*
|
168
|
* @param params, status
|
169
|
* @private
|
170
|
*/
|
171
|
private _getResults(params: Map<string, string>){
|
172
|
this.searchUtils.status = this.errorCodes.LOADING;
|
173
|
this.disableForms = true;
|
174
|
this.results = this.totalResults;
|
175
|
if(this.filters.length === 0) {
|
176
|
this.filters = this.createFilters();
|
177
|
}
|
178
|
this.searchUtils.totalResults = 0;
|
179
|
this.applyParams(params);
|
180
|
}
|
181
|
|
182
|
/**
|
183
|
* Return the communities in which user has permission to view or manage.
|
184
|
*/
|
185
|
private showCommunities() {
|
186
|
let ret: CommunityInfo[] = [];
|
187
|
for(let result of this.results) {
|
188
|
if (result.status == 'hidden') {
|
189
|
continue;
|
190
|
} else if (result.status == "manager") {
|
191
|
let mail = (this.user)?this.user.email:null;
|
192
|
if (mail == null) { // no user
|
193
|
continue;
|
194
|
} else if (Session.isCommunityCurator(this.user) || Session.isPortalAdministrator(this.user)) {
|
195
|
ret.push(result);
|
196
|
} else if (result.managers.indexOf(mail) != -1) {
|
197
|
ret.push(result);
|
198
|
}
|
199
|
continue;
|
200
|
}
|
201
|
ret.push(result);
|
202
|
}
|
203
|
this.results = ret;
|
204
|
}
|
205
|
|
206
|
/**
|
207
|
* Apply permission access validator,
|
208
|
* keyword searching, filter, paging and sorting.
|
209
|
*
|
210
|
* @param params
|
211
|
* @param status
|
212
|
*/
|
213
|
public applyParams(params: Map<string, string>) {
|
214
|
this.showCommunities();
|
215
|
if(this.searchUtils.keyword && this.searchUtils.keyword != '') {
|
216
|
this.searchForKeywords();
|
217
|
}
|
218
|
this.checkFilters(params);
|
219
|
this.sort();
|
220
|
this.searchUtils.totalResults = this.results.length;
|
221
|
// this.searchPage.checkSelectedFilters(this.filters);
|
222
|
// this.searchPage.updateBaseUrlWithParameters(this.filters);
|
223
|
// this.searchPage.buildPageURLParameters(this.filters, [], false);
|
224
|
this.filters = this.searchPage.prepareFiltersToShow(this.filters,this.searchUtils.totalResults);
|
225
|
|
226
|
this.results = this.results.slice((this.searchUtils.page-1)*this.searchUtils.size, (this.searchUtils.page*this.searchUtils.size));
|
227
|
this.searchUtils.status = this.errorCodes.DONE;
|
228
|
if(this.searchUtils.totalResults == 0 ){
|
229
|
this.searchUtils.status = this.errorCodes.NONE;
|
230
|
}
|
231
|
this.disableForms = false;
|
232
|
if(this.searchUtils.status == this.errorCodes.DONE) {
|
233
|
// Page out of limit!!!
|
234
|
let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
|
235
|
if(!(Number.isInteger(totalPages))) {
|
236
|
totalPages = (parseInt(totalPages, 10) + 1);
|
237
|
}
|
238
|
if(totalPages < this.searchUtils.page) {
|
239
|
this.searchUtils.totalResults = 0;
|
240
|
this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
|
241
|
}
|
242
|
}
|
243
|
HelperFunctions.scroll();
|
244
|
}
|
245
|
|
246
|
|
247
|
/**
|
248
|
* Parse the given keywords into array and check if any of the requirements field of a community includes
|
249
|
* one of the given words.
|
250
|
*/
|
251
|
private searchForKeywords() {
|
252
|
let ret: CommunityInfo[] = [];
|
253
|
let keywords: string[] = this.searchUtils.keyword.split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/,-1);
|
254
|
for(let i = 0; i < this.results.length; i++) {
|
255
|
for(let keyword of keywords) {
|
256
|
keyword = keyword.toLowerCase();
|
257
|
if (keyword != '' && (this.results[i].title.toLowerCase().includes(keyword) || this.results[i].shortTitle.toLowerCase().includes(keyword) ||
|
258
|
this.results[i].communityId.toLowerCase().includes(keyword) || this.results[i].description.toLowerCase().includes(keyword))) {
|
259
|
ret.push(this.results[i]);
|
260
|
break;
|
261
|
}
|
262
|
}
|
263
|
}
|
264
|
this.results = ret;
|
265
|
}
|
266
|
|
267
|
/**
|
268
|
* Check the current results if they satisfy the values of each filter category and
|
269
|
* update the number of possible results in each value.
|
270
|
*
|
271
|
* @param params
|
272
|
*/
|
273
|
private checkFilters(params: Map<string, string>) {
|
274
|
let typeResults: CommunityInfo[] = this.applyFilter('type', params);
|
275
|
let statusResults: CommunityInfo[] = this.results;
|
276
|
let roleResults: CommunityInfo[] = this.results;
|
277
|
if(this.user) {
|
278
|
statusResults = this.applyFilter('status', params);
|
279
|
roleResults = this.applyFilter('role', params);
|
280
|
this.resetFilterNumbers('status');
|
281
|
this.updateFilterNumbers(typeResults.filter(value => {
|
282
|
return roleResults.includes(value);
|
283
|
}), 'status');
|
284
|
this.resetFilterNumbers('role');
|
285
|
this.updateFilterNumbers(statusResults.filter(value => {
|
286
|
return typeResults.includes(value);
|
287
|
}), 'role');
|
288
|
}
|
289
|
this.resetFilterNumbers('type');
|
290
|
this.updateFilterNumbers(statusResults.filter(value => {
|
291
|
return roleResults.includes(value);
|
292
|
}), 'type');
|
293
|
this.results = statusResults.filter(value => {
|
294
|
return typeResults.includes(value);
|
295
|
})
|
296
|
this.results = this.results.filter(value => {
|
297
|
return roleResults.includes(value);
|
298
|
});
|
299
|
}
|
300
|
|
301
|
/**
|
302
|
* Apply filter with filterId and return the results
|
303
|
*
|
304
|
* @param filterId
|
305
|
* @param params
|
306
|
*/
|
307
|
private applyFilter(filterId: string, params): CommunityInfo[] {
|
308
|
let results:CommunityInfo[] = [];
|
309
|
let values: string[] = [];
|
310
|
if(params[filterId] != undefined) {
|
311
|
values = (StringUtils.URIDecode(params[filterId])).split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/,-1);
|
312
|
}
|
313
|
if(filterId == 'type') {
|
314
|
for (let i = 0; i < this.results.length; i++) {
|
315
|
if (values.length == 0) {
|
316
|
results.push(this.results[i]);
|
317
|
} else {
|
318
|
for (let value of values) {
|
319
|
if (this.results[i].type == value.replace(/["']/g, "")) {
|
320
|
results.push(this.results[i]);
|
321
|
break;
|
322
|
}
|
323
|
}
|
324
|
}
|
325
|
}
|
326
|
}
|
327
|
else if(filterId == 'status') {
|
328
|
for (let i = 0; i < this.results.length; i++) {
|
329
|
if (values.length == 0) {
|
330
|
results.push(this.results[i]);
|
331
|
} else {
|
332
|
for (let value of values) {
|
333
|
if (value.replace(/["']/g, "") == 'subscribed') {
|
334
|
if (this.results[i].isSubscribed) {
|
335
|
results.push(this.results[i]);
|
336
|
break;
|
337
|
}
|
338
|
} else if (value.replace(/["']/g, "") == 'nonsubscribed') {
|
339
|
if (!this.results[i].isSubscribed) {
|
340
|
results.push(this.results[i]);
|
341
|
break;
|
342
|
}
|
343
|
}
|
344
|
}
|
345
|
}
|
346
|
}
|
347
|
}
|
348
|
else if(filterId == 'role') {
|
349
|
for (let i = 0; i < this.results.length; i++) {
|
350
|
if (values.length == 0) {
|
351
|
results.push(this.results[i]);
|
352
|
} else {
|
353
|
for (let value of values) {
|
354
|
if (value.replace(/["']/g, "") == 'manager') {
|
355
|
if (this.results[i].isManager) {
|
356
|
results.push(this.results[i]);
|
357
|
break;
|
358
|
}
|
359
|
}
|
360
|
}
|
361
|
}
|
362
|
}
|
363
|
}
|
364
|
return results;
|
365
|
}
|
366
|
|
367
|
/**
|
368
|
* Reset the values of filter with id filterId with zero.
|
369
|
*
|
370
|
* @param filterId
|
371
|
*/
|
372
|
private resetFilterNumbers(filterId: string) {
|
373
|
for (let i = 0; i < this.filters.length; i++) {
|
374
|
if(this.filters[i].filterId == filterId) {
|
375
|
for (let j = 0; j < this.filters[i].values.length; j++) {
|
376
|
this.filters[i].values[j].number = 0;
|
377
|
}
|
378
|
break;
|
379
|
}
|
380
|
}
|
381
|
}
|
382
|
|
383
|
/**
|
384
|
* Update the values of filter with id filterId based on
|
385
|
* results.
|
386
|
*
|
387
|
* @param results
|
388
|
* @param filterId
|
389
|
*/
|
390
|
private updateFilterNumbers(results: CommunityInfo[], filterId: string) {
|
391
|
for(let k = 0; k < results.length; k++) {
|
392
|
for (let i = 0; i < this.filters.length; i++) {
|
393
|
if(this.filters[i].filterId == filterId) {
|
394
|
if (this.filters[i].filterId == 'type') {
|
395
|
for (let j = 0; j < this.filters[i].values.length; j++) {
|
396
|
if (results[k].type == this.filters[i].values[j].id) {
|
397
|
this.filters[i].values[j].number++;
|
398
|
}
|
399
|
}
|
400
|
}
|
401
|
else if (this.filters[i].filterId == 'status') {
|
402
|
if (results[k].isSubscribed) {
|
403
|
this.filters[i].values[0].number++;
|
404
|
} else {
|
405
|
this.filters[i].values[1].number++;
|
406
|
}
|
407
|
}
|
408
|
else if (this.filters[i].filterId == 'role') {
|
409
|
if (results[k].isManager) {
|
410
|
this.filters[i].values[0].number++;
|
411
|
}
|
412
|
}
|
413
|
break;
|
414
|
}
|
415
|
}
|
416
|
}
|
417
|
}
|
418
|
|
419
|
/**
|
420
|
* Sorting results based on sortBy.
|
421
|
*/
|
422
|
private sort() {
|
423
|
if(this.searchUtils.sortBy == '') {
|
424
|
this.results.sort((left, right): number => {
|
425
|
if (left.title > right.title) {
|
426
|
return 1;
|
427
|
} else if (left.title < right.title) {
|
428
|
return -1;
|
429
|
} else {
|
430
|
return 0;
|
431
|
}
|
432
|
})
|
433
|
} else if(this.searchUtils.sortBy == 'creationdate,descending') {
|
434
|
this.results.sort((left, right): number => {
|
435
|
if (!right.date || left.date > right.date) {
|
436
|
return -1;
|
437
|
} else if (!left.date || left.date < right.date) {
|
438
|
return 1;
|
439
|
} else {
|
440
|
return 0;
|
441
|
}
|
442
|
})
|
443
|
} else if(this.searchUtils.sortBy == 'creationdate,ascending') {
|
444
|
this.results.sort((left, right): number => {
|
445
|
if (!right.date || left.date > right.date) {
|
446
|
return 1;
|
447
|
} else if (!left.date || left.date < right.date) {
|
448
|
return -1;
|
449
|
} else {
|
450
|
return 0;
|
451
|
}
|
452
|
})
|
453
|
}
|
454
|
}
|
455
|
|
456
|
|
457
|
private isCommunityManager(community: CommunityInfo): boolean {
|
458
|
return Session.isCommunityCurator(this.user) || community.managers.indexOf(this.user.email) != -1;
|
459
|
}
|
460
|
|
461
|
/**
|
462
|
* Create Search Communities filters.
|
463
|
*
|
464
|
*/
|
465
|
private createFilters(): Filter[] {
|
466
|
let filter_names = [];
|
467
|
let filter_ids = [];
|
468
|
let searchFields = new SearchFields();
|
469
|
let filter_original_ids = searchFields.COMMUNITIES_SEARCH_FIELDS;
|
470
|
let value_names = [];
|
471
|
let value_original_ids=[];
|
472
|
this.showType = this.results.filter(community => community.type === 'ri').length > 0;
|
473
|
if(this.showType) {
|
474
|
filter_names.push("Type");
|
475
|
filter_ids.push("type");
|
476
|
value_names.push([ "Research Communities", "Research Initiatives" ]);
|
477
|
value_original_ids.push(["community","ri"]);
|
478
|
} else {
|
479
|
filter_original_ids = searchFields.COMMUNITIES_SEARCH_FIELDS.splice(0, 1);
|
480
|
}
|
481
|
if(this.user) {
|
482
|
filter_names.push("Status");
|
483
|
filter_ids.push("status");
|
484
|
value_names.push([ "Subscribed", "Non-subscribed"]);
|
485
|
value_original_ids.push(["subscribed", "nonsubscribed"]);
|
486
|
filter_names.push("Role");
|
487
|
filter_ids.push("role");
|
488
|
value_names.push([ "Manager"]);
|
489
|
value_original_ids.push(["manager"]);
|
490
|
}
|
491
|
let filters: Filter[] = [];
|
492
|
for(let i =0 ; i < filter_names.length; i++){
|
493
|
let values: Value[] = [];
|
494
|
for(let j =0 ; j < value_names[i].length; j++){
|
495
|
let value: Value = {name: value_names[i][j], id: value_original_ids[i][j], number:0, selected:false};
|
496
|
values.push(value);
|
497
|
}
|
498
|
let filter: Filter = {title: filter_names[i], filterId: filter_ids[i], originalFilterId: filter_original_ids[i], values : values, countSelectedValues:0, "filterOperator": 'or', valueIsExact: true , filterType: "checkbox"};
|
499
|
filters.push(filter);
|
500
|
}
|
501
|
return filters;
|
502
|
}
|
503
|
|
504
|
private handleError(message: string, error) {
|
505
|
console.error('Communities Search Page: ' + message, error);
|
506
|
}
|
507
|
|
508
|
}
|