141 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			141 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
| @namespace Oqtane.Modules.Admin.Themes
 | |
| @using System.Net
 | |
| @inherits ModuleBase
 | |
| @inject NavigationManager NavigationManager
 | |
| @inject IThemeService ThemeService
 | |
| @inject IPackageService PackageService
 | |
| @inject IStringLocalizer<Index> Localizer
 | |
| @inject IStringLocalizer<SharedResources> SharedLocalizer
 | |
| 
 | |
| @if (_themes == null)
 | |
| {
 | |
|     <p><em>@SharedLocalizer["Loading"]</em></p>
 | |
| }
 | |
| else
 | |
| {
 | |
|     <ActionLink Action="Add" Text="Install Theme" />
 | |
|     @((MarkupString)" ")
 | |
|     <ActionLink Action="Create" Text="Create Theme" ResourceKey="CreateTheme" Class="btn btn-secondary" />
 | |
| 
 | |
|     <Pager Items="@_themes">
 | |
|         <Header>
 | |
|             <th style="width: 1px;"> </th>
 | |
|             <th style="width: 1px;"> </th>
 | |
|             <th>@SharedLocalizer["Name"]</th>
 | |
|             <th>@SharedLocalizer["Version"]</th>
 | |
|             <th>@SharedLocalizer["Expires"]</th>
 | |
|             <th> </th>
 | |
|         </Header>
 | |
|         <Row>
 | |
|             <td><ActionLink Action="View" Parameters="@($"name=" + WebUtility.UrlEncode(context.ThemeName))" ResourceKey="ViewTheme" /></td>
 | |
|             <td>
 | |
|                 @if (context.AssemblyName != "Oqtane.Client")
 | |
|                     {
 | |
|                     <ActionDialog Header="Delete Theme" Message="@string.Format(Localizer["Confirm.Theme.Delete"], context.Name)" Action="Delete" Security="SecurityAccessLevel.Host" Class="btn btn-danger" OnClick="@(async () => await DeleteTheme(context))" ResourceKey="DeleteTheme" />
 | |
|                     }
 | |
|             </td>
 | |
|             <td>@context.Name</td>
 | |
|             <td>@context.Version</td>
 | |
|             <td>
 | |
|                 @((MarkupString)PurchaseLink(context.PackageName))
 | |
|             </td>
 | |
|             <td>
 | |
|                 @if (UpgradeAvailable(context.PackageName, context.Version))
 | |
|                 {
 | |
|                     <button type="button" class="btn btn-success" @onclick=@(async () => await DownloadTheme(context.PackageName, context.Version))>@SharedLocalizer["Upgrade"]</button>
 | |
|                 }
 | |
|             </td>
 | |
|             <td></td>
 | |
|         </Row>
 | |
|     </Pager>
 | |
| }
 | |
| 
 | |
| @code {
 | |
|     private List<Theme> _themes;
 | |
|     private List<Package> _packages;
 | |
| 
 | |
|     public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Host;
 | |
| 
 | |
|     protected override async Task OnParametersSetAsync()
 | |
|     {
 | |
|         try
 | |
|         {
 | |
|             _themes = await ThemeService.GetThemesAsync();
 | |
|             _packages = await PackageService.GetPackagesAsync("theme");
 | |
|         }
 | |
|         catch (Exception ex)
 | |
|         {
 | |
|             if (_themes == null)
 | |
|             {
 | |
|                 await logger.LogError(ex, "Error Loading Themes {Error}", ex.Message);
 | |
|                 AddModuleMessage(Localizer["Error.Theme.Load"], MessageType.Error);
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     private string PurchaseLink(string packagename)
 | |
|     {
 | |
|         string link = "";
 | |
|         if (!string.IsNullOrEmpty(packagename) && _packages != null)
 | |
|         {
 | |
|             var package = _packages.Where(item => item.PackageId == packagename).FirstOrDefault();
 | |
|             if (package != null)
 | |
|             {
 | |
|                 if (package.ExpiryDate != null && package.ExpiryDate.Value.Date != DateTime.MaxValue.Date)
 | |
|                 {
 | |
|                     link += "<span>" + package.ExpiryDate.Value.Date.ToString("MMM dd, yyyy") + "</span>";
 | |
|                     if (!string.IsNullOrEmpty(package.PaymentUrl))
 | |
|                     {
 | |
|                         link += "  <a class=\"btn btn-primary\" style=\"text-decoration: none !important\" href=\"" + package.PaymentUrl + "\" target=\"_new\">" + SharedLocalizer["Extend"] + "</a>";
 | |
|                     }
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
|         return link;
 | |
|     }
 | |
| 
 | |
|     private bool UpgradeAvailable(string packagename, string version)
 | |
|     {
 | |
|         var upgradeavailable = false;
 | |
|         if (!string.IsNullOrEmpty(packagename) && _packages != null)
 | |
|         {
 | |
|             var package = _packages.Where(item => item.PackageId == packagename).FirstOrDefault();
 | |
|             if (package != null)
 | |
|             {
 | |
|                 upgradeavailable = (Version.Parse(package.Version).CompareTo(Version.Parse(version)) > 0);
 | |
|             }
 | |
|         }
 | |
|         return upgradeavailable;
 | |
|     }
 | |
| 
 | |
|     private async Task DownloadTheme(string packagename, string version)
 | |
|     {
 | |
|         try
 | |
|         {
 | |
|             await PackageService.DownloadPackageAsync(packagename, version, Constants.PackagesFolder);
 | |
|             await logger.LogInformation("Theme Downloaded {ThemeName} {Version}", packagename, version);
 | |
|             await ThemeService.InstallThemesAsync();
 | |
|             AddModuleMessage(string.Format(Localizer["Success.Theme.Install"], NavigateUrl("admin/system")), MessageType.Success);
 | |
|         }
 | |
|         catch (Exception ex)
 | |
|         {
 | |
|             await logger.LogError(ex, "Error Downloading Theme {ThemeName} {Version} {Error}", packagename, version, ex.Message);
 | |
|             AddModuleMessage(Localizer["Error.Theme.Download"], MessageType.Error);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     private async Task DeleteTheme(Theme Theme)
 | |
|     {
 | |
|         try
 | |
|         {
 | |
|             await ThemeService.DeleteThemeAsync(Theme.ThemeName);
 | |
|             AddModuleMessage(Localizer["Success.Theme.Delete"], MessageType.Success);
 | |
|             NavigationManager.NavigateTo(NavigateUrl(PageState.Page.Path, true));
 | |
|         }
 | |
|         catch (Exception ex)
 | |
|         {
 | |
|             await logger.LogError(ex, "Error Deleting Theme {Theme} {Error}", Theme, ex.Message);
 | |
|             AddModuleMessage(Localizer["Error.Theme.Delete"], MessageType.Error);
 | |
|         }
 | |
|     }
 | |
| } | 
