/**
  * Function: Authentication
  * Check role by username
  * If is admin or manager or staff, continues else redirect to home page
  * Author: Le Dang Son
  * Date: 06/08/2012
  */
 public int Authentication()
 {
     if (Session["admin"] != null)
     {
         Members member = (Members)Session["admin"];
         if (member.Name != null && member.Name != "" && member.Role.ID < 4)
         {
             ViewBag.Name = member.Name;
             Menus menusModels = new Menus();
             menusModels.Controller = RouteData.Values["controller"].ToString().ToLower();
             menusModels.Action = RouteData.Values["action"].ToString().ToLower();
             var db = new FineArtContext();
             var query = db.Menus.Include("Role")
                 .Where(m => m.Controller == ((menusModels.Controller == "index") ? "" : menusModels.Controller)
                     && m.Action == ((menusModels.Action == "index") ? "" : menusModels.Action))
                     .FirstOrDefault();
             var role = query.Role.Where(r => r.ID == member.Role.ID).FirstOrDefault();
             if (role == null)
             {
                 return 2;
             }
             else
             {
                 return 1;
             }
         }
         else
         {
             return 0;
         }
     }
     else
     {
         return 0;
     }
 }
        public ActionResult Add(FormCollection form, HttpPostedFileBase Icon)
        {
            int admin = base.Authentication();
            if (admin == 0)
            {
                return Redirect("~/member/logout");
            }
            else if (admin == 1)
            {
                base.LoadMenu();
                var db = new FineArtContext();
                var query = db.Menus.Where(m => m.ParentID == -1);
                if (form["submit_menu"] != null)
                {
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.Append("<ul>");
                    Strings stringsLibs = new Strings();
                    if (form["Name"].Trim() == "")
                    {
                        stringBuilder.Append("<li>Please type menu name</li>");
                    }
                    else
                    {
                        try
                        {
                            string name = form["Name"];
                            var menu = db.Menus.Where(m => m.Name == name).First();
                            stringBuilder.Append("<li>This menu had been exists in database. Try a different.</li>");
                        }
                        catch
                        {

                        }
                    }

                    int Parent = Convert.ToInt16(form["Parent"]);
                    bool Display = false;
                    if (Parent != -1)
                    {
                        try
                        {
                            Menus menu = db.Menus.Where(m => m.ID == Parent && m.ParentID == -1).First();
                            if (form["Display"] == "on")
                            {
                                Display = true;
                            }
                        }
                        catch
                        {
                            stringBuilder.Append("<li>Please chose parent menu for this menu</li>");
                        }
                    }
                    else
                    {
                        Display = true;
                        if (Icon == null)
                        {
                            stringBuilder.Append("<li>Please chose icon for this menu</li>");
                        }
                    }
                    if (stringBuilder.ToString() == "<ul>")
                    {
                        if (Icon != null)
                        {
                            ImagesClass imageClass = new ImagesClass(Icon);
                            string path = Server.MapPath("~/Content/Images/admins/menu-icon" + form["Alias"] + ".jpg");
                            imageClass.CreateNewImage(path, 18, 16);
                        }
                        Menus menu = new Menus
                        {
                            Name = form["Name"].Trim(),
                            Controller = form["Controller"].Trim(),
                            Action = form["Action"].Trim(),
                            Description = form["Description"].Trim(),
                            Display = Display,
                            Icon = form["Alias"].Trim() + ".jpg",
                            ParentID = Parent
                        };
                        db.Menus.Add(menu);
                        db.SaveChanges();
                        ViewBag.success = "Add menu success!";
                    }
                    else
                    {
                        stringBuilder.Append("</ul>");
                        ViewBag.error = stringBuilder.ToString();
                        ViewBag.dataForm = form;
                    }
                }
                ViewBag.parentMenu = query;
                return View();
            }
            else
            {
                Session["errorContorllerAction"] = true;
                return Redirect("~/administrator");
            }
        }