public FrontController()
     : base()
 {
     this._khoitao_cookie();
     this._khachhang = null;
     this._giohang = new DonHang();
 }
 public int add(KhachHang obj)
 {
     //hash password first
     obj.matkhau = TextLibrary.GetSHA1HashData(obj.matkhau);
     //call add
     this._db.ds_khachhang.Add(obj);
     //commit
     this._db.SaveChanges();
     //return ma moi nhat
     return this._db.ds_khachhang.Max(x => x.id);
 }
 public ActionResult Add()
 {
     //check
     if (!this._nhanvien_permission.Contains("khachhang_add"))
     {
         return this._fail_permission("khachhang_add");
     }
     LoaiKhachHangController ctr_lkh = new LoaiKhachHangController();
     KhachHang obj = new KhachHang();
     //nếu chưa có loại kh nào thì báo lỗi k thể thêm
     if (ctr_lkh.timkiem("", "", "1").Count == 0)
     {
         return _show_notification("Yêu cầu phải có ít nhất 1 loại khách hàng active mới có thể thêm mới khách hàng");
     }
     ViewBag.KhachHang = obj;
     ViewBag.Title += " - Thêm mới";
     ViewBag.LoaiKhachHang_List = ctr_lkh.timkiem();
     ViewBag.add_mode = "1";
     return View("Index");
 }
 public ActionResult Submit()
 {
     //get obj id first
     int obj_id = TextLibrary.ToInt(Request["khachhang_id"]);
     KhachHangController ctr = new KhachHangController();
     LoaiKhachHangController ctr_lkh = new LoaiKhachHangController(ctr._db);
     //khoi tao moi doi tuong
     KhachHang obj;
     Boolean edit_mode = true;
     if (obj_id == 0)
     {
         //add mode
         //check permission
         if (!this._nhanvien_permission.Contains("khachhang_add"))
         {
             return this._fail_permission("khachhang_add");
         }
         obj = new KhachHang();
         edit_mode = false;
     }
     else
     {
         //update mode
         //check permission
         if (!this._nhanvien_permission.Contains("khachhang_edit"))
         {
             return _fail_permission("khachhang_edit");
         }
         //kiem tra ton tai
         if (ctr.is_exist(obj_id))
         {
             //get obj
             obj = ctr.get_by_id(obj_id);
         }
         else
         {
             //id khong ton tai
             return RedirectToAction("Index", "AdminKhachHangs");
         }
     }
     //assign data
     List<string> validate = new List<string>();
     string matkhau = TextLibrary.ToString(Request["khachhang_matkhau"]);
     string matkhau2 = TextLibrary.ToString(Request["khachhang_matkhau2"]);
     obj.email = TextLibrary.ToString(Request["khachhang_email"]);
     obj.sdt = TextLibrary.ToString(Request["khachhang_sdt"]);
     obj.diachi = TextLibrary.ToString(Request["khachhang_diachi"]);
     obj.tendangnhap =  TextLibrary.ToString(Request["khachhang_tendangnhap"]);
     obj.tendaydu =  TextLibrary.ToString(Request["khachhang_tendaydu"]);
     obj.bad = TextLibrary.ToBoolean(Request["khachhang_bad"]);
     obj.active = TextLibrary.ToBoolean(Request["khachhang_active"]);
     //validate
     validate.AddRange(ctr.validate(obj,matkhau,matkhau2));
     //action
     if (validate.Count==0)
     {
         if (edit_mode)
         {
             //call update for properties
             ctr._db.SaveChanges();
             //call set password
             ctr.set_password(obj.id,matkhau2);
             validate.Add("edit_ok");
         }
         else
         {
             //add mode mới có chuyện set loại KH
                 obj.loaikhachhang = ctr_lkh.get_by_id(
                 TextLibrary.ToInt(Request["khachhang_loaikhachhang_id"])
                 );
                 obj.diem = obj.loaikhachhang.mucdiem;
             //call update loaikh first
             obj._Update_LoaiKhachHang(ctr._db);
             //set raw password
             obj.matkhau = matkhau2;
             //call add
             int maxid = ctr.add(obj);
             //re assign id
             obj.id = maxid;
             validate.Add("add_ok");
         }
     }
     obj.loaikhachhang = ctr_lkh.get_by_id(
         TextLibrary.ToInt(Request["khachhang_loaikhachhang_id"])
     );
     ViewBag.KhachHang = obj;
     ViewBag.Title += " - Submit";
     ViewBag.State = validate;
     ViewBag.LoaiKhachHang_List = ctr_lkh.timkiem();
     ViewBag.add_mode = obj_id==0?"1":"0";
     return View("Index");
 }
 protected override void OnActionExecuting(ActionExecutingContext filterContext)
 {
     base.OnActionExecuting(filterContext);
     KhachHangController ctr_kh = new KhachHangController();
     NhomSanPhamController ctr = new NhomSanPhamController();
     List<NhomSanPham2> list1 = ctr.timkiem("", "", "", "1");
     SanPhamController ctr2 = new SanPhamController(ctr._db);
     List<SanPham>list2=ctr2.get_bestseller(4);
     if (list1 != null && list2 != null)
     {
         ViewBag.NhomSanPham2_List_All = list1;
         ViewBag.SanPham_BestSeller = list2;
     }
     else
     {
         ViewBag.NhomSanPham2_List_All = new List<NhomSanPham2>();
         ViewBag.SanPham_BestSeller = new List<SanPham>();
     }
     //tim kiem
     //build timkiem_sanpham
     if (Request.Cookies.Get("front_timkiem_sanpham") == null)
     {
         //chưa set cookies trước => tiến hành set cookies
         this._khoitao_cookie();
         this._luu_cookie();
     }
     else
     {
         try
         {
             this.front_timkiem_sanpham = CookieLibrary.Base64Decode(Request.Cookies.Get("front_timkiem_sanpham"));
         }
         catch (Exception)
         {
             this._khoitao_cookie();
             this._luu_cookie();
         }
     }
     ViewBag.front_timkiem_sanpham = this.front_timkiem_sanpham;
     //Load thong tin KhachHang
     if (!this._is_logged_in())
     {
         //Chưa có ai đăng nhập hệ thống
         if (Session["khachhang"] != null)
         {
             //nếu như KH đã đăng nhập rồi
             this._khachhang = ctr_kh.get_by_id(((KhachHang)Session["khachhang"]).id);
         }
         else
         {
             //lấy từ cookies lên
             //lay thong tin tu cookies
             HttpCookie _tmp = Request.Cookies.Get("khachhang");
             if (_tmp != null)
             {
                 int uid = TextLibrary.ToInt(_tmp["khachhang_id"].ToString());
                 string password = TextLibrary.ToString(_tmp["khachhang_password"].ToString());
                 //lay thong tin user theo yeu cau dang nhap
                 this._khachhang = ctr_kh.get_by_id_hash_password(uid, password);
             }
         }
     }
     //
     //
     ViewBag.nhanvien = this._nhanvien;
     ViewBag.khachhang = this._khachhang;
     //get cart
         if (Session["giohang"] != null)
         {
             try
             {
                 this._giohang = (DonHang)Session["giohang"];
             }
             catch (Exception)
             {
                 this._giohang = new DonHang();
             }
         }
         else
         {
             this._giohang = new DonHang();
         }
         //gán khach hang va nhan vien
         this._giohang.khachhang = this._khachhang;
         this._giohang.khachhang_nhanvien = this._nhanvien;
         //save cart
         this._save_cart_to_session();
     ViewBag.giohang = this._giohang;
 }
 public List<string> validate(KhachHang obj, string matkhau, string matkhau2)
 {
     List<string> re = new List<string>();
     string[] forbiden = { "admin", "mod", "moderator", "administrator", "root", "super", "user"};
     if (!ValidateLibrary.is_valid_email(obj.email))
     {
         re.Add("email_fail");
     }
     if (matkhau != matkhau2 || matkhau.Equals(""))
     {
         re.Add("matkhau_fail");
     }
     if (obj.tendangnhap.Equals(""))
     {
         re.Add("tendangnhap_fail");
     }
     if (obj.tendaydu.Equals(""))
     {
         re.Add("tendaydu_fail");
     }
     if (obj.sdt.Equals(""))
     {
         re.Add("sdt_fail");
     }
     /*if (obj.diachi.Equals(""))
     {
         re.Add("diachi_fail");
     }*/
     if (!this.can_use_tendangnhap(obj.id,obj.tendangnhap))
     {
         re.Add("tendangnhap_exist_fail");
     }
     if (forbiden.Contains(obj.tendangnhap.ToLower()))
     {
         re.Add("tendangnhap_exist_fail");
     }
     if (!this.can_use_email(obj.id, obj.email))
     {
         re.Add("email_exist_fail");
     }
     return re;
 }
        public ActionResult Submit()
        {
            KhachHangController ctr = new KhachHangController();
            //get post value
            string tendangnhap = TextLibrary.ToString(Request["khachhang_tendangnhap"]);
            string tendaydu = TextLibrary.ToString(Request["khachhang_tendaydu"]);
            string matkhau = TextLibrary.ToString(Request["khachhang_matkhau"]);
            string matkhau2 = TextLibrary.ToString(Request["khachhang_matkhau2"]);
            string diachi = TextLibrary.ToString(Request["khachhang_diachi"]);
            string sdt = TextLibrary.ToString(Request["khachhang_sdt"]);
            string email = TextLibrary.ToString(Request["khachhang_email"]);
            string captcha = TextLibrary.ToString(Request["khachhang_captcha"]);
            //pass to obj
            KhachHang obj = new KhachHang();
            obj.diachi = diachi;
            obj.email = email;
            obj.matkhau = matkhau;
            obj.sdt = sdt;
            obj.tendangnhap = tendangnhap;
            obj.tendaydu = tendaydu;

            //validate
            List<string> validate = new List<string>();
            //xét captcha trước
            if (!this.get_captcha_string().ToLower().Equals(captcha.ToLower()))
            {
                validate.Add("captcha_fail");
            }
            //validate obj
            validate.AddRange(ctr.validate(obj, matkhau, matkhau2));
            //check
            if (validate.Count == 0)
            {
                //call update loaikh first
                obj._Update_LoaiKhachHang(ctr._db);
                //tiến hành thêm và gán session auto đăng nhập
                int max_id = ctr.add(obj);
                obj.id = max_id;
                //save to session
                Session["khachhang"] = ctr.get_by_id(max_id);
                //đăng ký thành công
                //nếu được dẫn link từ FrontCart.CheckOut thì quay lại checkOut
                if (Session["link_after_login"] != null)
                {
                    string url_to = (string)Session["link_after_login"];
                    Session["link_after_login"] = null;
                    return Redirect(url_to);
                }
                return RedirectToAction("Index", "FrontHome");
            }
            //add and redirect or return error

               //set tmp validate
            ViewBag.State = validate;
            ViewBag.khachhang_register = obj;
               return View("Index");
        }