public ActionResult AddPage(int id = 0)
        {
            Post post;
            if (id == 0) {
                 post = new Post();
                 post.Created = DateTime.Now;
                 post.postType = (int)Post.PostType.Page;
            } else {
                post = _postRepo.FindAll().Where(p => p.ID == id).Include(i => i.Images).FirstOrDefault();
            }

            return View(post);
        }
        public ActionResult AddPage(int id = 0)
        {
            Post post;
            if (id == 0) {
                 post = new Post();
                 post.Created = DateTime.Now;
                 post.postType = (int)Post.PostType.Page;
            } else {
                 post = _postRepo.FindByID(id);
            }

            return View(post);
        }
        public ActionResult SavePage(Post Post, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                var theOldPost = _postRepo.FindByID(Post.ID);
                if (theOldPost == null)
                    theOldPost = Post;
                if (theOldPost.Images == null)
                    theOldPost.Images = new List<Image>();
                var oldImages = theOldPost.Images.ToList();
                var listOfImagesPaths = form["image"];
                string[] arrayOfImagesPaths = null;
                if (listOfImagesPaths != null)
                    arrayOfImagesPaths = listOfImagesPaths.Split(',');

                oldImages = theOldPost.Images.ToList();
                if (arrayOfImagesPaths != null && arrayOfImagesPaths.Count() > 0)
                {
                    foreach (var addedimg in _imgRepo.FindAll(c =>
                arrayOfImagesPaths.Any(cat => cat == c.ImagePath.ToString()) &&
                !c.Posts.Any(p => p.ID == theOldPost.ID)).ToList())
                        theOldPost.Images.Add(addedimg);
                }
                foreach (var removedimg in oldImages.Where(c => arrayOfImagesPaths
                == null || !arrayOfImagesPaths.Any(cat2 => cat2 == c.ImagePath.ToString())))
                    theOldPost.Images.Remove(removedimg);

                _postRepo.Save(Post);
                // add a message to the viewbag
                TempData["message"] = string.Format("{0} är sparad", Post.Title);
                // return the user to the list
                return RedirectToAction("Pages", "Post");
            }
            else
            {
                // there is something wrong with the data values
                return View("AddPage", Post);
            }
        }
        public ActionResult _FileUploadPartial(int postID = -1,  int categoryID = -1, int eventID = -1, int locationID = -1, bool badges = false)
        {
            var fuVM = new FileUploadViewModel();

            Post post = new Post();
            Category category = new Category();
            Event Event = new Event();
            Location Location = new Location();
            if (badges == true) {
                fuVM.Badges = true;
            }

            if (postID != -1) {
                post = _postRepo.FindAll().Where(p => p.ID == postID).Include(i => i.Images).Include(b => b.Badges).FirstOrDefault();
                fuVM.post = post;
            } if (categoryID != -1)
            {
                category = _categoryRepo.FindByID(categoryID);
                fuVM.Category = category;
            } if (eventID != -1)
            {
                Event = _eventRepo.FindByID(eventID);
                fuVM.Event = Event;
            } if (locationID != -1)
            {
                Location = _locationRepo.FindByID(locationID);
                fuVM.Location = Location;
            }
            var uploadedFiles = new List<UploadedFile>();

            var files = Directory.GetFiles(Server.MapPath("~/Content/image-uploads"));

            foreach (var file in files)
            {

                var uploadedFile = new UploadedFile() { Title = Path.GetFileName(file) };

                uploadedFile.PathUrl = ("/Content/image-uploads/") + Path.GetFileName(file);
                if (_imgRepo.FindAll().Where(i => i.ImagePath == uploadedFile.PathUrl).FirstOrDefault() == null)
                {
                    Image imageObj = new Image();
                    imageObj.ImagePath = uploadedFile.PathUrl;
                    _imgRepo.Save(imageObj);
                }

                var fileInfo = new FileInfo(file);
                if (postID >= 1 && (badges == false)) {
                        foreach (var postimage in post.Images)
                        {
                            if (postimage.ImagePath == uploadedFile.PathUrl)
                            {
                                uploadedFile.Checked = true;
                            }
                        }
                } if (postID >= 1 && (badges == true))
                {
                    foreach (var badgeImage in post.Badges)
                    {
                        if (badgeImage.ImagePath == uploadedFile.PathUrl)
                        {
                            uploadedFile.Checked = true;
                        }
                    }
                } if (categoryID >= 1)
                {
                        foreach (var categoryimage in category.Images)
                        {
                            if (categoryimage.ImagePath == uploadedFile.PathUrl)
                            {
                                uploadedFile.Checked = true;
                            }
                        }
                } if (eventID >= 1)
                {
                    foreach (var eventimage in Event.Images)
                    {
                        if (eventimage.ImagePath == uploadedFile.PathUrl)
                        {
                            uploadedFile.Checked = true;
                        }
                    }
                } if (locationID >= 1)
                {

                    foreach (var locationimage in Location.Images)
                    {
                        if (locationimage.ImagePath == uploadedFile.PathUrl)
                        {
                            uploadedFile.Checked = true;
                        }
                    }
                }

                uploadedFiles.Add(uploadedFile);
            }
            fuVM.UploadedFiles = uploadedFiles;

            return PartialView("_FileUploadPartialView", fuVM);
        }
        public bool SelectUpload(FileUploadViewModel fuVM, FormCollection form)
        {
            Post post = new Post();
            Category category = new Category();
            if (fuVM.post != null) {
                post = _postRepo.FindByID(fuVM.post.ID);
                post.Images = new List<Image>();
            } if (fuVM.Category != null)
            {
                category = _categoryRepo.FindByID(fuVM.Category.ID);
                category.Images = new List<Image>();
            }

            var listOfImagesPaths = form["images"];
            var arrayOfImagesPaths = listOfImagesPaths.Split(',');

            foreach (var path in arrayOfImagesPaths)
            {
                Image image = new Image();
                image.ImagePath = path;
                if (fuVM.post != null)
                {
                    post.Images.Add(image);
                    _postRepo.Save(post);
                } if (fuVM.Category != null)
                {
                    category.Images.Add(image);
                    _categoryRepo.Save(category);
                }

            }

            if (fuVM.post != null)
            {
                return true;
            } if (fuVM.Category != null)
            {
                return true;
            } return false;
        }
 public ActionResult SavePage(Post Post)
 {
     if (ModelState.IsValid)
     {
         _postRepo.Save(Post);
         // add a message to the viewbag
         TempData["message"] = string.Format("{0} är sparad", Post.Title);
         // return the user to the list
         return RedirectToAction("Pages", "Post");
     }
     else
     {
         // there is something wrong with the data values
         return View("AddPage", Post);
     }
 }
        public ActionResult SaveCourse(Post post, FormCollection postedForm)
        {
            if (NotAllowedHere()) return RedirectAway();

            if (ModelState.IsValid)
            {
                var theOldPost = _postRepo.FindByID(post.ID);
                if (theOldPost == null)
                    theOldPost = post;
                if (theOldPost.Images == null)
                    theOldPost.Images = new List<Image>();
                var oldImages = theOldPost.Images.ToList();
                var listOfImagesPaths = postedForm["image"];
                string[] arrayOfImagesPaths = null;
                if (listOfImagesPaths != null)
                    arrayOfImagesPaths = listOfImagesPaths.Split(',');

                oldImages = theOldPost.Images.ToList();
                if (arrayOfImagesPaths != null && arrayOfImagesPaths.Count() > 0)
                {
                    foreach (var addedimg in _imgRepo.FindAll(c =>
                arrayOfImagesPaths.Any(cat => cat == c.ImagePath.ToString()) &&
                !c.Posts.Any(p => p.ID == theOldPost.ID)).ToList())
                        theOldPost.Images.Add(addedimg);
                }
                foreach (var removedimg in oldImages.Where(c => arrayOfImagesPaths
                == null || !arrayOfImagesPaths.Any(cat2 => cat2 == c.ImagePath.ToString())))
                    theOldPost.Images.Remove(removedimg);

                if (theOldPost.Category == null)
                    theOldPost.Category = new List<Category>();
                var oldCategories = theOldPost.Category.ToList();
                var listOfCategoryIDs = postedForm["name"];
                string[] arrayOfCategoryIDs = null;
                if (listOfCategoryIDs != null)
                    arrayOfCategoryIDs = listOfCategoryIDs.Split(',');

                oldCategories = theOldPost.Category.ToList();
                if (arrayOfCategoryIDs != null && arrayOfCategoryIDs.Count() > 0)
                {
                    foreach (var addedCat in _categoryRepo.FindAll(c =>
                arrayOfCategoryIDs.Any(cat => cat == c.ID.ToString()) &&
                !c.Posts.Any(p => p.ID == theOldPost.ID)).ToList())
                        theOldPost.Category.Add(addedCat);
                }
                foreach (var removedCat in oldCategories.Where(c => arrayOfCategoryIDs
                == null || !arrayOfCategoryIDs.Any(cat2 => cat2 == c.ID.ToString())))
                    theOldPost.Category.Remove(removedCat);

                if (theOldPost.Badges == null)
                    theOldPost.Badges = new List<Image>();
                var oldBadges = theOldPost.Badges.ToList();
                var listOfBadges = postedForm["badge"];
                string[] arrayOfBadges = null;
                if (listOfBadges != null)
                    arrayOfBadges = listOfBadges.Split(',');

                oldBadges = theOldPost.Badges.ToList();
                if (arrayOfBadges != null && arrayOfBadges.Count() > 0)
                {
                    foreach (var addedbadge in _imgRepo.FindAll(c =>
                 arrayOfBadges.Any(cat => cat == c.ImagePath.ToString()) &&
                 !c.Posts.Any(p => p.ID == theOldPost.ID)).ToList())
                        theOldPost.Badges.Add(addedbadge);
                }
                foreach (var removedimg in oldBadges.Where(c => arrayOfBadges
                == null || !arrayOfBadges.Any(cat2 => cat2 == c.ImagePath.ToString())))
                    theOldPost.Badges.Remove(removedimg);

                _postRepo.Save(post);
                // add a message to the viewbag
                TempData["message"] = string.Format("{0} has been saved", post.Title);
                // return the user to the list
                return RedirectToAction("Index");
            }
            else
            {
                AddCourseViewModel vmCategory = new AddCourseViewModel();
                vmCategory.Post = post;
                if (post.Category != null) {
                    vmCategory.Categories = post.Category.ToList();
                }
                ViewData["events"] = new SelectList(_categoryRepo.FindAll().ToList(), "ID", "Name");
                // there is something wrong with the data values
                return View("AddCourse", vmCategory);
            }
        }
        public ActionResult SaveCourse(Post post, FormCollection postedForm)
        {
            if (ModelState.IsValid)
            {
                var listOfCategoryIDs = postedForm["name"];

                var arrayOfCategoryIDs = listOfCategoryIDs.Split(',');
                 if (arrayOfCategoryIDs.Count() > 0){
                     post.Category = new List<Category>();
                     foreach (var array in arrayOfCategoryIDs) {
                         int x = Convert.ToInt32(array);
                         var cat = _categoryRepo.FindByID(x);
                         cat.Posts.Add(post);
                         _categoryRepo.Save(cat);
                     }
                 }
                _postRepo.Save(post);
                // add a message to the viewbag
                TempData["message"] = string.Format("{0} has been saved", post.Title);
                // return the user to the list
                return RedirectToAction("Index");
            }
            else
            {
                AddCourseViewModel vmCategory = new AddCourseViewModel();
                vmCategory.Post = post;
                if (post.Category != null) {
                    vmCategory.Categories = post.Category.ToList();
                }
                ViewData["events"] = new SelectList(_categoryRepo.FindAll().ToList(), "ID", "Name");
                // there is something wrong with the data values
                return View("AddCourse", vmCategory);
            }
        }