Exemplo n.º 1
0
        public ActionResult Edit(ContentLibraryEditModel model)
        {
            ProcessImageUpload imageData = new ProcessImageUpload
            {
                PostedFile = model.UploadedFile,
            };

            if (!imageData.CheckImageOk())
            {
                ModelState.AddModelError("UploadedFile", imageData.Error);
                return(View(model));
            }

            if (imageData.Empty && !model.DeleteImage && !ModelState.IsValid)
            {
                return(View(model));
            }

            var content = Db.ContentLibraries.FirstOrDefault(c => c.Id == model.Id);

            if (content == null)
            {
                return(new HttpNotFoundResult("Content item was not found"));
            }
            if (model.DeleteImage)
            {
                if (DeleteImage(content.ImageUrl))
                {
                    content.ImageUrl = "";
                }
            }
            else
            {
                if (!imageData.Empty && imageData.PostedFileSaveAndResize(Server.MapPath(AppSettings.ContentLibraryImagePath), "item" + model.Id))
                {
                    content.ImageUrl = imageData.SavedName;
                }
            }
            //NOTE: model.sendDateTime will be in local time. We need it in the timezone the user has selected
            var correctDateTime = model.AvailableFromDate;

            if (correctDateTime.HasValue)
            {
                var timezone = System.TimeZoneInfo.FindSystemTimeZoneById(model.TimeZone);
                var newDate  = System.TimeZoneInfo.ConvertTimeToUtc(model.AvailableFromDate.Value, timezone);

                correctDateTime = newDate;
            }


            content.Title             = model.Title;
            content.ContentType       = model.ContentType;
            content.Precis            = model.Precis;
            content.Url               = model.Url;
            content.Featured          = model.Featured;
            content.AvailableFromDate = model.AvailableFromDate != null ? correctDateTime : DateTime.UtcNow;
            Db.SaveChanges();
            return(!imageData.Empty || model.DeleteImage ? RedirectToAction("Edit", new { id = model.Id }) : RedirectToAction("Index"));
        }
Exemplo n.º 2
0
        public ActionResult Create(ContentLibraryAddModel model)
        {
            ProcessImageUpload imageData = new ProcessImageUpload
            {
                PostedFile = model.UploadedFile,
            };

            if (!imageData.CheckImageOk())
            {
                ModelState.AddModelError("UploadedFile", imageData.Error);
                return(View(model));
            }

            if (imageData.Empty && !ModelState.IsValid)
            {
                return(View(model));
            }

            //NOTE: model.sendDateTime will be in local time. We need it in the timezone the user has selected
            var correctDateTime = model.AvailableFromDate;

            if (correctDateTime.HasValue)
            {
                var timezone = System.TimeZoneInfo.FindSystemTimeZoneById(model.TimeZone);
                var newDate  = System.TimeZoneInfo.ConvertTimeToUtc(model.AvailableFromDate.Value, timezone);

                correctDateTime = newDate;
            }

            var record = new ContentLibrary
            {
                Featured          = model.Featured,
                Title             = model.Title,
                Precis            = model.Precis,
                Url               = model.Url,
                Status            = ContentStatus.AwaitingDate,
                ContentType       = model.ContentType,
                Created           = DateTime.UtcNow,
                AvailableFromDate = model.AvailableFromDate != null? correctDateTime : DateTime.UtcNow,
                TimeZone          = model.TimeZone
            };

            try
            {
                Db.ContentLibraries.Add(record);
                Db.SaveChanges();
                if (!imageData.Empty && imageData.PostedFileSaveAndResize(Server.MapPath(AppSettings.ContentLibraryImagePath), "item" + record.Id))
                {
                    record.ImageUrl = imageData.SavedName;
                    Db.SaveChanges();
                }
            }
            catch (Exception)
            {
                return(View(model));
            }

            if (!ModelState.IsValid || !imageData.Empty)
            {
                return(RedirectToAction("Edit", new { id = record.Id }));
            }
            return(RedirectToAction("Index"));
        }