448 lines
17 KiB
C#
448 lines
17 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Xml;
|
|
using System.Xml.Linq;
|
|
using System.Xml.Serialization;
|
|
using HtmlAgilityPack;
|
|
using Kreta.BusinessLogic.Classes;
|
|
using Kreta.BusinessLogic.HelperClasses;
|
|
using Kreta.BusinessLogic.Helpers;
|
|
using Kreta.Core;
|
|
using Kreta.Core.ConnectionType;
|
|
using Kreta.DataAccess.Interfaces;
|
|
using Kreta.DataAccessManual;
|
|
using Kreta.DataAccessManual.Interfaces;
|
|
using Kreta.Enums;
|
|
using Kreta.Enums.ManualEnums;
|
|
using Kreta.Resources;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace Kreta.BusinessLogic.Utils
|
|
{
|
|
public static class CommonUtils
|
|
{
|
|
public static string GetIntezmenyUrl(string intezmenyAzonosito)
|
|
=> string.Format(CommonResource.IntezmenyUrl, intezmenyAzonosito);
|
|
|
|
public static string GetIntezmenyLinkWithNevEsUrl(int felhasznaloId, string intezmenyAzonosito, int intezmenyId, int tanevId)
|
|
=> string.Format(CommonResource.IntezmenyNevEsUrlLink, GetIntezmenyNev(felhasznaloId, intezmenyAzonosito, intezmenyId, tanevId), intezmenyAzonosito);
|
|
|
|
private static string GetIntezmenyNev(int felhasznaloId, string intezmenyAzonosito, int intezmenyId, int tanevId)
|
|
=> new IntezmenyHelper(new OrganizationConnectionType(felhasznaloId, intezmenyId, intezmenyAzonosito, tanevId)).GetAktivIntezmenyNevCim().Nev;
|
|
|
|
public static string GetIntezmenyFenntartoEmail(int felhasznaloId, string intezmenyAzonosito, int intezmenyId, int tanevId)
|
|
=> new IntezmenyHelper(new OrganizationConnectionType(felhasznaloId, intezmenyId, intezmenyAzonosito, tanevId)).GetAktivIntezmenyNevCim().Email;
|
|
|
|
public static bool TernaryXor(bool a, bool b, bool c)
|
|
{
|
|
return !a && b ^ c || a && !(b || c);
|
|
}
|
|
|
|
#region Gondviselő bejelentkezési név generálás
|
|
|
|
public static string GenerateGondviseloBejelentkezesiNev(IFelhasznaloBelepesDal felhasznaloBelepesDal, string oktatasiAzonosito, ref int gondviseloIndex, int tanevId)
|
|
{
|
|
string bejelentkezesiNev;
|
|
var letezoBelepesek = new List<IFelhasznaloBelepes>();
|
|
do
|
|
{
|
|
bejelentkezesiNev = $"{oktatasiAzonosito}G{gondviseloIndex:00}";
|
|
letezoBelepesek = felhasznaloBelepesDal.GetFelhasznaloBelepesei(bejelentkezesiNev, tanevId);
|
|
|
|
++gondviseloIndex;
|
|
} while (letezoBelepesek.Any());
|
|
|
|
return bejelentkezesiNev;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Felhasználó jelszó generálás
|
|
|
|
public static string GenerateJelszo()
|
|
{
|
|
return Guid.NewGuid().ToString().Substring(0, Constants.General.JelszoMinimumKarakterekSzama);
|
|
}
|
|
|
|
#endregion
|
|
|
|
public static bool HasDirtyWordsContains(string str)
|
|
{
|
|
var dirtyWordsFiles = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "DirtyWords.xml", SearchOption.AllDirectories);
|
|
if (dirtyWordsFiles.Length == 0)
|
|
{
|
|
throw new Exception("Not found DirtyWords.xml file!");
|
|
}
|
|
|
|
var xml = XElement.Load(dirtyWordsFiles[0]);
|
|
var lst = xml.Elements("Word").ToList().Select(x => x.Value);
|
|
HashSet<string> dirtyWords = new HashSet<string>(lst, StringComparer.OrdinalIgnoreCase);
|
|
|
|
string[] result = Regex.Split(str, @"\W");
|
|
return result.Any(szo => dirtyWords.Contains(szo));
|
|
}
|
|
|
|
public static string SerializeXmlFromClass<T>(T classForXml) where T : new()
|
|
{
|
|
var xmlSerializer = new XmlSerializer(classForXml.GetType());
|
|
string xml;
|
|
|
|
using (var stringWriter = new StringWriter())
|
|
{
|
|
using (var xmlWriter = XmlWriter.Create(stringWriter))
|
|
{
|
|
xmlSerializer.Serialize(xmlWriter, classForXml);
|
|
xml = stringWriter.ToString();
|
|
}
|
|
}
|
|
|
|
return xml;
|
|
}
|
|
|
|
public static string RemoveHtmlTags(string input)
|
|
=> input == null ? null : Regex.Replace(input, "<[^>]*?[^<br />|p]>", string.Empty);
|
|
|
|
public static string RemoveAllHtmlTags(string input)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(input))
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
var htmlDocument = new HtmlDocument();
|
|
htmlDocument.LoadHtml(input);
|
|
return htmlDocument.DocumentNode.InnerText;
|
|
}
|
|
|
|
public static string RemoveNotAllowedHtmlTags(string html, List<string> allowedTags)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(html))
|
|
{
|
|
return html;
|
|
}
|
|
|
|
var document = new HtmlDocument();
|
|
document.LoadHtml(html);
|
|
|
|
HtmlNodeCollection tryGetNodes = document.DocumentNode.SelectNodes("./*|./text()");
|
|
|
|
if (tryGetNodes == null || !tryGetNodes.Any())
|
|
{
|
|
return html;
|
|
}
|
|
|
|
var nodes = new Queue<HtmlNode>(tryGetNodes);
|
|
|
|
while (nodes.Count > 0)
|
|
{
|
|
var node = nodes.Dequeue();
|
|
var parentNode = node.ParentNode;
|
|
|
|
var childNodes = node.SelectNodes("./*|./text()");
|
|
|
|
if (childNodes != null)
|
|
{
|
|
foreach (var child in childNodes)
|
|
{
|
|
nodes.Enqueue(child);
|
|
}
|
|
}
|
|
|
|
if (!allowedTags.Any(tag => tag == node.Name))
|
|
{
|
|
if (childNodes != null)
|
|
{
|
|
foreach (var child in childNodes)
|
|
{
|
|
parentNode.InsertBefore(child, node);
|
|
}
|
|
}
|
|
|
|
parentNode.RemoveChild(node);
|
|
|
|
}
|
|
}
|
|
|
|
return document.DocumentNode.InnerHtml;
|
|
}
|
|
|
|
public static string ReplaceNewLineToHtmlTag(string input)
|
|
{
|
|
if (input == null)
|
|
return null;
|
|
return Regex.Replace(input, Constants.RegularExpressions.OneOfLineBreak, "<br />");
|
|
}
|
|
|
|
public static string CreateLinksInText(string text)
|
|
{
|
|
var result = new StringBuilder(text);
|
|
var regx = new Regex(Constants.RegularExpressions.URLPattern, RegexOptions.IgnoreCase);
|
|
|
|
var darabokMatches = Regex.Matches(text, Constants.RegularExpressions.RemoveLinkFromText).OfType<Match>();
|
|
if (darabokMatches.Any())
|
|
{
|
|
foreach (var darab in darabokMatches.OrderByDescending(x => x.Index))
|
|
{
|
|
result.Replace(darab.Groups["vege"].Value, "").Replace(darab.Groups["eleje"].Value, "");
|
|
}
|
|
}
|
|
var urlList = regx.Matches(result.ToString()).OfType<Match>().Select(m => m.Groups[0].Value).Distinct();
|
|
if (urlList.Any())
|
|
{
|
|
foreach (var url in urlList)
|
|
{
|
|
result = result.Replace(url, string.Format("<a href=\"{0}\" target=\"_blank\">{0}</a>", url));
|
|
}
|
|
}
|
|
|
|
return result.ToString();
|
|
}
|
|
|
|
public static string GetOrganizationIdentifier()
|
|
{
|
|
return KretaServer.KretaServer.Instance.GetOrganizationIdentifier();
|
|
}
|
|
|
|
public static int GetIntezmenyId(int felhasznaloId, string intezmenyAzonosito = null)
|
|
{
|
|
try
|
|
{
|
|
var intezmenyAzonositoValue = intezmenyAzonosito ?? GetOrganizationIdentifier();
|
|
|
|
IntezmenyHelper intezmenyHelper = new IntezmenyHelper(new OrganizationConnectionType(felhasznaloId, 0, intezmenyAzonositoValue, 0));
|
|
|
|
return intezmenyHelper.GetIntezmenyId();
|
|
}
|
|
catch
|
|
{
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
public static int GetAktualisTanevId(int felhasznaloId, string intezmenyAzonosito = null)
|
|
{
|
|
var intezmenyAzonositoValue = intezmenyAzonosito ?? GetOrganizationIdentifier();
|
|
|
|
TanevHelper tanevHelper = new TanevHelper(new OrganizationConnectionType(felhasznaloId, GetIntezmenyId(felhasznaloId, intezmenyAzonositoValue), intezmenyAzonositoValue, 0));
|
|
var aktivTanev = tanevHelper.GetTanevInfo();
|
|
return aktivTanev.Id;
|
|
}
|
|
|
|
public static DataSet GetLoginDashboardUzenet(IConnectionType connectionType)
|
|
{
|
|
return Dal.CustomConnection.Run(connectionType, h =>
|
|
{
|
|
IDashboardUzenetDal dal = h.DashboardUzenet();
|
|
return dal.GetLoginDashboardUzenet();
|
|
});
|
|
}
|
|
|
|
public static readonly List<HetNapjaTipusEnum> OrderedHetNapjaTipusEnumList = new List<HetNapjaTipusEnum>
|
|
{
|
|
HetNapjaTipusEnum.Hetfo,
|
|
HetNapjaTipusEnum.Kedd,
|
|
HetNapjaTipusEnum.Szerda,
|
|
HetNapjaTipusEnum.Csutortok,
|
|
HetNapjaTipusEnum.Pentek,
|
|
HetNapjaTipusEnum.Szombat,
|
|
HetNapjaTipusEnum.Vasarnap
|
|
};
|
|
|
|
/// <summary>
|
|
/// 2020/21-es tanévtől kezdődően (40, vagy annál nagyobb sorszám) új SZT van.
|
|
/// </summary>
|
|
public static bool IsSelectedTanev20_21OrLater(TanevCO selectedTanev)
|
|
{
|
|
var isSelectedTanev20_21OrLater = selectedTanev.Sorszam >= 40;
|
|
return isSelectedTanev20_21OrLater;
|
|
}
|
|
|
|
public static bool IsSelectedTanev21_22OrLater(TanevCO selectedTanev)
|
|
{
|
|
return selectedTanev.Sorszam >= 42;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Az igen/nem lista megjelenítését adja vissza a booleanDisplayFormatEnum alapján.
|
|
/// </summary>
|
|
/// <param name="booleanDisplayFormatEnum">Az igen/nem lista megjelenítését eldöntő enum: Igen/Nem, T/F, I/N</param>
|
|
/// <returns></returns>
|
|
public static List<string> GetIgenNemList(BooleanDisplayFormatEnum booleanDisplayFormatEnum)
|
|
{
|
|
var result = new List<string> { true.GetDisplayName(booleanDisplayFormatEnum), false.GetDisplayName(booleanDisplayFormatEnum) };
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// A value megjelenítéshez tartozó formázás.
|
|
/// </summary>
|
|
/// <typeparam name="T">A value típusa.</typeparam>
|
|
/// <param name="value">A formázni kívánt érték</param>
|
|
/// <param name="dateTimeToStringPattern">Ha a T az DateTime, akkor annak a formázáshoz használható paraméter. Ha null, akkor Kreta.Core.Constants.ToStringPattern.HungarianDate.</param>
|
|
/// <returns></returns>
|
|
public static string GetDisplayableString<T>(T value, int tanevId, string dateTimeToStringPattern = null)
|
|
{
|
|
string result;
|
|
switch (value)
|
|
{
|
|
case string stringValue:
|
|
result = !string.IsNullOrWhiteSpace(stringValue) ? stringValue : string.Empty;
|
|
break;
|
|
case DateTime dateValue:
|
|
string toStringPattern = string.IsNullOrWhiteSpace(dateTimeToStringPattern) ? Constants.ToStringPattern.HungarianDate : dateTimeToStringPattern;
|
|
result = dateValue.ToString(toStringPattern);
|
|
break;
|
|
case bool boolvalue:
|
|
result = boolvalue.GetDisplayName();
|
|
break;
|
|
case Enum enumValue:
|
|
result = enumValue.GetDisplayName(tanevId);
|
|
break;
|
|
default:
|
|
result = value != null ? value.ToString() : string.Empty;
|
|
break;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
#region NevElvalaszto
|
|
|
|
public static int GetNevElvalasztoIndex(string nev)
|
|
{
|
|
var textInfo = new CultureInfo(Constants.General.HungarianCulture, false).TextInfo;
|
|
var titleCaseName = textInfo.ToTitleCase(nev);
|
|
var nevElotag = GetNevElotag(titleCaseName);
|
|
|
|
var nevElotagNelkul = titleCaseName.Remove(0, nevElotag.Length);
|
|
|
|
var nevElvalasztoIndex = nevElotag.Length + nevElotagNelkul.IndexOf(" ");
|
|
return nevElvalasztoIndex;
|
|
}
|
|
|
|
public static string GetNevElotag(string nev)
|
|
{
|
|
var textInfo = new CultureInfo(Constants.General.HungarianCulture, false).TextInfo;
|
|
var titleCaseName = textInfo.ToTitleCase(nev);
|
|
|
|
var elotagMatch = Regex.Match(titleCaseName, Constants.RegularExpressions.NevElotagEllenorzes);
|
|
var elotag = new StringBuilder();
|
|
|
|
while (elotagMatch.Success)
|
|
{
|
|
elotag.Append(elotagMatch.Value);
|
|
titleCaseName = titleCaseName.Remove(0, elotagMatch.Value.Length).Trim();
|
|
elotagMatch = Regex.Match(titleCaseName, Constants.RegularExpressions.NevElotagEllenorzes);
|
|
}
|
|
return elotag.ToString();
|
|
}
|
|
|
|
#endregion NevElvalaszto
|
|
|
|
/// <summary>
|
|
/// 2019/20-es tanév előtti összes-re vonatkkozik
|
|
/// </summary>
|
|
public static bool IsSelectedTanevIsElozo(TanevCO selectedTanev)
|
|
{
|
|
var isSelectedTanevIsElozo = !selectedTanev.IsAktiv && !selectedTanev.IsKovetkezo;
|
|
return isSelectedTanevIsElozo;
|
|
}
|
|
|
|
public static bool IsSzcIntezmenyFenntartoAzonosito(IConnectionType connectionType)
|
|
{
|
|
var intezmenyAdatok = new IntezmenyHelper(connectionType).GetIntezmenyiAdatok();
|
|
if (int.TryParse(intezmenyAdatok.FenntartoAzonosito, out int fenntartoAzonosito))
|
|
{
|
|
var isSzcFenntartoAzonosito = (601 <= fenntartoAzonosito && fenntartoAzonosito <= 644) || fenntartoAzonosito == 998;
|
|
return isSzcFenntartoAzonosito;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static double ConvertByteToMByte(double value)
|
|
{
|
|
return value / 1024 / 1024;
|
|
}
|
|
|
|
public static double ConvertKByteToMByte(int value)
|
|
{
|
|
return value / 1024;
|
|
}
|
|
|
|
public static double ConvertKByteToGByte(int value)
|
|
{
|
|
return value / 1024 / 1024;
|
|
}
|
|
|
|
public static int GetHetNapja(DateTime date)
|
|
{
|
|
HetNapjaTipusEnum hetNapja = HetNapjaTipusEnum.na;
|
|
switch (date.DayOfWeek)
|
|
{
|
|
case DayOfWeek.Monday:
|
|
hetNapja = HetNapjaTipusEnum.Hetfo;
|
|
break;
|
|
case DayOfWeek.Tuesday:
|
|
hetNapja = HetNapjaTipusEnum.Kedd;
|
|
break;
|
|
case DayOfWeek.Wednesday:
|
|
hetNapja = HetNapjaTipusEnum.Szerda;
|
|
break;
|
|
case DayOfWeek.Thursday:
|
|
hetNapja = HetNapjaTipusEnum.Csutortok;
|
|
break;
|
|
case DayOfWeek.Friday:
|
|
hetNapja = HetNapjaTipusEnum.Pentek;
|
|
break;
|
|
case DayOfWeek.Saturday:
|
|
hetNapja = HetNapjaTipusEnum.Szombat;
|
|
break;
|
|
case DayOfWeek.Sunday:
|
|
hetNapja = HetNapjaTipusEnum.Vasarnap;
|
|
break;
|
|
}
|
|
return (int)hetNapja;
|
|
}
|
|
|
|
public static string GetEntitasIdJson(IEnumerable<int> ids)
|
|
{
|
|
return JsonConvert.SerializeObject(ids.Select(i => new { Id = i }).ToArray());
|
|
}
|
|
|
|
public static IEnumerable<string> TajSzamValidation(string tajSzam)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(tajSzam))
|
|
{
|
|
tajSzam = tajSzam.Replace("_", "");
|
|
|
|
if (tajSzam.Length > 0 && tajSzam.Length != 9)
|
|
{
|
|
yield return ErrorResource.TAJszamHosszaNemMegfelelo;
|
|
}
|
|
else if (string.Equals(tajSzam, "000000000", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
yield return ErrorResource.TAJszamCsupaNulla;
|
|
}
|
|
else if (tajSzam.Length == 9)
|
|
{
|
|
var tajszam = new int[9];
|
|
for (var x = 0; x < tajszam.Length; x++)
|
|
{
|
|
tajszam[x] = Convert.ToInt32(tajSzam.Substring(x, 1));
|
|
}
|
|
|
|
if ((((tajszam[1] + tajszam[3] + tajszam[5] + tajszam[7]) * 7) + ((tajszam[0] + tajszam[2] + tajszam[4] + tajszam[6]) * 3)) % 10 != tajszam[8])
|
|
{
|
|
yield return ErrorResource.TAJszamNemErvenyes;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|