Example #1
0
        public override ActionResult DeleteItems(RequestParameters requestParameters, IEnumerable <string> items)
        {
            var output = new JsonContentResultWriter(new StringWriter());

            WriteMetaData(output);

            foreach (var itemPath in items)
            {
                try
                {
                    var item = Database.GetItem(itemPath);
                    if (item == null)
                    {
                        continue;
                    }

                    if (!item.Parent.Security.CanDelete(Context.User))
                    {
                        output.WriteError("You do not have permission to delete item: " + item.Paths.Path);
                        continue;
                    }

                    item.Recycle();
                }
                catch (System.Exception ex)
                {
                    output.WriteError("Failed to delete item: " + ex.Message);
                }
            }

            return(output.ToContentResult());
        }
Example #2
0
        public override ActionResult SaveItems(RequestParameters requestParameters, Dictionary <string, string> fields)
        {
            var output = new JsonContentResultWriter(new StringWriter());

            WriteMetaData(output);

            var fieldList = new List <FieldEditorField>();

            foreach (var pair in fields)
            {
                var key   = pair.Key;
                var value = HttpUtility.UrlDecode(pair.Value) ?? string.Empty;

                var parts = key.Split('/');
                if (parts.Length != 5)
                {
                    output.WriteError("Invalid Field Uri: " + key);
                    continue;
                }

                var languageName = parts[2];
                if (languageName == "-")
                {
                    languageName = LanguageManager.DefaultLanguage.Name;
                }

                var versionNumber = parts[3];
                if (versionNumber == "-")
                {
                    versionNumber = "0";
                }

                var databaseName = parts[0];
                var itemId       = parts[1];
                var language     = Language.Parse(languageName);
                var version      = Version.Parse(versionNumber);
                var fieldId      = parts[4];

                var field = new FieldEditorField(databaseName, itemId, language, version, fieldId, value);

                fieldList.Add(field);
            }

            var items = new Dictionary <string, Item>();

            foreach (var field in fieldList)
            {
                var key = field.DatabaseName + "/" + field.ItemId + "/" + field.Language + "/" + field.Version;
                if (!items.TryGetValue(key, out var item))
                {
                    var database = Factory.GetDatabase(field.DatabaseName);

                    item = database.GetItem(field.ItemId, field.Language, field.Version);
                    if (item == null)
                    {
                        output.WriteError("Item not found: " + field.ItemId + "/" + field.Language.Name + "/" + field.Version.Number);
                        continue;
                    }

                    if (!item.Security.CanWrite(Context.User))
                    {
                        output.WriteError("You do not have permission to write to this item: " + item.Paths.Path);
                        continue;
                    }

                    items[key] = item;

                    try
                    {
                        item.Editing.BeginEdit();
                    }
                    catch (System.Exception ex)
                    {
                        output.WriteError("An exception occured while saving item: " + item.Paths.Path + "; " + ex.Message);
                        continue;
                    }
                }

                var itemField = item.Fields[field.FieldId];
                if (itemField != null && string.Equals(itemField.Type, "layout", System.StringComparison.OrdinalIgnoreCase))
                {
                    // layout field needs special handling because of layout deltas
                    var layoutFieldValue = GetLayoutFieldValue(item, field);
                    LayoutField.SetFieldValue(itemField, layoutFieldValue);
                    continue;
                }

                item[field.FieldId] = field.Value;
            }

            foreach (var pair in items)
            {
                pair.Value.Editing.EndEdit();
            }

            return(output.ToContentResult());
        }
Example #3
0
        public override ActionResult AddItem(RequestParameters requestParameters, string itemPath, string templateName)
        {
            var output = new JsonContentResultWriter(new StringWriter());

            WriteMetaData(output);

            var n = itemPath.LastIndexOf('/');

            if (n < 0)
            {
                output.WriteError("Parent path not found: " + itemPath);
                return(output.ToContentResult());
            }

            var parentPath = itemPath.Left(n);

            if (!ID.IsID(parentPath) && !parentPath.StartsWith("/", System.StringComparison.OrdinalIgnoreCase))
            {
                parentPath = "/" + parentPath;
            }

            var parentItem = Database.GetItem(parentPath);

            if (parentItem == null)
            {
                output.WriteError("Parent item not found: " + parentPath);
                return(output.ToContentResult());
            }

            if (!parentItem.Security.CanCreate(Context.User))
            {
                output.WriteError("You do not have permission to create items under item: " + parentItem.Paths.Path);
                return(output.ToContentResult());
            }

            var templateItem = Database.GetItem(templateName);

            if (templateItem == null)
            {
                output.WriteError("Template not found: " + templateName);
                return(output.ToContentResult());
            }

            var itemName = itemPath.Mid(n + 1);

            try
            {
                var newItem = parentItem.Add(itemName, new TemplateID(templateItem.ID));
                if (newItem != null)
                {
                    output.WriteStartObject("item");
                    WriteItemHeader(output, newItem);
                    output.WriteEndObject();
                }
                else
                {
                    output.WriteError("Failed to create item under " + parentItem.Paths.Path);
                }
            }
            catch (System.Exception ex)
            {
                output.WriteError("Failed to create item under " + parentItem.Paths.Path + ": " + ex.Message);
            }

            return(output.ToContentResult());
        }