/// <summary>
        /// Initializes a new instance of the <see cref="FlightVersionHistory"/> class.
        /// </summary>
        /// <param name="flight">
        /// The flight.
        /// </param>
        /// <param name="entityState">
        /// The entity state.
        /// </param>
        public FlightVersionHistory(Flight f, EntityState entityState)
        {
            this.State = entityState.ToString();

            this.FlightId = f.FlightId;
            this.Created = DateTime.Now;
            this.Deleted = f.Deleted;
            this.LastUpdated = f.LastUpdated;
            this.LastUpdatedBy = f.LastUpdatedBy;
            this.Description = f.Description;

            this.Date = f.Date;
            this.Departure = f.Departure;
            this.Landing = f.Landing;
            this.LandingCount = f.LandingCount;
            this.PlaneId = f.PlaneId;
            this.PilotId = f.PilotId;
            this.PilotBackseatId = f.PilotBackseatId;
            this.BetalerId = f.BetalerId;
            this.StartTypeId = f.StartTypeId;
            this.StartedFromId = f.StartedFromId;
            this.LandedOnId = f.LandedOnId;
            this.TachoDeparture = f.TachoDeparture;
            this.TachoLanding = f.TachoLanding;
        }
        /// <summary>
        /// Default ViewBag information for populating Flight select lists
        /// </summary>
        /// <param name="flight">The flight that is context to the dropdown lists</param>
        private void PopulateViewBag(Flight flight)
        {
            //this.ViewBag.PlaneId = new SelectList(this.db.Planes.Where(p => !p.ExitDate.HasValue || p.ExitDate.Value > DateTime.Today).OrderBy(p => p.CompetitionId), "PlaneId", "RenderName", (flight == null) ? (object)null : flight.PlaneId);
            var planeList = new List<ExtendedSelectListItem>();
            foreach (var plane in this.db.Planes.Where(p => !p.ExitDate.HasValue || p.ExitDate.Value > DateTime.Today).OrderBy(p => p.CompetitionId))
            {
                //planeList.Add(new ExtendedSelectListItem() { Value = plane.PlaneId.ToString(), Text = plane.RenderName, Selected = (flight != null && flight.PlaneId == plane.PlaneId), htmlAttributes = new { data_seats=plane.Seats, data_engine=plane.Engines, data_defaultStartType=plane.DefaultStartType.StartTypeId }});
                planeList.Add(new ExtendedSelectListItem() { Value = plane.PlaneId.ToString(), Text = plane.RenderName, Selected = (flight != null && flight.PlaneId == plane.PlaneId), htmlAttributes = new { data_seats = plane.Seats, data_engine = plane.Engines, data_defaultStartType = (plane.DefaultStartType==null)?1:plane.DefaultStartType.StartTypeId } });
            }
            this.ViewBag.PlaneId = planeList;
            this.ViewBag.StartedFromId = new SelectList(this.db.Locations.OrderBy(p => p.Name), "LocationId", "Name", (flight == null) ? (object)null : flight.StartedFromId);
            this.ViewBag.LandedOnId = new SelectList(this.db.Locations.OrderBy(p => p.Name), "LocationId", "Name", (flight == null) ? (object)null : flight.LandedOnId);    

            if (Request.IsClub())
            {
                var clubid = Request.Club().ClubId;
                this.ViewBag.BetalerId = new SelectList(this.db.Pilots.ToList().Where(p => p.ClubId == clubid).OrderBy(p => p.Name), "PilotId", "RenderName", (flight == null) ? (object)null : flight.BetalerId);
                this.ViewBag.PilotId = new SelectList(this.db.Pilots.ToList().Where(p => p.ClubId == clubid).OrderBy(p => p.Name), "PilotId", "RenderName", (flight == null) ? (object)null : flight.PilotId);
                this.ViewBag.PilotBackseatId = new SelectList(this.db.Pilots.ToList().Where(p => p.ClubId == clubid).OrderBy(p => p.Name), "PilotId", "RenderName", (flight == null) ? (object)null : flight.PilotBackseatId);
                this.ViewBag.StartTypeId = new SelectList(this.db.StartTypes.ToList().Where(p => p.ClubId == null || p.ClubId == clubid).OrderBy(p => p.Name), "StartTypeId", "Name", (flight == null) ? (object)null : flight.StartTypeId);
            }
            else
            {
                this.ViewBag.BetalerId = new SelectList(this.db.Pilots.ToList().OrderBy(p => p.Name), "PilotId", "RenderName", (flight == null) ? (object)null : flight.BetalerId);
                this.ViewBag.PilotId = new SelectList(this.db.Pilots.ToList().OrderBy(p => p.Name), "PilotId", "RenderName", (flight == null) ? (object)null : flight.PilotId);
                this.ViewBag.PilotBackseatId = new SelectList(this.db.Pilots.ToList().OrderBy(p => p.Name), "PilotId", "RenderName", (flight == null) ? (object)null : flight.PilotBackseatId);
                this.ViewBag.StartTypeId = new SelectList(this.db.StartTypes.ToList().Where(p => p.ClubId == null).OrderBy(p => p.Name), "StartTypeId", "Name", (flight == null) ? (object)null : flight.StartTypeId);
            }

            // TODO: Add any pilotid or starttypeid that is actually present in the flight but not present in the selectlist !
            //if (this.ViewBag.BetalerId.() flight.BetalerId 

            // Add request context for keeping the back button alive
            ViewBag.UrlReferrer = ResolveUrlReferrer();
        }
        public ActionResult Edit(Flight flight)
        {
            bool isEditable = false;
            if (User.IsAdministrator()) { isEditable = true; }
            if (User.IsManager()) { isEditable = true; }
            if (flight.Date != null && flight.Date.AddDays(3) >= DateTime.Now)
            {
                isEditable = true;
            }
            if (!isEditable)
            {
                throw new UnauthorizedAccessException(
                    string.Format("User {0} not allowed to edit this flight", this.Request.RequestContext.HttpContext.User.Identity.Name));
            }

            if (ModelState.IsValid)
            {
                this.db.Entry(flight).State = EntityState.Modified;
                flight.LastUpdated = DateTime.Now;
                flight.LastUpdatedBy = User.Pilot().ToString();
                this.db.SaveChanges();
                
                ViewBag.UrlReferrer = ResolveUrlReferrer();
                return RedirectPermanent(ViewBag.UrlReferrer);
                //return RedirectToAction("Grid");
            }
            ViewBag.ChangeHistory = this.GetChangeHistory(flight.FlightId);
            ViewBag.FlightId = flight.FlightId;
            this.PopulateViewBag(flight);
            return View(flight);
        }
        public ActionResult Create(Flight flight)
        {
            if (ModelState.IsValid)
            {
                // Remember base information in cookies
                Response.Cookies["StartTypeId"].Value = flight.StartTypeId.ToString();
                Response.Cookies["StartTypeId"].Expires = DateTime.Now.AddDays(31);
                Response.Cookies["StartedFromId"].Value = flight.StartedFromId.ToString();
                Response.Cookies["StartedFromId"].Expires = DateTime.Now.AddDays(31);
                if (flight.LandedOnId != null)
                {
                    Response.Cookies["LandedOnId"].Value = flight.LandedOnId.Value.ToString();
                    Response.Cookies["LandedOnId"].Expires = DateTime.Now.AddDays(31);
                }
                
                // Create base flight information fields
                flight.FlightId = Guid.NewGuid();
                flight.LastUpdated = DateTime.Now;
                flight.LastUpdatedBy = User.Pilot().ToString();
                this.db.Flights.Add(flight);
                this.db.SaveChanges();

                // Help handle clearing the cache on the availableDates of the Report Index page used on frontpage. 
                if (HttpContext.Application["FlightCreated" + ClubController.CurrentClub.ShortName] == null || (DateTime)HttpContext.Application["FlightCreated" + ClubController.CurrentClub.ShortName] != DateTime.Now.Date)
                {
                    HttpContext.Application["FlightCreated" + ClubController.CurrentClub.ShortName] = DateTime.Now.Date;
                    HttpContext.Application["AvailableDates" + ClubController.CurrentClub.ShortName] = null;
                }

                return RedirectToAction("Grid");
            }
            this.PopulateViewBag(flight);
            return View(flight);
        }
        public ActionResult Create()
        {
            var flight = new Flight
                {
                    Date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day)
                };

            int val;
            if (this.Request.Cookies != null)
            {
                if (this.Request.Cookies["StartTypeId"] != null && Int32.TryParse(this.Request.Cookies["StartTypeId"].Value, out val))
                {
                    flight.StartTypeId = val;
                }
                int val2;
                if (this.Request.Cookies["StartedFromId"] != null && Int32.TryParse(Request.Cookies["StartedFromId"].Value, out val2))
                {
                    flight.StartedFromId = val2;
                }
                int val3;
                if (this.Request.Cookies["LandedOnId"] != null && Int32.TryParse(Request.Cookies["LandedOnId"].Value, out val3))
                {
                    flight.LandedOnId = val3;
                }
            }
            this.PopulateViewBag(flight);
            return View(flight);
        }
 public ActionResult Clone(Flight flight)
 {
     return Create(flight);
 }
     public ActionResult Duplicate(Guid id)
     {
         Flight originalFlight = this.db.Flights.Find(id);
         var flight = new Flight
         {
             Date = originalFlight.Date,
             PlaneId = originalFlight.PlaneId,
             PilotId = originalFlight.PilotId,
             PilotBackseatId = originalFlight.PilotBackseatId,
             BetalerId = originalFlight.BetalerId,
             StartTypeId = originalFlight.StartTypeId,
             StartedFromId = originalFlight.StartedFromId,
             LandedOnId = originalFlight.LandedOnId
         };
 
         // Create base flight information fields
         flight.FlightId = Guid.NewGuid();
         flight.LastUpdated = DateTime.Now;
         flight.LastUpdatedBy = User.Pilot().ToString();
         this.db.Flights.Add(flight);
         this.db.SaveChanges();
         return RedirectToAction("Grid");
     }
 public ActionResult Clone(Guid id)
 {
     Flight originalFlight = this.db.Flights.Find(id);
     var flight = new Flight
     {
         Date = originalFlight.Date,
         PlaneId = originalFlight.PlaneId,
         PilotId = originalFlight.PilotId,
         PilotBackseatId = originalFlight.PilotBackseatId,
         BetalerId = originalFlight.BetalerId,
         StartTypeId = originalFlight.StartTypeId,
         StartedFromId = originalFlight.StartedFromId,
         LandedOnId = originalFlight.LandedOnId
     };
     this.PopulateViewBag(flight);
     return View("Create", flight);
 }
Example #9
0
 /// <summary>
 /// Return true if the current Flight is relevant for the Currently Selected Club
 /// </summary>
 /// <remarks>usable direcly in linq where statements</remarks>
 public static bool IsCurrent(Flight arg)
 {
     return ClubController.CurrentClub.ShortName == null
         || arg.StartedFromId == ClubController.CurrentClub.LocationId
         || arg.LandedOnId == ClubController.CurrentClub.LocationId
         || (arg.Pilot != null && arg.Pilot.ClubId == ClubController.CurrentClub.ClubId)
         || (arg.PilotBackseat != null && arg.PilotBackseat.ClubId == ClubController.CurrentClub.ClubId)
         || (arg.Betaler != null && arg.Betaler.ClubId == ClubController.CurrentClub.ClubId);
 }