public BaseService(DbContext currentContext = null) { RecMan = new RecordManager(currentContext); EntMan = new EntityManager(currentContext); SecMan = new SecurityManager(currentContext); RelMan = new EntityRelationManager(currentContext); Fs = new DbFileRepository(currentContext); }
public SaveToDatabaseViewModel() { _dbFileRepository = Locator.Current.GetService <DbFileRepository>(); RefreshDbFilesCommand = ReactiveCommand.Create(RefreshDbFiles); SaveFileCommand = ReactiveCommand.CreateFromTask(SafeFileAsync); LoadInfoFromCurrentFile(); }
public OpenFromDatabaseViewModel() { _dbFileRepository = Locator.Current.GetService <DbFileRepository>(); RefreshDbFilesCommand = ReactiveCommand.Create(RefreshDbFiles); OpenFileCommand = ReactiveCommand.CreateFromTask(OpenFileAsync); UpdateSelectedRowCommand = ReactiveCommand.Create <string, Unit>(UpdateSelectedRow); LoadInfoFromCurrentFile(); }
internal void SendEmail(Email email, SmtpService service) { try { if (service == null) { email.ServerError = "SMTP service not found"; email.Status = EmailStatus.Aborted; return; //save email in finally block will save changes } else if (!service.IsEnabled) { email.ServerError = "SMTP service is not enabled"; email.Status = EmailStatus.Aborted; return; //save email in finally block will save changes } var message = new MimeMessage(); if (!string.IsNullOrWhiteSpace(email.Sender?.Name)) { message.From.Add(new MailboxAddress(email.Sender?.Name, email.Sender?.Address)); } else { message.From.Add(new MailboxAddress(email.Sender?.Address)); } foreach (var recipient in email.Recipients) { if (recipient.Address.StartsWith("cc:")) { if (!string.IsNullOrWhiteSpace(recipient.Name)) { message.Cc.Add(new MailboxAddress(recipient.Name, recipient.Address.Substring(3))); } else { message.Cc.Add(new MailboxAddress(recipient.Address.Substring(3))); } } else if (recipient.Address.StartsWith("bcc:")) { if (!string.IsNullOrWhiteSpace(recipient.Name)) { message.Bcc.Add(new MailboxAddress(recipient.Name, recipient.Address.Substring(4))); } else { message.Bcc.Add(new MailboxAddress(recipient.Address.Substring(4))); } } else { if (!string.IsNullOrWhiteSpace(recipient.Name)) { message.To.Add(new MailboxAddress(recipient.Name, recipient.Address)); } else { message.To.Add(new MailboxAddress(recipient.Address)); } } } if (!string.IsNullOrWhiteSpace(email.ReplyToEmail)) { string[] replyToEmails = email.ReplyToEmail.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (var replyEmail in replyToEmails) { message.ReplyTo.Add(new MailboxAddress(replyEmail)); } } else { message.ReplyTo.Add(new MailboxAddress(email.Sender?.Address)); } message.Subject = email.Subject; var bodyBuilder = new BodyBuilder(); bodyBuilder.HtmlBody = email.ContentHtml; bodyBuilder.TextBody = email.ContentText; if (email.Attachments != null && email.Attachments.Count > 0) { foreach (var att in email.Attachments) { var filepath = att; if (!filepath.StartsWith("/")) { filepath = "/" + filepath; } filepath = filepath.ToLowerInvariant(); if (filepath.StartsWith("/fs")) { filepath = filepath.Substring(3); } DbFileRepository fsRepository = new DbFileRepository(); var file = fsRepository.Find(filepath); var bytes = file.GetBytes(); var extension = Path.GetExtension(filepath).ToLowerInvariant(); new FileExtensionContentTypeProvider().Mappings.TryGetValue(extension, out string mimeType); var attachment = new MimePart(mimeType) { Content = new MimeContent(new MemoryStream(bytes)), ContentDisposition = new ContentDisposition(ContentDisposition.Attachment), ContentTransferEncoding = ContentEncoding.Base64, FileName = Path.GetFileName(filepath) }; bodyBuilder.Attachments.Add(attachment); } } ProcessHtmlContent(bodyBuilder); message.Body = bodyBuilder.ToMessageBody(); using (var client = new SmtpClient()) { //accept all SSL certificates (in case the server supports STARTTLS) client.ServerCertificateValidationCallback = (s, c, h, e) => true; client.Connect(service.Server, service.Port, service.ConnectionSecurity); if (!string.IsNullOrWhiteSpace(service.Username)) { client.Authenticate(service.Username, service.Password); } client.Send(message); client.Disconnect(true); } email.SentOn = DateTime.UtcNow; email.Status = EmailStatus.Sent; email.ScheduledOn = null; email.ServerError = null; } catch (Exception ex) { email.SentOn = null; email.ServerError = ex.Message; email.RetriesCount++; if (email.RetriesCount >= service.MaxRetriesCount) { email.ScheduledOn = null; email.Status = EmailStatus.Aborted; } else { email.ScheduledOn = DateTime.UtcNow.AddMinutes(service.RetryWaitMinutes); email.Status = EmailStatus.Pending; } } finally { new SmtpInternalService().SaveEmail(email); } }
public static void ProcessHtmlContent(BodyBuilder builder) { if (builder == null) { return; } if (string.IsNullOrWhiteSpace(builder.HtmlBody)) { return; } try { var htmlDoc = new HtmlDocument(); htmlDoc.Load(new MemoryStream(Encoding.UTF8.GetBytes(builder.HtmlBody))); if (htmlDoc.DocumentNode == null) { return; } foreach (HtmlNode node in htmlDoc.DocumentNode.SelectNodes("//img[@src]")) { var src = node.Attributes["src"].Value.Split('?', StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(); if (!string.IsNullOrWhiteSpace(src) && src.StartsWith("/fs")) { try { Uri uri = new Uri(src); src = uri.AbsolutePath; } catch { } if (src.StartsWith("/fs")) { src = src.Substring(3); } DbFileRepository fsRepository = new DbFileRepository(); var file = fsRepository.Find(src); if (file == null) { continue; } var bytes = file.GetBytes(); var extension = Path.GetExtension(src).ToLowerInvariant(); new FileExtensionContentTypeProvider().Mappings.TryGetValue(extension, out string mimeType); var imagePart = new MimePart(mimeType) { ContentId = MimeUtils.GenerateMessageId(), Content = new MimeContent(new MemoryStream(bytes)), ContentDisposition = new ContentDisposition(ContentDisposition.Attachment), ContentTransferEncoding = ContentEncoding.Base64, FileName = Path.GetFileName(src) }; builder.LinkedResources.Add(imagePart); node.SetAttributeValue("src", $"cid:{imagePart.ContentId}"); } } builder.HtmlBody = htmlDoc.DocumentNode.OuterHtml; if (string.IsNullOrWhiteSpace(builder.TextBody) && !string.IsNullOrWhiteSpace(builder.HtmlBody)) { builder.TextBody = ConvertToPlainText(builder.HtmlBody); } } catch { return; } }
public void QueueEmail(EmailAddress recipient, EmailAddress sender, string replyTo, string subject, string textBody, string htmlBody, EmailPriority priority = EmailPriority.Normal, List <string> attachments = null) { ValidationException ex = new ValidationException(); if (recipient == null) { ex.AddError("recipientEmail", "Recipient is not specified."); } else { var address = recipient.Address; if (address.StartsWith("cc:")) { address = address.Substring(3); } if (address.StartsWith("bcc:")) { address = address.Substring(4); } if (string.IsNullOrEmpty(address)) { ex.AddError("recipientEmail", "Recipient email is not specified."); } else if (!address.IsEmail()) { ex.AddError("recipientEmail", "Recipient email is not valid email address."); } } if (!string.IsNullOrWhiteSpace(replyTo)) { if (!replyTo.IsEmail()) { ex.AddError("recipientEmail", "Reply To email is not valid email address."); } } if (string.IsNullOrEmpty(subject)) { ex.AddError("subject", "Subject is required."); } ex.CheckAndThrow(); Email email = new Email(); email.Id = Guid.NewGuid(); email.Sender = sender ?? new EmailAddress { Address = DefaultSenderEmail, Name = DefaultSenderName }; if (string.IsNullOrWhiteSpace(replyTo)) { email.ReplyToEmail = DefaultReplyToEmail; } else { email.ReplyToEmail = replyTo; } email.Recipients = new List <EmailAddress> { recipient }; email.Subject = subject; email.ContentHtml = htmlBody; email.ContentText = textBody; email.CreatedOn = DateTime.UtcNow; email.SentOn = null; email.Priority = priority; email.Status = EmailStatus.Pending; email.ServerError = string.Empty; email.ScheduledOn = email.CreatedOn; email.RetriesCount = 0; email.ServiceId = Id; email.Attachments = new List <string>(); if (attachments != null && attachments.Count > 0) { DbFileRepository fsRepository = new DbFileRepository(); foreach (var att in attachments) { var filepath = att; if (!filepath.StartsWith("/")) { filepath = "/" + filepath; } filepath = filepath.ToLowerInvariant(); if (filepath.StartsWith("/fs")) { filepath = filepath.Substring(3); } var file = fsRepository.Find(filepath); if (file == null) { throw new Exception($"Attachment file '{filepath}' not found."); } email.Attachments.Add(filepath); } } new SmtpInternalService().SaveEmail(email); }
public void SendEmail(EmailAddress recipient, string subject, string textBody, string htmlBody, List <string> attachments) { ValidationException ex = new ValidationException(); if (recipient == null) { ex.AddError("recipientEmail", "Recipient is not specified."); } else { if (string.IsNullOrEmpty(recipient.Address)) { ex.AddError("recipientEmail", "Recipient email is not specified."); } else if (!recipient.Address.IsEmail()) { ex.AddError("recipientEmail", "Recipient email is not valid email address."); } } if (string.IsNullOrEmpty(subject)) { ex.AddError("subject", "Subject is required."); } ex.CheckAndThrow(); var message = new MimeMessage(); if (!string.IsNullOrWhiteSpace(DefaultSenderName)) { message.From.Add(new MailboxAddress(DefaultSenderName, DefaultSenderEmail)); } else { message.From.Add(new MailboxAddress(DefaultSenderEmail)); } if (!string.IsNullOrWhiteSpace(recipient.Name)) { message.To.Add(new MailboxAddress(recipient.Name, recipient.Address)); } else { message.To.Add(new MailboxAddress(recipient.Address)); } if (!string.IsNullOrWhiteSpace(DefaultReplyToEmail)) { message.ReplyTo.Add(new MailboxAddress(DefaultReplyToEmail)); } message.Subject = subject; var bodyBuilder = new BodyBuilder(); bodyBuilder.HtmlBody = htmlBody; bodyBuilder.TextBody = textBody; if (attachments != null && attachments.Count > 0) { foreach (var att in attachments) { var filepath = att; if (!filepath.StartsWith("/")) { filepath = "/" + filepath; } filepath = filepath.ToLowerInvariant(); if (filepath.StartsWith("/fs")) { filepath = filepath.Substring(3); } DbFileRepository fsRepository = new DbFileRepository(); var file = fsRepository.Find(filepath); var bytes = file.GetBytes(); var extension = Path.GetExtension(filepath).ToLowerInvariant(); new FileExtensionContentTypeProvider().Mappings.TryGetValue(extension, out string mimeType); var attachment = new MimePart(mimeType) { Content = new MimeContent(new MemoryStream(bytes)), ContentDisposition = new ContentDisposition(ContentDisposition.Attachment), ContentTransferEncoding = ContentEncoding.Base64, FileName = Path.GetFileName(filepath) }; bodyBuilder.Attachments.Add(attachment); } } SmtpInternalService.ProcessHtmlContent(bodyBuilder); message.Body = bodyBuilder.ToMessageBody(); using (var client = new SmtpClient()) { //accept all SSL certificates (in case the server supports STARTTLS) client.ServerCertificateValidationCallback = (s, c, h, e) => true; client.Connect(Server, Port, ConnectionSecurity); if (!string.IsNullOrWhiteSpace(Username)) { client.Authenticate(Username, Password); } client.Send(message); client.Disconnect(true); } Email email = new Email(); email.Id = Guid.NewGuid(); email.Sender = new EmailAddress { Address = DefaultSenderEmail, Name = DefaultSenderName }; email.ReplyToEmail = DefaultReplyToEmail; email.Recipients = new List <EmailAddress> { recipient }; email.Subject = subject; email.ContentHtml = htmlBody; email.ContentText = textBody; email.CreatedOn = DateTime.UtcNow; email.SentOn = email.CreatedOn; email.Priority = EmailPriority.Normal; email.Status = EmailStatus.Sent; email.ServerError = string.Empty; email.ScheduledOn = null; email.RetriesCount = 0; email.ServiceId = Id; if (attachments != null && attachments.Count > 0) { DbFileRepository fsRepository = new DbFileRepository(); foreach (var att in attachments) { var filepath = att; if (!filepath.StartsWith("/")) { filepath = "/" + filepath; } filepath = filepath.ToLowerInvariant(); if (filepath.StartsWith("/fs")) { filepath = filepath.Substring(3); } var file = fsRepository.Find(filepath); if (file == null) { throw new Exception($"Attachment file '{filepath}' not found."); } email.Attachments.Add(filepath); } } new SmtpInternalService().SaveEmail(email); }
public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { if (!isVisible) { output.SuppressOutput(); return(Task.CompletedTask); } if (Width == 0) { Width = null; } if (Height == 0) { Height = null; } #region << Init >> var initSuccess = InitField(context, output); if (!initSuccess) { return(Task.CompletedTask); } if (Value != null && !String.IsNullOrWhiteSpace(Value.ToString())) { if (!Value.StartsWith("/fs") && !Value.StartsWith("http")) { Value = "/fs" + Value; } FileName = Path.GetFileName(Value); } if (!((string)Value).StartsWith("http")) { if (!String.IsNullOrWhiteSpace(Value)) { var fileDbPath = Value; if (Value.StartsWith("/fs")) { fileDbPath = Value.Substring(3); } var file = new DbFileRepository().Find(fileDbPath); if (file != null) { var fileContent = file.GetBytes(); using (Image <Rgba32> image = SixLabors.ImageSharp.Image.Load(fileContent)) { OriginalWidth = image.Width; OriginalHeight = image.Height; OriginalRatio = ((decimal)(OriginalWidth ?? 1)) / (OriginalHeight ?? 1); } } } if (Height != null || Width != null) { QueryCommandsList.Add($"action=resize"); QueryCommandsList.Add($"mode={ResizeAction}"); } if (Height == null && Width != null) { Height = Decimal.ToInt32(Math.Round((OriginalRatio / Width ?? 1), 0)); } if (Height != null) { StylesList.Add($"height:{Height}px"); QueryCommandsList.Add($"height={Height}"); } if (Width == null && Height != null) { Width = Decimal.ToInt32(Math.Round((OriginalRatio * Height ?? 1), 0)); } if (Width != null) { StylesList.Add($"width:{Width}px"); QueryCommandsList.Add($"width={Width}"); } } WrapperStyle = String.Join(";", StylesList); ImageQuery = "?" + String.Join("&", QueryCommandsList); #endregion #region << Render >> if (Mode == FieldRenderMode.Form) { if (Access == FieldAccess.Full || Access == FieldAccess.FullAndCreate) { var outsideWrapper = new TagBuilder("div"); var viewWrapper = new TagBuilder("div"); viewWrapper.Attributes.Add("id", $"view-{FieldId}"); viewWrapper.AddCssClass($"image-wrapper erp-image view-wrapper {(String.IsNullOrWhiteSpace(Value)?"d-none":"")} {(ValidationErrors.Count > 0 ? "is-invalid" : "")}"); viewWrapper.Attributes.Add("style", WrapperStyle); var viewImage = new TagBuilder("img"); viewImage.AddCssClass("wrapper-image"); viewImage.Attributes.Add("src", $"{Value}{ImageQuery}"); viewImage.Attributes.Add("title", $"{FileName}"); viewWrapper.InnerHtml.AppendHtml(viewImage); var viewRemoveLink = new TagBuilder("a"); viewRemoveLink.Attributes.Add("href", "javascript:void(0)"); viewRemoveLink.AddCssClass("action remove"); viewRemoveLink.InnerHtml.AppendHtml(TextRemove); viewWrapper.InnerHtml.AppendHtml(viewRemoveLink); outsideWrapper.InnerHtml.AppendHtml(viewWrapper); var editWrapper = new TagBuilder("div"); editWrapper.Attributes.Add("id", $"edit-{FieldId}"); editWrapper.AddCssClass($"image-wrapper erp-image edit-wrapper {(String.IsNullOrWhiteSpace(Value) ? "" : "d-none")} {(ValidationErrors.Count > 0 ? "is-invalid" : "")}"); editWrapper.Attributes.Add("style", WrapperStyle); var editInput = new TagBuilder("input"); editInput.Attributes.Add("type", "file"); editInput.Attributes.Add("value", ""); editInput.Attributes.Add("id", $"file-{FieldId}"); editInput.Attributes.Add("accept", $"{Accept}"); editWrapper.InnerHtml.AppendHtml(editInput); var editWrapperIconDiv = new TagBuilder("div"); editWrapperIconDiv.AddCssClass("wrapper-icon"); var editWrapperIconSpan = new TagBuilder("span"); editWrapperIconSpan.AddCssClass("fa fa-image fa-fw"); editWrapperIconDiv.InnerHtml.AppendHtml(editWrapperIconSpan); editWrapper.InnerHtml.AppendHtml(editWrapperIconDiv); var editWrapperTextDiv = new TagBuilder("div"); editWrapperTextDiv.AddCssClass("wrapper-text"); editWrapperTextDiv.Attributes.Add("id", $"fake-{Name}-{FieldId}"); var editWrapperTextLink = new TagBuilder("button"); editWrapperTextLink.Attributes.Add("type", "button"); editWrapperTextLink.AddCssClass("btn btn-secondary btn-sm"); editWrapperTextLink.InnerHtml.AppendHtml(TextSelect); editWrapperTextDiv.InnerHtml.AppendHtml(editWrapperTextLink); var editWrapperTextSpan = new TagBuilder("span"); editWrapperTextSpan.AddCssClass("d-none"); editWrapperTextDiv.InnerHtml.AppendHtml(editWrapperTextSpan); editWrapper.InnerHtml.AppendHtml(editWrapperTextDiv); outsideWrapper.InnerHtml.AppendHtml(editWrapper); var hiddenInput = new TagBuilder("input"); hiddenInput.Attributes.Add("type", "hidden"); hiddenInput.Attributes.Add("id", $"input-{FieldId}"); hiddenInput.Attributes.Add("name", $"{Name}"); hiddenInput.Attributes.Add("value", (Value ?? "").ToString()); hiddenInput.Attributes.Add("data-original-value", (Value ?? "").ToString()); hiddenInput.Attributes.Add("data-original-filename", Name); outsideWrapper.InnerHtml.AppendHtml(hiddenInput); output.Content.AppendHtml(outsideWrapper); var jsCompressor = new JavaScriptCompressor(); #region << Init Scripts >> var tagHelperInitialized = false; if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldImage) + "-form")) { var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldImage) + "-form"]; tagHelperInitialized = tagHelperContext.Initialized; } if (!tagHelperInitialized) { var scriptContent = FileService.GetEmbeddedTextResource("form.js", "WebVella.Erp.Web.TagHelpers.WvFieldImage"); var scriptEl = new TagBuilder("script"); scriptEl.Attributes.Add("type", "text/javascript"); scriptEl.InnerHtml.AppendHtml(jsCompressor.Compress(scriptContent)); output.PostContent.AppendHtml(scriptEl); ViewContext.HttpContext.Items[typeof(WvFieldImage) + "-form"] = new WvTagHelperContext() { Initialized = true }; } #endregion #region << Add Inline Init Script for this instance >> var initScript = new TagBuilder("script"); initScript.Attributes.Add("type", "text/javascript"); var scriptTemplate = @" $(function(){ ImageFormInit(""{{FieldId}}"",""{{Name}}"",{{ConfigJson}}); });" ; scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString()); scriptTemplate = scriptTemplate.Replace("{{Name}}", Name); var fieldConfig = new WvFieldImageConfig() { ApiUrl = ApiUrl, CanAddValues = Access == FieldAccess.FullAndCreate ? true : false, Accept = Accept, Width = Width, Height = Height, ResizeAction = ResizeAction }; scriptTemplate = scriptTemplate.Replace("{{ConfigJson}}", JsonConvert.SerializeObject(fieldConfig)); initScript.InnerHtml.AppendHtml(jsCompressor.Compress(scriptTemplate)); output.PostContent.AppendHtml(initScript); #endregion } else if (Access == FieldAccess.ReadOnly) { var outerWrapper = new TagBuilder("div"); var viewWrapper = new TagBuilder("div"); viewWrapper.Attributes.Add("id", $"view-{FieldId}"); viewWrapper.AddCssClass($"image-wrapper erp-image view-wrapper {(String.IsNullOrWhiteSpace(Value)?"d-none":"")} {(ValidationErrors.Count > 0 ? "is-invalid" : "")}"); viewWrapper.Attributes.Add("style", $"{WrapperStyle}"); var viewImage = new TagBuilder("img"); viewImage.AddCssClass("wrapper-image"); viewImage.Attributes.Add("src", $"{Value}{ImageQuery}"); viewImage.Attributes.Add("title", $"{FileName}"); viewWrapper.InnerHtml.AppendHtml(viewImage); outerWrapper.InnerHtml.AppendHtml(viewWrapper); //Hidden input with the value var hiddenInput = new TagBuilder("input"); hiddenInput.Attributes.Add("type", "hidden"); hiddenInput.Attributes.Add("id", $"input-{FieldId}"); hiddenInput.Attributes.Add("name", Name); hiddenInput.Attributes.Add("value", (Value ?? "").ToString()); output.PostContent.AppendHtml(hiddenInput); output.PostContent.AppendHtml(outerWrapper); } } else if (Mode == FieldRenderMode.Display) { if (!String.IsNullOrWhiteSpace(Value)) { var outerWrapper = new TagBuilder("div"); var viewWrapper = new TagBuilder("div"); viewWrapper.Attributes.Add("id", $"view-{FieldId}"); viewWrapper.AddCssClass($"image-wrapper erp-image view-wrapper {(String.IsNullOrWhiteSpace(Value) ? "d-none" : "")} {(ValidationErrors.Count > 0 ? "is-invalid" : "")}"); viewWrapper.Attributes.Add("style", $"{WrapperStyle}"); var viewImage = new TagBuilder("img"); viewImage.AddCssClass("wrapper-image"); viewImage.Attributes.Add("src", $"{Value}{ImageQuery}"); viewImage.Attributes.Add("title", $"{FileName}"); viewWrapper.InnerHtml.AppendHtml(viewImage); outerWrapper.InnerHtml.AppendHtml(viewWrapper); output.PostContent.AppendHtml(outerWrapper); } else { output.Content.AppendHtml(EmptyValEl); } } else if (Mode == FieldRenderMode.Simple) { output.SuppressOutput(); var viewImage = new TagBuilder("img"); viewImage.AddCssClass("img-fluid"); viewImage.Attributes.Add("src", $"{Value}{ImageQuery}"); viewImage.Attributes.Add("title", $"{FileName}"); output.Content.AppendHtml(viewImage); return(Task.CompletedTask); } else if (Mode == FieldRenderMode.InlineEdit) { if (Access == FieldAccess.Full || Access == FieldAccess.FullAndCreate) { var outsideWrapper = new TagBuilder("div"); var viewWrapper = new TagBuilder("div"); viewWrapper.Attributes.Add("id", $"view-{FieldId}"); viewWrapper.AddCssClass($"image-wrapper erp-image view-wrapper {(String.IsNullOrWhiteSpace(Value) ? "d-none" : "")} {(ValidationErrors.Count > 0 ? "is-invalid" : "")}"); viewWrapper.Attributes.Add("style", WrapperStyle); var viewImage = new TagBuilder("img"); viewImage.AddCssClass("wrapper-image"); viewImage.Attributes.Add("src", $"{Value}{ImageQuery}"); viewImage.Attributes.Add("title", $"{FileName}"); viewWrapper.InnerHtml.AppendHtml(viewImage); var viewRemoveLink = new TagBuilder("a"); viewRemoveLink.Attributes.Add("href", "javascript:void(0)"); viewRemoveLink.AddCssClass("action remove"); viewRemoveLink.InnerHtml.AppendHtml(TextRemove); viewWrapper.InnerHtml.AppendHtml(viewRemoveLink); outsideWrapper.InnerHtml.AppendHtml(viewWrapper); var editWrapper = new TagBuilder("div"); editWrapper.Attributes.Add("id", $"edit-{FieldId}"); editWrapper.AddCssClass($"image-wrapper erp-image edit-wrapper {(String.IsNullOrWhiteSpace(Value) ? "" : "d-none")} {(ValidationErrors.Count > 0 ? "is-invalid" : "")}"); editWrapper.Attributes.Add("style", WrapperStyle); var editInput = new TagBuilder("input"); editInput.Attributes.Add("type", "file"); editInput.Attributes.Add("value", ""); editInput.Attributes.Add("id", $"file-{FieldId}"); editInput.Attributes.Add("accept", $"{Accept}"); editWrapper.InnerHtml.AppendHtml(editInput); var editWrapperIconDiv = new TagBuilder("div"); editWrapperIconDiv.AddCssClass("wrapper-icon"); var editWrapperIconSpan = new TagBuilder("span"); editWrapperIconSpan.AddCssClass("fa fa-image fa-fw"); editWrapperIconDiv.InnerHtml.AppendHtml(editWrapperIconSpan); editWrapper.InnerHtml.AppendHtml(editWrapperIconDiv); var editWrapperTextDiv = new TagBuilder("div"); editWrapperTextDiv.AddCssClass("wrapper-text"); editWrapperTextDiv.Attributes.Add("id", $"fake-{Name}-{FieldId}"); var editWrapperTextLink = new TagBuilder("button"); editWrapperTextLink.Attributes.Add("type", "button"); editWrapperTextLink.AddCssClass("btn btn-secondary btn-sm"); editWrapperTextLink.InnerHtml.AppendHtml(TextSelect); editWrapperTextDiv.InnerHtml.AppendHtml(editWrapperTextLink); var editWrapperTextSpan = new TagBuilder("span"); editWrapperTextSpan.AddCssClass("d-none"); editWrapperTextDiv.InnerHtml.AppendHtml(editWrapperTextSpan); editWrapper.InnerHtml.AppendHtml(editWrapperTextDiv); outsideWrapper.InnerHtml.AppendHtml(editWrapper); var hiddenInput = new TagBuilder("input"); hiddenInput.Attributes.Add("type", "hidden"); hiddenInput.Attributes.Add("id", $"input-{FieldId}"); hiddenInput.Attributes.Add("name", $"{Name}"); hiddenInput.Attributes.Add("value", (Value ?? "").ToString()); hiddenInput.Attributes.Add("data-original-value", (Value ?? "").ToString()); hiddenInput.Attributes.Add("data-original-filename", Name); outsideWrapper.InnerHtml.AppendHtml(hiddenInput); output.Content.AppendHtml(outsideWrapper); var jsCompressor = new JavaScriptCompressor(); #region << Init Scripts >> var tagHelperInitialized = false; if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldImage) + "-inline-edit")) { var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldImage) + "-inline-edit"]; tagHelperInitialized = tagHelperContext.Initialized; } if (!tagHelperInitialized) { var scriptContent = FileService.GetEmbeddedTextResource("inline-edit.js", "WebVella.Erp.Web.TagHelpers.WvFieldImage"); var scriptEl = new TagBuilder("script"); scriptEl.Attributes.Add("type", "text/javascript"); scriptEl.InnerHtml.AppendHtml(jsCompressor.Compress(scriptContent)); output.PostContent.AppendHtml(scriptEl); ViewContext.HttpContext.Items[typeof(WvFieldImage) + "-inline-edit"] = new WvTagHelperContext() { Initialized = true }; } #endregion #region << Add Inline Init Script for this instance >> var initScript = new TagBuilder("script"); initScript.Attributes.Add("type", "text/javascript"); var scriptTemplate = @" $(function(){ ImageInlineEditInit(""{{FieldId}}"",""{{Name}}"",""{{EntityName}}"",""{{RecordId}}"",{{ConfigJson}}); });" ; scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString()); scriptTemplate = scriptTemplate.Replace("{{Name}}", Name); scriptTemplate = scriptTemplate.Replace("{{EntityName}}", EntityName); scriptTemplate = scriptTemplate.Replace("{{RecordId}}", (RecordId ?? null).ToString()); var fieldConfig = new WvFieldImageConfig() { ApiUrl = ApiUrl, CanAddValues = Access == FieldAccess.FullAndCreate ? true : false, Accept = Accept, Width = Width, Height = Height, ResizeAction = ResizeAction }; scriptTemplate = scriptTemplate.Replace("{{ConfigJson}}", JsonConvert.SerializeObject(fieldConfig)); initScript.InnerHtml.AppendHtml(jsCompressor.Compress(scriptTemplate)); output.PostContent.AppendHtml(initScript); #endregion } else if (Access == FieldAccess.ReadOnly) { var outerWrapper = new TagBuilder("div"); var viewWrapper = new TagBuilder("div"); viewWrapper.Attributes.Add("id", $"view-{FieldId}"); viewWrapper.AddCssClass($"image-wrapper erp-image view-wrapper {(String.IsNullOrWhiteSpace(Value) ? "d-none" : "")} {(ValidationErrors.Count > 0 ? "is-invalid" : "")}"); viewWrapper.Attributes.Add("style", $"{WrapperStyle}"); var viewImage = new TagBuilder("img"); viewImage.AddCssClass("wrapper-image"); viewImage.Attributes.Add("src", $"{Value}{ImageQuery}"); viewImage.Attributes.Add("title", $"{FileName}"); viewWrapper.InnerHtml.AppendHtml(viewImage); outerWrapper.InnerHtml.AppendHtml(viewWrapper); output.PostContent.AppendHtml(outerWrapper); } } #endregion //Finally if (SubInputEl != null) { output.PostContent.AppendHtml(SubInputEl); } return(Task.CompletedTask); }