Ejemplo n.º 1
0
        public JsonResult Save(BaseUser model, FormCollection form, bool multiUser = false)
        {
            BoolString validation = model.BeforeSave(db);

            if (validation.BoolValue)
            {
                return(Json(new { Message = validation.StringValue }));
            }
            if (model.ID != 0)
            {
                validation = model.BeforeEdit(db);
                if (validation.BoolValue)
                {
                    return(Json(new { Message = validation.StringValue }));
                }
                if (ModelState.IsValid)
                {
                    bool logout = false;
                    if (model.username != Helper.getData("SELECT username from [BaseUser] where ID=" + model.ID, db).Rows[0][0].ToString())
                    {
                        logout = true;
                    }
                    if (model.password == Permission.defaultShowPassword)
                    {
                        model.password = Helper.getData("SELECT password from [BaseUser] where ID=" + model.ID, db).Rows[0][0].ToString();
                    }
                    else
                    {
                        model.password = Permission.CalculateMD5Hash(model.password);
                    }

                    if (Request.Files.Count > 0)
                    {
                        var file = Request.Files["imageUrl"];
                        if (file != null && file.ContentLength > 0)
                        {
                            string extension = Path.GetExtension(file.FileName);
                            string filename  = model.ID + ".png";
                            string filePath  = Path.Combine(HttpContext.Server.MapPath("~/Uploads/UserImages/"),
                                                            Path.GetFileName(filename));
                            file.SaveAs(filePath);
                            model.imageUrl = filename;
                        }
                    }
                    if (model.imageUrl == null)
                    {
                        model.imageUrl = string.IsNullOrEmpty(form["imgActual"]) ? "" : form["imgActual"];
                    }
                    db.Entry(model).State = EntityState.Modified;
                    if (db.SaveChanges() != 0)
                    {
                        if (logout)
                        {
                            WebSecurity.Logout();
                        }
                    }
                }

                validation = model.AfterEdit(db);
                if (validation.BoolValue)
                {
                    return(Json(new { Message = validation.StringValue }));
                }
            }
            else
            {
                if (Request.Files.Count > 0)
                {
                    var file = Request.Files["imageUrl"];
                    if (file != null && file.ContentLength > 0)
                    {
                        string extension = Path.GetExtension(file.FileName);
                        string filename  = model.ID + ".png";
                        string filePath  = Path.Combine(HttpContext.Server.MapPath("~/Uploads/UserImages/"), Path.GetFileName(filename));
                        file.SaveAs(filePath);
                        model.imageUrl = filename;
                    }
                }
                if (form["employeeType"] != null)
                {
                    model.employeeType_Type = int.Parse(form["employeeType"].Split(',')[0]);
                }

                if (form["office"] != null)
                {
                    model.office_office = int.Parse(form["office"].Split(',')[0]);
                }

                validation = model.BeforeCreate(db);
                if (validation.BoolValue)
                {
                    return(Json(new { Message = validation.StringValue }));
                }

                model.password = Permission.CalculateMD5Hash(model.password);
                db.BaseUsers.Add(model);
                db.SaveChanges();

                validation = model.AfterCreate(db);
                if (validation.BoolValue)
                {
                    return(Json(new { Message = validation.StringValue }));
                }
            }
            validation = model.AfterSave(db);
            if (validation.BoolValue)
            {
                return(Json(new { Message = validation.StringValue }));
            }

            if (multiUser)
            {
                Helper.executeNonQUery("DuplicateUser", db);
            }
            db.SaveChanges();

            //if (form["employeeType"] != null)
            //{
            //    foreach (var item in form["employeeType"].Split(','))
            //    {
            //        SMEmployeeType tpg = db.SMEmployeeTypes.Find(int.Parse(item));
            //        if (tpg != null)
            //        {
            //            employeeModel.SMEmployeeTypes.Add(tpg);
            //        }
            //    }
            //}
            db.SaveChanges();
            return(Json(model.ID));
        }