Is there any easy way to convert the body text from an activity, task or other HTML field in Acumatica into a readable format that shows well in a report or generic inquiry? I could create a SQL function to strip out the HTML, a SQL View containing the stripped code, and then add that object to Acumatica but I'm looking for a simpler, more native way.
1
votes
1 Answers
2
votes
You can create a custom attribute that subscribes to the FieldSelecting event to dynamically convert the field to plain text. There are many ways to do the actual conversion, but I would recommend using HtmlAgilityPack, an open-source library that already ships with Acumatica to do the work.
Here's how I would define the attribute:
using System;
using System.IO;
using HtmlAgilityPack;
namespace PX.Data
{
[PXString(IsUnicode = true)]
public class HtmlToTextAttribute : PXAggregateAttribute, IPXFieldSelectingSubscriber
{
protected Type _htmlField;
public HtmlToTextAttribute(Type htmlField)
{
if (htmlField == null) {
throw new PXArgumentException(nameof(htmlField), ErrorMessages.ArgumentNullException);
}
_htmlField = htmlField;
}
public virtual void FieldSelecting(PXCache sender, PXFieldSelectingEventArgs e)
{
var html = sender.GetValue(e.Row, _htmlField.Name) as string;
if(!String.IsNullOrWhiteSpace(html))
{
e.ReturnValue = ConvertToPlainText(html);
}
}
//Source: https://github.com/ceee/ReadSharp/blob/master/ReadSharp/HtmlUtilities.cs
public static string ConvertToPlainText(string html)
{
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
StringWriter sw = new StringWriter();
ConvertTo(doc.DocumentNode, sw);
sw.Flush();
return sw.ToString();
}
private static void ConvertContentTo(HtmlNode node, TextWriter outText)
{
foreach (HtmlNode subnode in node.ChildNodes)
{
ConvertTo(subnode, outText);
}
}
private static void ConvertTo(HtmlNode node, TextWriter outText)
{
string html;
switch (node.NodeType)
{
case HtmlNodeType.Comment:
// don't output comments
break;
case HtmlNodeType.Document:
ConvertContentTo(node, outText);
break;
case HtmlNodeType.Text:
// script and style must not be output
string parentName = node.ParentNode.Name;
if ((parentName == "script") || (parentName == "style"))
break;
// get text
html = ((HtmlTextNode)node).Text;
// is it in fact a special closing node output as text?
if (HtmlNode.IsOverlappedClosingElement(html))
break;
// check the text is meaningful and not a bunch of whitespaces
if (html.Trim().Length > 0)
{
outText.Write(HtmlEntity.DeEntitize(html));
}
break;
case HtmlNodeType.Element:
switch (node.Name)
{
case "p":
// treat paragraphs as crlf
outText.Write("\r\n");
break;
case "br":
outText.Write("\r\n");
break;
}
if (node.HasChildNodes)
{
ConvertContentTo(node, outText);
}
break;
}
}
}
}
To use this attribute, just define a new (non-persisted) field and decorate it with the HtmlToText attribute, for example as an extension on the CRActivity table:
#region UsrBodyText
[HtmlToText(typeof(CRActivity.body))]
[PXUIField(DisplayName="Body (text)")]
public virtual string UsrBodyText { get; set; }
public abstract class usrBodyText : PX.Data.BQL.BqlString.Field<usrBodyText> { }
#endregion