public ActionResult Index(HomePageCustomizeModel model)
        {
            this.CheckUser();
            var existing = GetModel();

            if (existing != null)
            {
                model.CopyFrom(existing);
            }
            model.SetPlaceHolders();
            return(View(model));
        }
        private HomePageCustomizeModel GetModel()
        {
            HomePageCustomizeModel existing = null;
            Guid homePageGuid = typeof(HomePageCustomizeModel).GUID;
            var  context      = this.HttpContext.GetOwinContext();
            var  db           = context.Get <ApplicationDbContext>();

            // populate existing data into the form
            var homePageCustomizations = (from i in db.Customizations where i.Id == homePageGuid select i).FirstOrDefault();

            if (homePageCustomizations != null)
            {
                string blobMame = homePageCustomizations.JsonBlobId;
                existing = CustomizationBlobs.GetCustomizationJson <HomePageCustomizeModel>(blobMame);
            }
            return(existing);
        }
示例#3
0
        public ActionResult HomePageCustomize(HomePageCustomizeModel model)
        {
            this.CheckUser();
            Guid homePageGuid = typeof(HomePageCustomizeModel).GUID;

            if (!ViewBag.IsAdmin)
            {
                ViewBag.Message = "You do not have admin permission";
            }
            else
            {
                var context = this.HttpContext.GetOwinContext();
                var db      = context.Get <ApplicationDbContext>();
                if (ModelState.IsValid)
                {
                    try
                    {
                        int iconsUpdated = 0;
                        foreach (string name in Request.Files)
                        {
                            HttpPostedFileBase upload   = Request.Files.Get(name);
                            string             filename = upload.FileName;
                            byte[]             data     = CustomizationBlobs.ReadToEnd(upload.InputStream);
                            if (!string.IsNullOrEmpty(filename) && data.Length > 0)
                            {
                                CustomizationBlobs.UpdateBlob(name, data);

                                if (name == "FileIcon1")
                                {
                                    model.IconUrl1 = filename;
                                }
                                else if (name == "FileIcon2")
                                {
                                    model.IconUrl2 = filename;
                                }
                                iconsUpdated++;
                            }
                        }

                        CustomizationBlobs.UpdateCustomizationJson <HomePageCustomizeModel>("HomePageJson", model);
                        var homePageCustomizations = (from i in db.Customizations where i.Id == homePageGuid select i).FirstOrDefault();
                        if (homePageCustomizations == null)
                        {
                            homePageCustomizations = new CustomizationsModel()
                            {
                                Id = homePageGuid
                            };
                            db.Customizations.Add(homePageCustomizations);
                        }
                        homePageCustomizations.JsonBlobId = "HomePageJson";
                        int    count  = db.SaveChanges();
                        string suffix = "";
                        switch (iconsUpdated)
                        {
                        case 0:
                            suffix = " but not the icons";
                            break;

                        case 1:
                            suffix = " and one of the icons";
                            break;

                        case 2:
                            suffix = " and both icons";
                            break;

                        default:
                            break;
                        }
                        ViewBag.Message = "Updated home page customizations" + suffix;
                    }
                    catch (Exception ex)
                    {
                        ViewBag.Message = "Error: " + ex.Message;
                    }
                }
                else
                {
                    // populate existing data into the form
                    var homePageCustomizations = (from i in db.Customizations where i.Id == homePageGuid select i).FirstOrDefault();
                    if (homePageCustomizations != null)
                    {
                        string blobMame = homePageCustomizations.JsonBlobId;
                        HomePageCustomizeModel existing = CustomizationBlobs.GetCustomizationJson <HomePageCustomizeModel>(blobMame);
                        if (existing != null)
                        {
                            model.CopyFrom(existing);
                        }
                    }
                }
            }
            return(View(model));
        }