public Dictionary <Tuple <string, int>, string> GetFieldValues(SCItem sourceItem, Guid sourceFieldId, string defaultValue, bool stripHtml = false) { var values = new Dictionary <Tuple <string, int>, string>(); var fieldType = sourceItem.Fields.FirstOrDefault(f => f.FieldId == sourceFieldId)?.Type; IEnumerable <Tuple <string, int> > langVersions = sourceItem.Fields.Where(f => f.Version != null && f.Language != null).Select(f => new Tuple <string, int>(f.Language, (int)f.Version)).Distinct(); var languages = sourceItem.Fields.Where(f => f.Language != null).Select(f => f.Language).Distinct(); foreach (var langVersion in langVersions) { var value = sourceItem.Fields.FirstOrDefault(f => f.FieldId == sourceFieldId && f.Language == langVersion.Item1 && f.Version == langVersion.Item2)?.Value; if (fieldType == FieldType.Unversioned) { value = sourceItem.Fields.FirstOrDefault(f => f.FieldId == sourceFieldId && f.Language == langVersion.Item1)?.Value; } if (stripHtml) { value = XmlHelper.StripHtml(value); } values.Add(langVersion, value ?? defaultValue); } return(values); }
private List <SCItem> ConvertItemAndFields(SCItem sourceItem, Guid destParentId) { List <SCItem> destItems = new List <SCItem>(); List <SCItem> convertedItems = new List <SCItem>(); var destItem = new SCItem() { ID = sourceItem.ID, Name = sourceItem.Name, MasterID = Guid.Empty, ParentID = destParentId, Created = sourceItem.Created, Updated = sourceItem.Updated, TemplateID = _itemMetadataTemplate.destTemplateId, Fields = sourceItem.Fields }; // Create descendant items destItems.AddRange(_itemFactory.CreateDescendantItems(_itemMetadataTemplate, destItem)); if (destItems.Any()) { var lastDescendantItem = destItems.FirstOrDefault(item => !destItems.Select(i => i.ParentID).Contains(item.ID)); convertedItems = ConvertFields(destItem, lastDescendantItem); } else { convertedItems = ConvertFields(destItem, null); } destItems.AddRange(convertedItems); return(destItems); }
public void ConvertInformation(SCItem sectionItem) { var informationItemName = "Information"; var textMetadata = _metadataProvider.GetItemMetadataByTemplateName("Text"); DeleteItem(sectionItem.ID, informationItemName, textMetadata); // Create Text Item with text in Information parameter var localizedParameters = sectionItem.Fields.FirstOrDefault(field => field.FieldId == new Guid(SectionConstants.SectionLocalizedParametersFieldId))?.Value; var information = XmlHelper.GetXmlElementValue(localizedParameters, SectionConstants.SectionInformationElementName); if (!string.IsNullOrEmpty(information)) { // Create Text item var fieldValues = _fieldProvider.GetFieldValues(sectionItem, new Guid(SectionConstants.SectionLocalizedParametersFieldId), string.Empty, false); var informationFieldValues = new Dictionary <Tuple <string, int>, string>(); foreach (var fieldValue in fieldValues) { informationFieldValues.Add(fieldValue.Key, XmlHelper.GetXmlElementValue(fieldValue.Value, SectionConstants.SectionInformationElementName)); } // Set text field textMetadata.fields.newFields.First(field => field.destFieldId == new Guid(TextConstants.TextFieldId)).values = informationFieldValues; // Set __Sortorder field textMetadata.fields.newFields.First(field => field.destFieldId == new Guid(BaseTemplateConstants.SortOrderFieldId)).value = "-50"; // Second item in the page, after title WriteNewItem(textMetadata.destTemplateId, sectionItem, informationItemName, textMetadata); } }
private SCItem CreateItem(SCItem parentItem, string itemName) { var itemId = Guid.NewGuid(); // Check on Guid existence (super super super rare event that the Guid already exists) while (_destMasterRepository.ItemExists(itemId)) { itemId = Guid.NewGuid(); } if (_appSettings.enableOnlyAnalysisByDefault && parentItem == null) { parentItem = CreateDummyItem(); } return(new SCItem() { ID = itemId, Name = itemName, MasterID = Guid.Empty, ParentID = parentItem.ID, Created = DateTime.Now, Updated = DateTime.Now, TemplateID = _itemMetadataTemplate.destTemplateId, Fields = CreateFields(itemId, parentItem) }); }
public void ConvertIntroduction(SCItem form, SCItem pageItem) { var introductionItemName = "Introduction"; var textMetadata = _metadataProvider.GetItemMetadataByTemplateName("Text"); pageItem = CheckItemNotNullForAnalysis(pageItem); DeleteItem(pageItem.ID, introductionItemName, textMetadata); var showIntroduction = form.Fields.FirstOrDefault(field => field.FieldId == new Guid(FormConstants.FormShowIntroductionFieldId)); if (showIntroduction != null && showIntroduction.Value == "1") { // Create Text Item with text in Introduction field var introduction = form.Fields.FirstOrDefault(field => field.FieldId == new Guid(FormConstants.FormIntroductionFieldId))?.Value; if (!string.IsNullOrEmpty(introduction)) { // Create Text item var parentItem = _destMasterRepository.GetSitecoreItem(pageItem.ID); var fieldValues = _fieldProvider.GetFieldValues(form, new Guid(FormConstants.FormIntroductionFieldId), string.Empty, false); // Set text field textMetadata.fields.newFields.First(field => field.destFieldId == new Guid(TextConstants.TextFieldId)).values = fieldValues; // Set __Sortorder field textMetadata.fields.newFields.First(field => field.destFieldId == new Guid(BaseTemplateConstants.SortOrderFieldId)).value = "-50"; // Second item in the page, after title WriteNewItem(textMetadata.destTemplateId, parentItem, introductionItemName, textMetadata); } } }
private List <SCField> CreateFields(Guid itemId, SCItem parentItem) { var destFields = new List <SCField>(); IEnumerable <Tuple <string, int> > langVersions = parentItem.Fields.Where(f => f.Version != null && f.Language != null).Select(f => new Tuple <string, int>(f.Language, (int)f.Version)).Distinct(); var languages = parentItem.Fields.Where(f => f.Language != null).Select(f => f.Language).Distinct(); foreach (var newField in _itemMetadataTemplate.fields.newFields) { destFields.AddRange(_fieldFactory.CreateFields(newField, itemId, langVersions, languages)); } return(destFields); }
public SCItem Create(Guid destTemplateId, SCItem parentItem, string itemName, MetadataTemplate metadataTemplate = null) { if (metadataTemplate != null) { _itemMetadataTemplate = metadataTemplate; } else { _itemMetadataTemplate = _metadataProvider.GetItemMetadataByTemplateId(destTemplateId); } itemName = RemoveInvalidChars(itemName); return(CreateItem(parentItem, itemName)); }
// GET: ShoppingCarts/Remove/5 public ActionResult Remove(Guid?item_id, Guid?shoppingCart_id) { if (shoppingCart_id == null || item_id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ShoppingCart shoppingCart = db.ShoppingCarts.Find(shoppingCart_id); if (shoppingCart == null) { return(HttpNotFound()); } SCItem scItem = shoppingCart.SCItems.FirstOrDefault(x => x.Id == item_id); if (scItem == null) { return(HttpNotFound()); } shoppingCart.RemoveFromCart(scItem); db.SaveChanges(); return(RedirectToAction("Details", new { id = shoppingCart.Id })); }
private List <SCField> CreateFields(Guid itemId, SCItem parentItem) { var destFields = new List <SCField>(); IEnumerable <Tuple <string, int> > langVersions = parentItem.Fields.Where(f => f.Version != null && f.Language != null).Select(f => new Tuple <string, int>(f.Language, (int)f.Version)).Distinct(); var languages = parentItem.Fields.Where(f => f.Language != null).Select(f => f.Language).Distinct(); var fieldLangVersions = _itemMetadataTemplate?.fields?.newFields?.Where(f => f.values != null).SelectMany(f => f.values.Select(v => v.Key).Distinct()).Distinct(); if (fieldLangVersions != null && fieldLangVersions.Any()) { langVersions = fieldLangVersions; languages = fieldLangVersions.Select(lv => lv.Item1).Distinct(); } foreach (var newField in _itemMetadataTemplate.fields.newFields) { destFields.AddRange(_fieldFactory.CreateFields(newField, itemId, langVersions, languages)); } return(destFields); }
public List <SCItem> Convert(SCItem scItem, Guid destParentId) { _itemMetadataTemplate = _metadataProvider.GetItemMetadataByTemplateId(scItem.TemplateID); if (_itemMetadataTemplate.sourceMappingFieldId != null && _itemMetadataTemplate.sourceMappingFieldId != Guid.Empty) { var sourceMappingFieldValue = scItem.Fields .FirstOrDefault(f => f.FieldId == _itemMetadataTemplate.sourceMappingFieldId)?.Value; var mappedMetadataTemplate = _metadataProvider.GetItemMetadataBySourceMappingFieldValue(sourceMappingFieldValue); if (mappedMetadataTemplate != null) { _itemMetadataTemplate = mappedMetadataTemplate; } else { // Add record in conversion analysis _conversionReporter.AddUnmappedFormFieldItem(scItem.ID, sourceMappingFieldValue); } } List <SCItem> destItems = ConvertItemAndFields(scItem, destParentId); return(destItems); }
public void ConvertTitle(SCItem sectionItem) { var titleItemName = "Title"; var textMetadata = _metadataProvider.GetItemMetadataByTemplateName("Text"); DeleteItem(sectionItem.ID, titleItemName, textMetadata); var parameters = sectionItem.Fields.FirstOrDefault(field => field.FieldId == new Guid(SectionConstants.SectionParametersFieldId)); if (parameters == null) { return; } var showLegend = XmlHelper.GetXmlElementValue(parameters.Value, SectionConstants.SectionShowLegendElementName); if (showLegend == null || string.Equals(showLegend, "Yes", StringComparison.InvariantCultureIgnoreCase)) { // Create Text Item with text in Title field using Title Tag HTML element var title = sectionItem.Fields.FirstOrDefault(field => field.FieldId == new Guid(SectionConstants.SectionTitleFieldId))?.Value; if (!string.IsNullOrEmpty(title)) { // Create Text item var fieldValues = _fieldProvider.GetFieldValues(sectionItem, new Guid(SectionConstants.SectionTitleFieldId), titleItemName); // Set text field textMetadata.fields.newFields.First(field => field.destFieldId == new Guid(TextConstants.TextFieldId)).values = fieldValues; // Set Html Tag field textMetadata.fields.newFields.First(field => field.destFieldId == new Guid(TextConstants.TextHtmlTagFieldId)) .value = "h2"; // Set __Sortorder field textMetadata.fields.newFields.First(field => field.destFieldId == new Guid(BaseTemplateConstants.SortOrderFieldId)).value = "-100"; // First item in the section WriteNewItem(textMetadata.destTemplateId, sectionItem, titleItemName, textMetadata); } } }
public void ConvertTitle(SCItem form, SCItem pageItem) { var titleItemName = "Title"; var textMetadata = _metadataProvider.GetItemMetadataByTemplateName("Text"); pageItem = CheckItemNotNullForAnalysis(pageItem); DeleteItem(pageItem.ID, titleItemName, textMetadata); var showTitle = form.Fields.FirstOrDefault(field => field.FieldId == new Guid(FormConstants.FormShowTitleFieldId)); if (showTitle == null || showTitle.Value == "1") { // Create Text Item with text in Title field using Title Tag HTML element var title = form.Fields.FirstOrDefault(field => field.FieldId == new Guid(FormConstants.FormTitleFieldId))?.Value; if (!string.IsNullOrEmpty(title)) { var titleTagField = form.Fields.FirstOrDefault(field => field.FieldId == new Guid(FormConstants.FormTitleTagFieldId)); var titleTag = titleTagField != null ? titleTagField.Value : FormConstants.FormTitleTagStandardValue; // Create Text item var parentItem = _destMasterRepository.GetSitecoreItem(pageItem.ID); var fieldValues = _fieldProvider.GetFieldValues(form, new Guid(FormConstants.FormTitleFieldId), titleItemName); // Set text field textMetadata.fields.newFields.First(field => field.destFieldId == new Guid(TextConstants.TextFieldId)).values = fieldValues; // Set Html Tag field textMetadata.fields.newFields.First(field => field.destFieldId == new Guid(TextConstants.TextHtmlTagFieldId)).value = ConvertTitleTag(titleTag); // Set __Sortorder field textMetadata.fields.newFields.First(field => field.destFieldId == new Guid(BaseTemplateConstants.SortOrderFieldId)).value = "-100"; // First item in the page WriteNewItem(textMetadata.destTemplateId, parentItem, titleItemName, textMetadata); } } }