Skip to content

Commit

Permalink
add loads of expressions to aid in strong typing to widget value retr…
Browse files Browse the repository at this point in the history
…ievals

refactor image sizes
  • Loading branch information
ninianne98 committed Jul 11, 2024
1 parent f59764e commit 35184e2
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 58 deletions.
197 changes: 173 additions & 24 deletions CMSInterfaces/ParmParser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;

/*
Expand All @@ -17,49 +19,196 @@ namespace Carrotware.CMS.Interface {

public static class ParmParser {

public static string GetParmValue(this Dictionary<string, string> parmDictionary, string sKey) {
public static void SetGuidValue<T>(this T widgetObject, Expression<Func<T, Guid>> widgetProperty, Guid? value) {
SetGuidValue(widgetObject, widgetProperty, value, Guid.Empty);
}

public static void SetGuidValue<T>(this T widgetObject, Expression<Func<T, Guid>> widgetProperty, Guid? value, Guid defaultValue) {
Guid val = defaultValue;
if (value.HasValue) {
val = value.Value;
}

SetValue(widgetObject, widgetProperty, val);
}

public static void SetStringValue<T>(this T widgetObject, Expression<Func<T, string>> widgetProperty, string value) {
SetStringValue(widgetObject, widgetProperty, value, string.Empty);
}

public static void SetStringValue<T>(this T widgetObject, Expression<Func<T, string>> widgetProperty, string value, string defaultValue) {
string val = defaultValue;
if (!string.IsNullOrEmpty(value)) {
val = value;
}

SetValue(widgetObject, widgetProperty, val);
}

public static void SetIntValue<T>(this T widgetObject, Expression<Func<T, int>> widgetProperty, int? value) {
SetIntValue(widgetObject, widgetProperty, value, 0);
}

public static void SetIntValue<T>(this T widgetObject, Expression<Func<T, int>> widgetProperty, int? value, int defaultValue) {
int val = defaultValue;
if (value.HasValue) {
val = value.Value;
}

SetValue(widgetObject, widgetProperty, val);
}

public static void SetBoolValue<T>(this T widgetObject, Expression<Func<T, bool>> widgetProperty, bool? value) {
SetBoolValue(widgetObject, widgetProperty, value, false);
}

public static void SetBoolValue<T>(this T widgetObject, Expression<Func<T, bool>> widgetProperty, bool? value, bool defaultValue) {
bool val = defaultValue;
if (value.HasValue) {
val = value.Value;
}

SetValue(widgetObject, widgetProperty, val);
}

public static void SetValue<T, TValue>(this T widgetObject, Expression<Func<T, TValue>> widgetProperty, TValue value) {
var selector = widgetProperty.Body as MemberExpression;
if (selector != null) {
var property = selector.Member as PropertyInfo;
if (property != null) {
property.SetValue(widgetObject, value, null);
}
}
}

public static string GetValue<T, TValue>(this T widgetObject, Expression<Func<T, TValue>> widgetProperty) {
return GetValue(widgetObject, widgetProperty, string.Empty);
}

public static string GetStringValue<T, TValue>(this T widgetObject, Expression<Func<T, TValue>> widgetProperty) {
return GetValue(widgetObject, widgetProperty, string.Empty);
}

public static string GetValue<T, TValue>(this T widgetObject, Expression<Func<T, TValue>> widgetProperty, string defaultVal) {
string foundVal = defaultVal;
var selector = widgetProperty.Body as MemberExpression;

if (selector != null && widgetObject is IWidgetParmData) {
var property = selector.Member as PropertyInfo;
foundVal = ((IWidgetParmData)widgetObject).PublicParmValues.GetParmValue(property.Name, defaultVal);
}

return foundVal;
}

public static int GetIntValue<T, TValue>(this T widgetObject, Expression<Func<T, TValue>> widgetProperty) {
return GetValue(widgetObject, widgetProperty, 0);
}

public static int GetValue<T, TValue>(this T widgetObject, Expression<Func<T, TValue>> widgetProperty, int defaultVal) {
int foundVal = defaultVal;
var selector = widgetProperty.Body as MemberExpression;

if (selector != null && widgetObject is IWidgetParmData) {
var property = selector.Member as PropertyInfo;
var foundString = ((IWidgetParmData)widgetObject).PublicParmValues.GetParmValue(property.Name, defaultVal);
if (!string.IsNullOrEmpty(foundString)) {
foundVal = Convert.ToInt32(foundString);
}
}

return foundVal;
}

public static bool GetBoolValue<T, TValue>(this T widgetObject, Expression<Func<T, TValue>> widgetProperty) {
return GetValue(widgetObject, widgetProperty, false);
}

public static bool GetValue<T, TValue>(this T widgetObject, Expression<Func<T, TValue>> widgetProperty, bool defaultVal) {
bool foundVal = defaultVal;
var selector = widgetProperty.Body as MemberExpression;

if (selector != null && widgetObject is IWidgetParmData) {
var property = selector.Member as PropertyInfo;
var foundString = ((IWidgetParmData)widgetObject).PublicParmValues.GetParmValue(property.Name, defaultVal);
if (!string.IsNullOrEmpty(foundString)) {
foundVal = Convert.ToBoolean(foundString);
}
}

return foundVal;
}

public static Guid GetGuidValue<T, TValue>(this T widgetObject, Expression<Func<T, TValue>> widgetProperty) {
return GetValue(widgetObject, widgetProperty, Guid.Empty);
}

public static Guid GetValue<T, TValue>(this T widgetObject, Expression<Func<T, TValue>> widgetProperty, Guid defaultVal) {
Guid foundVal = defaultVal;
var selector = widgetProperty.Body as MemberExpression;

if (selector != null && widgetObject is IWidgetParmData) {
var property = selector.Member as PropertyInfo;
var foundString = ((IWidgetParmData)widgetObject).PublicParmValues.GetParmValue(property.Name, defaultVal.ToString());
if (!string.IsNullOrEmpty(foundString)) {
foundVal = new Guid(foundString);
}
}

return foundVal;
}

public static string GetParmValue(this Dictionary<string, string> parmDictionary, string key) {
string ret = null;

if (parmDictionary.Any()) {
ret = (from c in parmDictionary
where c.Key.ToLowerInvariant() == sKey.ToLowerInvariant()
where c.Key.ToLowerInvariant() == key.ToLowerInvariant()
select c.Value).FirstOrDefault();
}

return ret;
}

public static string GetParmValue(this Dictionary<string, string> parmDictionary, string sKey, string sDefault) {
public static string GetParmValue(this Dictionary<string, string> parmDictionary, string key, string defaultValue) {
string ret = null;

if (parmDictionary.Any()) {
ret = (from c in parmDictionary
where c.Key.ToLowerInvariant() == sKey.ToLowerInvariant()
where c.Key.ToLowerInvariant() == key.ToLowerInvariant()
select c.Value).FirstOrDefault();
}

ret = ret == null ? sDefault : ret;
ret = ret == null ? defaultValue : ret;

return ret;
}

public static string GetParmValueDefaultEmpty(Dictionary<string, string> parmDictionary, string sKey, string sDefault) {
string ret = GetParmValue(parmDictionary, sKey, sDefault);
public static string GetParmValue(this Dictionary<string, string> parmDictionary, string key, bool defaultValue) {
return parmDictionary.GetParmValue(key, defaultValue.ToString().ToLowerInvariant());
}

public static string GetParmValue(this Dictionary<string, string> parmDictionary, string key, int defaultValue) {
return parmDictionary.GetParmValue(key, defaultValue.ToString());
}

public static string GetParmValueDefaultEmpty(this Dictionary<string, string> parmDictionary, string key, string defaultValue) {
string ret = GetParmValue(parmDictionary, key, defaultValue);

ret = string.IsNullOrEmpty(ret) ? sDefault : ret;
ret = string.IsNullOrEmpty(ret) ? defaultValue : ret;

return ret;
}

public static List<string> GetParmValueList(Dictionary<string, string> parmDictionary, string sKey) {
sKey = sKey.EndsWith("|") ? sKey : sKey + "|";
sKey = sKey.ToLowerInvariant();
public static List<string> GetParmValueList(this Dictionary<string, string> parmDictionary, string key) {
key = key.EndsWith("|") ? key : key + "|";
key = key.ToLowerInvariant();

List<string> ret = new List<string>();

if (parmDictionary.Any()) {
ret = (from c in parmDictionary
where c.Key.ToLowerInvariant().StartsWith(sKey)
where c.Key.ToLowerInvariant().StartsWith(key)
select c.Value).ToList();
}

Expand All @@ -68,6 +217,10 @@ where c.Key.ToLowerInvariant().StartsWith(sKey)

#region QueryString Parsers

public static bool IsWebView {
get { return (HttpContext.Current != null); }
}

public static Guid GetGuidPageIDFromQuery() {
return GetGuidParameterFromQuery("pageid");
}
Expand All @@ -76,24 +229,20 @@ public static Guid GetGuidIDFromQuery() {
return GetGuidParameterFromQuery("id");
}

public static Guid GetGuidParameterFromQuery(string ParmName) {
public static Guid GetGuidParameterFromQuery(string parmName) {
Guid id = Guid.Empty;
if (HttpContext.Current != null) {
if (HttpContext.Current.Request.QueryString[ParmName] != null
&& !string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[ParmName].ToString())) {
id = new Guid(HttpContext.Current.Request.QueryString[ParmName].ToString());
}
if (IsWebView && HttpContext.Current.Request.QueryString[parmName] != null
&& !string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[parmName].ToString())) {
id = new Guid(HttpContext.Current.Request.QueryString[parmName].ToString());
}
return id;
}

public static string GetStringParameterFromQuery(string ParmName) {
public static string GetStringParameterFromQuery(string parmName) {
string id = string.Empty;
if (HttpContext.Current != null) {
if (HttpContext.Current.Request.QueryString[ParmName] != null
&& !string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[ParmName].ToString())) {
id = HttpContext.Current.Request.QueryString[ParmName].ToString();
}
if (IsWebView && HttpContext.Current.Request.QueryString[parmName] != null
&& !string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[parmName].ToString())) {
id = HttpContext.Current.Request.QueryString[parmName].ToString();
}
return id;
}
Expand Down
5 changes: 4 additions & 1 deletion PluginPhotoGallery/GalleryObjects/GalleryMetaData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ internal GalleryMetaData(tblGalleryImageMeta gal) {
public string ImageMetaData { get; set; }

public void ValidateGalleryImage() {
if (string.IsNullOrEmpty(this.GalleryImage)) {
throw new Exception("Image path must be provided.");
}
if (this.GalleryImage.Contains("../") || this.GalleryImage.Contains(@"..\")) {
throw new Exception("Cannot use relative paths.");
}
if (this.GalleryImage.Contains(":")) {
throw new Exception("Cannot specify drive letters.");
throw new Exception("Cannot specify drive letters or other protocols.");
}
if (this.GalleryImage.Contains("//") || this.GalleryImage.Contains(@"\\")) {
throw new Exception("Cannot use UNC paths.");
Expand Down
12 changes: 8 additions & 4 deletions PluginPhotoGallery/PhotoGalleryAdmin.ascx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ protected string SetSitePath(string sPath) {

protected void BuildFolderList() {
List<FileData> lstFolders = new List<FileData>();
var now = DateTime.Now.Date;

string sRoot = HttpContext.Current.Server.MapPath("~/");

Expand All @@ -40,12 +41,12 @@ protected void BuildFolderList() {
subdirs = null;
}

lstFolders.Add(new FileData { FileName = " -- whole site -- ", FolderPath = "/", FileDate = DateTime.Now });
lstFolders.Add(new FileData { FileName = " -- whole site -- ", FolderPath = "/", FileDate = now });

if (subdirs != null) {
foreach (string theDir in subdirs) {
string w = FileDataHelper.MakeWebFolderPath(theDir);
lstFolders.Add(new FileData { FileName = w, FolderPath = w, FileDate = DateTime.Now });
lstFolders.Add(new FileData { FileName = w, FolderPath = w, FileDate = now });

string[] subdirs2;
try {
Expand All @@ -57,7 +58,7 @@ protected void BuildFolderList() {
if (subdirs2 != null) {
foreach (string theDir2 in subdirs2) {
string w2 = FileDataHelper.MakeWebFolderPath(theDir2);
lstFolders.Add(new FileData { FileName = w2, FolderPath = w2, FileDate = DateTime.Now });
lstFolders.Add(new FileData { FileName = w2, FolderPath = w2, FileDate = now });
}
}
}
Expand All @@ -66,9 +67,12 @@ protected void BuildFolderList() {
lstFolders.RemoveAll(f => f.FileName.ToLower().StartsWith(SiteData.AdminFolderPath));
lstFolders.RemoveAll(f => f.FileName.ToLower().StartsWith("/bin/"));
lstFolders.RemoveAll(f => f.FileName.ToLower().StartsWith("/obj/"));
lstFolders.RemoveAll(f => f.FileName.ToLower().StartsWith("/."));
lstFolders.RemoveAll(f => f.FileName.ToLower().StartsWith("/app_code/"));
lstFolders.RemoveAll(f => f.FileName.ToLower().StartsWith("/app_data/"));
lstFolders.RemoveAll(f => f.FileName.ToLower().StartsWith("/app_start/"));

ddlFolders.DataSource = lstFolders.OrderBy(f => f.FileName);
ddlFolders.DataSource = lstFolders.Distinct().OrderBy(f => f.FileName);
ddlFolders.DataBind();
}

Expand Down
5 changes: 4 additions & 1 deletion PluginPhotoGallery/PhotoGalleryAdminMetaData.ascx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,14 @@ protected void btnSave_Click(object sender, EventArgs e) {
}

protected void ValidateGalleryImage(string imageFile) {
if (string.IsNullOrEmpty(imageFile)) {
throw new Exception("Image path must be provided.");
}
if (imageFile.Contains("../") || imageFile.Contains(@"..\")) {
throw new Exception("Cannot use relative paths.");
}
if (imageFile.Contains(":")) {
throw new Exception("Cannot specify drive letters.");
throw new Exception("Cannot specify drive letters or other protocols.");
}
if (imageFile.Contains("//") || imageFile.Contains(@"\\")) {
throw new Exception("Cannot use UNC paths.");
Expand Down
12 changes: 6 additions & 6 deletions PluginPhotoGallery/PhotoGalleryPrettyPhoto.ascx.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using Carrotware.CMS.Core;
using Carrotware.CMS.Interface;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Carrotware.CMS.Core;
using Carrotware.CMS.Interface;

namespace Carrotware.CMS.UI.Plugins.PhotoGallery {

Expand All @@ -26,7 +26,7 @@ public string GetThumbSize() {

[Description("Gallery appearance (pretty photo skin)")]
[Widget(WidgetAttribute.FieldMode.DropDownList, "lstPrettySkins")]
public string PrettyPhotoSkin { get; set; }
public string PrettyPhotoSkin { get; set; } = "light_rounded";

[Widget(WidgetAttribute.FieldMode.DictionaryList)]
public Dictionary<string, string> lstPrettySkins {
Expand Down Expand Up @@ -55,13 +55,13 @@ protected void Page_Load(object sender, EventArgs e) {
try {
string sFoundVal = GetParmValue("PrettyPhotoSkin", "light_rounded");

if (!String.IsNullOrEmpty(sFoundVal)) {
if (!string.IsNullOrEmpty(sFoundVal)) {
this.PrettyPhotoSkin = sFoundVal;
}
} catch (Exception ex) { }
}

if (String.IsNullOrEmpty(PrettyPhotoSkin)) {
if (string.IsNullOrEmpty(PrettyPhotoSkin)) {
PrettyPhotoSkin = "light_rounded";
}

Expand Down
Loading

0 comments on commit 35184e2

Please sign in to comment.