Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using PX.Data;
- using PX.Data.BQL;
- using PX.Data.BQL.Fluent;
- using System.Xml.Linq;
- using System.Drawing;
- using System.IO;
- using System.Collections.Specialized;
- using PX.Common;
- using System.Text.RegularExpressions;
- namespace DefaultReports
- {
- public class CSReportColorsMaint : PXGraph<CSReportColorsMaint, CSReportColors>
- {
- public SelectFrom<CSReportColors>.View ReportColors;
- #region Event Handlers
- protected virtual void _(Events.FieldVerifying<CSReportColors, CSReportColors.pcrsushexcolor1> e)
- {
- if (e.Row == null || e.NewValue == null) return;
- CheckHexColor((string) e.NewValue);
- }
- protected virtual void _(Events.FieldVerifying<CSReportColors, CSReportColors.pcrsushexcolor2> e)
- {
- if (e.Row == null || e.NewValue == null) return;
- CheckHexColor((string)e.NewValue);
- }
- protected virtual void _(Events.FieldVerifying<CSReportColors, CSReportColors.pcrsushexcolor3> e)
- {
- if (e.Row == null || e.NewValue == null) return;
- CheckHexColor((string)e.NewValue);
- }
- protected virtual void _(Events.FieldVerifying<CSReportColors, CSReportColors.pcrsushexcolor4> e)
- {
- if (e.Row == null || e.NewValue == null) return;
- CheckHexColor((string)e.NewValue);
- }
- protected virtual void _(Events.FieldVerifying<CSReportColors, CSReportColors.pcrsushexcolor5> e)
- {
- if (e.Row == null || e.NewValue == null) return;
- CheckHexColor((string)e.NewValue);
- }
- protected virtual void _(Events.FieldVerifying<CSReportColors, CSReportColors.pcrsushexcolor6> e)
- {
- if (e.Row == null || e.NewValue == null) return;
- CheckHexColor((string)e.NewValue);
- }
- protected virtual void _(Events.FieldVerifying<CSReportColors, CSReportColors.pcrsushexcolor7> e)
- {
- if (e.Row == null || e.NewValue == null) return;
- CheckHexColor((string)e.NewValue);
- }
- protected virtual void _(Events.FieldVerifying<CSReportColors, CSReportColors.pcrsushexcolor8> e)
- {
- if (e.Row == null || e.NewValue == null) return;
- CheckHexColor((string)e.NewValue);
- }
- protected virtual void _(Events.FieldVerifying<CSReportColors, CSReportColors.pcrsushexcolor9> e)
- {
- if (e.Row == null || e.NewValue == null) return;
- CheckHexColor((string)e.NewValue);
- }
- protected virtual void _(Events.FieldVerifying<CSReportColors, CSReportColors.pcrsushexcolor10> e)
- {
- if (e.Row == null || e.NewValue == null) return;
- CheckHexColor((string)e.NewValue);
- }
- protected void _(Events.RowPersisted<CSReportColors> x)
- {
- CSReportColors row = x.Row;
- string FileName = Path.Combine(PX.Data.Update.PXInstanceHelper.RootFolder, "ReportsDefault\\pcrsusstylestemplate.rpx");
- bool result = File.Exists(FileName);
- if (result == true) {
- string[] hexcolorfield = { row.Pcrsushexcolor1, row.Pcrsushexcolor2, row.Pcrsushexcolor3, row.Pcrsushexcolor4, row.Pcrsushexcolor5, row.Pcrsushexcolor6, row.Pcrsushexcolor7, row.Pcrsushexcolor8, row.Pcrsushexcolor9, row.Pcrsushexcolor10 };
- for (int i = 0; i < 10; i++) {
- WriteColor(hexcolorfield[i], i+1, FileName);
- }
- }
- else { }
- }
- #endregion
- private static void CheckHexColor(string fieldvalue)
- {
- var regexColorCode = new Regex("^#[a-fA-F0-9]{6}$");
- if (!regexColorCode.IsMatch(fieldvalue.Trim()))
- {
- throw new PXSetPropertyException(Messages.IllegalHexColor);
- }
- }
- private static void WriteColor(string color, int i, string FileName)
- {
- string Name = "Company" + i + "-DocColor-N-R";
- string Name2 = "Company" + i + "-DocLineColor";
- if (color == null)
- {
- XDocument doc = XDocument.Load(FileName);
- foreach (XElement elm in doc.Element("Report").Element("StyleSheet").Elements("StyleRule"))
- {
- if (elm.Element("Name").Value == Name)
- {
- elm.Element("Style").Element("Color").Value = "72,61,139";
- }
- else if (elm.Element("Name").Value == Name2)
- {
- elm.Element("Style").Element("BorderColor").Element("Bottom").Value = "72,61,139";
- }
- }
- doc.Save(FileName);
- }
- else
- {
- var regexColorCode = new Regex("^#[a-fA-F0-9]{6}$");
- if (!regexColorCode.IsMatch(color.Trim())) { }
- else
- {
- XDocument doc = XDocument.Load(FileName);
- Color colour = ColorTranslator.FromHtml(color);
- var rgb = $"{colour.R}, {colour.G}, {colour.B}";
- foreach (XElement elm in doc.Element("Report").Element("StyleSheet").Elements("StyleRule"))
- {
- if (elm.Element("Name").Value == Name)
- {
- elm.Element("Style").Element("Color").Value = rgb;
- }
- else if (elm.Element("Name").Value == Name2)
- {
- elm.Element("Style").Element("BorderColor").Element("Bottom").Value = rgb;
- }
- }
- doc.Save(FileName);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement