public ActionResult Edit(int primaryKey1 = 0, int primaryKey2 = 0, string primaryKey3 = "", string primaryKey4 = "")
        {
            DateTime meetingPKDateTime;

            if (!DateTime.TryParse(primaryKey3, out meetingPKDateTime))
            {
                return(HttpNotFound());
            }

            string discItemPKTitle = HttpUtility.UrlDecode(primaryKey4);

            DiscItem discItem = db.DiscItem.Find(primaryKey1, primaryKey2, meetingPKDateTime, discItemPKTitle);

            if (discItem == null)
            {
                return(HttpNotFound());
            }

            ViewData["voteTypes"] = new SelectList(db.VoteType, "Type", "Type");
            ViewBag.Category      = new SelectList(db.Category, "Type", "Description");

            ViewData["voteTypes"] = new SelectList(db.VoteType, "Type", "Type");
            //Find if a discussion item document exists for this discussion item
            DiscItemDocument discItemDoc = new DiscItemDocument();

            discItemDoc = db.DiscItemDocument.Find(discItem.Meeting_Comm_CommOwn_ID, discItem.Meeting_Comm_ID, discItem.Meeting_DateTime, discItem.Title);

            if (discItemDoc != null && discItem.DiscItemDocument.FileImage.Length > 0)
            {
                Session["fileImage"]   = discItem.DiscItemDocument.FileImage;
                Session["contentType"] = discItem.DiscItemDocument.ContentType;
                Session["fileLength"]  = discItem.DiscItemDocument.FileImage.Length;
            }
            return(View(discItem));
        }
        public ActionResult Download(int primaryKey1 = 0, int primaryKey2 = 0, string primaryKey3 = "", string primaryKey4 = "")
        {
            DateTime meetingPKDateTime;

            if (!DateTime.TryParse(primaryKey3, out meetingPKDateTime))
            {
                return(HttpNotFound());
            }

            DiscItemDocument discitemdoc = db.DiscItemDocument.Find(primaryKey1, primaryKey2, meetingPKDateTime, primaryKey4);

            var cd = new System.Net.Mime.ContentDisposition
            {
                // for example foo.bak
                FileName = discitemdoc.Filename,

                // the browser always to try to show the file inline
                // set to false if you want to always prompt the user for downloading,
                Inline = true,
            };

            Response.AppendHeader("Content-Disposition", cd.ToString());
            return(File(discitemdoc.FileImage, discitemdoc.ContentType));
        }
        public ActionResult Details(int primaryKey1 = 0, int primaryKey2 = 0, string primaryKey3 = "", string primaryKey4 = "")
        {
            DateTime meetingPKDateTime;

            if (!DateTime.TryParse(primaryKey3, out meetingPKDateTime))
            {
                return(HttpNotFound());
            }

            DiscItem discItem = db.DiscItem.Find(primaryKey1, primaryKey2, meetingPKDateTime, primaryKey4);

            if (discItem == null)
            {
                return(HttpNotFound());
            }
            else
            {
                //determine if current user is a current CA of meetings and send result to view
                if (discItem.Meeting.Comm.CommMember.Any(cm => cm.Member_Email == User.Identity.Name &&
                                                         (cm.IsAdministrator == "Y" || cm.IsConvener == "Y") &&
                                                         cm.StartDate <= DateTime.Today &&
                                                         cm.EndDate >= DateTime.Today))
                {
                    ViewBag.isCommitteeAdmin = true;
                }
                else
                {
                    ViewBag.isCommitteeAdmin = false;
                }

                // Check if there is a file for this discuission item
                DiscItemDocument discItemDoc = new DiscItemDocument();
                discItemDoc = db.DiscItemDocument.Find(discItem.Meeting_Comm_CommOwn_ID, discItem.Meeting_Comm_ID, discItem.Meeting_DateTime, discItem.Title);

                if (discItemDoc != null && discItem.DiscItemDocument.FileImage.Length > 0)
                {
                    ViewBag.fileExists = true;
                    ViewBag.fileName   = discItem.DiscItemDocument.Filename;
                }
                else
                {
                    ViewBag.fileExists = false;
                }

                //check if user is voting member
                if (discItem.Meeting.Comm.CommMember.Any(cm => cm.Member_Email == User.Identity.Name &&
                                                         cm.Voting_Non_Voting == "V" &&
                                                         cm.StartDate <= DateTime.Today &&
                                                         cm.EndDate >= DateTime.Today))
                {
                    ViewBag.isVotingMember = true;
                }
                else
                {
                    ViewBag.isVotingMember = false;
                }
                // Get a list of all discussion associated with this discussion item
                List <Discussion> discItemDisc = new List <Discussion>();
                discItemDisc = discItem.Discussion.ToList();

                ViewBag.voted = "N";
                if (discItem.IsVoted == "Y")
                {
                    // Check if the user voted on this discussion item
                    foreach (var disc in discItemDisc)
                    {
                        if (disc.SysUser_Email == User.Identity.Name &&
                            disc.HasVoted == "Y")
                        {
                            ViewBag.voted = "Y";
                            break;
                        }
                    }
                }

                ViewBag.read = "N";
                if (discItem.IsRead == "Y")
                {
                    // Check if the user read this discussion item
                    foreach (var disc in discItemDisc)
                    {
                        if (disc.DiscItem_Meeting_Comm_CommOwn_ID == discItem.Meeting_Comm_CommOwn_ID &&
                            disc.DiscItem_Meeting_Comm_ID == discItem.Meeting_Comm_ID &&
                            disc.DiscItem_Meeting_DateTime == discItem.Meeting_DateTime &&
                            disc.DiscItem_Title == discItem.Title &&
                            disc.SysUser_Email == User.Identity.Name &&
                            disc.HasRead == "Y")
                        {
                            ViewBag.read = "Y";
                            break;
                        }
                    }
                }

                ViewBag.votetypes    = new SelectList(discItem.VoteType, "Type", "Type");
                ViewBag.votable      = discItem.IsVoted;
                ViewBag.readable     = discItem.IsRead;
                ViewBag.IsAnonVoting = discItem.IsAnonVoting;
                return(View(discItem));
            }
        }
        public ActionResult Create(DiscItem discItem, String Tags, string[] voteTypes)
        {
            discItem.CreatedDate = DateTime.Now;
            var di = db.DiscItem.Include(d => d.Meeting).ToList();

            if (ModelState.IsValid)
            {
                db.DiscItem.Add(discItem);

                // Add vote types to DiscItemVoteType table if discussion item is votable.
                if (discItem.IsVoted == "Y")
                {
                    if (voteTypes == null)
                    {
                        ModelState.AddModelError("VoteType", "You must select the vote types");
                        ViewData["voteTypes"] = new SelectList(db.VoteType, "Type", "Type");
                        ViewBag.MeetingTime   = discItem.Meeting_DateTime;
                        return(View(discItem));
                    }

                    VoteType newVoteType;
                    foreach (string vt in voteTypes)
                    {
                        newVoteType = new VoteType {
                            Type = vt
                        };
                        db.VoteType.Attach(newVoteType);
                        discItem.VoteType.Add(newVoteType);
                    }
                }

                // Check if the title is valid
                if (db.DiscItem.Any(disc => disc.Meeting_Comm_CommOwn_ID == discItem.Meeting_Comm_CommOwn_ID &&
                                    disc.Meeting_Comm_ID == discItem.Meeting_Comm_ID &&
                                    disc.Meeting_DateTime == discItem.Meeting_DateTime &&
                                    disc.Title == discItem.Title))
                {
                    ModelState.AddModelError("Title", "A discussion item already exists with this title");
                    ViewData["voteTypes"] = new SelectList(db.VoteType, "Type", "Type");
                    return(View(discItem));
                }


                HttpPostedFileBase file = Request.Files[0];

                DiscItemDocument discItemDoc = new DiscItemDocument();

                if (file.ContentLength > 0) // checks if file was uploaded to the form
                {
                    DateTime discItemDateTime = discItem.Meeting_DateTime;

                    // sets the PKs of the disc Item Document
                    discItemDoc.DiscItem_Meeting_Comm_CommOwn_ID = discItem.Meeting_Comm_CommOwn_ID;
                    discItemDoc.DiscItem_Meeting_Comm_ID         = discItem.Meeting_Comm_ID;
                    discItemDoc.DiscItem_Meeting_DateTime        = discItemDateTime;
                    discItemDoc.DiscItem_Title = discItem.Title;

                    // getting size of file
                    int fileLen = file.ContentLength;

                    // create write buffer
                    byte[] byteFile = new byte[fileLen];
                    file.InputStream.Read(byteFile, 0, fileLen);

                    // write file to discItemDoc
                    discItemDoc.FileImage   = byteFile;
                    discItemDoc.Filename    = file.FileName;
                    discItemDoc.ContentType = file.ContentType;

                    // gets tags of file
                    discItemDoc.Tags = Tags;
                }

                if (discItemDoc != null && file.ContentLength > 0)
                {
                    db.DiscItemDocument.Add(discItemDoc);
                }

                db.SaveChanges();

                return(RedirectToAction("Details", "DiscItem", new
                {
                    primaryKey1 = discItem.Meeting_Comm_CommOwn_ID,
                    primaryKey2 = discItem.Meeting_Comm_ID,
                    primaryKey3 = discItem.Meeting_DateTime.ToString("MM-dd-yyyy h.mm.ss t\\M"),
                    primaryKey4 = discItem.Title
                }));
            }


            ViewData["voteTypes"] = new SelectList(db.VoteType, "Type", "Type");
            ViewBag.MeetingTime   = db.Meeting.Find(discItem.Meeting.Comm_CommOwn_ID, discItem.Meeting.Comm_ID, discItem.Meeting.DateTime).DateTime;
            return(View("Details", "DiscItem", new
            {
                primaryKey1 = discItem.Meeting_Comm_CommOwn_ID,
                primaryKey2 = discItem.Meeting_Comm_ID,
                primaryKey3 = discItem.Meeting_DateTime.ToString("MM-dd-yyyy h.mm.ss t\\M"),
                primaryKey4 = discItem.Title
            }));
        }