diff --git a/.gitignore b/.gitignore index 5112fe00..1122c142 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ config/userConfig.json CarCareTracker.csproj.user Properties/launchSettings.json data/cartracker-log.db +data/widgets.html diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index b0861108..a210d0f7 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -523,6 +523,27 @@ public ActionResult GetVehicleSelector(int vehicleId) } return PartialView("_VehicleSelector", vehiclesStored); } + [Authorize(Roles = nameof(UserData.IsRootUser))] + [HttpGet] + public IActionResult GetCustomWidgetEditor() + { + var customWidgetData = _fileHelper.GetWidgets(); + return PartialView("_WidgetEditor", customWidgetData); + } + [Authorize(Roles = nameof(UserData.IsRootUser))] + [HttpPost] + public IActionResult SaveCustomWidgets(string widgetsData) + { + var saveResult = _fileHelper.SaveWidgets(widgetsData); + return Json(saveResult); + } + [Authorize(Roles = nameof(UserData.IsRootUser))] + [HttpPost] + public IActionResult DeleteCustomWidgets() + { + var deleteResult = _fileHelper.DeleteWidgets(); + return Json(deleteResult); + } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { diff --git a/Controllers/Vehicle/ReportController.cs b/Controllers/Vehicle/ReportController.cs index d80d107f..7d4810f5 100644 --- a/Controllers/Vehicle/ReportController.cs +++ b/Controllers/Vehicle/ReportController.cs @@ -22,6 +22,8 @@ public IActionResult GetReportPartialView(int vehicleId) var odometerRecords = _odometerRecordDataAccess.GetOdometerRecordsByVehicleId(vehicleId); var userConfig = _config.GetUserConfig(User); var viewModel = new ReportViewModel(); + //check if custom widgets are configured + viewModel.CustomWidgetsConfigured = _fileHelper.WidgetsExist(); //get totalCostMakeUp viewModel.CostMakeUpForVehicle = new CostMakeUpForVehicle { @@ -528,5 +530,11 @@ public IActionResult GetCostByMonthByVehicle(int vehicleId, List sel }).ToList(); return PartialView("_GasCostByMonthReport", groupedRecord); } + [HttpGet] + public IActionResult GetAdditionalWidgets() + { + var widgets = _fileHelper.GetWidgets(); + return PartialView("_ReportWidgets", widgets); + } } } diff --git a/Helper/FileHelper.cs b/Helper/FileHelper.cs index e3c70fa3..e62aaa22 100644 --- a/Helper/FileHelper.cs +++ b/Helper/FileHelper.cs @@ -16,6 +16,10 @@ public interface IFileHelper int ClearTempFolder(); int ClearUnlinkedThumbnails(List linkedImages); int ClearUnlinkedDocuments(List linkedDocuments); + string GetWidgets(); + bool WidgetsExist(); + bool SaveWidgets(string widgetsData); + bool DeleteWidgets(); } public class FileHelper : IFileHelper { @@ -100,6 +104,7 @@ public bool RestoreBackup(string fileName, bool clearExisting = false) var documentPath = Path.Combine(tempPath, "documents"); var translationPath = Path.Combine(tempPath, "translations"); var dataPath = Path.Combine(tempPath, StaticHelper.DbName); + var widgetPath = Path.Combine(tempPath, StaticHelper.AdditionalWidgetsPath); var configPath = Path.Combine(tempPath, StaticHelper.UserConfigPath); if (Directory.Exists(imagePath)) { @@ -174,6 +179,10 @@ public bool RestoreBackup(string fileName, bool clearExisting = false) //data path will always exist as it is created on startup if not. File.Move(dataPath, StaticHelper.DbName, true); } + if (File.Exists(widgetPath)) + { + File.Move(widgetPath, StaticHelper.AdditionalWidgetsPath, true); + } if (File.Exists(configPath)) { //check if config folder exists. @@ -223,6 +232,7 @@ public string MakeBackup() var documentPath = Path.Combine(_webEnv.WebRootPath, "documents"); var translationPath = Path.Combine(_webEnv.WebRootPath, "translations"); var dataPath = StaticHelper.DbName; + var widgetPath = StaticHelper.AdditionalWidgetsPath; var configPath = StaticHelper.UserConfigPath; if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath); @@ -262,6 +272,12 @@ public string MakeBackup() Directory.CreateDirectory(newPath); File.Copy(dataPath, $"{newPath}/{Path.GetFileName(dataPath)}"); } + if (File.Exists(widgetPath)) + { + var newPath = Path.Combine(tempPath, "data"); + Directory.CreateDirectory(newPath); + File.Copy(widgetPath, $"{newPath}/{Path.GetFileName(widgetPath)}"); + } if (File.Exists(configPath)) { var newPath = Path.Combine(tempPath, "config"); @@ -323,12 +339,20 @@ public int ClearTempFolder() var tempPath = GetFullFilePath("temp", false); if (Directory.Exists(tempPath)) { + //delete files var files = Directory.GetFiles(tempPath); foreach (var file in files) { File.Delete(file); filesDeleted++; } + //delete folders + var folders = Directory.GetDirectories(tempPath); + foreach(var folder in folders) + { + Directory.Delete(folder, true); + filesDeleted++; + } } return filesDeleted; } @@ -368,5 +392,57 @@ public int ClearUnlinkedDocuments(List linkedDocuments) } return filesDeleted; } + public string GetWidgets() + { + if (File.Exists(StaticHelper.AdditionalWidgetsPath)) + { + try + { + //read file + var widgets = File.ReadAllText(StaticHelper.AdditionalWidgetsPath); + return widgets; + } + catch (Exception ex) + { + _logger.LogError(ex.Message); + return string.Empty; + } + } + return string.Empty; + } + public bool WidgetsExist() + { + return File.Exists(StaticHelper.AdditionalWidgetsPath); + } + public bool SaveWidgets(string widgetsData) + { + try + { + //Delete Widgets if exists + DeleteWidgets(); + File.WriteAllText(StaticHelper.AdditionalWidgetsPath, widgetsData); + return true; + } catch (Exception ex) + { + _logger.LogError(ex.Message); + return false; + } + } + public bool DeleteWidgets() + { + try + { + if (File.Exists(StaticHelper.AdditionalWidgetsPath)) + { + File.Delete(StaticHelper.AdditionalWidgetsPath); + } + return true; + } + catch (Exception ex) + { + _logger.LogError(ex.Message); + return false; + } + } } } diff --git a/Helper/StaticHelper.cs b/Helper/StaticHelper.cs index a9f12fcf..e994dcb8 100644 --- a/Helper/StaticHelper.cs +++ b/Helper/StaticHelper.cs @@ -12,6 +12,7 @@ public static class StaticHelper public static string VersionNumber = "1.4.0"; public static string DbName = "data/cartracker.db"; public static string UserConfigPath = "config/userConfig.json"; + public static string AdditionalWidgetsPath = "data/widgets.html"; public static string GenericErrorMessage = "An error occurred, please try again later"; public static string ReminderEmailTemplate = "defaults/reminderemailtemplate.txt"; public static string DefaultAllowedFileExtensions = ".png,.jpg,.jpeg,.pdf,.xls,.xlsx,.docx"; diff --git a/Models/Report/ReportViewModel.cs b/Models/Report/ReportViewModel.cs index 7696f3d8..87a6c54b 100644 --- a/Models/Report/ReportViewModel.cs +++ b/Models/Report/ReportViewModel.cs @@ -8,5 +8,6 @@ public class ReportViewModel public ReminderMakeUpForVehicle ReminderMakeUpForVehicle { get; set; } = new ReminderMakeUpForVehicle(); public List Years { get; set; } = new List(); public List Collaborators { get; set; } = new List(); + public bool CustomWidgetsConfigured { get; set; } = false; } } diff --git a/Views/Home/_Settings.cshtml b/Views/Home/_Settings.cshtml index e648f546..b27733b8 100644 --- a/Views/Home/_Settings.cshtml +++ b/Views/Home/_Settings.cshtml @@ -335,6 +335,12 @@ + @@ -153,6 +159,15 @@ +@if (Model.CustomWidgetsConfigured) +{ + +}