Exemple #1
0
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                ItemDetails itemDetails;

                //Parse the JSON
                using (var sr = new StreamReader(context.Request.InputStream))
                {
                    itemDetails = JsonConvert.DeserializeObject <ItemDetails>(sr.ReadToEnd());
                }

                var editingLanguage = !string.IsNullOrEmpty(itemDetails.Language) ? LanguageManager.GetLanguage(itemDetails.Language) : LanguageManager.DefaultLanguage;

                var itemVersion = !string.IsNullOrEmpty(itemDetails.Version) ? Sitecore.Data.Version.Parse(itemDetails.Version) : Sitecore.Data.Version.Parse(1);

                //Load the item
                var currentItem = Context.ContentDatabase.GetItem(new ID(itemDetails.ItemId), editingLanguage, itemVersion);

                using (new EditContext(currentItem, false, false))
                {
                    currentItem["__Updated"] = DateUtil.IsoNow;
                    currentItem.Fields["__Updated by"].SetValue(Context.User.Name, true);

                    foreach (var clientFieldValue in itemDetails.Fields)
                    {
                        var field = currentItem.Fields[new ID(clientFieldValue.FieldId)];

                        if (!string.IsNullOrEmpty(clientFieldValue.Value))
                        {
                            switch (field.Type)
                            {
                            case "Date":
                            case "Datetime":
                                field.Value = DateUtil.ToIsoDate(DateUtil.ParseDateTime(clientFieldValue.Value, System.DateTime.MinValue));
                                break;

                            case "Droptree":
                                //The value for the tree is: [Source ID]|[Path to the item]
                                var values = clientFieldValue.Value.Split(new [] { "|" }, StringSplitOptions.RemoveEmptyEntries);

                                var sourceItem = currentItem.Database.GetItem(new ID(values[0]));

                                if (sourceItem != null)
                                {
                                    if (values.Length == 1)
                                    {
                                        field.Reset();
                                    }
                                    else
                                    {
                                        var selectedItemName = values[1].Split('/').LastOrDefault();

                                        //Get Source Path then add the Path to the item
                                        var itemPath = sourceItem.Parent.Parent.Paths.Path + "/" + values[1];

                                        //Remove the display name from the item path
                                        itemPath = itemPath.Replace(selectedItemName, "");

                                        //Get The Parent Tree Item
                                        var dropTreeSelectedItemParent = sourceItem.Database.GetItem(itemPath);

                                        var childItem = (from i in dropTreeSelectedItemParent.Children
                                                         where i.DisplayName == selectedItemName
                                                         select i).FirstOrDefault();

                                        if (childItem != null)
                                        {
                                            field.Value = childItem.ID.ToString();
                                        }
                                    }
                                }

                                break;

                            case "File":
                            case "Image":
                                var       mediaPath = "/sitecore/Media Library" + clientFieldValue.Value;
                                MediaItem mediaItem = currentItem.Database.GetItem(mediaPath);

                                var xmlValue = new XmlValue("", field.Type.ToLower());

                                if (mediaItem != null)
                                {
                                    var shellOptions = MediaUrlOptions.GetShellOptions();
                                    var mediaUrl     = MediaManager.GetMediaUrl(mediaItem, shellOptions);

                                    xmlValue.SetAttribute("mediaid", mediaItem.ID.ToString());
                                    xmlValue.SetAttribute("mediapath", mediaItem.MediaPath);
                                    xmlValue.SetAttribute("src", mediaUrl);
                                }
                                else
                                {
                                    xmlValue.SetAttribute("mediaid", string.Empty);
                                    xmlValue.SetAttribute("mediapath", string.Empty);
                                    xmlValue.SetAttribute("src", string.Empty);
                                }

                                field.Value = xmlValue.ToString();

                                break;

                            default:
                                field.Value = clientFieldValue.Value;
                                break;
                            }
                        }
                        else
                        {
                            field.Reset();
                        }
                    }

                    if (!string.IsNullOrEmpty(itemDetails.NameFormatString))
                    {
                        var displayName = BuildName(itemDetails.NameFormatString, currentItem).Trim();
                        var name        = ItemUtil.ProposeValidItemName(displayName, "Unnamed item");

                        string uniqueName = ItemUtil.GetUniqueName(currentItem.Parent, name);

                        if (!string.IsNullOrEmpty(name))
                        {
                            currentItem["__Display name"] = displayName;
                            currentItem.Name = uniqueName;
                        }
                    }
                }

                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    displayName = string.IsNullOrEmpty(currentItem["__Display name"]) ? currentItem.Name : currentItem["__Display name"]
                }));
            }
            catch (Exception ex)
            {
                Sitecore.Diagnostics.Log.Fatal("Error saving sub-item", ex, this);

                context.Response.StatusCode = 500;
                context.Response.Write(string.Format("Exception {0}({1}):\n{2}", ex.Message, ex.GetType().Name, ex.StackTrace));
            }
        }
        protected virtual bool UpdateVariableValues(MultivariateTestVariableItem variableItem, out List <ID> modifiedVariations)
        {
            Assert.ArgumentNotNull(variableItem, "variableItem");
            modifiedVariations = new List <ID>();
            List <VariableValueItemStub> variableValues = VariableValues;
            var list2    = new List <MultivariateTestValueItem>(TestingUtil.MultiVariateTesting.GetVariableValues(variableItem));
            var comparer = new DefaultComparer();

            list2.Sort((lhs, rhs) => comparer.Compare(lhs, rhs));
            int num        = (list2.Count > 0) ? (list2[0].InnerItem.Appearance.Sortorder - 1) : Settings.DefaultSortOrder;
            var templateID = new TemplateID(MultivariateTestValueItem.TemplateID);
            var list3      = new List <KeyValuePair <MultivariateTestValueItem, VariableValueItemStub> >();
            var list4      = new List <KeyValuePair <int, VariableValueItemStub> >();

            for (int i = variableValues.Count - 1; i >= 0; i--)
            {
                VariableValueItemStub stub = variableValues[i];
                ID  currentId = stub.Id;
                int index     = list2.FindIndex(item => item.ID == currentId);
                if (index < 0)
                {
                    var pair = new KeyValuePair <int, VariableValueItemStub>(num--, stub);
                    list4.Add(pair);
                }
                else
                {
                    MultivariateTestValueItem item = list2[index];
                    if (IsVariableValueChanged(item, stub))
                    {
                        list3.Add(new KeyValuePair <MultivariateTestValueItem, VariableValueItemStub>(item, stub));
                    }
                    list2.RemoveAt(index);
                }
            }
            if (list2.Count != 0)
            {
            }

            foreach (Item item2 in list2)
            {
                modifiedVariations.Add(item2.ID);
                item2.Delete();
            }
            foreach (var pair2 in list4)
            {
                VariableValueItemStub variableStub = pair2.Value;
                int    key  = pair2.Key;
                string name = variableStub.Name;
                if (ContainsNonASCIISymbols(name))
                {
                    Item item3 = variableItem.Database.GetItem(templateID.ID);
                    name = (item3 != null) ? item3.Name : "Unnamed item";
                }
                if (!ItemUtil.IsItemNameValid(name))
                {
                    try
                    {
                        name = ItemUtil.ProposeValidItemName(name);
                    }
                    catch (Exception)
                    {
                        return(false);
                    }
                }
                name = ItemUtil.GetUniqueName(variableItem, name);
                Item item4 = variableItem.InnerItem.Add(name, templateID);
                Assert.IsNotNull(item4, "newVariableValue");
                UpdateVariableValueItem((MultivariateTestValueItem)item4, variableStub, key);
            }
            foreach (var pair3 in list3)
            {
                MultivariateTestValueItem variableValue = pair3.Key;
                VariableValueItemStub     stub3         = pair3.Value;
                modifiedVariations.Add(variableValue.ID);
                UpdateVariableValueItem(variableValue, stub3);
            }
            return(true);
        }