/// <summary>
 /// we will now assign all of the values in the 'save' model to the DTO object
 /// </summary>
 /// <param name="saveModel"></param>
 /// <param name="dto"></param>
 private static void MapPropertyValuesFromSaved(TModelSave saveModel, ContentItemDto <TPersisted> dto)
 {
     foreach (var p in saveModel.Properties.Where(p => dto.Properties.Any(x => x.Alias == p.Alias)))
     {
         dto.Properties.Single(x => x.Alias == p.Alias).Value = p.Value;
     }
 }
Example #2
0
        /// <summary>
        /// Returns a new instance of a content item from a dto
        /// For use when working with items in bags.
        /// Use with care: Will create a new instance if used and saved on a primary content item.
        /// </summary>
        public static ContentItem ToContentItem(this ContentItemDto content)
        {
            var serialized   = JObject.FromObject(content);
            var deserialized = serialized.ToObject <ContentItem>();

            return(deserialized);
        }
Example #3
0
        //TODO: Validate the property type data
        private bool ValidateData(ContentItemSave postedItem, ContentItemDto realItem, HttpActionContext actionContext)
        {
            foreach (var p in realItem.Properties)
            {
                var editor = PropertyEditorResolver.Current.GetById(p.DataType.ControlId);
                if (editor == null)
                {
                    var message = string.Format("The property editor with id: {0} was not found for property with id {1}", p.DataType.ControlId, p.Id);
                    actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
                    return(false);
                }

                //get the posted value for this property
                var postedValue = postedItem.Properties.Single(x => x.Id == p.Id).Value;

                //get the pre-values for this property
                var preValues = TestContentService.GetPreValue(p.DataType.Id);

                //TODO: when we figure out how to 'override' certain pre-value properties we'll either need to:
                // * Combine the preValues with the overridden values stored with the document type property (but how to combine?)
                // * Or, pass in the overridden values stored with the doc type property separately

                foreach (var v in editor.ValueEditor.Validators)
                {
                    foreach (var result in v.Validate(postedValue, preValues, editor))
                    {
                        //if there are no member names supplied then we assume that the validation message is for the overall property
                        // not a sub field on the property editor
                        if (!result.MemberNames.Any())
                        {
                            //add a model state error for the entire property
                            actionContext.ModelState.AddModelError(p.Alias, result.ErrorMessage);
                        }
                        else
                        {
                            //there's assigned field names so we'll combine the field name with the property name
                            // so that we can try to match it up to a real sub field of this editor
                            foreach (var field in result.MemberNames)
                            {
                                actionContext.ModelState.AddModelError(string.Format("{0}.{1}", p.Alias, field), result.ErrorMessage);
                            }
                        }
                    }
                }
            }

            //create the response if there any errors
            if (!actionContext.ModelState.IsValid)
            {
                actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Forbidden, actionContext.ModelState);
            }

            return(actionContext.ModelState.IsValid);
        }
        //TODO: Validate the property type data
        private bool ValidateData(ContentItemSave postedItem, ContentItemDto realItem, HttpActionContext actionContext)
        {
            foreach (var p in realItem.Properties)
            {
                var editor = PropertyEditorResolver.Current.GetById(p.DataType.ControlId);
                if (editor == null)
                {
                    var message = string.Format("The property editor with id: {0} was not found for property with id {1}", p.DataType.ControlId, p.Id);
                    actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
                    return false;
                }

                //get the posted value for this property
                var postedValue = postedItem.Properties.Single(x => x.Id == p.Id).Value;

                //get the pre-values for this property
                var preValues = TestContentService.GetPreValue(p.DataType.Id);

                //TODO: when we figure out how to 'override' certain pre-value properties we'll either need to:
                // * Combine the preValues with the overridden values stored with the document type property (but how to combine?)
                // * Or, pass in the overridden values stored with the doc type property separately

                foreach (var v in editor.ValueEditor.Validators)
                {
                    foreach (var result in v.Validate(postedValue, preValues, editor))
                    {
                        //if there are no member names supplied then we assume that the validation message is for the overall property
                        // not a sub field on the property editor
                        if (!result.MemberNames.Any())
                        {
                            //add a model state error for the entire property
                            actionContext.ModelState.AddModelError(p.Alias, result.ErrorMessage);
                        }
                        else
                        {
                            //there's assigned field names so we'll combine the field name with the property name
                            // so that we can try to match it up to a real sub field of this editor
                            foreach (var field in result.MemberNames)
                            {
                                actionContext.ModelState.AddModelError(string.Format("{0}.{1}", p.Alias, field), result.ErrorMessage);
                            }
                        }
                    }
                }
            }

            //create the response if there any errors
            if (!actionContext.ModelState.IsValid)
            {
                actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Forbidden, actionContext.ModelState);
            }

            return actionContext.ModelState.IsValid;
        }
        private static ContentItemDto CreateContentItemDto()
        {
            var contentItem = new ContentItemDto {
                Id = 10, ContentType = "TestContentType", DisplayType = "TestDisplayType"
            };

            contentItem.Zones = new List <ZoneDto>();


            var zone1 = new ZoneDto {
                Name = "zone1"
            };

            zone1.Elements = new List <ContentElementDto>();
            contentItem.Zones.Add(zone1);

            var commonPart = new CommonPartDto {
                Id = 10, ResourceUrl = "resourceUrl", CreatedUtc = "2017-02-08T21:18:41.8420836Z", PublishedUtc = "2017-02-08T21:18:41.8420836Z"
            };

            zone1.Elements.Add(commonPart);

            var titlePart = new TitlePartDto {
                Title = "title1", Type = "TitlePart"
            };

            zone1.Elements.Add(titlePart);


            var zone2 = new ZoneDto {
                Name = "zone2"
            };

            zone2.Elements = new List <ContentElementDto>();
            contentItem.Zones.Add(zone2);

            var bodyPart = new BodyPartDto {
                Html = "html"
            };

            zone2.Elements.Add(bodyPart);

            var booleanField = new BooleanFieldDto {
                Value = true
            };

            zone2.Elements.Add(booleanField);


            return(contentItem);
        }
 /// <summary>
 /// we will now assign all of the values in the 'save' model to the DTO object
 /// </summary>
 /// <param name="saveModel"></param>
 /// <param name="dto"></param>
 private static void MapPropertyValuesFromSaved(TModelSave saveModel, ContentItemDto <TPersisted> dto)
 {
     //NOTE: Don't convert this to linq, this is much quicker
     foreach (var p in saveModel.Properties)
     {
         foreach (var propertyDto in dto.Properties)
         {
             if (propertyDto.Alias != p.Alias)
             {
                 continue;
             }
             propertyDto.Value = p.Value;
             break;
         }
     }
 }
Example #7
0
        /// <summary>
        /// Ensure all of the ids in the post are valid
        /// </summary>
        /// <param name="postedItem"></param>
        /// <param name="actionContext"></param>
        /// <param name="realItem"></param>
        /// <returns></returns>
        private bool ValidateProperties(ContentItemSave postedItem, ContentItemDto realItem, HttpActionContext actionContext)
        {
            foreach (var p in postedItem.Properties)
            {
                //ensure the property actually exists in our server side properties
                if (!realItem.Properties.Contains(p))
                {
                    //TODO: Do we return errors here ? If someone deletes a property whilst their editing then should we just
                    //save the property data that remains? Or inform them they need to reload... not sure. This problem exists currently too i think.

                    var message = string.Format("property with id: {0} was not found", p.Id);
                    actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
                    return(false);
                }
            }
            return(true);
        }
        // ContentItem
        public IContentItem Create(ContentItemDto contentItemDto)
        {
            var contentItem = new ContentItem();

            contentItem.Id          = contentItemDto.Id;
            contentItem.ContentType = contentItemDto.ContentType;
            contentItem.DisplayType = contentItemDto.DisplayType;

            foreach (var zoneDto in contentItemDto.Zones)
            {
                var zone = CreateZone(zoneDto, contentItem);
                contentItem.Zones.Add(zone);
            }

            contentItem.Alternates.Add($"ContentItem_{contentItem.Id}");
            contentItem.Alternates.Add($"ContentItem_{contentItem.ContentType}_{contentItem.DisplayType}");
            contentItem.Alternates.Add($"ContentItem_{contentItem.DisplayType}");
            contentItem.Alternates.Add($"ContentItem_{contentItem.ContentType}");
            contentItem.Alternates.Add($"ContentItem");

            return(contentItem);
        }
Example #9
0
 /// <summary>
 /// Ensure the content exists
 /// </summary>
 /// <param name="postedItem"></param>
 /// <param name="actionContext"></param>
 /// <param name="found"></param>
 /// <returns></returns>
 private bool ValidateExistingContent(ContentItemSave postedItem, HttpActionContext actionContext, out ContentItemDto found)
 {
     //TODO: We need to of course change this to the real umbraco api
     found = TestContentService.GetContentItem(postedItem.Id);
     if (found == null)
     {
         var message = string.Format("content with id: {0} was not found", postedItem.Id);
         actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
         return(false);
     }
     return(true);
 }
        /// <summary>
        /// Ensure all of the ids in the post are valid
        /// </summary>
        /// <param name="postedItem"></param>
        /// <param name="actionContext"></param>
        /// <param name="realItem"></param>
        /// <returns></returns>
        private bool ValidateProperties(ContentItemSave postedItem, ContentItemDto realItem, HttpActionContext actionContext)
        {
            foreach (var p in postedItem.Properties)
            {
                //ensure the property actually exists in our server side properties
                if (!realItem.Properties.Contains(p))
                {
                    //TODO: Do we return errors here ? If someone deletes a property whilst their editing then should we just
                    //save the property data that remains? Or inform them they need to reload... not sure. This problem exists currently too i think.

                    var message = string.Format("property with id: {0} was not found", p.Id);
                    actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
                    return false;
                }
            }
            return true;
        }
 /// <summary>
 /// Ensure the content exists
 /// </summary>
 /// <param name="postedItem"></param>
 /// <param name="actionContext"></param>
 /// <param name="found"></param>
 /// <returns></returns>
 private bool ValidateExistingContent(ContentItemSave postedItem, HttpActionContext actionContext, out ContentItemDto found)
 {
     //TODO: We need to of course change this to the real umbraco api
     found = TestContentService.GetContentItem(postedItem.Id);
     if (found == null)
     {
         var message = string.Format("content with id: {0} was not found", postedItem.Id);
         actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
         return false;
     }
     return true;
 }
        private void SetDisplayIncludeInActiveBundle(ContentItemDto contentItem, string currentUserId)
        {
            var loggedInUserId = Session.GetLoggedInUser() != null ? Session.GetLoggedInUser().ObjectId : string.Empty;
            if (contentItem.IncludedInBundle)
            {
                if (Session.GetLoggedInUserRoleName() == RoleNames.ADMINISTRATORS || loggedInUserId == contentItem.TeacherId)
                {
                    contentItem.DisplayIsIncludedInBundle = true;
                }

                else if (contentItem.IncludedInActiveBundleTeachersId != null &&
                         contentItem.IncludedInActiveBundleTeachersId.Select(o => o.ToString().Split(new char[] {'|'})[0])
                             .Contains(currentUserId))
                {
                    contentItem.DisplayIsIncludedInBundle = true;
                }
                else if (contentItem.IncludedInActiveBundle)
                {
                    contentItem.DisplayIsIncludedInBundle = true;
                }
            }
        }