Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

CustomValues.cs

using System.Xml.Linq;

namespace Nop.Services.Orders;

/// 
/// Represents the list of custom value
/// 
public partial class CustomValues : List
{
    #region Methods

    /// 
    /// Get custom values by specified display location
    /// 
    /// Display location
    /// List of custom values filtered by display location
    public virtual List GetValuesByDisplayLocation(CustomValueDisplayLocation displayLocation)
    {
        return this.Where(cv => cv.DisplayLocation == displayLocation).ToList();
    }

    /// 
    /// Serialize list of custom values into XML format
    /// 
    /// XML of custom values
    public virtual string SerializeToXml()
    {
        if (!this.Any())
            return string.Empty;

        using var textWriter = new StringWriter();

        var root = new XElement("DictionarySerializer");
        root.Add(this.Select(value => new XElement("item",
            new XElement("key", value.Name),
            new XElement("value", value.Value),
            new XElement("displayToCustomer", value.DisplayToCustomer),
            new XElement("location", (int)value.DisplayLocation),
            new XElement("createdOn", value.CreatedOnUtc?.Ticks ?? 0))));

        var document = new XDocument();
        document.Add(root);
        document.Save(textWriter, SaveOptions.DisableFormatting);

        var result = textWriter.ToString();

        return result;
    }

    /// 
    /// Fill order custom values by XML
    /// 
    /// XML of custom values
    /// The flag indicates whether we should use only values that are allowed for customers
    /// Custom values
    public virtual void FillByXml(string customValuesXml, bool displayToCustomerOnly = false)
    {
        Clear();

        if (string.IsNullOrWhiteSpace(customValuesXml))
            return;

        //use the 'Payment' value as default for compatibility with previous versions
        var defaultLocation = CustomValueDisplayLocation.Payment;

        //display a custom value by default for compatibility with previous versions
        var defaultDisplayToCustomer = true;

        try
        {
            using var textReader = new StringReader(customValuesXml);
            var rootElement = XDocument.Load(textReader).Root;
            AddRange(rootElement!.Elements("item").Select(i =>
            {
                var name = i.Element("key")!.Value;
                var value = i.Element("value")!.Value;
                var displayLocation = int.TryParse(i.Element("location")?.Value, out var location) && Enum.IsDefined(typeof(CustomValueDisplayLocation), location)
                    ? (CustomValueDisplayLocation)location
                    : defaultLocation;
                var displayToCustomer = bool.TryParse(i.Element("displayToCustomer")?.Value, out var display)
                    ? display
                    : defaultDisplayToCustomer;
                var createdOn = long.TryParse(i.Element("createdOn")?.Value, out var ticks)
                    ? (ticks > 0 ? new DateTime(ticks) : (DateTime?)null)
                    : null;

                return new CustomValue(name, value, displayLocation, displayToCustomer, createdOn);
            }).Where(value => value.DisplayToCustomer || !displayToCustomerOnly));
        }
        catch
        {
            //ignore
        }
    }

    /// 
    /// Removes the custom value with the specified name
    /// 
    /// The name of the custom value.
    /// 
    ///  if the element is successfully removed; otherwise, .  This method also returns  if  was not found.
    /// 
    public virtual bool Remove(string name)
    {
        return TryGetValue(name, out var value) && Remove(value);
    }

    /// 
    /// Gets the value associated with the specified name
    /// 
    /// The name whose value to get.
    /// When this method returns, the value associated with the specified name, if the name is found; otherwise, the null value
    /// 
    ///  if the custom values list contains an element with the specified name; otherwise, .
    /// 
    public virtual bool TryGetValue(string name, out CustomValue customValue)
    {
        customValue = this.FirstOrDefault(cv => cv.Name.Equals(name));

        return customValue != null;
    }

    #endregion

    #region Properties

    /// 
    /// Gets or sets the value of custom value with the specified name
    /// 
    /// The name of the custom value to get or set
    /// The value of custom value with the specified name
    public virtual string this[string name]
    {
        get
        {
            var value = this.First(cv => cv.Name.Equals(name));

            return value.Value;
        }
        set
        {
            Remove(name);
            Add(new CustomValue(name, value));
        }
    }

    #endregion
}