Rock.Web.UI.Controls.MergeFieldPicker.FormatSelectedValue C# (CSharp) Method

FormatSelectedValue() public static method

Formats the selected value (node path) into a liquid merge field.
public static FormatSelectedValue ( string selectedValue ) : string
selectedValue string The selected value.
return string
        public static string FormatSelectedValue(string selectedValue)
        {
            var idParts = selectedValue.Split( new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries ).ToList();
            if ( idParts.Count > 0 )
            {
                if ( idParts.Count == 2 && idParts[0] == "GlobalAttribute" )
                {
                    return string.Format( "{{{{ 'Global' | Attribute:'{0}' }}}}", idParts[1] );
                }

                if ( idParts.Count == 1 )
                {
                    if ( idParts[0] == "Campuses" )
                    {
                        return @"
            {% for campus in Campuses %}
            <p>
            Name: {{ campus.Name }}<br/>
            Description: {{ campus.Description }}<br/>
            Is Active: {{ campus.IsActive }}<br/>
            Short Code: {{ campus.ShortCode }}<br/>
            Url: {{ campus.Url }}<br/>
            Phone Number: {{ campus.PhoneNumber }}<br/>
            Service Times:
            {% for serviceTime in campus.ServiceTimes %}
            {{ serviceTime.Day }} {{ serviceTime.Time }},
            {% endfor %}
            <br/>
            {% endfor %}
            ";
                    }

                    if ( idParts[0] == "Date" )
                    {
                        return "{{ 'Now' | Date:'MM/dd/yyyy' }}";
                    }

                    if ( idParts[0] == "Time" )
                    {
                        return "{{ 'Now' | Date:'hh:mm:ss tt' }}";
                    }

                    if ( idParts[0] == "DayOfWeek" )
                    {
                        return "{{ 'Now' | Date:'dddd' }}";
                    }

                    if ( idParts[0] == "PageParameter" )
                    {
                        return "{{ PageParameter.[Enter Page Parameter Name Here] }}";
                    }
                }

                var workingParts = new List<string>();

                // Get the root type
                int pathPointer = 0;
                EntityTypeCache entityType = null;
                while ( entityType == null && pathPointer < idParts.Count() )
                {
                    string item = idParts[pathPointer];
                    string[] itemParts = item.Split( new char[] { '^' }, StringSplitOptions.RemoveEmptyEntries );

                    string itemName = itemParts.Length > 1 ? itemParts[0] : string.Empty;
                    string itemType = itemParts.Length > 1 ? itemParts[1] : item;

                    entityType = EntityTypeCache.Read( itemType, false );

                    workingParts.Add( entityType != null ?
                        ( itemName != string.Empty ? itemName : entityType.FriendlyName.Replace( " ", string.Empty) ) :
                        idParts[pathPointer] );
                    pathPointer++;
                }

                if ( entityType != null )
                {
                    Type type = entityType.GetEntityType();

                    var formatString = "{0}";

                    // Traverse the Property path
                    bool itemIsCollection = false;
                    bool lastItemIsProperty = true;

                    while ( idParts.Count > pathPointer )
                    {
                        string propertyName = idParts[pathPointer];
                        workingParts.Add( propertyName );

                        var childProperty = type.GetProperty( propertyName );
                        if ( childProperty != null )
                        {
                            lastItemIsProperty = true;
                            type = childProperty.PropertyType;

                            if ( type.IsGenericType &&
                                type.GetGenericTypeDefinition() == typeof( ICollection<> ) &&
                                type.GetGenericArguments().Length == 1 )
                            {
                                string propertyNameSingularized = propertyName.Singularize();
                                string forString = string.Format( "<% for {0} in {1} %> {{0}} <% endfor %>", propertyNameSingularized, workingParts.AsDelimited( "." ) );
                                workingParts.Clear();
                                workingParts.Add( propertyNameSingularized );
                                formatString = string.Format( formatString, forString );

                                type = type.GetGenericArguments()[0];

                                itemIsCollection = true;
                            }
                            else
                            {
                                itemIsCollection = false;
                            }
                        }
                        else
                        {
                            lastItemIsProperty = false;
                        }

                        pathPointer++;
                    }

                    string itemString = string.Empty;
                    if ( !itemIsCollection )
                    {
                        if ( lastItemIsProperty )
                        {
                            itemString = string.Format( "<< {0} >>", workingParts.AsDelimited( "." ) );
                        }
                        else
                        {
                            string partPath = workingParts.Take( workingParts.Count - 1 ).ToList().AsDelimited( "." );
                            itemString = string.Format( "{{{{ {0} | Attribute:'{1}' }}}}", partPath, workingParts.Last() );
                        }

                    }

                    return string.Format( formatString, itemString ).Replace( "<", "{" ).Replace( ">", "}" );
                }

                return string.Format( "{{{{ {0} }}}}", idParts.AsDelimited( "." ) );

            }

            return string.Empty;
        }