Files
2026-10-05 17:26:41 -04:00

276 lines
11 KiB
C#

using PX.Common;
using PX.Data;
using PX.Data.BQL;
using PX.Data.BQL.Fluent;
using PX.Data.Maintenance.GI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.Compilation;
namespace GISidePanels
{
// Acuminator disable once PX1016 ExtensionDoesNotDeclareIsActiveMethod This universal extension resolves applicability from the configured target screen at runtime.
public class GISidePanelUniversalExtension : PXGraphExtension<PXGraph>
{
public override void Initialize()
{
base.Initialize();
GISidePanelRuntime.RegisterConfiguredSidePanels(Base);
}
}
public static class GISidePanelRuntime
{
private sealed class ConfiguredTarget
{
public Guid DesignID { get; set; }
public string TargetScreenID { get; set; }
public string Title { get; set; }
public string Icon { get; set; }
public int SortOrder { get; set; }
}
private sealed class NavigateActionHandler
{
private readonly PXGraph _graph;
private readonly Guid _designID;
private readonly string _targetScreenID;
internal NavigateActionHandler(PXGraph graph, Guid designID, string targetScreenID)
{
_graph = graph;
_designID = designID;
_targetScreenID = targetScreenID;
}
internal IEnumerable Invoke(PXAdapter adapter)
{
NavigateToGenericInquiry(_graph, _designID, _targetScreenID);
return adapter.Get();
}
}
private sealed class ConfigurationSlot : IPrefetchable
{
private const string SlotName = nameof(GISidePanelRuntime) + "." + nameof(ConfigurationSlot);
private readonly Dictionary<string, List<ConfiguredTarget>> _targetsByScreen =
new Dictionary<string, List<ConfiguredTarget>>(StringComparer.OrdinalIgnoreCase);
void IPrefetchable.Prefetch()
{
_targetsByScreen.Clear();
foreach (PXDataRecord record in PXDatabase.SelectMulti<GISidePanelTarget>(
new PXDataField<GISidePanelTarget.designID>(),
new PXDataField<GISidePanelTarget.targetScreenID>(),
new PXDataField<GISidePanelTarget.title>(),
new PXDataField<GISidePanelTarget.icon>(),
new PXDataField<GISidePanelTarget.sortOrder>(),
new PXDataFieldValue<GISidePanelTarget.isActive>(PXDbType.Bit, true)))
{
Guid? designID = record.GetGuid(0);
string screenID = NormalizeScreenID(record.GetString(1));
if (designID == null || string.IsNullOrWhiteSpace(screenID))
{
continue;
}
if (!_targetsByScreen.TryGetValue(screenID, out List<ConfiguredTarget> targets))
{
targets = new List<ConfiguredTarget>();
_targetsByScreen.Add(screenID, targets);
}
targets.Add(new ConfiguredTarget
{
DesignID = designID.Value,
TargetScreenID = screenID,
Title = record.GetString(2),
Icon = record.GetString(3),
SortOrder = record.GetInt32(4) ?? 0
});
}
foreach (List<ConfiguredTarget> targets in _targetsByScreen.Values)
{
targets.Sort((left, right) =>
{
int orderComparison = left.SortOrder.CompareTo(right.SortOrder);
return orderComparison != 0
? orderComparison
: left.DesignID.CompareTo(right.DesignID);
});
}
}
internal static IReadOnlyList<ConfiguredTarget> GetTargets(string screenID)
{
ConfigurationSlot slot = PXDatabase.GetSlot<ConfigurationSlot>(
SlotName,
typeof(GISidePanelTarget));
if (slot._targetsByScreen.TryGetValue(screenID, out List<ConfiguredTarget> targets))
{
return targets;
}
return Array.Empty<ConfiguredTarget>();
}
}
internal static bool IsMappableSourceField(PXCache cache, string fieldName)
{
return cache != null
&& !string.IsNullOrWhiteSpace(fieldName)
&& cache.GetBqlField(fieldName) != null;
}
public static Type ResolveTargetGraphType(PXGraph graph, string screenID)
{
if (graph == null || string.IsNullOrWhiteSpace(screenID))
{
return null;
}
PX.SM.SiteMap siteMapNode = PX.SM.SiteMap.UK.Find(graph, screenID);
if (siteMapNode == null || string.IsNullOrWhiteSpace(siteMapNode.Graphtype))
{
return null;
}
Type graphType = PXBuildManager.GetType(siteMapNode.Graphtype, false);
if (graphType == null || graphType.IsAbstract || graphType.ContainsGenericParameters || !typeof(PXGraph).IsAssignableFrom(graphType))
{
return null;
}
return graphType;
}
internal static string[] GetTargetViewNames(Type graphType)
{
if (graphType == null)
{
return Array.Empty<string>();
}
return GraphHelper.GetGraphViews(graphType, false)
.Where(view => view.Cache?.CacheType != null && !string.IsNullOrWhiteSpace(view.Name))
.Select(view => view.Name)
.Distinct(StringComparer.OrdinalIgnoreCase)
.OrderBy(viewName => viewName, StringComparer.OrdinalIgnoreCase)
.ToArray();
}
internal static Type ResolveTargetViewCacheType(Type graphType, string viewName)
{
if (graphType == null || string.IsNullOrWhiteSpace(viewName))
{
return null;
}
PXViewInfo viewInfo = GraphHelper.GetGraphViews(graphType, false)
.FirstOrDefault(view => string.Equals(view.Name, viewName, StringComparison.OrdinalIgnoreCase));
return viewInfo?.Cache?.CacheType;
}
internal static void RegisterConfiguredSidePanels(PXGraph graph)
{
string screenID = NormalizeScreenID(graph.Accessinfo.ScreenID ?? PXContext.GetScreenID());
if (string.IsNullOrWhiteSpace(screenID) || graph.PrimaryItemType == null)
{
return;
}
Type targetGraphType = ResolveTargetGraphType(graph, screenID);
if (targetGraphType == null || !targetGraphType.IsAssignableFrom(graph.GetType()))
{
return;
}
string customActionPrefix = PXGraph.SidePanelAction.Prefix + "GISP_";
graph.SidePanelActions.RemoveAll(action => action.ActionName?.StartsWith(
customActionPrefix,
StringComparison.OrdinalIgnoreCase) == true);
foreach (ConfiguredTarget target in ConfigurationSlot.GetTargets(screenID))
{
string actionName = PXGraph.SidePanelAction.Prefix + "GISP_" + target.DesignID.ToString("N");
if (graph.Actions[actionName] == null)
{
NavigateActionHandler handler = new NavigateActionHandler(
graph,
target.DesignID,
target.TargetScreenID);
PXNamedAction.AddHiddenAction(graph, graph.PrimaryItemType, actionName, handler.Invoke);
}
string title = string.IsNullOrWhiteSpace(target.Title)
? GISidePanelMessages.GenericInquirySidePanel
: target.Title;
string icon = string.IsNullOrWhiteSpace(target.Icon) ? "apps" : target.Icon;
graph.SidePanelActions.Add(new PXGraph.SidePanelAction(actionName, icon, title));
}
}
private static string NormalizeScreenID(string screenID)
{
return screenID?.Replace(".", string.Empty).Trim();
}
private static void NavigateToGenericInquiry(PXGraph graph, Guid designID, string targetScreenID)
{
GIDesign design = GIDesign.PK.Find(graph, designID);
if (design == null || string.IsNullOrWhiteSpace(design.Name))
{
throw new PXException(GISidePanelMessages.GenericInquiryUnavailable, designID);
}
Dictionary<string, string> parameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
PXResultset<GISidePanelParameter> mappings = SelectFrom<GISidePanelParameter>
.Where<GISidePanelParameter.designID.IsEqual<@P.AsGuid>
.And<GISidePanelParameter.targetScreenID.IsEqual<@P.AsString>>>
.OrderBy<GISidePanelParameter.lineNbr.Asc>
.View.ReadOnly.Select(graph, designID, targetScreenID);
foreach (GISidePanelParameter mapping in mappings.RowCast<GISidePanelParameter>())
{
PXView sourceView = graph.Views[mapping.SourceViewName]
?? throw new PXException(GISidePanelMessages.SourceViewUnavailable, mapping.SourceViewName, targetScreenID);
PXCache sourceCache = sourceView.Cache;
if (!IsMappableSourceField(sourceCache, mapping.SourceFieldName))
{
throw new PXException(GISidePanelMessages.SourceFieldUnavailable, mapping.SourceFieldName, mapping.SourceViewName, targetScreenID);
}
object currentRecord = sourceCache.Current
?? throw new PXException(GISidePanelMessages.SourceRecordUnavailable, mapping.SourceViewName, targetScreenID);
object value = sourceCache.GetValue(currentRecord, mapping.SourceFieldName);
string valueText = value == null
? null
: Convert.ToString(value, CultureInfo.CurrentCulture);
if (string.IsNullOrWhiteSpace(valueText))
{
throw new PXException(
GISidePanelMessages.SourceFieldValueUnavailable,
mapping.SourceFieldName,
mapping.SourceViewName,
targetScreenID);
}
parameters[mapping.ParameterName] = valueText;
}
throw new PXRedirectToGIRequiredException(
design.Name,
parameters,
PXBaseRedirectException.WindowMode.Layer);
}
}
}