/** * Created by stefania on 7/13/17. */ import { Injectable } from '@angular/core'; import {HttpClient, HttpErrorResponse} from "@angular/common/http"; import { Observable } from 'rxjs'; import { Page } from "../utils/entities/adminTool/page"; import { PageHelpContent } from "../utils/entities/adminTool/page-help-content"; import { Portal } from "../utils/entities/adminTool/portal"; import { Entity } from "../utils/entities/adminTool/entity"; import { DivId } from "../utils/entities/adminTool/divId"; import { DivHelpContent } from "../utils/entities/adminTool/div-help-content"; import {StatisticsDisplay, StatisticsSummary} from '../connect/statistics/statisticsEntities'; import { CustomOptions } from './servicesUtils/customOptions.class'; import {catchError, map} from "rxjs/operators"; import {properties} from "../../../environments/environment"; @Injectable() export class HelpContentService { constructor(private http:HttpClient) { } static removeNulls(obj){ var isArray = obj instanceof Array; for (var k in obj){ if (obj[k]===null || obj[k]==='') isArray ? obj.splice(k,1) : delete obj[k]; else if (typeof obj[k]=="object") HelpContentService.removeNulls(obj[k]); } } getEntities(helpContentUrl:string) { return this.http.get>(helpContentUrl + 'entity') //.map(res => > res.json()) .pipe(catchError(this.handleError)); } saveEntity(entity: Entity, helpContentUrl:string) { HelpContentService.removeNulls(entity); return this.http.post(helpContentUrl + 'entity/save', JSON.stringify(entity), CustomOptions.getAuthOptionsWithBody()) //.map(res => res.json()) .pipe(catchError(this.handleError)); } updateEntity(entity: Entity, helpContentUrl:string) { HelpContentService.removeNulls(entity); return this.http.post(helpContentUrl + 'entity/update', JSON.stringify(entity), CustomOptions.getAuthOptionsWithBody()) //.map(res => res.json()) .pipe(catchError(this.handleError)); } deleteEntities(ids : string[], helpContentUrl:string) { return this.http.post(helpContentUrl + 'entity/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody()) .pipe(catchError(this.handleError)); } // toggleEntity(selectedCommunityId: string, id : string,status : boolean) { // let headers = new Headers({'Content-Type': 'application/json'}); // let options = new RequestOptions({headers: headers}); // // return this.http.post(helpContentUrl + 'community/'+selectedCommunityId+'/entity/toggle?status='+ status.toString()+'&entityId='+id.toString(), options) // .catch(this.handleError); // } getCommunityEntities(pid: string, helpContentUrl:string) { return this.http.get>(helpContentUrl + properties.adminToolsPortalType + '/'+pid+'/entities') //.map(res => > res.json()) .pipe(catchError(this.handleError)); } toggleEntities(pid: string, ids : string[],status : boolean, helpContentUrl:string) { return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/'+pid+ '/entity/toggle?status='+ status.toString(), JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody()) //.map( res => res.json()) .pipe(catchError(this.handleError)); } // unused toggleEntityOfPage(pageId: string, entityId : string,status : boolean, helpContentUrl:string) { return this.http.post(helpContentUrl + 'page/'+pageId+'/entity/toggle?status='+ status.toString()+'&entityId='+entityId.toString(), CustomOptions.getAuthOptionsWithBody()) .pipe(catchError(this.handleError)); } // getDivIdsFull(page_id: string, helpContentUrl:string, pid: string = null) { // let parameters: string = ""; // if(page_id || pid) { // parameters = "?"; // if(page_id) { // parameters += "&page="+page_id; // } // if(pid) { // parameters += "&portal="+pid; // } // } // // return this.http.get>(helpContentUrl + 'div/full'+parameters) // //.map(res => > res.json()) // .pipe(catchError(this.handleError)); // } // Replacing getDivIdsFull getAllDivIdsFull(helpContentUrl:string) { return this.http.get>(helpContentUrl + 'div/full') //.map(res => > res.json()) .pipe(catchError(this.handleError)); } getDivIdsFullByPortal(page_id: string, helpContentUrl:string, pid: string) { let parameters: string = page_id ? "?&page="+page_id : ""; return this.http.get>(helpContentUrl + properties.adminToolsPortalType + '/'+pid + '/div/full'+parameters) .pipe(catchError(this.handleError)); } // End of replacing getDivIdsFull // unused getDivId(divId: string, helpContentUrl:string) { return this.http.get(helpContentUrl + 'div/'+divId) //.map(res => res.json()) .pipe(catchError(this.handleError)); } getDivIdFull(divId: string, helpContentUrl:string, pid: string) { return this.http.get(helpContentUrl + properties.adminToolsPortalType + "/" + pid + '/div/'+divId+"/full") //.map(res => res.json()) .pipe(catchError(this.handleError)); } updateDivId(divId: DivId, helpContentUrl:string) { HelpContentService.removeNulls(divId); return this.http.post(helpContentUrl + 'div/update', JSON.stringify(divId), CustomOptions.getAuthOptionsWithBody()) //.map(res => res.json()) .pipe(catchError(this.handleError)); } saveDivId(divId: DivId, helpContentUrl:string) { HelpContentService.removeNulls(divId); return this.http.post(helpContentUrl + 'div/save', JSON.stringify(divId), CustomOptions.getAuthOptionsWithBody()) //.map(res => res.json()) .pipe(catchError(this.handleError)); } deleteDivIds(ids : string[], helpContentUrl:string) { return this.http.post(helpContentUrl + 'div/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody()) .pipe(catchError(this.handleError)); } getPageIdsFromDivIds(pid: string, helpContentUrl:string) { //let parameters = (pid ? "?portal="+pid : ""); return this.http.get>(helpContentUrl + properties.adminToolsPortalType + "/" + pid + '/div/pages') //.map(res => >> res.json()) .pipe(catchError(this.handleError)); } /* getCommunitiesWithDivId(helpContentUrl:string) { return this.http.get(helpContentUrl + 'community?div=true') .map(res => > res.json()) .catch(this.handleError); } */ getCommunityDivHelpContents(pid: string, helpContentUrl:string) { return this.http.get>(helpContentUrl + properties.adminToolsPortalType + "/" + pid + '/divhelpcontent') //.map(res => > res.json()) .pipe(catchError(this.handleError)); } getDivHelpContent(id : string, helpContentUrl:string, pid: string) { return this.http.get(helpContentUrl + properties.adminToolsPortalType + "/" + pid + '/divhelpcontent/' + id) //.map(res => res.json()) .pipe(catchError(this.handleError)); } // unused insertOrUpdateDivHelpContent(divHelpContent: DivHelpContent, helpContentUrl:string, pid: string) { HelpContentService.removeNulls(divHelpContent); return this.http.post(helpContentUrl + properties.adminToolsPortalType + "/" + pid + '/divhelpcontent', JSON.stringify(divHelpContent), CustomOptions.getAuthOptionsWithBody()) //.map(res => res.json()) .pipe(catchError(this.handleError)); } saveDivHelpContent(divHelpContent: DivHelpContent, helpContentUrl:string, pid: string) { HelpContentService.removeNulls(divHelpContent); return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/divhelpcontent/save', JSON.stringify(divHelpContent), CustomOptions.getAuthOptionsWithBody()) .pipe(catchError(this.handleError)); } updateDivHelpContent(divHelpContent: DivHelpContent, helpContentUrl:string, pid: string) { HelpContentService.removeNulls(divHelpContent); return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/' + pid +'/divhelpcontent/update', JSON.stringify(divHelpContent), CustomOptions.getAuthOptionsWithBody()) .pipe(catchError(this.handleError)); } deleteDivHelpContents(ids : string[], helpContentUrl:string, pid: string) { return this.http.post(helpContentUrl + properties.adminToolsPortalType + "/" + pid + '/divhelpcontent/delete', JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody()) .pipe(catchError(this.handleError)); } toggleDivHelpContents(ids : string[],status : boolean, helpContentUrl:string, pid: string) { return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/divhelpcontent/toggle?status='+ status.toString(), JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody()) //.map( res => res.json()) .pipe(catchError(this.handleError)); } // unused getPageHelpContents(helpContentUrl:string) { return this.http.get>(helpContentUrl + 'pagehelpcontent') //.map(res => > res.json()) .pipe(catchError(this.handleError)); } getCommunityPageHelpContents(pid: string, helpContentUrl:string) { return this.http.get>(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/pagehelpcontent') //.map(res => > res.json()) .pipe(catchError(this.handleError)); } getPageHelpContent(id : string, helpContentUrl:string, pid: string) { return this.http.get(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/pagehelpcontent/' + id) //.map(res => res.json()) .pipe(catchError(this.handleError)); } savePageHelpContent(pageHelpContent: PageHelpContent, helpContentUrl:string, pid: string) { HelpContentService.removeNulls(pageHelpContent); return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/pagehelpcontent/' + (pageHelpContent._id ? 'update' : 'save'), JSON.stringify(pageHelpContent), CustomOptions.getAuthOptionsWithBody()) //.map(res => res.json()) .pipe(catchError(this.handleError)); } updatePageHelpContent(pageHelpContent: PageHelpContent, helpContentUrl:string, pid: string) { HelpContentService.removeNulls(pageHelpContent); return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/' + pid +'/pagehelpcontent/update', JSON.stringify(pageHelpContent), CustomOptions.getAuthOptionsWithBody()) //.map(res => res.json()) .pipe(catchError(this.handleError)); } deletePageHelpContents(ids : string[], helpContentUrl:string, pid: string) { return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/pagehelpcontent/delete', JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody()) .pipe(catchError(this.handleError)); } togglePageHelpContents(ids : string[],status : boolean, helpContentUrl:string, pid: string) { return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/pagehelpcontent/toggle?status='+ status.toString(), JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody()) //.map( res => res.json()) .pipe(catchError(this.handleError)); } getCommunityPagesWithDivId(pid: string, helpContentUrl:string) { return this.http.get>(helpContentUrl + properties.adminToolsPortalType + '/'+pid+'/pages?div=true') //.map(res => > res.json()) .pipe(catchError(this.handleError)); } // getCommunityPages(pid: string, params: string, helpContentUrl:string) { // return this.http.get>(helpContentUrl + properties.adminToolsPortalType + '/'+pid+'/pages'+params, CustomOptions.getAuthOptions()) // //.map(res => > res.json()) // .pipe(catchError(this.handleError)); // } // Replacing getCommunityPages getCommunityPagesByType(pid: string, type: string, helpContentUrl:string) { return this.http.get>(helpContentUrl + properties.adminToolsPortalType + '/'+pid+'/pages' + (type ? '?page_type='+type : '')) //.map(res => > res.json()) .pipe(catchError(this.handleError)); } // End of replacing getCommunityPages // Replacing part of getPages (now getAllPages) getCommunityPagesWithPositions(pid: string, helpContentUrl:string) { return this.http.get>(helpContentUrl + properties.adminToolsPortalType + '/'+pid+'/pages?with_positions=true') //.map(res => > res.json()) .pipe(catchError(this.handleError)); } // End of replacing part of getPages (now getAllPages) getAllPages(helpContentUrl:string) {//,pid:string,with_positions:boolean=null) { // let parameters: string = ""; // if(pid || with_positions == true || with_positions == false) { // parameters = "?"; // if(pid) { // parameters += "&pid="+pid; // } // if(with_positions == true || with_positions == false) { // parameters += "&with_positions="+with_positions; // } // } return this.http.get>(helpContentUrl + 'page') //.map(res => > res.json()) .pipe(catchError(this.handleError)); } getAllPagesFull(helpContentUrl:string) { return this.http.get>(helpContentUrl + 'page/full')//+(pid?("?pid="+pid):"")) //.map(res => > res.json()) .pipe(catchError(this.handleError)); } getPageByPortal(pageId:string, helpContentUrl:string, pid: string) { return this.http.get(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/page/'+pageId) //.map(res => res.json()) .pipe(catchError(this.handleError)); } getCommunityPageByRoute(route:string, helpContentUrl:string, pid: string) { return this.http.get(helpContentUrl + properties.adminToolsPortalType +'/' + pid + '/page/?page_route='+route) .pipe(catchError(this.handleError)); } savePage(page: Page, helpContentUrl:string) { HelpContentService.removeNulls(page); return this.http.post(helpContentUrl + 'page/save', JSON.stringify(page), CustomOptions.getAuthOptionsWithBody()) //.map(res => res.json()) .pipe(catchError(this.handleError)); } updatePage(page: Page, helpContentUrl:string) { HelpContentService.removeNulls(page); return this.http.post(helpContentUrl + 'page/update', JSON.stringify(page), CustomOptions.getAuthOptionsWithBody()) //.map(res => res.json()) .pipe(catchError(this.handleError)); } togglePages(selectedPortalPid: string, ids : string[],status : boolean, helpContentUrl:string) { return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/'+selectedPortalPid+'/page/toggle?status='+ status.toString(), JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody()) .pipe(catchError(this.handleError)); } deletePages(ids : string[], helpContentUrl:string) { return this.http.post(helpContentUrl + 'page/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody()) .pipe(catchError(this.handleError)); } // unused getCommunities( helpContentUrl:string) { return this.http.get>(helpContentUrl + properties.adminToolsPortalType) //.map(res => > res.json()) .pipe(catchError(this.handleError)); } getCommunity(pid: string, helpContentUrl:string) { return this.http.get(helpContentUrl + properties.adminToolsPortalType + '/'+pid) //.map(res => res.json()) .pipe(catchError(this.handleError)); } getCommunitiesFull( helpContentUrl:string) { return this.http.get>(helpContentUrl + properties.adminToolsPortalType + '/full') //.map(res => > res.json()) .pipe(catchError(this.handleError)); } getPortalsFull( helpContentUrl:string) { return this.http.get>(helpContentUrl + 'portal/full') //.map(res => > res.json()) .pipe(catchError(this.handleError)); } getCommunityFull(portal_pid: string, helpContentUrl:string) { return this.http.get(helpContentUrl + properties.adminToolsPortalType + '/'+portal_pid+'/full') //.map(res => res.json()) .pipe(catchError(this.handleError)); } saveCommunity(portal: Portal, helpContentUrl:string) { // let headers = new Headers({'Content-Type': 'application/json'}); // let options = new RequestOptions({headers: headers}); HelpContentService.removeNulls(portal); return this.http.post(helpContentUrl + portal.type + '/save', JSON.stringify(portal), CustomOptions.getAuthOptionsWithBody()) //.map(res => res.json()) .pipe(catchError(this.handleError)); } updateCommunity(portal: Portal, helpContentUrl:string) { // let headers = new Headers({'Content-Type': 'application/json'}); // let options = new RequestOptions({headers: headers}); HelpContentService.removeNulls(portal); return this.http.post(helpContentUrl + portal.type + '/update', JSON.stringify(portal), CustomOptions.getAuthOptionsWithBody()) //.map(res => res.json()) .pipe(catchError(this.handleError)); } deleteCommunities(ids : string[], helpContentUrl:string) { // let headers = new Headers({'Content-Type': 'application/json'}); // let options = new RequestOptions({headers: headers}); return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody()) .pipe(catchError(this.handleError)); } private handleError(error: HttpErrorResponse) { // in a real world app, we may send the error to some remote logging infrastructure // instead of just logging it to the console console.error(error); return Observable.throw(error.error || 'Server error'); } // getDataProviders() { // return this.http.get('https://beta.services.openaire.eu/search/v2/api/datasources?format=json').map(res => res.json()).map(res => res.results).do(res => {console.log(res)}).catch(this.handleError); // } getCommunityStatistics(apiUrl: string, portalId: string): Observable { const url = `${apiUrl}communities/${portalId}`; //console.log(`getting statistics summary from: ${url}`); return this.http.get(url) //.map(res => res.json()) .pipe(map(res => res['statistics'])); } getCommunityAdminStatisticsChoices(apiUrl: string, portalId: string): Observable { const url = `${apiUrl}statistics/${portalId}`; //console.log(`getting admin choices for statistics from: ${url}`); return this.http.get(url) //.map(stats => stats.json()) .pipe(catchError(this.handleError)); } postCommunityAdminStatisticsChoices(apiUrl: string, portalId: string, entity: string, chartsOrNumbers: string, title: string, status: boolean, monitor: boolean): Observable { const url = `${apiUrl}statistics/${portalId}/${entity}/${chartsOrNumbers}?status=${status.toString()}&monitor=${monitor.toString()}`; //console.log(`getting admin choices for statistics from: ${url}`); return this.http.post(url, title, CustomOptions.getAuthOptionsWithBody()) //.map(stats => stats.json()) .pipe(catchError(this.handleError)); } statisticsIsActiveToggle(apiURL: string, id: string): Observable { const url = apiURL + '/statistics/' + encodeURIComponent(id) + '/toggle'; return this.http.post(url, {}, CustomOptions.getAuthOptionsWithBody()).pipe(catchError(this.handleError)); } }