Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.86 KB | None | 0 0
  1. using System;
  2. using PX.Data;
  3. using PX.Data.BQL;
  4. using PX.Data.BQL.Fluent;
  5. using System.Xml.Linq;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Collections.Specialized;
  9. using PX.Common;
  10. using System.Text.RegularExpressions;
  11.  
  12. namespace DefaultReports
  13. {
  14.     public class CSReportColorsMaint : PXGraph<CSReportColorsMaint, CSReportColors>
  15.     {
  16.  
  17.         public SelectFrom<CSReportColors>.View ReportColors;
  18.  
  19.         #region Event Handlers
  20.         protected virtual void _(Events.FieldVerifying<CSReportColors, CSReportColors.pcrsushexcolor1> e)
  21.         {
  22.             if (e.Row == null || e.NewValue == null) return;
  23.             CheckHexColor((string) e.NewValue);
  24.         }
  25.         protected virtual void _(Events.FieldVerifying<CSReportColors, CSReportColors.pcrsushexcolor2> e)
  26.         {
  27.             if (e.Row == null || e.NewValue == null) return;
  28.             CheckHexColor((string)e.NewValue);
  29.         }
  30.         protected virtual void _(Events.FieldVerifying<CSReportColors, CSReportColors.pcrsushexcolor3> e)
  31.         {
  32.             if (e.Row == null || e.NewValue == null) return;
  33.             CheckHexColor((string)e.NewValue);
  34.         }
  35.         protected virtual void _(Events.FieldVerifying<CSReportColors, CSReportColors.pcrsushexcolor4> e)
  36.         {
  37.             if (e.Row == null || e.NewValue == null) return;
  38.             CheckHexColor((string)e.NewValue);
  39.         }
  40.         protected virtual void _(Events.FieldVerifying<CSReportColors, CSReportColors.pcrsushexcolor5> e)
  41.         {
  42.             if (e.Row == null || e.NewValue == null) return;
  43.             CheckHexColor((string)e.NewValue);
  44.         }
  45.         protected virtual void _(Events.FieldVerifying<CSReportColors, CSReportColors.pcrsushexcolor6> e)
  46.         {
  47.             if (e.Row == null || e.NewValue == null) return;
  48.             CheckHexColor((string)e.NewValue);
  49.         }
  50.         protected virtual void _(Events.FieldVerifying<CSReportColors, CSReportColors.pcrsushexcolor7> e)
  51.         {
  52.             if (e.Row == null || e.NewValue == null) return;
  53.             CheckHexColor((string)e.NewValue);
  54.         }
  55.         protected virtual void _(Events.FieldVerifying<CSReportColors, CSReportColors.pcrsushexcolor8> e)
  56.         {
  57.             if (e.Row == null || e.NewValue == null) return;
  58.             CheckHexColor((string)e.NewValue);
  59.         }
  60.         protected virtual void _(Events.FieldVerifying<CSReportColors, CSReportColors.pcrsushexcolor9> e)
  61.         {
  62.             if (e.Row == null || e.NewValue == null) return;
  63.             CheckHexColor((string)e.NewValue);
  64.         }
  65.         protected virtual void _(Events.FieldVerifying<CSReportColors, CSReportColors.pcrsushexcolor10> e)
  66.         {
  67.             if (e.Row == null || e.NewValue == null) return;
  68.             CheckHexColor((string)e.NewValue);
  69.         }
  70.  
  71.         protected void _(Events.RowPersisted<CSReportColors> x)
  72.         {
  73.             CSReportColors row = x.Row;
  74.             string FileName = Path.Combine(PX.Data.Update.PXInstanceHelper.RootFolder, "ReportsDefault\\pcrsusstylestemplate.rpx");
  75.  
  76.             bool result = File.Exists(FileName);
  77.             if (result == true) {
  78.                 string[] hexcolorfield = { row.Pcrsushexcolor1, row.Pcrsushexcolor2, row.Pcrsushexcolor3, row.Pcrsushexcolor4, row.Pcrsushexcolor5, row.Pcrsushexcolor6, row.Pcrsushexcolor7, row.Pcrsushexcolor8, row.Pcrsushexcolor9, row.Pcrsushexcolor10 };
  79.                 for (int i = 0; i < 10; i++) {
  80.                     WriteColor(hexcolorfield[i], i+1, FileName);
  81.                 }
  82.             }
  83.             else { }
  84.         }
  85.  
  86.         #endregion
  87.         private static void CheckHexColor(string fieldvalue)
  88.         {
  89.             var regexColorCode = new Regex("^#[a-fA-F0-9]{6}$");
  90.             if (!regexColorCode.IsMatch(fieldvalue.Trim()))
  91.             {
  92.                 throw new PXSetPropertyException(Messages.IllegalHexColor);
  93.             }
  94.         }
  95.  
  96.     private static void WriteColor(string color, int i, string FileName)
  97.         {
  98.             string Name = "Company" + i + "-DocColor-N-R";
  99.             string Name2 = "Company" + i + "-DocLineColor";
  100.             if (color == null)
  101.             {
  102.                 XDocument doc = XDocument.Load(FileName);
  103.                 foreach (XElement elm in doc.Element("Report").Element("StyleSheet").Elements("StyleRule"))
  104.                 {
  105.                     if (elm.Element("Name").Value == Name)
  106.                     {
  107.                         elm.Element("Style").Element("Color").Value = "72,61,139";
  108.                     }
  109.                     else if (elm.Element("Name").Value == Name2)
  110.                     {
  111.                         elm.Element("Style").Element("BorderColor").Element("Bottom").Value = "72,61,139";
  112.                     }
  113.                 }
  114.                 doc.Save(FileName);
  115.             }
  116.             else
  117.             {
  118.                 var regexColorCode = new Regex("^#[a-fA-F0-9]{6}$");
  119.                 if (!regexColorCode.IsMatch(color.Trim())) { }
  120.                 else
  121.                 {
  122.                     XDocument doc = XDocument.Load(FileName);
  123.                     Color colour = ColorTranslator.FromHtml(color);
  124.                     var rgb = $"{colour.R}, {colour.G}, {colour.B}";
  125.                     foreach (XElement elm in doc.Element("Report").Element("StyleSheet").Elements("StyleRule"))
  126.                     {
  127.                         if (elm.Element("Name").Value == Name)
  128.                         {
  129.                             elm.Element("Style").Element("Color").Value = rgb;
  130.                         }
  131.                         else if (elm.Element("Name").Value == Name2)
  132.                         {
  133.                             elm.Element("Style").Element("BorderColor").Element("Bottom").Value = rgb;
  134.                         }
  135.                     }
  136.                     doc.Save(FileName);
  137.                 }
  138.             }
  139.  
  140.  
  141.          
  142.         }
  143.     }
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement