Project

General

Profile

1 56649 k.triantaf
import {Component, EventEmitter, Input, Output, ViewChild} from '@angular/core';
2
import {Location} from '@angular/common';
3
import {ActivatedRoute, Router} from '@angular/router';
4
import {Meta, Title} from '@angular/platform-browser';
5 50169 argiro.kok
6 56649 k.triantaf
import {AdvancedField} from '../searchUtils/searchHelperClasses.class';
7 56059 argiro.kok
import {SearchCustomFilter, SearchUtilsClass} from './searchUtils.class';
8 56649 k.triantaf
import {ModalLoading} from '../../utils/modal/loading.component';
9
import {Dates, StringUtils} from '../../utils/string-utils.class';
10
import {ErrorCodes} from '../../utils/properties/errorCodes';
11
import {RouterHelper} from '../../utils/routerHelper.class';
12 51835 sofia.balt
13 56649 k.triantaf
import {PiwikService} from '../../utils/piwik/piwik.service';
14
import {EnvProperties} from '../../utils/properties/env-properties';
15
import {SEOService} from '../../sharedComponents/SEO/SEO.service';
16
import {HelperService} from "../../utils/helper/helper.service";
17 51835 sofia.balt
18 50169 argiro.kok
@Component({
19 56649 k.triantaf
  selector: 'advanced-search-page',
20
  templateUrl: 'advancedSearchPage.component.html'
21 50169 argiro.kok
})
22
export class AdvancedSearchPageComponent {
23 51746 argiro.kok
  @Input() piwikSiteId = null;
24 56840 konstantin
  @Input() hasPrefix: boolean = true;
25 50169 argiro.kok
  @Input() pageTitle = "";
26
  @Input() results = [];
27
  @Input() type;
28
  @Input() entityType;
29 56649 k.triantaf
  @Input() searchUtils: SearchUtilsClass = new SearchUtilsClass();
30
  @Input() fieldIds: string[];
31 50169 argiro.kok
  @Input() fieldIdsMap;//:{ [key:string]:{ name:string, operator:string, type:string, indexField:string, equalityOperator:string  }} ;
32 56649 k.triantaf
  @Input() selectedFields: AdvancedField[];
33 50169 argiro.kok
  @Input() simpleSearchUrl: string;
34 56649 k.triantaf
  @ViewChild(ModalLoading) loading: ModalLoading;
35 50169 argiro.kok
  @Input() csvParams: string;
36
  @Input() csvPath: string;
37
  @Input() simpleSearchLink: string = "";
38 56649 k.triantaf
  @Input() disableForms: boolean = false;
39 50169 argiro.kok
  @Input() loadPaging: boolean = true;
40
  @Input() oldTotalResults: number = 0;
41 50586 argiro.kok
  @Input() openaireLink: string;
42 56649 k.triantaf
  @Input() customFilter: SearchCustomFilter = null;
43 53937 konstantin
  @Input() sort: boolean = false;
44 53353 argiro.kok
  @Input() searchFormClass: string = "searchForm";
45 50169 argiro.kok
  piwiksub: any;
46 56649 k.triantaf
  public parameterNames: string[] = [];
47
  public parameterValues: string[] = [];
48 50169 argiro.kok
49 56649 k.triantaf
  public baseURLWithParameters: string = '';
50 50169 argiro.kok
51 50586 argiro.kok
  public csvLimit: number = 0;
52
  public pagingLimit: number = 0;
53
  public resultsPerPage: number = 0;
54
  isPiwikEnabled = false;
55 56649 k.triantaf
  properties: EnvProperties;
56
  public pageContents = null;
57
  public divContents = null;
58
  public routerHelper: RouterHelper = new RouterHelper();
59
  public errorCodes: ErrorCodes = new ErrorCodes();
60 54075 konstantin
61 53740 argiro.kok
  url = null;
62 50169 argiro.kok
63 56649 k.triantaf
  constructor(private route: ActivatedRoute,
64
              private location: Location,
65
              private _meta: Meta,
66
              private _title: Title,
67
              private _piwikService: PiwikService,
68
              private router: Router,
69
              private seoService: SEOService,
70
              private helper: HelperService) {
71
  }
72
73 50169 argiro.kok
  ngOnInit() {
74 50586 argiro.kok
    this.route.data
75
      .subscribe((data: { envSpecific: EnvProperties }) => {
76
        this.properties = data.envSpecific;
77 56649 k.triantaf
        //this.getDivContents();
78
        this.getPageContents();
79 50586 argiro.kok
        this.pagingLimit = data.envSpecific.pagingLimit;
80 56649 k.triantaf
        this.resultsPerPage = data.envSpecific.resultsPerPage;
81 50586 argiro.kok
        this.csvLimit = data.envSpecific.csvLimit;
82
        this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
83 56649 k.triantaf
        if (typeof window !== 'undefined') {
84
          this.updateUrl(data.envSpecific.baseLink + location.pathname);
85
          this.url = data.envSpecific.baseLink + location.pathname
86 50586 argiro.kok
        }
87 56649 k.triantaf
        if (typeof document !== 'undefined' && this.isPiwikEnabled) {
88 51746 argiro.kok
          this.piwiksub = this._piwikService.trackView(this.properties, this.pageTitle, this.piwikSiteId).subscribe();
89 50586 argiro.kok
        }
90
      });
91
92 56649 k.triantaf
    var description = "Openaire, search, repositories, open access, type, content provider, funder, project, " + this.pageTitle;
93 50586 argiro.kok
94 56840 konstantin
    this.updateTitle(this.pageTitle);
95 51835 sofia.balt
    this.updateDescription(description);
96 50169 argiro.kok
97 51835 sofia.balt
    this.searchUtils.baseUrl = "/" + this.searchUtils.baseUrl;
98 53740 argiro.kok
99 51835 sofia.balt
    this.updateBaseUrlWithParameters();
100 56649 k.triantaf
    this.seoService.createLinkForCanonicalURL(this.properties.baseLink + this.router.url, false);
101 50586 argiro.kok
102 53740 argiro.kok
103 50169 argiro.kok
  }
104
105 56649 k.triantaf
  private getPageContents() {
106 58417 konstantin
    this.helper.getPageHelpContents(this.properties, (this.customFilter)?this.customFilter.valueId:null, this.router.url).subscribe(contents => {
107 56649 k.triantaf
108
      this.pageContents = contents;
109
    })
110
  }
111
112
  private getDivContents() {
113 58417 konstantin
    this.helper.getDivHelpContents(this.properties, (this.customFilter)?this.customFilter.valueId:null, this.router.url).subscribe(contents => {
114 56649 k.triantaf
      this.divContents = contents;
115
    })
116
  }
117
118 50169 argiro.kok
  ngOnDestroy() {
119 56649 k.triantaf
    if (this.piwiksub) {
120 51835 sofia.balt
      this.piwiksub.unsubscribe();
121
    }
122 50169 argiro.kok
  }
123 56649 k.triantaf
124
  updateDescription(description: string) {
125
    this._meta.updateTag({content: description}, "name='description'");
126
    this._meta.updateTag({content: description}, "property='og:description'");
127 50169 argiro.kok
  }
128 56649 k.triantaf
129
  updateTitle(title: string) {
130 56840 konstantin
    var _prefix = "";
131
    if(this.hasPrefix) {
132
      _prefix = "OpenAIRE | ";
133
    }
134 56649 k.triantaf
    var _title = _prefix + ((title.length > 50) ? title.substring(0, 50) : title);
135 51835 sofia.balt
    this._title.setTitle(_title);
136 56649 k.triantaf
    this._meta.updateTag({content: _title}, "property='og:title'");
137 50169 argiro.kok
  }
138 56649 k.triantaf
139
  updateUrl(url: string) {
140
    this._meta.updateTag({content: url}, "property='og:url'");
141 50169 argiro.kok
  }
142
143 51835 sofia.balt
  public getSelectedFiltersFromUrl(params) {
144 56649 k.triantaf
    for (var i = 0; i < this.fieldIds.length; i++) {
145 50169 argiro.kok
146 56649 k.triantaf
      var fieldId = this.fieldIds[i];
147
      var fieldparam = (this.fieldIdsMap[fieldId]) ? this.fieldIdsMap[fieldId].param : "";
148
      if (!this.fieldIdsMap[fieldId]) {
149 50169 argiro.kok
150 56649 k.triantaf
        console.error("Field: " + fieldId + " not found in fieldIds map");
151
      }
152 50169 argiro.kok
153 56649 k.triantaf
      var operatorId = this.getOperatorParameter(fieldparam);
154
      if (params[fieldparam] != undefined && params[operatorId] != undefined) {
155
        var values: string [] = StringUtils.URIDecode(params[fieldparam]).split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/, -1);
156
        var operators: string [] = (StringUtils.URIDecode(params[operatorId])).split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/, -1);
157
        if (values.length == operators.length) {
158
          for (var j = 0; j < values.length; j++) {
159
            if (this.fieldIdsMap[fieldId].type == "date") {
160
              var value: string = StringUtils.unquote(values[j]);
161
              var validDates: boolean = true;
162
              var dateField: AdvancedField = new AdvancedField(fieldId, fieldparam, this.fieldIdsMap[fieldId].name, this.fieldIdsMap[fieldId].type, value, operators[j]);
163
              if (value.indexOf("range") != -1) {
164
                dateField.dateValue.type = "range";
165
                if (value.length < 26) {
166
                  validDates = false;
167
                } else {
168
                  if (!Dates.isValidDate(value.substring(5, 15)) || !Dates.isValidDate(value.substring(16, 26))) {
169
                    validDates = false;
170
                  } else {
171
                    dateField.dateValue.from = Dates.getDateFromString(value.substring(5, 15));
172
                    dateField.dateValue.to = Dates.getDateFromString(value.substring(16, 26));
173
                  }
174
                }
175
                //  "rangeYYYY-MM-DD:YYYY-MM-DD"
176
              } else {
177
                dateField.dateValue.setDatesByType(value);
178
              }
179
              if (validDates) {
180
                this.selectedFields.push(dateField);
181
              }
182 50169 argiro.kok
183 56649 k.triantaf
            } else {
184
              this.selectedFields.push(new AdvancedField(fieldId, fieldparam, this.fieldIdsMap[fieldId].name, this.fieldIdsMap[fieldId].type, StringUtils.unquote(values[j]), operators[j]));
185
            }
186
          }
187
        }
188
      }
189
    }
190
    if (this.selectedFields.length == 0) {
191
      this.selectedFields.push(new AdvancedField(this.fieldIds[0], fieldparam, this.fieldIdsMap[this.fieldIds[0]].name, this.fieldIdsMap[this.fieldIds[0]].type, "", "and"));
192
    }
193 50169 argiro.kok
  }
194 56649 k.triantaf
195 58030 argiro.kok
  /**
196
   * Create Page URL parameters based on the selected fields of the advanced form
197
   * @param includePage
198
   */
199 56649 k.triantaf
  private createUrlParameters(includePage: boolean) {
200
    var params = "";
201
    this.parameterNames.splice(0, this.parameterNames.length);
202
    this.parameterValues.splice(0, this.parameterValues.length);
203
    var fields: { [key: string]: { values: string[], operators: string[] } } = {};
204
    for (var i = 0; i < this.selectedFields.length; i++) {
205
      if (this.fieldIdsMap[this.selectedFields[i].id] != undefined && (this.selectedFields[i].value.length > 0 || this.selectedFields[i].type == "date")) {
206
        if (!fields[this.selectedFields[i].id]) {
207
          fields[this.selectedFields[i].id] = {values: [], operators: []};
208
          fields[this.selectedFields[i].id].values = [];
209
          fields[this.selectedFields[i].id].operators = [];
210 50169 argiro.kok
        }
211 56649 k.triantaf
        if (this.selectedFields[i].type == "date") {
212
          if (this.selectedFields[i].dateValue.type == "range") {
213
            fields[this.selectedFields[i].id].values.push(StringUtils.quote(StringUtils.URIEncode("range" + Dates.getDateToString(this.selectedFields[i].dateValue.from) + ":" + Dates.getDateToString(this.selectedFields[i].dateValue.to))));
214
          } else {
215
            fields[this.selectedFields[i].id].values.push(StringUtils.quote(StringUtils.URIEncode(this.selectedFields[i].dateValue.type)));
216 50169 argiro.kok
          }
217 56649 k.triantaf
        } else {
218 50169 argiro.kok
          fields[this.selectedFields[i].id].values.push(StringUtils.quote(StringUtils.URIEncode(this.selectedFields[i].value)));
219
        }
220
        fields[this.selectedFields[i].id].operators.push(this.selectedFields[i].operatorId);
221
222
      }
223
    }
224 56649 k.triantaf
    for (var i = 0; i < this.fieldIds.length; i++) {
225
      if (fields[this.fieldIds[i]]) {
226 50169 argiro.kok
227 56649 k.triantaf
        params += "&" + this.fieldIdsMap[this.fieldIds[i]].param + "=" + fields[this.fieldIds[i]].values.join() +
228
          "&" + this.fieldIdsMap[this.fieldIds[i]].operator + "=" + fields[this.fieldIds[i]].operators.join()
229 50169 argiro.kok
        this.parameterNames.push(this.fieldIdsMap[this.fieldIds[i]].param);
230
        this.parameterValues.push(fields[this.fieldIds[i]].values.join());
231 56649 k.triantaf
        this.parameterNames.push(this.fieldIdsMap[this.fieldIds[i]].operator);
232 50169 argiro.kok
        this.parameterValues.push(fields[this.fieldIds[i]].operators.join());
233
      }
234
    }
235 56649 k.triantaf
    if (includePage && this.searchUtils.page != 1) {
236
      params += "&page=" + this.searchUtils.page;
237 50169 argiro.kok
    }
238 53919 konstantin
239 56649 k.triantaf
    if (this.searchUtils.size != 10) {
240
      params += ((params.length == 0) ? '' : '&') + 'size=' + this.searchUtils.size;
241 53919 konstantin
      this.parameterNames.push("size");
242 56649 k.triantaf
      this.parameterValues.push("" + this.searchUtils.size);
243 53919 konstantin
    }
244
245 56649 k.triantaf
    if (this.sort && this.searchUtils.sortBy) {
246
      params += ((params.length == 0) ? '' : '&') + 'sortBy=' + this.searchUtils.sortBy;
247 53937 konstantin
      this.parameterNames.push("sortBy");
248
      this.parameterValues.push(this.searchUtils.sortBy);
249
    }
250
251 56649 k.triantaf
    return '?' + params;
252 50169 argiro.kok
  }
253 56649 k.triantaf
254 58030 argiro.kok
  /**
255
   * Create Search API query based on the selected fields of the advanced form
256
   */
257 56649 k.triantaf
  public createQueryParameters() {
258
    var params = "";
259 50169 argiro.kok
    var countParams = 0;
260 56649 k.triantaf
    for (var i = 0; i < this.selectedFields.length; i++) {
261
      if (this.fieldIdsMap[this.selectedFields[i].id] != undefined && (this.selectedFields[i].value != "" || this.selectedFields[i].type == "date")) {
262 54775 konstantin
        //console.log("createQueryParameters::"+this.selectedFields[i].type);
263 56649 k.triantaf
        if (this.selectedFields[i].type == "date") {
264
          if (this.selectedFields[i].dateValue.type != "any") {
265
            params += (countParams == 0 ? "" : this.selectedFields[i].operatorId) + " " + this.selectedFields[i].id + this.fieldIdsMap[this.selectedFields[i].id].equalityOperator + '"' + StringUtils.URIEncode(Dates.getDateToString(this.selectedFields[i].dateValue.from)) + " "
266
              + StringUtils.URIEncode(Dates.getDateToString(this.selectedFields[i].dateValue.to)) + '"' + " ";
267 50169 argiro.kok
          }
268 56649 k.triantaf
        } else {
269
          if (this.selectedFields[i].id == "q") {
270 50169 argiro.kok
            var op = "";
271
            // if()
272
            params += (countParams == 0 ? "" : this.selectedFields[i].operatorId) + " " + '"' + StringUtils.URIEncode(this.selectedFields[i].value) + '"' + " ";
273 56649 k.triantaf
          } else if (countParams == 0 && this.selectedFields[i].operatorId == "not") {
274
            params += " " + this.selectedFields[i].id + " <> " + '"' + StringUtils.URIEncode(this.selectedFields[i].value) + '"' + " ";
275
          } else {
276
            params += (countParams == 0 ? "" : this.selectedFields[i].operatorId + " ") + this.selectedFields[i].id + this.fieldIdsMap[this.selectedFields[i].id].equalityOperator + '"' + encodeURIComponent(this.selectedFields[i].value) + '"' + " ";
277 50169 argiro.kok
278
          }
279 56649 k.triantaf
        }
280 50169 argiro.kok
        countParams++;
281
      }
282
    }
283 52027 argiro.kok
284 56649 k.triantaf
    if (this.customFilter) {
285
      params += (countParams == 0 ? "" : " and ") + this.customFilter.queryFieldName + " exact " + StringUtils.quote((this.customFilter.valueId));
286 56059 argiro.kok
    }
287 56649 k.triantaf
    return params;
288 50169 argiro.kok
289
  }
290 56649 k.triantaf
291
  goTo(page: number = 1) {
292
    this.searchUtils.page = page;
293 58030 argiro.kok
    this.createUrlParameters(true);
294 56649 k.triantaf
    this.router.navigate([this.searchUtils.baseUrl], {queryParams: this.routerHelper.createQueryParams(this.parameterNames, this.parameterValues)});
295 50169 argiro.kok
    /* Code For Piwik*/
296 56649 k.triantaf
    if (typeof localStorage !== 'undefined') {
297
      //console.log("In PreviousRouteRecorder : "+this.router.url );
298
      localStorage.setItem('previousRoute', this.router.url);
299
    }
300
    if (this.isPiwikEnabled && (typeof document !== 'undefined')) {
301
      this.piwiksub = this._piwikService.trackView(this.properties, this.pageTitle, this.piwikSiteId).subscribe();
302
    }
303
    /* End Piwik Code */
304 50169 argiro.kok
  }
305
306
  queryChanged($event) {
307
308 56649 k.triantaf
    this.goTo(1);
309 50169 argiro.kok
  }
310 56649 k.triantaf
311 50169 argiro.kok
  pageChanged($event) {
312
    this.searchUtils.page = +$event.value;
313
    this.goTo(this.searchUtils.page);
314
  }
315 56649 k.triantaf
316 53919 konstantin
  sizeChanged($event) {
317 56649 k.triantaf
    this.searchUtils.size = $event.value;
318
    this.goTo(1);
319 53919 konstantin
  }
320 56649 k.triantaf
321 53937 konstantin
  sortByChanged($event) {
322 56649 k.triantaf
    this.searchUtils.sortBy = $event.value;
323
    this.goTo(1);
324 53937 konstantin
  }
325
326 50169 argiro.kok
  /*
327
  * Update the url with proper parameters. This is used as base url in Paging Component
328
  */
329 56649 k.triantaf
  public updateBaseUrlWithParameters() {
330 50169 argiro.kok
    this.baseURLWithParameters = this.searchUtils.baseUrl + this.createUrlParameters(false);
331 56649 k.triantaf
  }
332
333
  getOperatorParameter(parameter: string): string {
334
    for (let id of this.fieldIds) {
335
      if (this.fieldIdsMap[id]["param"] == parameter) {
336
        return this.fieldIdsMap[id]["operator"];
337
      }
338
    }
339
  }
340
341
  // for loading
342
  public openLoading() {
343
    this.loading.open();
344
  }
345
346
  public closeLoading() {
347
    this.loading.close();
348
  }
349 50169 argiro.kok
}