// GET: Orgs/Edit/5
        public ActionResult Edit(short?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Org org = db.Orgs.Find(id);

            if (org == null)
            {
                return(HttpNotFound());
            }
            ViewBag.BaseLocationId = new SelectList(db.Locales.OrderBy(l => l.Name), "LocaleId", "Name", org.BaseLocationId);

            short     infoid = org.InfoId;
            Info      info   = db.Infos.Find(infoid);
            OrgEditVM model  = new OrgEditVM(org, info);

            List <short> selectedTags = db.InfoTags.Where(t => t.InfoId == infoid).Select(t => t.TagId).ToList();

            ViewBag.Selected = selectedTags;
            ViewBag.Tags     = db.Tags.ToList();

            return(View(model));
        }
        public ActionResult Edit([Bind(Include = "OrgId,InfoId,Name,IsPlayerEnabled,SymbolFileName,BaseLocationId,AboutText,IsPublished, Blurb, Artist, IsSecret")] OrgEditPostVM org,
                                 List <short> tags,
                                 HttpPostedFileBase picture,
                                 string submit
                                 )
        {
            #region Pre-model picture check
            if (picture != null)
            {
                string[] goodExts = { ".jpg", ".jpeg", ".gif", ".png" };
                string   imgName  = picture.FileName;

                var    length = picture.ContentLength;
                string ext    = imgName.Substring(imgName.LastIndexOf('.'));

                if (!goodExts.Contains(ext.ToLower()))
                {
                    ModelState.AddModelError("SymbolFileName", "You have submitted a incorrect file type for your portrait. Please use either: .jpg, .jpeg, .gif, or .png");
                }

                if (org.Artist == null)
                {
                    ModelState.AddModelError("Artist", "Katherine, you're trying to submit something with a picture without an artist. That's a no-no! But seriously, if something came up that means you need to change this rule, you know who to call.");
                }
            }
            else if (org.SymbolFileName != "default.jpg" && org.Artist == null)
            {
                ModelState.AddModelError("Artist", "Yo bud, you tired? Seems you deleted the artist by accident. Why don't ya fix that?");
            }

            #endregion

            if (ModelState.IsValid)
            {
                #region Save or Publish?
                switch (submit)
                {
                case "Save Progress":
                case "Un-Publish":
                    org.IsPublished = false;
                    break;

                case "Publish":
                case "Save":
                    org.IsPublished = true;
                    break;

                case "Save and go to complex edit":
                    break;
                }
                #endregion

                var infoid = org.InfoId;
                #region Info Update
                //Info info = db.Infos.Find(infoid);
                Info info = db.Infos.Where(i => i.InfoId == infoid).FirstOrDefault();
                info.Name        = org.Name;
                info.Blurb       = org.Blurb;
                info.IsPublished = org.IsPublished;
                info.IsSecret    = org.IsSecret;
                #endregion

                #region Update tags
                List <short> currentTagIds = db.InfoTags.Where(x => x.InfoId == infoid).Select(x => x.TagId).ToList();

                if (tags != null)
                {
                    foreach (short tag in tags)
                    {
                        //if this is an already existing tag
                        if (currentTagIds.Contains(tag))
                        {
                            currentTagIds.Remove(tag);
                        }
                        //if this is a newly added tag
                        else
                        {
                            InfoTag newTag = new InfoTag {
                                InfoId = infoid, TagId = tag
                            };
                            db.InfoTags.Add(newTag);
                        }
                    }
                }

                if (currentTagIds.Count != 0)
                {
                    foreach (short id in currentTagIds)
                    {
                        InfoTag gone = db.InfoTags.Where(x => x.InfoId == infoid & x.TagId == id).FirstOrDefault();
                        db.InfoTags.Remove(gone);
                    }
                }

                #endregion

                #region Image update/upload
                string[] goodExts = { ".jpg", ".jpeg", ".gif", ".png" };

                #region Image Upload - picture
                if (picture != null)
                {
                    string imgName = picture.FileName;

                    string ext = imgName.Substring(imgName.LastIndexOf('.'));

                    if (goodExts.Contains(ext.ToLower()))
                    {
                        imgName = "symbol-" + infoid + ext;

                        picture.SaveAs(Server.MapPath("~/Content/img/org/" + imgName));
                    }
                    //remove old picture if it had a different extension (and thus would not be overridden)
                    string oldName = org.SymbolFileName;
                    string oldExt  = oldName.Substring(oldName.LastIndexOf('.'));
                    if (oldName != "default.jpg" && oldExt != ext)
                    {
                        string fullPath = Request.MapPath("~/Content/img/org/" + oldName);
                        if (System.IO.File.Exists(fullPath))
                        {
                            System.IO.File.Delete(fullPath);
                        }
                    }
                    //assign new SymbolFileName
                    org.SymbolFileName = imgName;
                }
                #endregion
                #endregion

                #region Update Org
                Org daOrg = new Org
                {
                    OrgId           = org.OrgId,
                    InfoId          = org.InfoId,
                    Name            = org.Name,
                    IsPlayerEnabled = org.IsPlayerEnabled,
                    SymbolFileName  = org.SymbolFileName,
                    BaseLocationId  = org.BaseLocationId,
                    AboutText       = org.AboutText,
                    IsPublished     = org.IsPublished,
                    Artist          = org.Artist
                };
                db.Entry(daOrg).State = EntityState.Modified;
                db.Entry(info).State  = EntityState.Modified;
                db.SaveChanges();
                #endregion
                if (submit == "Save and go to complex edit")
                {
                    return(RedirectToAction("AssoEdit", new { id = org.OrgId }));
                }
                return(RedirectToAction("Details", new { id = org.OrgId }));
            }

            //if model invalid
            ViewBag.BaseLocationId = new SelectList(db.Locales.OrderBy(l => l.Name), "LocaleId", "Name", org.BaseLocationId);
            ViewBag.Tags           = db.Tags.ToList();
            if (tags != null)
            {
                ViewBag.Selected = tags;
            }
            else
            {
                ViewBag.Selected = new List <short>();
            }

            if (picture != null)
            {
                ModelState.AddModelError("SymbolFileName", "Hey, there was some error, so you have to re-upload the picture");
            }
            ModelState.AddModelError("", "Something has gone wrong. Look for red text to see where is went wrong");
            OrgEditVM aOrg = new OrgEditVM(org);
            return(View(aOrg));
        }