/*-----------------------------------------
 * Event Controller
 * -------------------------------------*/
 public ActionResult AddEvent(int id = 0)
 {
     ModelState.Clear();
     Event Event = new Event();
     Event.PostID = id;
     Event.Created = DateTime.Now;
     Event.StartDate = DateTime.Now;
     Event.EndDate = DateTime.Now;
     return View("AddEvent", Event);
 }
        /*-----------------------------------------
        * Event Controller
        * -------------------------------------*/
        public ActionResult AddEvent(int id = 0)
        {
            if (NotAllowedHere()) return RedirectAway();

            ModelState.Clear();
            Event Event = new Event();
            Event.PostID = id;
            Event.Created = DateTime.Now;
            Event.StartDate = DateTime.Now;
            Event.EndDate = DateTime.Now;
            return View("AddEvent", Event);
        }
        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 ActionResult SaveEvent(Event Event, FormCollection form)
        {
            if (NotAllowedHere()) return RedirectAway();

            if (ModelState.IsValid)
            {
                var theOldPost = _eventRepo.FindByID(Event.ID);
                if (theOldPost == null)
                    theOldPost = Event;
                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);
                _eventRepo.Save(Event);
                // add a message to the viewbag
                TempData["message"] = string.Format("{0} has been saved", Event.Title);
                // return the user to the list
                return RedirectToAction("Course", new { id = Event.PostID });
            }
            else
            {
                // there is something wrong with the data values
                return View("AddEvent", Event);
            }
        }
 public ActionResult SaveEvent(Event Event)
 {
     if (ModelState.IsValid)
     {
         _eventRepo.Save(Event);
         // add a message to the viewbag
         TempData["message"] = string.Format("{0} has been saved", Event.Title);
         // return the user to the list
         return RedirectToAction("Course", new { id = Event.PostID });
     }
     else
     {
         // there is something wrong with the data values
         return View("AddEvent", Event);
     }
 }