Skip to content

Instantly share code, notes, and snippets.

@jeremy-farrance
Last active September 20, 2021 23:12
Show Gist options
  • Save jeremy-farrance/4ea58d2f886bd7e8faaf77fc8b3b6d57 to your computer and use it in GitHub Desktop.
Save jeremy-farrance/4ea58d2f886bd7e8faaf77fc8b3b6d57 to your computer and use it in GitHub Desktop.
2sxc - Accuraty reusable library of functions (started fall 2019)
@* common ASL functions that help or simplyfy things in 2sxc
usage from another Razor script:
// get library of helpful functions/commands
var lib = CreateInstance("_Helpers--Basic.cshtml");
// then call them with @lib.ConvertLineBreaks(myString)
*@
@using System.Text.RegularExpressions -- used by ToSlug()
@using DotNetNuke.Entities.Portals -- used by IsAdminOrHost()
@using DotNetNuke.Entities.Users -- used by IsAdminOrHost(), IsContentManager()
@using DotNetNuke.Entities.Tabs -- used by GetParentTab()
@using ToSic.Razor.Blade -- used by AddAttribute()
@* unused?
@using System.Dynamic
@using System.IO
@using DotNetNuke.Security.Permissions
*@
@functions {
// generate a short "likely unique" ID string; e.g.
public string quickUID() {
return Guid.NewGuid().ToString().GetHashCode().ToString("x");
}
// convert \n line breaks to <br>s ... this version is better, handles both CrLf and Lf
// should we be using RazorBlade's Tags.Nl2Br() isntead?
public System.Web.IHtmlString ConvertLineBreaks(string original) {
return Html.Raw(HttpUtility.HtmlEncode(original).Replace("\r\n", "<br />").Replace("\n", "<br />"));
}
// conditionally add an HTML attribute or (literally) nothing
public ToSic.Razor.Blade.Attribute AddAttribute(bool doIt, string attributeName, string attributeValue) {
return doIt ? Tags.Attribute(attributeName, attributeValue) : null;
}
// Turn any text or title in to a URL Slug, source = https://stackoverflow.com/questions/2920744/url-slugify-algorithm-in-c
public string ToSlug(string phrase)
{
byte[] bytes = System.Text.Encoding.GetEncoding("Cyrillic").GetBytes(phrase);
string str = System.Text.Encoding.ASCII.GetString(bytes);
str = str.ToLower();
// invalid chars
str = Regex.Replace(str, @"[^a-z0-9\s-]", "");
// convert multiple spaces into one space
str = Regex.Replace(str, @"\s+", " ").Trim();
// cut and trim
str = str.Substring(0, str.Length <= 45 ? str.Length : 45).Trim();
str = Regex.Replace(str, @"\s", "-"); // hyphens
return str;
}
// DNN // is the current user an Admin or Host?
public bool IsAdminOrHost(UserInfo userInfo) {
return userInfo.IsSuperUser || userInfo.IsInRole(PortalSettings.Current.AdministratorRoleName);
}
// DNN // is the current user a Content or Site Manager (or Admin+)?
public bool IsContentManager(UserInfo userInfo) {
return userInfo.IsInRole("Content Manager")
|| userInfo.IsInRole("Site Manager")
|| IsAdminOrHost(userInfo);
}
// DNN // for TabInfo return the Parent TabInfo
public TabInfo GetParentTab(TabInfo aTab) {
return TabController.Instance.GetTab(aTab.ParentId, Dnn.Portal.PortalId);
}
}
@jeremy-farrance
Copy link
Author

Very early version of what became AccuKit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment