using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; using System.IO; using System.Text; using Sleis.Models; namespace Sleis.Utility { public static class ViewFieldUtility { public static string ViewForField(this HtmlHelper htmlHelper, Dictionary fields, string fieldId) { return ViewForField(htmlHelper, fields, fieldId, SessionUtility.CurrentUser.IsAgencyUser()); } public static string ViewForField(this HtmlHelper htmlHelper, Dictionary fields, string fieldId, bool renderAsAgency) { StringBuilder html = new StringBuilder(); html.Append(" data-field-field-key=" + fieldId); if (fields != null && fields.ContainsKey(fieldId)) { PageFieldModel field = fields[fieldId]; if ((renderAsAgency && field.AgencyStyle == CustomFieldDataStyleType.Hidden) || (!renderAsAgency && field.PublicStyle == CustomFieldDataStyleType.Hidden)) { html.Append(" class='hide' "); } } return html.ToString(); } public static string TextForField(this HtmlHelper htmlHelper, Dictionary fields, string fieldId) { return Parse(fields, fieldId, SessionUtility.CurrentUser.IsAgencyUser(), htmlHelper); } public static string TextForField(this HtmlHelper htmlHelper, Dictionary fields, string fieldId, bool renderAsAgency) { return Parse(fields, fieldId, renderAsAgency, htmlHelper); } private static string Parse(Dictionary fields, string fieldId, bool renderAsAgency, HtmlHelper htmlHelper) { StringBuilder html = new StringBuilder(); html.AppendFormat(" id='{0}' ", fieldId); if (fields != null && fields.ContainsKey(fieldId)) { PageFieldModel field = fields[fieldId]; html.AppendFormat(" data-field-page-id='{0}' ", field.PageId); html.AppendFormat(" data-field-field-key='{0}' ", field.Key); html.AppendFormat(" data-field-field-title='{0}' ", HttpUtility.HtmlEncode(field.Title)); html.AppendFormat(" data-field-field-help='{0}' ", HttpUtility.HtmlEncode(field.HelpText)); if (renderAsAgency) { html.AppendFormat(" data-field-field-style='{0}' ", field.AgencyStyle); } else { html.AppendFormat(" data-field-field-style='{0}' ", field.PublicStyle); } html.Append(" class='dynamicField"); if ( (renderAsAgency && field.AgencyStyle == CustomFieldDataStyleType.Hidden) || (!renderAsAgency && field.PublicStyle == CustomFieldDataStyleType.Hidden) ) { html.Append(" hide "); } html.Append("' "); } else { html.Append(" class='dynamicField' "); } //TODO: This is a a bit of a hack to close the tag, and add the img help icon. html.Append(">