Files
Module.EventRegistration/Client/Modules/SZUAbsolventenverein.Module.EventRegistration/Details.razor

210 lines
7.2 KiB
Plaintext

@using Oqtane
@using Oqtane.Modules.Controls
@using SZUAbsolventenverein.Module.EventRegistration.Services
@using SZUAbsolventenverein.Module.EventRegistration.Models
@using System.Text.RegularExpressions
@namespace SZUAbsolventenverein.Module.EventRegistration
@inherits ModuleBase
@inject IEventRegistrationService EventRegistrationService
@inject NavigationManager NavigationManager
@inject IStringLocalizer<Edit> Localizer
@inject IStringLocalizer<SharedResources> SharedLocalizer
@inject IUserService UserService
@inject IProfileService ProfileService
@inject ISettingService SettingService
<h3>Anmeldung zum Event</h3>
<p>Willst du am Event (@_name) teilnehmen?</p>
<span class="mb-6">@_eventDate.ToLocalTime() - @_location</span>
<div>
@((MarkupString)_description)
</div>
@if (PageState.User != null) {
@if (Status != null)
{
<p class="mt-2"><strong>Status: </strong>
@if (Status == true)
{
<span class ="fontsizeInf">@Localizer["Zugesagt"]</span><br />
<p><button class="btn btn-danger" @onclick="Absage">@Localizer["Absagen"]</button></p>
} else
{
<span class="fontsizeInf"> @Localizer["Abgesagt"]</span><br />
<p><button class="btn btn-success" @onclick="Zusage">@Localizer["Zusagen"]</button></p>
}
</p>
} else {
<div class="buttons">
<button class="btn btn-success" @onclick="Zusage">@Localizer["Zusagen"]</button>
<button class="btn btn-danger" @onclick="Absage">@Localizer["Absagen"]</button>
</div>
}
} else
{
<p class="mt-3">Um dich für dieses Event zu registrieren, muss man sich zuerst anmelden.</p>
<Login />
@* @if(PageState.Site.AllowRegistration)
{
<Register />
} *@
}
@code {
public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.View;
public override string Actions => "Details";
public override string Title => "Event Details";
public override List<Resource> Resources => new List<Resource>()
{
new Resource { ResourceType = ResourceType.Stylesheet, Url = ModulePath() + "Module.css" }
};
// private ElementReference form;
// private bool validated = false;
private int _id;
private string _name;
private string _description;
private DateTime _eventDate;
private string _location;
private Response _response;
private bool? Status;
private List<Profile> _profiles = new List<Profile>();
private Dictionary<string, string> _settings;
private async Task SendResponse(bool response)
{
if(_response == null)
{
_response = new Response();
_response.EventRegistrationId = _id;
_response.OwnerId = PageState.User.UserId;
_response.ModuleId = ModuleState.ModuleId;
_response.ResponseType = response;
_response = await EventRegistrationService.AddResponseAsync(_response);
} else
{
_response.ResponseType = response;
_response = await EventRegistrationService.UpdateResponseAsync(_response);
}
if(_response != null) Status = _response.ResponseType;
}
private async void Zusage()
{
if(ValidateProfiles())
{
await SendResponse(true);
} else
{
var currentPathAndQuery = new Uri(NavigationManager.Uri).PathAndQuery;
var encodedReturnUrl = Uri.EscapeDataString(currentPathAndQuery);
var link = $"/profile?tab=Profile&returnurl={encodedReturnUrl}";
AddModuleMessage(string.Format(SharedLocalizer["ProfileRequired"], $"Vervollständige hier dein Profil mit deinem Jahrgang und deiner Fachrichtung: <a href=\"{link}\">Link zum Profil</a>"), MessageType.Warning);
}
}
private async void Absage()
{
if(ValidateProfiles())
{
await SendResponse(false);
} else
{
var currentPathAndQuery = new Uri(NavigationManager.Uri).PathAndQuery;
var encodedReturnUrl = Uri.EscapeDataString(currentPathAndQuery);
var link = $"/profile?tab=profile&returnurl={encodedReturnUrl}";
AddModuleMessage(string.Format(SharedLocalizer["ProfileRequired"], $"Vervollständige hier dein Profil mit deinem Jahrgang und deiner Fachrichtung: {link}"), MessageType.Warning);
}
}
protected override async Task OnInitializedAsync()
{
try
{
_profiles = await ProfileService.GetProfilesAsync(PageState.Site.SiteId);
var user = await UserService.GetUserAsync(PageState.User.UserId, PageState.Site.SiteId);
if (user != null)
{
_settings = user.Settings;
}
_id = Int32.Parse(PageState.QueryString["id"]);
Event currentEvent;
Response rsvp;
(currentEvent, rsvp) = await EventRegistrationService.GetEventDetails(_id, ModuleState.ModuleId);
if (currentEvent != null)
{
_name = currentEvent.Name;
_description = currentEvent.Description;
_eventDate = currentEvent.EventDate;
_location = currentEvent.Location;
}
if(rsvp != null)
{
_response = rsvp;
Status = rsvp.ResponseType;
}
}
catch (Exception ex)
{
await logger.LogError(ex, "Error Loading EventRegistration {EventRegistrationId} {Error}", _id, ex.Message);
AddModuleMessage(Localizer["Message.LoadError"], MessageType.Error);
}
}
private bool ValidateProfiles()
{
foreach (Profile profile in _profiles)
{
var value = GetProfileValue(profile.Name, string.Empty);
if (string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(profile.DefaultValue))
{
_settings = SettingService.SetSetting(_settings, profile.Name, profile.DefaultValue);
}
if (!profile.IsPrivate || UserSecurity.IsAuthorized(PageState.User, RoleNames.Admin))
{
if (profile.IsRequired && string.IsNullOrEmpty(value))
{
AddModuleMessage(string.Format(SharedLocalizer["ProfileRequired"], profile.Title), MessageType.Warning);
return false;
}
if (!string.IsNullOrEmpty(profile.Validation))
{
Regex regex = new Regex(profile.Validation);
bool valid = regex.Match(value).Success;
if (!valid)
{
AddModuleMessage(string.Format(SharedLocalizer["ProfileInvalid"], profile.Title), MessageType.Warning);
return false;
}
}
}
}
return true;
}
private string GetProfileValue(string SettingName, string DefaultValue)
{
string value = SettingService.GetSetting(_settings, SettingName, DefaultValue);
if (value.Contains("]"))
{
value = value.Substring(value.IndexOf("]") + 1);
}
return value;
}
}