public ToolEditModel GetToolForEdit(long?toolId) { List <BusinessModel> businessTypes = (from b in this.Db.BusinessTypes select new BusinessModel { BusinessTypeId = (int)b.BusinessTypeId, BusinessTypeDescription = b.Description }).ToList(); if (toolId == null) { return new ToolEditModel { BusinessTypes = businessTypes } } ; ToolEditModel tool = (from t in this.Db.Context.Tools where t.ToolId == toolId select new ToolEditModel { ToolId = t.ToolId, Name = t.Name, Description = t.Description, Filename = t.Filename }).FirstOrDefault(); tool.BusinessTypes = businessTypes; return(tool); }
public ActionResult ToolEdit(FormCollection form, ToolEditModel tool) { var description = form["Description"]; var createHyperLink = form["createHyperLink"]; if (string.IsNullOrEmpty(description)) { description = "VRV-WEB-Xpress is a new internet based systematic selection software for Daikin VRV systems (Air-Cooled) and VRV-W systems (Water-Cooled). It allows you to make quick or detailed system selection for quotation with easy operation. Detailed reports and mechanical schedules can be generated upon building complete selection."; } if (tool.Description == null || tool.Description.Length == 0) { tool.Description = (string)description; } if (description.Contains("href") && !description.Contains("https")) { int index = description.IndexOf("href") + 5; string newDescription = description.Insert(index + 1, "https://"); description = newDescription; } if (createHyperLink == "true" && !description.Contains("href")) { tool.Description += "<br /><br />"; tool.Description += "<a href='https://webtools.daikin.eu' target='_blank'> Click here to access</a>"; } this.ServiceResponse = new PermissionServices().PostTool(tool, Request); ViewData["PageMessages"] = this.ServiceResponse.Messages; if (this.ServiceResponse.Messages.HasErrors == true) { return(View("ToolEdit", tool)); } return(RedirectToAction("Tools", "CityCMS")); }
public ServiceResponse PostTool(ToolEditModel tool, HttpRequestBase Request) { this.Db.ReadOnly = false; this.Response.Model = tool; if (string.IsNullOrEmpty(tool.Name)) { this.Response.AddError("Tool Name is required"); return(this.Response); } if (tool.ToolId == null) { //add new tool if (Request == null || Request.Files.Count != 1 || Request.Files[0] == null || Request.Files[0].ContentLength == 0) { this.Response.AddError("Please select a file to upload"); return(this.Response); } var file = Request.Files[0]; if (Path.GetExtension(file.FileName) != ".zip") { this.Response.AddError("Only zip files can be uploaded"); return(this.Response); } string targetFilePath = Utilities.GetDocumentDirectory() + "\\Tools\\" + file.FileName; if (File.Exists(targetFilePath)) { this.Response.AddError("Zip file with name \"" + file.FileName + "\" already exists"); return(this.Response); } try { file.SaveAs(targetFilePath); } catch (Exception) { this.Response.AddError("Unable to upload file, please try again"); return(this.Response); } if (tool.PostedBusinessTypeIds.Length == 0) { this.Response.AddError("Please select at least one business type that can view this tool"); return(this.Response); } var newTool = new Tool { Name = tool.Name, Description = (string.IsNullOrEmpty(tool.Description)) ? "" : tool.Description, Filename = file.FileName }; var nextToolId = 10; var lastExistingTool = this.Db.Tools.OrderByDescending(i => i.ToolId).FirstOrDefault(); if (lastExistingTool != null) { nextToolId = lastExistingTool.ToolId + 10; } newTool.ToolId = nextToolId; this.Db.Context.Tools.Add(newTool); List <Permission> NewPermissions = new List <Permission>(); //loop through business types, add permissions //to that type as well as all of their users foreach (int selectedBusinessType in tool.PostedBusinessTypeIds) { Permission businessTypeLevelPermission = new Permission { PermissionId = this.Context.GenerateNextLongId(), ObjectId = selectedBusinessType, PermissionTypeId = PermissionTypeEnum.Tool, ReferenceId = newTool.ToolId }; NewPermissions.Add(businessTypeLevelPermission); List <Business> BusinessesInType = (from b in this.Db.Businesses where (int)b.BusinessTypeId == selectedBusinessType select b).ToList(); foreach (var business in BusinessesInType) { Permission businessPermission = new Permission { PermissionId = this.Context.GenerateNextLongId(), ParentPermissionId = businessTypeLevelPermission.PermissionId, ObjectId = business.BusinessId, PermissionTypeId = PermissionTypeEnum.Tool, ReferenceId = newTool.ToolId }; NewPermissions.Add(businessPermission); List <User> usersInBusiness = (from u in this.Db.Users where u.BusinessId == business.BusinessId select u).ToList(); foreach (var user in usersInBusiness) { Permission userPermission = new Permission { PermissionId = this.Context.GenerateNextLongId(), ParentPermissionId = businessPermission.PermissionId, ObjectId = user.UserId, PermissionTypeId = PermissionTypeEnum.Tool, ReferenceId = newTool.ToolId }; NewPermissions.Add(userPermission); } } } this.Db.Context.Permissions.AddRange(NewPermissions); this.Db.SaveChanges(); this.Response.AddSuccess("Tool \"" + tool.Name + "\" Added Successfully"); } else { //save existing tool Tool existingTool = this.Db.Tools.Where(m => m.ToolId == tool.ToolId).FirstOrDefault(); if (existingTool == null) { this.Response.AddError("Tool no longer exists"); return(this.Response); } //save new zip file if included in post if (Request != null && Request.Files.Count == 1 && Request.Files[0] != null && Request.Files[0].ContentLength > 0) { //save new zip file var file = Request.Files[0]; if (Path.GetExtension(file.FileName) != ".zip") { this.Response.AddError("Only zip files can be uploaded"); return(this.Response); } string targetFilePath = Utilities.GetDocumentDirectory() + "\\Tools\\" + file.FileName; if (File.Exists(targetFilePath)) { this.Response.AddError("Zip file with name \"" + file.FileName + "\" already exists"); return(this.Response); } try { file.SaveAs(targetFilePath); } catch (Exception) { this.Response.AddError("Unable to upload file, please try again"); return(this.Response); } //delete out old zip file string oldTargetFilePath = Utilities.GetDocumentDirectory() + "\\Tools\\" + existingTool.Filename; if (targetFilePath != oldTargetFilePath) { try { File.Delete(oldTargetFilePath); } catch (Exception) { } } existingTool.Filename = file.FileName; } existingTool.Name = tool.Name; existingTool.Description = (string.IsNullOrEmpty(tool.Description)) ? "" : tool.Description; this.Response.AddSuccess("Tool changes saved"); } this.Db.SaveChanges(); return(this.Response); }