//public static ExpandoObject GetExpandoForItem(Item item, ModuleConfiguration config) //{ //} public static ExpandoObject GetExpandoForItem(Item item) { var fields = Field.GetAllForDefinition(item.DefinitionGuid); if (fields == null || item == null) { return(null); } dynamic itemExpando = new ExpandoObject(); itemExpando.Guid = item.ItemGuid; itemExpando.SortOrder = item.SortOrder; List <ItemFieldValue> fieldValues = ItemFieldValue.GetItemValues(item.ItemGuid); foreach (Field field in fields) { foreach (ItemFieldValue fieldValue in fieldValues) { if (field.FieldGuid == fieldValue.FieldGuid) { ((IDictionary <String, Object>)itemExpando)[field.Name] = fieldValue.FieldValue; } } } return(itemExpando); }
private IndexItem GetIndexItem(PageSettings pageSettings, int moduleID, Item item) { Module module = new Module(moduleID); log.Debug($"moduleid: {moduleID} for module {module.ModuleTitle}"); ModuleConfiguration config = new ModuleConfiguration(module); if (!config.IncludeInSearch) { return(null); } SuperFlexiDisplaySettings displaySettings = new SuperFlexiDisplaySettings(); ModuleDefinition flexiFeature = new ModuleDefinition(config.FeatureGuid); IndexItem indexItem = new IndexItem(); indexItem.SiteId = pageSettings.SiteId; indexItem.PageId = pageSettings.PageId; indexItem.PageName = pageSettings.PageName; indexItem.ViewRoles = pageSettings.AuthorizedRoles; indexItem.FeatureId = flexiFeature.FeatureGuid.ToString(); indexItem.FeatureName = String.IsNullOrWhiteSpace(config.ModuleFriendlyName) ? module.ModuleTitle : config.ModuleFriendlyName; indexItem.FeatureResourceFile = flexiFeature.ResourceFile; indexItem.ItemId = item.ItemID; indexItem.CreatedUtc = item.CreatedUtc; indexItem.LastModUtc = item.LastModUtc; if (pageSettings.UseUrl) { if (pageSettings.UrlHasBeenAdjustedForFolderSites) { indexItem.ViewPage = pageSettings.UnmodifiedUrl.Replace("~/", string.Empty); } else { indexItem.ViewPage = pageSettings.Url.Replace("~/", string.Empty); } indexItem.UseQueryStringParams = false; } SearchDef searchDef = SearchDef.GetByFieldDefinition(item.DefinitionGuid); bool hasSearchDef = true; if (searchDef == null) { searchDef = new SearchDef(); hasSearchDef = false; } System.Text.StringBuilder sbTitle = new System.Text.StringBuilder(searchDef.Title); System.Text.StringBuilder sbKeywords = new System.Text.StringBuilder(searchDef.Keywords); System.Text.StringBuilder sbDescription = new System.Text.StringBuilder(searchDef.Description); System.Text.StringBuilder sbLink = new System.Text.StringBuilder(searchDef.Link); System.Text.StringBuilder sbLinkQueryAddendum = new System.Text.StringBuilder(searchDef.LinkQueryAddendum); SiteSettings siteSettings = new SiteSettings(pageSettings.SiteGuid); SuperFlexiHelpers.ReplaceStaticTokens(sbTitle, config, false, displaySettings, module, pageSettings, siteSettings, out sbTitle); SuperFlexiHelpers.ReplaceStaticTokens(sbKeywords, config, false, displaySettings, module, pageSettings, siteSettings, out sbKeywords); SuperFlexiHelpers.ReplaceStaticTokens(sbDescription, config, false, displaySettings, module, pageSettings, siteSettings, out sbDescription); SuperFlexiHelpers.ReplaceStaticTokens(sbLink, config, false, displaySettings, module, pageSettings, siteSettings, out sbLink); SuperFlexiHelpers.ReplaceStaticTokens(sbLinkQueryAddendum, config, false, displaySettings, module, pageSettings, siteSettings, out sbLinkQueryAddendum); var fieldValues = ItemFieldValue.GetItemValues(item.ItemGuid); log.Debug($"SuperFlexi Index: total field value count for ItemGuid ({item.ItemGuid}) is {fieldValues.Count}"); foreach (ItemFieldValue fieldValue in fieldValues) { Field field = new Field(fieldValue.FieldGuid); if (field == null || !field.Searchable) { continue; } if (hasSearchDef) { sbTitle.Replace(field.Token, fieldValue.FieldValue); sbKeywords.Replace(field.Token, fieldValue.FieldValue); sbDescription.Replace(field.Token, fieldValue.FieldValue); sbLink.Replace(field.Token, fieldValue.FieldValue); sbLinkQueryAddendum.Replace(field.Token, fieldValue.FieldValue); } else { sbKeywords.Append(fieldValue.FieldValue); } if (debugLog) { log.DebugFormat("RebuildIndex indexing item [{0} = {1}]", field.Name, fieldValue.FieldValue); } } if (hasSearchDef) { sbTitle.Replace("$_ItemID_$", item.ItemID.ToString()); sbKeywords.Replace("$_ItemID_$", item.ItemID.ToString()); sbDescription.Replace("$_ItemID_$", item.ItemID.ToString()); sbLink.Replace("$_ItemID_$", item.ItemID.ToString()); sbLinkQueryAddendum.Replace("$_ItemID_$", item.ItemID.ToString()); indexItem.Content = sbDescription.ToString(); indexItem.Title = sbTitle.ToString(); if (sbLink.Length > 0) { indexItem.ViewPage = sbLink.ToString(); } if (sbLinkQueryAddendum.Length > 0) { indexItem.QueryStringAddendum = sbLinkQueryAddendum.ToString(); indexItem.UseQueryStringParams = false; } } else { indexItem.ModuleTitle = pageSettings.PageName; indexItem.Title = String.IsNullOrWhiteSpace(config.ModuleFriendlyName) ? module.ModuleTitle : config.ModuleFriendlyName; } indexItem.PageMetaKeywords = sbKeywords.ToString(); indexItem.Categories = sbKeywords.ToString(); return(indexItem); }
private void PopulateControls() { string featuredImageUrl = string.Empty; string markupTop = string.Empty; string markupBottom = string.Empty; featuredImageUrl = String.IsNullOrWhiteSpace(config.InstanceFeaturedImage) ? featuredImageUrl : WebUtils.GetRelativeSiteRoot() + config.InstanceFeaturedImage; markupTop = displaySettings.ModuleInstanceMarkupTop; markupBottom = displaySettings.ModuleInstanceMarkupBottom; strOutput.Append(markupTop); if (config.UseHeader && config.HeaderLocation == "InnerBodyPanel" && !String.IsNullOrWhiteSpace(config.HeaderContent) && !String.Equals(config.HeaderContent, "<p> </p>")) { try { strOutput.Append(string.Format(displaySettings.HeaderContentFormat, config.HeaderContent)); } catch (FormatException ex) { log.ErrorFormat(markupErrorFormat, "HeaderContentFormat", moduleTitle, ex); } } StringBuilder jsonString = new StringBuilder(); StringWriter stringWriter = new StringWriter(jsonString); JsonTextWriter jsonWriter = new JsonTextWriter(stringWriter); // http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_DateTimeZoneHandling.htm jsonWriter.DateTimeZoneHandling = DateTimeZoneHandling.Utc; // http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_DateFormatHandling.htm jsonWriter.DateFormatHandling = DateFormatHandling.IsoDateFormat; string jsonObjName = "sflexi" + module.ModuleId.ToString() + (config.IsGlobalView ? "Modules" : "Items"); if (config.RenderJSONOfData) { jsonWriter.WriteRaw("var " + jsonObjName + " = "); if (config.JsonLabelObjects || config.IsGlobalView) { jsonWriter.WriteStartObject(); } else { jsonWriter.WriteStartArray(); } } List <IndexedStringBuilder> itemsMarkup = new List <IndexedStringBuilder>(); //List<Item> categorizedItems = new List<Item>(); bool usingGlobalViewMarkup = !String.IsNullOrWhiteSpace(displaySettings.GlobalViewMarkup); int currentModuleID = -1; foreach (Item item in items) { bool itemIsEditable = isEditable || WebUser.IsInRoles(item.EditRoles); bool itemIsViewable = WebUser.IsInRoles(item.ViewRoles) || itemIsEditable; if (!itemIsViewable) { continue; } //int itemCount = 0; //StringBuilder content = new StringBuilder(); IndexedStringBuilder content = new IndexedStringBuilder(); ModuleConfiguration itemModuleConfig = new ModuleConfiguration(module); item.ModuleFriendlyName = itemModuleConfig.ModuleFriendlyName; if (String.IsNullOrWhiteSpace(itemModuleConfig.ModuleFriendlyName)) { Module itemModule = new Module(item.ModuleGuid); if (itemModule != null) { item.ModuleFriendlyName = itemModule.ModuleTitle; } } if (config.IsGlobalView) { content.SortOrder1 = itemModuleConfig.GlobalViewSortOrder; content.SortOrder2 = item.SortOrder; } else { content.SortOrder1 = item.SortOrder; } List <ItemFieldValue> fieldValues = ItemFieldValue.GetItemValues(item.ItemGuid); //using item.ModuleID here because if we are using a 'global view' we need to be sure the item edit link uses the correct module id. string itemEditUrl = WebUtils.GetSiteRoot() + "/SuperFlexi/Edit.aspx?pageid=" + pageId + "&mid=" + item.ModuleID + "&itemid=" + item.ItemID; string itemEditLink = itemIsEditable ? String.Format(displaySettings.ItemEditLinkFormat, itemEditUrl) : string.Empty; if (config.RenderJSONOfData) { if (config.IsGlobalView) { if (currentModuleID != item.ModuleID) { if (currentModuleID != -1) { jsonWriter.WriteEndObject(); jsonWriter.WriteEndObject(); } currentModuleID = item.ModuleID; //always label objects in globalview jsonWriter.WritePropertyName("m" + currentModuleID.ToString()); jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName("Module"); jsonWriter.WriteValue(item.ModuleFriendlyName); jsonWriter.WritePropertyName("Items"); jsonWriter.WriteStartObject(); } } if (config.JsonLabelObjects || config.IsGlobalView) { jsonWriter.WritePropertyName("i" + item.ItemID.ToString()); } jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName("ItemId"); jsonWriter.WriteValue(item.ItemID.ToString()); jsonWriter.WritePropertyName("SortOrder"); jsonWriter.WriteValue(item.SortOrder.ToString()); if (IsEditable) { jsonWriter.WritePropertyName("EditUrl"); jsonWriter.WriteValue(itemEditUrl); } } content.Append(displaySettings.ItemMarkup); foreach (Field field in fields) { if (String.IsNullOrWhiteSpace(field.Token)) { field.Token = "$_NONE_$"; //just in case someone has loaded the database with fields without using a source file. } bool fieldValueFound = false; foreach (ItemFieldValue fieldValue in fieldValues) { if (field.FieldGuid == fieldValue.FieldGuid) { fieldValueFound = true; if (String.IsNullOrWhiteSpace(fieldValue.FieldValue) || fieldValue.FieldValue.StartsWith("&deleted&") || fieldValue.FieldValue.StartsWith("&deleted&") || fieldValue.FieldValue.StartsWith("<p>&deleted&</p>") || fieldValue.FieldValue.StartsWith("<p>&deleted&</p>")) { content.Replace("^" + field.Token + "^", string.Empty); content.Replace("^" + field.Token, string.Empty); content.Replace(field.Token + "^", string.Empty); content.Replace(field.Token, string.Empty); } else { if (IsDateField(field)) { DateTime dateTime = new DateTime(); if (DateTime.TryParse(fieldValue.FieldValue, out dateTime)) { /// ^field.Token is used when we don't want the preTokenString and postTokenString to be used content.Replace("^" + field.Token + "^", dateTime.ToString(field.DateFormat)); content.Replace("^" + field.Token, dateTime.ToString(field.DateFormat) + field.PostTokenString); content.Replace(field.Token + "^", field.PreTokenString + dateTime.ToString(field.DateFormat)); content.Replace(field.Token, field.PreTokenString + dateTime.ToString(field.DateFormat) + field.PostTokenString); } } if (IsCheckBoxListField(field) || IsRadioButtonListField(field)) { foreach (CheckBoxListMarkup cblm in config.CheckBoxListMarkups) { if (cblm.Field == field.Name) { StringBuilder cblmContent = new StringBuilder(); List <string> values = fieldValue.FieldValue.SplitOnCharAndTrim(';'); if (values.Count > 0) { foreach (string value in values) { //why did we use _ValueItemID_ here instead of _ItemID_? cblmContent.Append(cblm.Markup.Replace(field.Token, value).Replace("$_ValueItemID_$", item.ItemID.ToString()) + cblm.Separator); cblm.SelectedValues.Add(new CheckBoxListMarkup.SelectedValue { Value = value, ItemID = item.ItemID }); //cblm.SelectedValues.Add(fieldValue); } } cblmContent.Length -= cblm.Separator.Length; content.Replace(cblm.Token, cblmContent.ToString()); } } } //else //{ /// ^field.Token is used when we don't want the preTokenString and postTokenString to be used content.Replace("^" + field.Token + "^", fieldValue.FieldValue); content.Replace("^" + field.Token, fieldValue.FieldValue + field.PostTokenString); content.Replace(field.Token + "^", field.PreTokenString + fieldValue.FieldValue); content.Replace(field.Token, field.PreTokenString + fieldValue.FieldValue + field.PostTokenString); //} } //if (!String.IsNullOrWhiteSpace(field.LinkedField)) //{ // Field linkedField = fields.Find(delegate(Field f) { return f.Name == field.LinkedField; }); // if (linkedField != null) // { // ItemFieldValue linkedValue = fieldValues.Find(delegate(ItemFieldValue fv) { return fv.FieldGuid == linkedField.FieldGuid; }); // content.Replace(linkedField.Token, linkedValue.FieldValue); // } //} if (config.RenderJSONOfData) { jsonWriter.WritePropertyName(field.Name); //if (IsDateField(field)) //{ // DateTime dateTime = new DateTime(); // if (DateTime.TryParse(fieldValue.FieldValue, out dateTime)) // { // jsonWriter.WriteValue(dateTime); // } //} //else //{ jsonWriter.WriteValue(fieldValue.FieldValue); //} } } } if (!fieldValueFound) { content.Replace(field.Token, field.DefaultValue); } } if (config.RenderJSONOfData) { //if (config.IsGlobalView) //{ // jsonWriter.WriteEndObject(); //} jsonWriter.WriteEndObject(); } content.Replace("$_EditLink_$", itemEditLink); content.Replace("$_ItemID_$", item.ItemID.ToString()); content.Replace("$_SortOrder_$", item.SortOrder.ToString()); if (!String.IsNullOrWhiteSpace(content)) { itemsMarkup.Add(content); } } if (config.DescendingSort) { itemsMarkup.Sort(delegate(IndexedStringBuilder a, IndexedStringBuilder b) { int xdiff = b.SortOrder1.CompareTo(a.SortOrder1); if (xdiff != 0) { return(xdiff); } else { return(b.SortOrder2.CompareTo(a.SortOrder2)); } }); } else { itemsMarkup.Sort(delegate(IndexedStringBuilder a, IndexedStringBuilder b) { int xdiff = a.SortOrder1.CompareTo(b.SortOrder1); if (xdiff != 0) { return(xdiff); } else { return(a.SortOrder2.CompareTo(b.SortOrder2)); } }); } StringBuilder allItems = new StringBuilder(); if (displaySettings.ItemsPerGroup == -1) { foreach (IndexedStringBuilder sb in itemsMarkup) { //allItems.Append(displaySettings.GlobalViewModuleGroupMarkup.Replace("$_ModuleGroupName_$", sb.GroupName)); allItems.Append(sb.ToString()); } if (usingGlobalViewMarkup) { strOutput.AppendFormat(displaySettings.ItemsWrapperFormat, displaySettings.GlobalViewMarkup.Replace("$_ModuleGroups_$", allItems.ToString())); } else { strOutput.AppendFormat(displaySettings.ItemsWrapperFormat, allItems.ToString()); } } else { int itemIndex = 0; decimal totalGroupCount = Math.Ceiling(itemsMarkup.Count / Convert.ToDecimal(displaySettings.ItemsPerGroup)); if (totalGroupCount < 1 && itemsMarkup.Count > 0) { totalGroupCount = 1; } int currentGroup = 1; List <StringBuilder> groups = new List <StringBuilder>(); while (currentGroup <= totalGroupCount && itemIndex < itemsMarkup.Count) { StringBuilder group = new StringBuilder(); group.Append(displaySettings.ItemsRepeaterMarkup); //group.SortOrder1 = itemsMarkup[itemIndex].SortOrder1; //group.SortOrder2 = itemsMarkup[itemIndex].SortOrder2; //group.GroupName = itemsMarkup[itemIndex].GroupName; for (int i = 0; i < displaySettings.ItemsPerGroup; i++) { if (itemIndex < itemsMarkup.Count) { group.Replace("$_Items[" + i.ToString() + "]_$", itemsMarkup[itemIndex].ToString()); itemIndex++; } else { break; } } groups.Add(group); currentGroup++; } //groups.Sort(delegate (IndexedStringBuilder a, IndexedStringBuilder b) { // int xdiff = a.SortOrder1.CompareTo(b.SortOrder1); // if (xdiff != 0) return xdiff; // else return a.SortOrder2.CompareTo(b.SortOrder2); //}); foreach (StringBuilder group in groups) { allItems.Append(group.ToString()); } strOutput.AppendFormat(displaySettings.ItemsWrapperFormat, Regex.Replace(allItems.ToString(), @"(\$_Items\[[0-9]+\]_\$)", string.Empty, RegexOptions.Multiline)); } //strOutput.Append(displaySettings.ItemListMarkupBottom); if (config.RenderJSONOfData) { if (config.JsonLabelObjects || config.IsGlobalView) { jsonWriter.WriteEndObject(); if (config.IsGlobalView) { jsonWriter.WriteEndObject(); jsonWriter.WriteEnd(); } } else { jsonWriter.WriteEndArray(); } MarkupScript jsonScript = new MarkupScript(); jsonWriter.Close(); stringWriter.Close(); jsonScript.RawScript = stringWriter.ToString(); jsonScript.Position = config.JsonRenderLocation; jsonScript.ScriptName = "sflexi" + module.ModuleId.ToString() + config.MarkupDefinitionName.ToCleanFileName() + "-JSON"; List <MarkupScript> scripts = new List <MarkupScript>(); scripts.Add(jsonScript); SuperFlexiHelpers.SetupScripts(scripts, config, displaySettings, IsEditable, IsPostBack, ClientID, ModuleId, PageId, Page, this); } if (config.UseFooter && config.FooterLocation == "InnerBodyPanel" && !String.IsNullOrWhiteSpace(config.FooterContent) && !String.Equals(config.FooterContent, "<p> </p>")) { try { strOutput.AppendFormat(displaySettings.FooterContentFormat, config.FooterContent); } catch (System.FormatException ex) { log.ErrorFormat(markupErrorFormat, "FooterContentFormat", moduleTitle, ex); } } strOutput.Append(markupBottom); SuperFlexiHelpers.ReplaceStaticTokens(strOutput, config, isEditable, displaySettings, module.ModuleId, pageSettings, siteSettings, out strOutput); //this is for displaying all of the selected values from the items outside of the items themselves foreach (CheckBoxListMarkup cblm in config.CheckBoxListMarkups) { StringBuilder cblmContent = new StringBuilder(); if (fields.Count > 0 && cblm.SelectedValues.Count > 0) { Field theField = fields.Where(field => field.Name == cblm.Field).Single(); if (theField != null) { List <CheckBoxListMarkup.SelectedValue> distinctSelectedValues = new List <CheckBoxListMarkup.SelectedValue>(); foreach (CheckBoxListMarkup.SelectedValue selectedValue in cblm.SelectedValues) { CheckBoxListMarkup.SelectedValue match = distinctSelectedValues.Find(i => i.Value == selectedValue.Value); if (match == null) { distinctSelectedValues.Add(selectedValue); } else { match.Count++; } } //var selectedValues = cblm.SelectedValues.GroupBy(selectedValue => selectedValue.Value) // .Select(distinctSelectedValue => new { Value = distinctSelectedValue.Key, Count = distinctSelectedValue.Count(), ItemID = distinctSelectedValue.ItemID }) // .OrderBy(x => x.Value); foreach (CheckBoxListMarkup.SelectedValue value in distinctSelectedValues) { cblmContent.Append(cblm.Markup.Replace(theField.Token, value.Value).Replace("$_ValueItemID_$", value.ItemID.ToString()) + cblm.Separator); cblmContent.Replace("$_CBLValueCount_$", value.Count.ToString()); } } if (cblmContent.Length >= cblm.Separator.Length) { cblmContent.Length -= cblm.Separator.Length; } strOutput.Replace(cblm.Token, cblmContent.ToString()); } strOutput.Replace(cblm.Token, string.Empty); } theLit.Text = strOutput.ToString(); }
private void PopulateControls() { string featuredImageUrl = string.Empty; featuredImageUrl = String.IsNullOrWhiteSpace(config.InstanceFeaturedImage) ? featuredImageUrl : SiteUtils.GetNavigationSiteRoot() + config.InstanceFeaturedImage; //dynamic expando = new ExpandoObject(); var itemModels = new List <dynamic>(); //var model = expando as IDictionary<string, object>; WidgetModel model = new WidgetModel(); model.ModuleGuid = module.ModuleGuid; model.ModuleId = module.ModuleId; model.ModuleTitle = module.ModuleTitle; model.ModuleFriendlyName = config.ModuleFriendlyName; model.SortDescending = config.DescendingSort; model.GlobalView = config.IsGlobalView; model.GlobalViewSortOrder = config.GlobalViewSortOrder; model.Header = config.HeaderContent; model.Footer = config.FooterContent; model.FeaturedImage = featuredImageUrl; model.PageId = pageSettings.PageId; model.PageUrl = pageSettings.Url; model.PageName = pageSettings.PageName; //dynamic dModel = model; foreach (Item item in items) { bool itemIsEditable = isEditable || WebUser.IsInRoles(item.EditRoles); bool itemIsViewable = WebUser.IsInRoles(item.ViewRoles) || itemIsEditable; if (!itemIsViewable) { continue; } string itemEditUrl = SiteUtils.GetNavigationSiteRoot() + "/SuperFlexi/Edit.aspx?pageid=" + pageId + "&mid=" + item.ModuleID + "&itemid=" + item.ItemID; //var itemModel = new ItemModel(); //itemModel.Id = item.ItemID; //itemModel.Guid = item.ItemGuid; //itemModel.SortOrder = item.SortOrder; //itemModel.EditUrl = itemEditUrl; dynamic itemModel = new ExpandoObject(); itemModel.Id = item.ItemID; itemModel.Guid = item.ItemGuid; itemModel.SortOrder = item.SortOrder; itemModel.EditUrl = itemEditUrl; //var itemModel = localexpando as IDictionary<string, object>; //itemModel.Add("Id", item.ItemID); //itemModel.Add("Guid", item.ItemGuid); //itemModel.Add("SortOrder", item.SortOrder); //itemModel.Add("EditUrl", itemEditUrl); List <ItemFieldValue> fieldValues = ItemFieldValue.GetItemValues(item.ItemGuid); //using item.ModuleID here because if we are using a 'global view' we need to be sure the item edit link uses the correct module id. //string itemEditLink = itemIsEditable ? String.Format(displaySettings.ItemEditLinkFormat, itemEditUrl) : string.Empty; foreach (Field field in fields) { foreach (ItemFieldValue fieldValue in fieldValues) { if (field.FieldGuid == fieldValue.FieldGuid) { ((IDictionary <String, String>)itemModel)[field.Name] = fieldValue.FieldValue; //var thisItem = itemModel; //itemModel.AddProperty(field.Name.ToString(), fieldValue.FieldValue); //itemModel = thisItem; //dItemModel[field.Name.ToString()] = fieldValue.FieldValue; //itemModel.Add(field.Name.ToString(), fieldValue.FieldValue); } } } itemModels.Add(itemModel); } //dModel["Items"] = itemModels; model.Items = itemModels; //model.AddProperty("Items", itemModels); //theLit.Text = RazorBridge.RenderPartialToString("_SuperFlexiRazor", model, "SuperFlexi"); theLit.Text = RazorBridge.RenderPartialToString("_SuperFlexiRazor", model, "SuperFlexi"); }
protected override void RenderContents(HtmlTextWriter output) { string featuredImageUrl = string.Empty; featuredImageUrl = String.IsNullOrWhiteSpace(config.InstanceFeaturedImage) ? featuredImageUrl : SiteUtils.GetNavigationSiteRoot() + config.InstanceFeaturedImage; //dynamic expando = new ExpandoObject(); var superFlexiItemClass = CreateClass(); var itemModels = new List <object> (); //var model = expando as IDictionary<string, object>; WidgetModel model = new WidgetModel { Module = new ModuleModel { Id = module.ModuleId, Guid = module.ModuleGuid, Title = module.ModuleTitle, TitleElement = module.HeadElement }, Config = Config, Page = new PageModel { Id = CurrentPage.PageId, Url = CurrentPage.Url, Name = CurrentPage.PageName }, Site = new SiteModel { Id = module.SiteId, SkinPath = SiteUtils.DetermineSkinBaseUrl(SiteUtils.GetSkinName(true, this.Page)) }, SiteRoot = SiteUtils.GetNavigationSiteRoot() }; //dynamic dModel = model; foreach (Item item in items) { var itemObject = Activator.CreateInstance(superFlexiItemClass); bool itemIsEditable = IsEditable || WebUser.IsInRoles(item.EditRoles); bool itemIsViewable = WebUser.IsInRoles(item.ViewRoles) || itemIsEditable; if (!itemIsViewable) { continue; } string itemEditUrl = SiteUtils.GetNavigationSiteRoot() + "/SuperFlexi/Edit.aspx?pageid=" + PageId + "&mid=" + item.ModuleID + "&itemid=" + item.ItemID; SetItemClassProperty(itemObject, "Id", item.ItemID); SetItemClassProperty(itemObject, "Guid", item.ItemGuid); SetItemClassProperty(itemObject, "SortOrder", item.SortOrder); SetItemClassProperty(itemObject, "EditUrl", itemEditUrl); //dynamic itemModel = new ExpandoObject(); //itemModel.Id = item.ItemID; //itemModel.Guid = item.ItemGuid; //itemModel.SortOrder = item.SortOrder; //itemModel.EditUrl = itemEditUrl; List <ItemFieldValue> fieldValues = ItemFieldValue.GetItemValues(item.ItemGuid); //foreach (Field field in fields) //{ // foreach (ItemFieldValue fieldValue in fieldValues) // { // if (field.FieldGuid == fieldValue.FieldGuid) // { // if (field.ControlType == "CheckBox") // { // ((IDictionary<string, object>)itemModel)[field.Name] = Convert.ToBoolean(fieldValue.FieldValue); // } // ((IDictionary<string, object>)itemModel)[field.Name] = fieldValue.FieldValue; // } // } //} //var itemModel = new //{ // Id = item.ItemID, // Guid = item.ItemGuid, // SortOrder = item.SortOrder, // EditUrl = itemEditUrl, //}; foreach (Field field in fields) { foreach (ItemFieldValue fieldValue in fieldValues) { if (field.FieldGuid == fieldValue.FieldGuid) { switch (field.ControlType) { case "CheckBox": SetItemClassProperty(itemObject, field.Name, Convert.ToBoolean(fieldValue.FieldValue)); break; case "CheckBoxList": case "DynamicCheckBoxList": SetItemClassProperty(itemObject, field.Name, fieldValue.FieldValue.SplitOnCharAndTrim(';')); break; case "DateTime": case "Date": if (!string.IsNullOrWhiteSpace(fieldValue.FieldValue)) { DateTime.TryParse(fieldValue.FieldValue, out DateTime dt); SetItemClassProperty(itemObject, field.Name, dt); } break; case "TextBox": default: SetItemClassProperty(itemObject, field.Name, fieldValue.FieldValue); break; } } } } itemModels.Add(itemObject); } model.Items = itemModels; var viewPath = config.RelativeSolutionLocation + "/" + config.ViewName; string text; try { text = RazorBridge.RenderPartialToString(viewPath, model, "SuperFlexi"); } catch (Exception ex) { renderDefaultView(ex.ToString()); } void renderDefaultView(string error = "") { if (!string.IsNullOrWhiteSpace(error)) { log.ErrorFormat( "chosen layout ({0}) for _SuperFlexiRazor was not found in skin {1} or SuperFlexi Solution. Perhaps it is in a different skin or Solution. Error was: {2}", config.ViewName, SiteUtils.GetSkinBaseUrl(true, Page), error ); } text = RazorBridge.RenderPartialToString("_SuperFlexiRazor", model, "SuperFlexi"); } output.Write(text); }
private void ImportBtn_Click(object sender, EventArgs e) { var results = new StringBuilder(); var importCount = 0; var partialImportCount = 0; var updateCount = 0; var partialUpdateCount = 0; var failCount = 0; if (uploader.HasFile) { var recordsToImport = ImportHelper.GetDynamicListFromCSV(uploader.FileContent); if (recordsToImport != null) { if (chkDelete.Checked) { _ = ItemFieldValue.DeleteByModule(module.ModuleGuid); _ = Item.DeleteByModule(module.ModuleGuid); } foreach (IDictionary <string, object> record in recordsToImport) { var existingGuid = Guid.Empty; var hasGuid = false; if (record.ContainsKey("Guid")) { hasGuid = Guid.TryParse(Convert.ToString(record["Guid"]), out existingGuid); } var isUpdate = true; Item importedItem = null; if (hasGuid && chkUpdate.Checked) { importedItem = new Item(existingGuid); } if (importedItem == null || importedItem.DefinitionGuid != config.FieldDefinitionGuid || importedItem.ModuleGuid != module.ModuleGuid) { //todo: report to user why record isn't being updated isUpdate = false; importedItem = new Item { SiteGuid = siteSettings.SiteGuid, FeatureGuid = config.FeatureGuid, ModuleGuid = module.ModuleGuid, ModuleID = module.ModuleId, DefinitionGuid = config.FieldDefinitionGuid, ItemGuid = Guid.NewGuid() }; } var sortOrder = 500; if (record.ContainsKey("SortOrder")) { var sortOrderStr = Convert.ToString(record["SortOrder"]); if (!string.IsNullOrWhiteSpace(sortOrderStr)) { sortOrder = int.Parse(sortOrderStr); } } importedItem.SortOrder = sortOrder; importedItem.LastModUtc = DateTime.UtcNow; //we don't want to do this on each item that has been imported because that's a lot of work during the import process //importedItem.ContentChanged += new ContentChangedEventHandler(sflexiItem_ContentChanged); if (importedItem.Save()) { var fullyImported = true; //partialCount++; List <Field> fields = null; if (config.FieldDefinitionGuid != Guid.Empty) { fields = Field.GetAllForDefinition(config.FieldDefinitionGuid); } else { //todo: need to show a message about definition guid missing log.ErrorFormat("definitionGuid is missing from the field configuration file named {0}.", config.FieldDefinitionSrc); return; } if (fields == null) { return; } foreach (Field field in fields) { foreach (var kvp in record) { if (field.Name == kvp.Key.Replace(" ", string.Empty)) { List <ItemFieldValue> fieldValues = ItemFieldValue.GetItemValues(importedItem.ItemGuid); ItemFieldValue fieldValue = null; try { fieldValue = fieldValues.Where(saved => saved.FieldGuid == field.FieldGuid).Single(); } catch (InvalidOperationException) { //field is probably new fieldValue = new ItemFieldValue(); } //ItemFieldValue fieldValue = new ItemFieldValue(item.ItemGuid, field.FieldGuid); fieldValue.FieldGuid = field.FieldGuid; fieldValue.SiteGuid = field.SiteGuid; fieldValue.FeatureGuid = field.FeatureGuid; fieldValue.ModuleGuid = module.ModuleGuid; fieldValue.ItemGuid = importedItem.ItemGuid; fieldValue.FieldValue = kvp.Value.ToString(); if (!fieldValue.Save()) { fullyImported = false; _ = results.AppendLine(string.Format("<div><strong>Partial Failure:</strong> {0}</div>", string.Join(";", record.Select(x => x.Key + "=" + x.Value)))); } } } } if (fullyImported) { if (isUpdate) { updateCount++; } else { importCount++; } } else { if (isUpdate) { partialUpdateCount++; } else { partialImportCount++; } } } else { failCount++; _ = results.AppendFormat("<div><strong>Failed:</strong> {0}</div>", string.Join(";", record.Select(x => x.Key + "=" + x.Value))); } } _ = results.Insert(0, string.Format(@" <div><strong>Imported</strong> {0}</div> <div><strong>Partially Imported</strong> {1}</div> <div><strong>Updated</strong> {2}</div> <div><strong>Partially Updated</strong> {3}</div> <div><strong>Failed</strong> {4}</div>" , importCount.ToString() , partialImportCount.ToString() , updateCount.ToString() , partialUpdateCount.ToString() , failCount.ToString())); } else { _ = results.Insert(0, "<div class=\"alert alert-danger\">No records found in CSV file</div>"); } litResults.Text = results.ToString(); CacheHelper.ClearModuleCache(moduleId); SuperFlexiIndexBuilderProvider indexBuilder = new SuperFlexiIndexBuilderProvider(); indexBuilder.RebuildIndex(CacheHelper.GetCurrentPage(), IndexHelper.GetSearchIndexPath(siteSettings.SiteId)); SiteUtils.QueueIndexing(); } }
private void ImportBtn_Click(Object sender, EventArgs e) { StringBuilder results = new StringBuilder(); int importCount = 0; int partialImportCount = 0; int updateCount = 0; int partialUpdateCount = 0; int failCount = 0; if (uploader.HasFile) { var recordsToImport = ImportHelper.GetDynamicListFromCSV(uploader.FileContent); if (recordsToImport != null) { foreach (IDictionary <string, object> record in recordsToImport) { var existingGuid = record.ContainsKey("Guid") ? Guid.Parse(record["Guid"].ToString()) : Guid.Empty; bool isUpdate = true; Item importedItem = null; if (existingGuid != Guid.Empty && chkUpdate.Checked) { importedItem = new Item(existingGuid); } if (importedItem == null || importedItem.DefinitionGuid != config.FieldDefinitionGuid || importedItem.ModuleGuid != module.ModuleGuid) { //todo: report to use why record isn't being updated isUpdate = false; importedItem = new Item(); importedItem.SiteGuid = siteSettings.SiteGuid; importedItem.FeatureGuid = config.FeatureGuid; importedItem.ModuleGuid = module.ModuleGuid; importedItem.ModuleID = module.ModuleId; importedItem.DefinitionGuid = config.FieldDefinitionGuid; importedItem.ItemGuid = Guid.NewGuid(); } int sortOrder = record.ContainsKey("SortOrder") ? int.Parse(record["SortOrder"].ToString()) : 500; importedItem.SortOrder = sortOrder; importedItem.LastModUtc = DateTime.UtcNow; importedItem.ContentChanged += new ContentChangedEventHandler(sflexiItem_ContentChanged); if (importedItem.Save()) { bool fullyImported = true; //partialCount++; List <Field> fields = null; if (config.FieldDefinitionGuid != Guid.Empty) { fields = Field.GetAllForDefinition(config.FieldDefinitionGuid); } else { //todo: need to show a message about definition guid missing log.ErrorFormat("definitionGuid is missing from the field configuration file named {0}.", config.FieldDefinitionSrc); return; } if (fields == null) { return; } foreach (Field field in fields) { foreach (var kvp in record) { if (field.Name == kvp.Key.Replace(" ", string.Empty)) { List <ItemFieldValue> fieldValues = ItemFieldValue.GetItemValues(importedItem.ItemGuid); ItemFieldValue fieldValue; try { fieldValue = fieldValues.Where(saved => saved.FieldGuid == field.FieldGuid).Single(); } catch (System.InvalidOperationException ex) { //field is probably new fieldValue = new ItemFieldValue(); } //ItemFieldValue fieldValue = new ItemFieldValue(item.ItemGuid, field.FieldGuid); fieldValue.FieldGuid = field.FieldGuid; fieldValue.SiteGuid = field.SiteGuid; fieldValue.FeatureGuid = field.FeatureGuid; fieldValue.ModuleGuid = module.ModuleGuid; fieldValue.ItemGuid = importedItem.ItemGuid; fieldValue.FieldValue = kvp.Value.ToString(); if (!fieldValue.Save()) { fullyImported = false; results.AppendLine(String.Format("<div><strong>Partial Failure:</strong> {0}</div>", string.Join(";", record.Select(x => x.Key + "=" + x.Value)))); } } } } CacheHelper.ClearModuleCache(importedItem.ModuleID); SiteUtils.QueueIndexing(); if (fullyImported) { if (isUpdate) { updateCount++; } else { importCount++; } } else { if (isUpdate) { partialUpdateCount++; } else { partialImportCount++; } } } else { failCount++; results.AppendFormat("<div><strong>Failed:</strong> {0}</div>", string.Join(";", record.Select(x => x.Key + "=" + x.Value))); } } results.Insert(0, String.Format(@" <div><strong>Imported</strong> {0}</div> <div><strong>Partially Imported</strong> {1}</div> <div><strong>Updated</strong> {2}</div> <div><strong>Partially Updated</strong> {3}</div> <div><strong>Failed</strong> {4}</div>" , importCount.ToString() , partialImportCount.ToString() , updateCount.ToString() , partialUpdateCount.ToString() , failCount.ToString())); } else { results.Insert(0, "<div class=\"alert alert-danger\">No records found in CSV file</div>"); } litResults.Text = results.ToString(); } }