public static HtmlString RenderWidget(string placeHolderName) { StringBuilder sb = new StringBuilder(); string sWidgetZone = String.Empty; string masterWidgetWrapper = String.Empty; string widgetMenuTemplate = String.Empty; string sStatusTemplate = String.Empty; if (SecurityData.AdvancedEditMode) { widgetMenuTemplate = "<li id=\"liMenu\"><a href=\"javascript:[[JS_CALL]]\" id=\"cmsMenuEditLink\" class=\"cmsWidgetBarLink cmsWidgetBarIconPencil\" alt=\"[[CAP]]\" title=\"[[CAP]]\"> [[CAP]]</a></li>"; sWidgetZone = ControlUtilities.GetManifestResourceStream("Carrotware.CMS.UI.Components._WidgetZone.cshtml"); masterWidgetWrapper = ControlUtilities.GetManifestResourceStream("Carrotware.CMS.UI.Components._WidgetWrapper.cshtml"); sWidgetZone = sWidgetZone.Replace("[[PLACEHOLDER]]", placeHolderName); masterWidgetWrapper = masterWidgetWrapper.Replace("[[PLACEHOLDER]]", placeHolderName); } int iWidgetCount = 0; var widgetList = (from w in CmsPage.TheWidgets where w.PlaceholderName.ToLower() == placeHolderName.ToLower() orderby w.WidgetOrder, w.EditDate select w).ToList(); foreach (Widget widget in widgetList) { bool IsWidgetClass = false; string widgetKey = String.Format("WidgetId_{0}_{1}", placeHolderName, iWidgetCount); if (Html.ViewContext.Controller is IContentController) { IContentController cc = (Html.ViewContext.Controller as IContentController); widgetKey = String.Format("WidgetId_{0}", cc.WidgetCount); } iWidgetCount++; string widgetText = String.Empty; string widgetWrapper = String.Empty; Dictionary<string, string> lstMenus = new Dictionary<string, string>(); if (widget.ControlPath.Contains(":")) { string[] path = widget.ControlPath.Split(':'); string objectPrefix = path[0]; string objectClass = path[1]; string altView = path.Length >= 3 ? path[2] : String.Empty; Object obj = null; Object settings = null; try { Type objType = ReflectionUtilities.GetTypeFromString(objectClass); obj = Activator.CreateInstance(objType); if (objectPrefix.ToUpper() != "CLASS") { IsWidgetClass = false; // assumed to be a controller action/method Object attrib = ReflectionUtilities.GetAttribute<WidgetActionSettingModelAttribute>(objType, objectPrefix); if (attrib != null && attrib is WidgetActionSettingModelAttribute) { string attrClass = (attrib as WidgetActionSettingModelAttribute).ClassName; Type s = ReflectionUtilities.GetTypeFromString(attrClass); settings = Activator.CreateInstance(s); } } else { IsWidgetClass = true; // a class widget is its own setting object settings = obj; } if (settings != null) { if (settings is IWidget) { IWidget w = settings as IWidget; w.SiteID = CmsPage.TheSite.SiteID; w.RootContentID = widget.Root_ContentID; w.PageWidgetID = widget.Root_WidgetID; w.IsDynamicInserted = true; w.IsBeingEdited = SecurityData.AdvancedEditMode; w.WidgetClientID = widgetKey; List<WidgetProps> lstProp = widget.ParseDefaultControlProperties(); w.PublicParmValues = lstProp.ToDictionary(t => t.KeyName, t => t.KeyValue); lstMenus = w.JSEditFunctions; if (!lstMenus.Any() && w.EnableEdit) { lstMenus.Add("Edit", "cmsGenericEdit('" + widget.Root_ContentID.ToString() + "','" + widget.Root_WidgetID.ToString() + "')"); } } if (settings is IWidgetView) { if (!String.IsNullOrEmpty(altView)) { (settings as IWidgetView).AlternateViewFile = altView; } } if (settings is IWidgetRawData) { (settings as IWidgetRawData).RawWidgetData = widget.ControlProperties; } } if (obj != null && settings != null && obj is IWidgetDataObject) { (obj as IWidgetDataObject).WidgetPayload = settings; } if (IsWidgetClass && obj is IHtmlString) { widgetText = (obj as IHtmlString).ToHtmlString(); } else { widgetText = GetResultViewStringFromController(objectPrefix, objType, obj); } } catch (Exception ex) { LiteralMessage msg = new LiteralMessage(ex, widgetKey, widget.ControlPath); obj = msg; widgetText = msg.ToHtmlString(); } } widgetText = widgetText ?? String.Empty; if (!widget.ControlPath.Contains(":") && String.IsNullOrEmpty(widgetText)) { string[] path = widget.ControlPath.Split('|'); string viewPath = path[0]; string modelClass = String.Empty; if (path.Length > 1) { modelClass = path[1]; } try { if (viewPath.EndsWith(".cshtml") || viewPath.EndsWith(".vbhtml")) { if (String.IsNullOrEmpty(modelClass)) { widgetText = RenderPartialToString(viewPath); } else { Type objType = ReflectionUtilities.GetTypeFromString(modelClass); Object model = Activator.CreateInstance(objType); if (model is IWidgetRawData) { (model as IWidgetRawData).RawWidgetData = widget.ControlProperties; } if (model is IWidget) { IWidget w = model as IWidget; w.SiteID = CmsPage.TheSite.SiteID; w.RootContentID = widget.Root_ContentID; w.PageWidgetID = widget.Root_WidgetID; w.IsDynamicInserted = true; w.IsBeingEdited = SecurityData.AdvancedEditMode; w.WidgetClientID = widgetKey; List<WidgetProps> lstProp = widget.ParseDefaultControlProperties(); w.PublicParmValues = lstProp.ToDictionary(t => t.KeyName, t => t.KeyValue); lstMenus = w.JSEditFunctions; if (!lstMenus.Any() && w.EnableEdit) { lstMenus.Add("Edit", "cmsGenericEdit('" + widget.Root_ContentID.ToString() + "','" + widget.Root_WidgetID.ToString() + "')"); } } widgetText = RenderPartialToString(viewPath, model); } } } catch (Exception ex) { LiteralMessage msg = new LiteralMessage(ex, widgetKey, widget.ControlPath); widgetText = msg.ToHtmlString(); } } if (widgetText == null || widget.ControlPath.ToLower().EndsWith(".ascx")) { LiteralMessage msg = new LiteralMessage("The widget is not supported.", widgetKey, widget.ControlPath); widgetText = msg.ToHtmlString(); } widgetText = widgetText ?? String.Empty; if (SecurityData.AdvancedEditMode) { if (widget.IsWidgetActive) { sStatusTemplate = "<a href=\"javascript:cmsRemoveWidgetLink('[[ITEM_ID]]');\" id=\"cmsContentRemoveLink\" class=\"cmsWidgetBarLink cmsWidgetBarIconCross\" alt=\"Remove\" title=\"Remove\"> Disable</a>"; } else { sStatusTemplate = "<a href=\"javascript:cmsActivateWidgetLink('[[ITEM_ID]]');\" id=\"cmsActivateWidgetLink\" class=\"cmsWidgetBarLink cmsWidgetBarIconActive\" alt=\"Activate\" title=\"Activate\"> Enable</a>"; } widgetWrapper = masterWidgetWrapper; widgetWrapper = widgetWrapper.Replace("[[STATUS_LINK]]", sStatusTemplate); widgetWrapper = widgetWrapper.Replace("[[WIDGET_PATH]]", widget.ControlPath); widgetWrapper = widgetWrapper.Replace("[[sequence]]", widget.WidgetOrder.ToString()); widgetWrapper = widgetWrapper.Replace("[[ITEM_ID]]", widget.Root_WidgetID.ToString()); CMSPlugin plug = (from p in CmsPage.Plugins where p.FilePath.ToLower() == widget.ControlPath.ToLower() select p).FirstOrDefault(); string captionPrefix = String.Empty; if (!widget.IsWidgetActive) { captionPrefix = String.Format("{0} {1}", CMSConfigHelper.InactivePagePrefix, captionPrefix); } if (widget.IsRetired) { captionPrefix = String.Format("{0} {1}", CMSConfigHelper.RetiredPagePrefix, captionPrefix); } if (widget.IsUnReleased) { captionPrefix = String.Format("{0} {1}", CMSConfigHelper.UnreleasedPagePrefix, captionPrefix); } if (widget.IsWidgetPendingDelete) { captionPrefix = String.Format("{0} {1}", CMSConfigHelper.PendingDeletePrefix, captionPrefix); } if (plug != null) { string sysControl = (plug.SystemPlugin ? "[CMS]" : String.Empty); widgetWrapper = widgetWrapper.Replace("[[WIDGET_CAPTION]]", String.Format("{0} {1} {2}", captionPrefix, plug.Caption, sysControl).Trim()); } else { widgetWrapper = widgetWrapper.Replace("[[WIDGET_CAPTION]]", String.Format("{0} UNTITLED", captionPrefix).Trim()); } StringBuilder sbMenu = new StringBuilder(); sbMenu.AppendLine(); if (lstMenus != null) { foreach (var d in lstMenus) { sbMenu.AppendLine(widgetMenuTemplate.Replace("[[JS_CALL]]", d.Value).Replace("[[CAP]]", d.Key)); } } widgetWrapper = widgetWrapper.Replace("[[MENU_ITEMS]]", sbMenu.ToString().Trim()); widgetWrapper = widgetWrapper.Replace("[[WIDGET_CAPTION]]", widget.ControlPath + captionPrefix); widgetWrapper = widgetWrapper.Replace("[[CONTENT]]", widgetText); } else { widgetWrapper = widgetText; } if (!String.IsNullOrEmpty(widgetWrapper)) { sb.AppendLine(widgetWrapper); } } string bodyText = String.Empty; if (SecurityData.AdvancedEditMode) { bodyText = sWidgetZone.Replace("[[CONTENT]]", sb.ToString()); } else { bodyText = sb.ToString(); } return new HtmlString(bodyText); }
public static HtmlString RenderWidget(string placeHolderName) { StringBuilder sb = new StringBuilder(); string sWidgetZone = String.Empty; string masterWidgetWrapper = String.Empty; string widgetMenuTemplate = String.Empty; string sStatusTemplate = String.Empty; if (SecurityData.AdvancedEditMode) { widgetMenuTemplate = "<li id=\"liMenu\"><a href=\"javascript:[[JS_CALL]]\" id=\"cmsMenuEditLink\" class=\"cmsWidgetBarLink cmsWidgetBarIconPencil\" alt=\"[[CAP]]\" title=\"[[CAP]]\"> [[CAP]]</a></li>"; sWidgetZone = ControlUtilities.GetManifestResourceStream("Carrotware.CMS.UI.Components._WidgetZone.cshtml"); masterWidgetWrapper = ControlUtilities.GetManifestResourceStream("Carrotware.CMS.UI.Components._WidgetWrapper.cshtml"); sWidgetZone = sWidgetZone.Replace("[[PLACEHOLDER]]", placeHolderName); masterWidgetWrapper = masterWidgetWrapper.Replace("[[PLACEHOLDER]]", placeHolderName); } int iWidgetCount = 0; var widgetList = (from w in CmsPage.TheWidgets where w.PlaceholderName.ToLowerInvariant() == placeHolderName.ToLowerInvariant() orderby w.WidgetOrder, w.EditDate select w).ToList(); foreach (Widget widget in widgetList) { bool IsWidgetClass = false; string widgetKey = String.Format("WidgetId_{0}_{1}", placeHolderName, iWidgetCount); if (Html.ViewContext.Controller is IContentController) { IContentController cc = (Html.ViewContext.Controller as IContentController); widgetKey = String.Format("WidgetId_{0}", cc.WidgetCount); } iWidgetCount++; string widgetText = String.Empty; string widgetWrapper = String.Empty; Dictionary <string, string> lstMenus = new Dictionary <string, string>(); if (widget.ControlPath.Contains(":")) { string[] path = widget.ControlPath.Split(':'); string objectPrefix = path[0]; string objectClass = path[1]; string altView = path.Length >= 3 ? path[2] : String.Empty; Object obj = null; Object settings = null; try { Type objType = ReflectionUtilities.GetTypeFromString(objectClass); obj = Activator.CreateInstance(objType); if (objectPrefix.ToUpperInvariant() != "CLASS") { IsWidgetClass = false; // assumed to be a controller action/method Object attrib = ReflectionUtilities.GetAttribute <WidgetActionSettingModelAttribute>(objType, objectPrefix); if (attrib != null && attrib is WidgetActionSettingModelAttribute) { string attrClass = (attrib as WidgetActionSettingModelAttribute).ClassName; Type s = ReflectionUtilities.GetTypeFromString(attrClass); settings = Activator.CreateInstance(s); } } else { IsWidgetClass = true; // a class widget is its own setting object settings = obj; } if (settings != null) { if (settings is IWidget) { IWidget w = settings as IWidget; w.SiteID = CmsPage.TheSite.SiteID; w.RootContentID = widget.Root_ContentID; w.PageWidgetID = widget.Root_WidgetID; w.IsDynamicInserted = true; w.IsBeingEdited = SecurityData.AdvancedEditMode; w.WidgetClientID = widgetKey; List <WidgetProps> lstProp = widget.ParseDefaultControlProperties(); w.PublicParmValues = lstProp.ToDictionary(t => t.KeyName, t => t.KeyValue); lstMenus = w.JSEditFunctions; if (!lstMenus.Any() && w.EnableEdit) { lstMenus.Add("Edit", "cmsGenericEdit('" + widget.Root_ContentID.ToString() + "','" + widget.Root_WidgetID.ToString() + "')"); } } if (settings is IWidgetView) { if (!String.IsNullOrEmpty(altView)) { (settings as IWidgetView).AlternateViewFile = altView; } } if (settings is IWidgetRawData) { (settings as IWidgetRawData).RawWidgetData = widget.ControlProperties; } } if (obj != null && settings != null && obj is IWidgetDataObject) { (obj as IWidgetDataObject).WidgetPayload = settings; } if (IsWidgetClass && obj is IHtmlString) { widgetText = (obj as IHtmlString).ToHtmlString(); } else { widgetText = GetResultViewStringFromController(objectPrefix, objType, obj); } } catch (Exception ex) { LiteralMessage msg = new LiteralMessage(ex, widgetKey, widget.ControlPath); obj = msg; widgetText = msg.ToHtmlString(); } } widgetText = widgetText ?? String.Empty; if (!widget.ControlPath.Contains(":") && String.IsNullOrEmpty(widgetText)) { string[] path = widget.ControlPath.Split('|'); string viewPath = path[0]; string modelClass = String.Empty; if (path.Length > 1) { modelClass = path[1]; } try { if (viewPath.EndsWith(".cshtml") || viewPath.EndsWith(".vbhtml")) { if (String.IsNullOrEmpty(modelClass)) { widgetText = RenderPartialToString(viewPath); } else { Type objType = ReflectionUtilities.GetTypeFromString(modelClass); Object model = Activator.CreateInstance(objType); if (model is IWidgetRawData) { (model as IWidgetRawData).RawWidgetData = widget.ControlProperties; } if (model is IWidget) { IWidget w = model as IWidget; w.SiteID = CmsPage.TheSite.SiteID; w.RootContentID = widget.Root_ContentID; w.PageWidgetID = widget.Root_WidgetID; w.IsDynamicInserted = true; w.IsBeingEdited = SecurityData.AdvancedEditMode; w.WidgetClientID = widgetKey; List <WidgetProps> lstProp = widget.ParseDefaultControlProperties(); w.PublicParmValues = lstProp.ToDictionary(t => t.KeyName, t => t.KeyValue); lstMenus = w.JSEditFunctions; if (!lstMenus.Any() && w.EnableEdit) { lstMenus.Add("Edit", "cmsGenericEdit('" + widget.Root_ContentID.ToString() + "','" + widget.Root_WidgetID.ToString() + "')"); } } widgetText = RenderPartialToString(viewPath, model); } } } catch (Exception ex) { LiteralMessage msg = new LiteralMessage(ex, widgetKey, widget.ControlPath); widgetText = msg.ToHtmlString(); } } if (widgetText == null || widget.ControlPath.ToLowerInvariant().EndsWith(".ascx")) { LiteralMessage msg = new LiteralMessage("The widget is not supported.", widgetKey, widget.ControlPath); widgetText = msg.ToHtmlString(); } widgetText = widgetText ?? String.Empty; if (SecurityData.AdvancedEditMode) { if (widget.IsWidgetActive) { sStatusTemplate = "<a href=\"javascript:cmsRemoveWidgetLink('[[ITEM_ID]]');\" id=\"cmsContentRemoveLink\" class=\"cmsWidgetBarLink cmsWidgetBarIconCross\" alt=\"Remove\" title=\"Remove\"> Disable</a>"; } else { sStatusTemplate = "<a href=\"javascript:cmsActivateWidgetLink('[[ITEM_ID]]');\" id=\"cmsActivateWidgetLink\" class=\"cmsWidgetBarLink cmsWidgetBarIconActive\" alt=\"Activate\" title=\"Activate\"> Enable</a>"; } widgetWrapper = masterWidgetWrapper; widgetWrapper = widgetWrapper.Replace("[[STATUS_LINK]]", sStatusTemplate); widgetWrapper = widgetWrapper.Replace("[[WIDGET_PATH]]", widget.ControlPath); widgetWrapper = widgetWrapper.Replace("[[sequence]]", widget.WidgetOrder.ToString()); widgetWrapper = widgetWrapper.Replace("[[ITEM_ID]]", widget.Root_WidgetID.ToString()); CMSPlugin plug = (from p in CmsPage.Plugins where p.FilePath.ToLowerInvariant() == widget.ControlPath.ToLowerInvariant() select p).FirstOrDefault(); string captionPrefix = String.Empty; if (!widget.IsWidgetActive) { captionPrefix = String.Format("{0} {1}", CMSConfigHelper.InactivePagePrefix, captionPrefix); } if (widget.IsRetired) { captionPrefix = String.Format("{0} {1}", CMSConfigHelper.RetiredPagePrefix, captionPrefix); } if (widget.IsUnReleased) { captionPrefix = String.Format("{0} {1}", CMSConfigHelper.UnreleasedPagePrefix, captionPrefix); } if (widget.IsWidgetPendingDelete) { captionPrefix = String.Format("{0} {1}", CMSConfigHelper.PendingDeletePrefix, captionPrefix); } if (plug != null) { string sysControl = (plug.SystemPlugin ? "[CMS]" : String.Empty); widgetWrapper = widgetWrapper.Replace("[[WIDGET_CAPTION]]", String.Format("{0} {1} {2}", captionPrefix, plug.Caption, sysControl).Trim()); } else { widgetWrapper = widgetWrapper.Replace("[[WIDGET_CAPTION]]", String.Format("{0} UNTITLED", captionPrefix).Trim()); } StringBuilder sbMenu = new StringBuilder(); sbMenu.AppendLine(); if (lstMenus != null) { foreach (var d in lstMenus) { sbMenu.AppendLine(widgetMenuTemplate.Replace("[[JS_CALL]]", d.Value).Replace("[[CAP]]", d.Key)); } } widgetWrapper = widgetWrapper.Replace("[[MENU_ITEMS]]", sbMenu.ToString().Trim()); widgetWrapper = widgetWrapper.Replace("[[WIDGET_CAPTION]]", widget.ControlPath + captionPrefix); widgetWrapper = widgetWrapper.Replace("[[CONTENT]]", widgetText); } else { widgetWrapper = widgetText; } if (!String.IsNullOrEmpty(widgetWrapper)) { sb.AppendLine(widgetWrapper); } } string bodyText = String.Empty; if (SecurityData.AdvancedEditMode) { bodyText = sWidgetZone.Replace("[[CONTENT]]", sb.ToString()); } else { bodyText = sb.ToString(); } return(new HtmlString(bodyText)); }