/**
  * Controller: Members
  * Action: Index
  * Show login form and check admin
  * 
  * @param name="form":data for login
  * @returns:if is admin:redirect to admin index page else show login form
  * 
  * Author: Le Dang Son
  * Date: 06/08/2012
  */
 public ActionResult Index(string id)
 {
     int admin = base.Authentication();
     if (admin == 0)
     {
         return Redirect("~/member/logout");
     }
     else if (admin == 1)
     {
         base.LoadMenu();
         int currentPage = Paging.GetPage(id);
         decimal totalRecord = GlobalInfo.NumberRecordInPage;
         Members membersModels = new Members();
         decimal totalMember = membersModels.TotalMember();
         int totalPage = (int)Math.Ceiling(Convert.ToDecimal(totalMember / totalRecord));
         Paging.numPage = totalPage;
         Paging.numLinkDisplay = GlobalInfo.NumLinkPagingDisplay;
         Paging.currentPage = currentPage;
         string url = "administrator/members/index";
         ViewBag.pagingString = Paging.GenerateLinkPaging(url);
         ViewBag.Title += " Members";
         return View(membersModels.ListMembers((int)((currentPage - 1) * totalRecord), (int)totalRecord));
     }
     else
     {
         Session["errorContorllerAction"] = true;
         return Redirect("~/administrator");
     }
 }
 public ActionResult DesignMember(string id)
 {
     int admin = base.Authentication();
     if (admin == 0)
     {
         return Redirect("~/member/logout");
     }
     else if (admin == 1)
     {
         base.LoadMenu();
         try
         {
             int idd = Convert.ToInt16(id);
             Members membersModels = new Members();
             membersModels.ID = idd;
             membersModels = membersModels.GetMemberWithID();
             ViewBag.Title += " Designs of " + membersModels.Name + " Member";
             List<Designs> listDesign = membersModels.Design.ToList();
             List<Designs> listDesignNew = new List<Designs>();
             listDesign.ForEach(delegate(Designs design)
             {
                 design = design.GetDesignByID();
                 listDesignNew.Add(design);
             });
             return View(listDesignNew);
         }
         catch
         {
             Session["admin"] = null;
             return Redirect("~/");
         }
     }
     else
     {
         Session["errorContorllerAction"] = true;
         return Redirect("~/administrator");
     }
 }
 public ActionResult Login(FormCollection form)
 {
     if (Session["user-loged"] != null)
     {
         if (Session["total_login"] != null && 4 - (int)Session["total_login"] <= 0)
         {
             return Redirect("~/");
         }
         else
         {
             if (form["submit_admin_login"] != null && form["submit_admin_login"] == "Login Admin")
             {
                 Members membersModels = new Members();
                 Strings stringLib = new Strings();
                 MD5 md5Hash = MD5.Create();
                 string password = form["password"];
                 password = stringLib.GetMd5Hash(md5Hash, stringLib.GetMd5Hash(md5Hash, password) + "hashpassword");
                 membersModels = membersModels.GetMemberByUserAndPass(form["username"], password);
                 try
                 {
                     if (membersModels.Name != null && membersModels.Name != "")
                     {
                         Session["admin"] = membersModels;
                         return Redirect("/administrator/");
                     }
                     else
                     {
                         if (Session["total_login"] == null)
                         {
                             Session["total_login"] = 1;
                         }
                         else
                         {
                             string path = Server.MapPath("~/");
                             Log log = new Log(path, "LoginController.Index");
                             log.WriteLog("Username: "******"username"] + " Password: "******"password"]);
                             Session["total_login"] = (int)Session["total_login"] + 1;
                         }
                         ViewBag.form_error = "Username or password wrong. Try again! You have to login " +
                                 (5 - (int)Session["total_login"]) + " time!";
                     }
                 }
                 catch (Exception e)
                 {
                     string path = Server.MapPath("~/");
                     Log log = new Log(path, "LoginController.Login");
                     if (Session["total_login"] == null)
                     {
                         Session["total_login"] = 1;
                     }
                     else
                     {
                         string pathother = Server.MapPath("~/");
                         Log objLog = new Log(path, "LoginController.Login");
                         objLog.WriteLog(e.Message.ToString());
                         log.WriteLog("Username: "******"username"] + " Password: "******"password"]);
                         Session["total_login"] = (int)Session["total_login"] + 1;
                     }
                     ViewBag.form_error = "Username or password wrong. Try again! You have to login " +
                             (5 - (int)Session["total_login"]) + " time!";
                 }
             }
         }
     }
     else
     {
         return Redirect("~/member");
     }
     return View();
 }
        public ActionResult Register(FormCollection form, HttpPostedFileBase Images)
        {
            if (Session["user-loged"] == null)
            {
                if (form["submit-register"] != null)
                {
                    var db = new FineArtContext();
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.Append("<ul>");
                    if (form["name"].Trim() == "" || form["name"].Trim().ToLower() == "full name")
                    {
                        stringBuilder.Append("<li>Please type your full name.</li>");
                    }
                    if (form["username"].Trim() == "" || form["username"].Trim().ToLower() == "username")
                    {
                        stringBuilder.Append("<li>Please type username.</li>");
                    }
                    else
                    {
                        string username = form["username"].Trim();
                        Members member = db.Members.Where(m => m.Username == username).FirstOrDefault();
                        if (member != null)
                        {
                            stringBuilder.Append("<li>Has been exists this username in database. Try other username.</li>");
                        }
                    }
                    if (!Validator.ISEmail(form["email"]))
                    {
                        stringBuilder.Append("<li>Email not valid.</li>");
                    }
                    else if (form["email"] != form["verifyemail"])
                    {
                        stringBuilder.Append("<li>Please verify email.</li>");
                    }
                    else
                    {
                        string email = form["email"].Trim();
                        Members member = db.Members.Where(m => m.Email == email).FirstOrDefault();
                        if (member != null)
                        {
                            stringBuilder.Append("<li>This email has been using. Try other email.</li>");
                        }
                    }

                    if (form["password"].Trim() == "" || form["password"].Trim().ToLower() == "password")
                    {
                        stringBuilder.Append("<li>Please type password.</li>");
                    }
                    else if (form["password"] != form["verifypassword"])
                    {
                        stringBuilder.Append("<li>Please verify password.</li>");
                    }
                    string birthday = form["day"] + "/" + form["month"] + "/" + form["year"];
                    DateTime Birthday = new DateTime();
                    try
                    {
                        Birthday = DateTime.Parse(birthday);
                    }
                    catch
                    {
                        stringBuilder.Append("<li>Your birthday not valid.</li>");
                    }

                    if (form["address"].Trim() == "" || form["address"].Trim().ToLower() == "address")
                    {
                        stringBuilder.Append("<li>Please type your address.</li>");
                    }

                    if (form["phone"].Trim() == "" || form["phone"].Trim().ToLower() == "phone")
                    {
                        stringBuilder.Append("<li>Please type your phone.</li>");
                    }
                    else if (!Validator.ISPhoneNumber(form["phone"]))
                    {
                        stringBuilder.Append("<li>Your phone number not valid.</li>");
                    }

                    if (Images == null)
                    {
                        stringBuilder.Append("<li>Please choose your avatar.</li>");
                    }
                    string gender;
                    switch (form["gender"])
                    {
                        case "0": gender = "Male"; break;
                        case "1": gender = "Female"; break;
                        default: gender = "Male"; break;
                    }

                    if (stringBuilder.ToString() == "<ul>")
                    {
                        MD5 md5Hash = MD5.Create();
                        Strings stringLib = new Strings();
                        string password = stringLib.GetMd5Hash(md5Hash, stringLib.GetMd5Hash(md5Hash, form["password"]) + "hashpassword");
                        eProjectsSemIII.Models.Roles role = db.Roles.Where(r => r.ID == 4).First();
                        ImagesClass imageLib = new ImagesClass(Images);
                        string path = Server.MapPath("~/Content/Images/students/" + form["username"] + ".jpg");
                        imageLib.CreateNewImage(path, 200, 240);
                        Members member = new Members
                        {
                            Name = form["name"].Trim(),
                            Username = form["username"].Trim(),
                            Password = password,
                            Email = form["email"].Trim(),
                            Birthday = Birthday,
                            Address = form["address"].Trim(),
                            Phone = form["phone"].Trim(),
                            Gender = gender,
                            Datejoin = DateTime.Now,
                            Images = form["username"].Trim() + ".jpg",
                            Role = role,
                        };
                        db.Members.Add(member);
                        db.SaveChanges();
                        ViewBag.success = true;
                    }
                    else
                    {
                        ViewBag.dataForm = form;
                        stringBuilder.Append("</ul>");
                        ViewBag.error = stringBuilder.ToString();
                    }
                }
            }
            else
            {
                return Redirect("~/");
            }
            return View();
        }