111 lines
3.8 KiB
C#
111 lines
3.8 KiB
C#
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using gehGassi.Core.Interfaces;
|
|
using gehGassi.Domain.Placeholders;
|
|
using gehGassi.Domain.Users;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace gehGassi.Core.Services
|
|
{
|
|
/// <summary>
|
|
/// Service der Funktionen rund um die Variablen/Platzhalter bietet
|
|
/// </summary>
|
|
public class PlaceHolderService : IPlaceHolderService
|
|
{
|
|
private readonly string _directory;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="directory">Pfad in welchem die Platzhalter-Dateien liegen</param>
|
|
public PlaceHolderService(string directory)
|
|
{
|
|
_directory = directory;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt die Liste der verfügbaren Platzhalter-Kategorien zurück
|
|
/// </summary>
|
|
/// <param name="lang">Sprache ISO2</param>
|
|
/// <returns>Liste Kategorien</returns>
|
|
public List<PlaceHolderCategory> GetCategories(string lang)
|
|
{
|
|
var list = Load(lang);
|
|
return list;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt die Liste der Platzhalter-Variablen für eine Kategorie zurück
|
|
/// </summary>
|
|
/// <param name="categoryId">Id der Kategorie</param>
|
|
/// <param name="lang">Sprache ISO2</param>
|
|
/// <returns>Liste Platzhalter</returns>
|
|
public List<PlaceHolder> GetPlaceholders(int categoryId, string lang)
|
|
{
|
|
var list = new List<PlaceHolder>();
|
|
|
|
var categories = Load(lang);
|
|
if (categories == null || !categories.Any()) return list;
|
|
|
|
var category = categories.FirstOrDefault(c => c.Id == categoryId);
|
|
if (category != null)
|
|
list = category.PlaceHolders.ToList();
|
|
|
|
return list;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ersetzt in einem Text alle Platzhalter mit den entsprechenden Daten, sofern diese zur Verfügung gestellt wurden.
|
|
/// </summary>
|
|
/// <param name="content">Text in welchem die Platzhalter ausgefüllt werden sollen</param>
|
|
/// <param name="lang">Sprache ISO2</param>
|
|
/// <param name="timezoneId">Id der Timezone für Datums-Operationen</param>
|
|
/// <param name="useHtml">Soll bei manchen Ersatzoperationen HTML verwendet werden? Z.B. Adress-Block</param>
|
|
/// <param name="tenant">Optional: Mandant</param>
|
|
/// <param name="user">Optional: Benutzer</param>
|
|
/// <param name="course">Optional: Kurs</param>
|
|
/// <param name="student">Optional: Student</param>
|
|
/// <returns>Anzahl ersetzer Platzhalter. Der Text ist als ref Parameter bearbeitet</returns>
|
|
public int FillIn(ref string content, string lang, string timezoneId, bool useHtml = false, ApplicationUser user = null)
|
|
{
|
|
var replaceCount = 0;
|
|
|
|
return replaceCount;
|
|
}
|
|
|
|
#region Private
|
|
|
|
/// <summary>
|
|
/// Laden der Datei mit den Paltzhaltern
|
|
/// </summary>
|
|
/// <param name="lang"></param>
|
|
/// <returns></returns>
|
|
private List<PlaceHolderCategory> Load(string lang)
|
|
{
|
|
var list = new List<PlaceHolderCategory>();
|
|
|
|
//Prüfen ob das Verzeichnis existiert
|
|
if (!Directory.Exists(_directory))
|
|
Directory.CreateDirectory(_directory);
|
|
|
|
try
|
|
{
|
|
var filename = Path.Combine(_directory, $"placeholders_{lang}.json");
|
|
if (File.Exists(filename))
|
|
{
|
|
list = JsonConvert.DeserializeObject<PlaceHolderCategory[]>(File.ReadAllText(filename)).ToList();
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// ignored
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|