8

I am using ugc conditional statement in my code, the equals condition is working fine, but how can be used other conditional operator like ">" "<" and "Not Equals".

<%
HttpContext.Current.Items["CommentCount"] = 0;
%>

<ugc:Choose runat="server">
  <ugc:When test="ugcItemStats.numberOfComments > CommentCount" runat="server">
         HTML1
  </ugc:When>
  <ugc:Otherwise runat="server">
         HTML2
  </ugc:Otherwise>
</ugc:Choose>

What operator should be used, if numberofComments is greater than 0, I tried like this way and also tried "notequals" instead of ">" but its does't work.

Please suggest

Priyank Gupta
  • 922
  • 1
  • 7
  • 26

4 Answers4

4

Tridion ug:when will be work only with " equal " and "==" if you want to use other operator then you have to create the other customer control for this.

I have created and i hope it will be work with "==,>=",<=,>,<,!=" operator.

its working in my project.

using System;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;

namespace Tridion.ContentDelivery.UGC.Web.UI
{
    [DefaultProperty("Test"), ToolboxData("<{0}:WhenCond runat=server></{0}:WhenCond>"), ParseChildren(ChildrenAsProperties = false)]
    public class WhenCond : BaseUGCServerControl
    {
        private string test;
        private static Regex pattern = new Regex(@"\.");
        protected virtual bool Condition()
        {
            if (this.test == null)
            {
                return false;
            }
            string[] sep = new string[] { "==", "<", ">", "<=", ">=" ,"!="};
            string[] testArray = test.Split(sep, StringSplitOptions.None);
            if (testArray.Length == 2)
            {
                object value1 = EvaluateVariable(testArray[0].Trim(), HttpContext.Current);
                object value2 = EvaluateVariable(testArray[1].Trim(), HttpContext.Current);
                if (value1 != null && value2 != null)
                {
                    if (isNumeric(value1.ToString(), NumberStyles.Number) && isNumeric(value2.ToString(), NumberStyles.Number))
                    {
                        return NumericCondition(double.Parse(value1.ToString()), double.Parse(value2.ToString()), GetSepartor());
                    }
                    else
                    {
                        return AlphaNumericCondition(value1.ToString(), value2.ToString(), GetSepartor());
                    }
                }
                else
                {
                    return false;
                }
            }
            return false;
        }

        public static object EvaluateVariable(string varProperty, HttpContext usedContext)
        {
            if (!string.IsNullOrEmpty(varProperty))
            {
                string[] strArray = pattern.Split(varProperty);
                if (!string.IsNullOrEmpty(strArray[0]))
                {
                    object obj2 = usedContext.Items[strArray[0]];
                    if (obj2 != null)
                    {
                        object obj3 = obj2;
                        for (int i = 1; i < strArray.Length; i++)
                        {
                            if (obj3 != null)
                            {
                                string str = strArray[i];
                                if (!string.IsNullOrEmpty(str))
                                {
                                    string str2 = str.Substring(0, 1);
                                    string str3 = str.Substring(1);
                                    string name = str2.ToUpper() + str3;
                                    PropertyInfo property = obj3.GetType().GetProperty(name);
                                    if (property != null)
                                    {
                                        obj3 = property.GetValue(obj3, null);
                                    }
                                }
                            }
                        }
                        return obj3;
                    }
                }
            }
            return null;
        }

        public bool isNumeric(string val, System.Globalization.NumberStyles NumberStyle)
        {
            Double result;
            return Double.TryParse(val, NumberStyle, System.Globalization.CultureInfo.CurrentCulture, out result);
        }

        private string GetSepartor()
        {
            string sept = string.Empty;
            sept = this.test.Contains("==") ? "==" : string.Empty;
            sept = string.IsNullOrEmpty(sept) ?(this.test.Contains(">") ? ">" : string.Empty):sept;
            sept = string.IsNullOrEmpty(sept) ? (this.test.Contains("<") ? "<" : string.Empty) : sept;
            sept = string.IsNullOrEmpty(sept) ?(this.test.Contains(">=") ? ">=" : string.Empty):sept;
            sept = string.IsNullOrEmpty(sept) ?(this.test.Contains("<=") ? "<=" : string.Empty):sept;
            sept = string.IsNullOrEmpty(sept) ? (this.test.Contains("!=") ? "!=" : string.Empty) : sept;
            return sept;
        }

        private bool NumericCondition(double value1, double value2, string sept)
        {
            bool returnFlag = false;
            switch (sept)
            {
                case "==":
                    returnFlag = (value1 == value2);
                    break;
                case ">":
                    returnFlag = (value1 > value2);
                    break;
                case "<":
                    returnFlag = (value1 < value2);
                    break;
                case ">=":
                    returnFlag = (value1 >= value2);
                    break;
                case "<=":
                    returnFlag = (value1 <= value2);
                    break;
                case "!=":
                    returnFlag = (value1 != value2);
                    break;
            }
            return returnFlag;
        }

        private bool AlphaNumericCondition(string value1, string value2, string sept)
        {
            bool returnFlag = false;
            switch (sept)
            {
                case "==":
                    returnFlag = (value1.CompareTo(value2) == 0);
                    break;
                case "!=":
                    returnFlag = (!value1.Equals(value2));
                    break;
                case ">":
                    returnFlag = (value1.CompareTo(value2) > 0);
                    break;
                case "<":
                    returnFlag = (value1.CompareTo(value2) < 0);
                    break;
            }
            return returnFlag;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if ((HttpContext.Current != null) && (HttpContext.Current.Application != null))
            {
                Control parent = this.Parent;
                if (!(parent is Choose))
                {
                    throw new InvalidOperationException("WhenCond control must have a Tridion Web UI Choose server control as parent!!!");
                }
                Choose choose = (Choose)parent;
                if (!choose.AlreadyMatchedCondition() && this.Condition())
                {
                    choose.MatchedCondition();
                    this.RenderChildren(writer);
                }
            }
        }

        [Category("Appearance"), DefaultValue(""), Bindable(true)]
        public string Test
        {
            get
            {
                return this.test;
            }
            set
            {
                this.test = value;
            }
        }
    }
}

implementaion in aspx page

<%@ Register assembly="Tridion.Custom.Web.UI" namespace="Tridion.ContentDelivery.UGC.Web.UI" tagprefix="cc1" %>


<ugc:Choose runat="server">
  <cc1:WhenCond test="ugcItemStats.numberOfComments > CommentCount" runat="server">
         HTML1
  </cc1:WhenCond>
  <ugc:Otherwise runat="server">
         HTML2
  </ugc:Otherwise>
</ugc:Choose>

if you face any problem please let me know.

Shekhar Gigras
  • 654
  • 3
  • 5
  • its work fine, you have created own tag. its help lot and easy to use this code in tridion. Thanks a lot for your code. – Priyank Gupta Aug 23 '12 at 07:22
2

To best of my knowledge, When conditions support only two conditions either == and equals. So you may have to use these to workaround your test conditions (both are same as equal).

Could someone validate or confirm that above is true?

Ram G
  • 4,821
  • 1
  • 14
  • 26
  • In Tridion UGC, its not accepting "==" keywords, its only work with equals. And i need other condition operator symbol. – Priyank Gupta Aug 20 '12 at 14:33
  • `==` works for me. I have used it before in j2ee app (should not matter for .net either). are you trying this `ugcItemStats.numberOfComments==CommentCount` ? – Ram G Aug 20 '12 at 14:41
  • No, its not working for Tridion UGC Conditional statement, i have tried many times. – Priyank Gupta Aug 20 '12 at 14:56
1

Have you tried using &gt;? That normally does the trick for similar things, e.g. XPath.

Jeremy Grand-Scrutton
  • 2,802
  • 12
  • 20
1

Sorry, the When tag currently only supports "==" or "equals".

Peter Kjaer
  • 4,316
  • 11
  • 23
  • I tried it many times, its only support "equals". tell me how to use == in your code? – Priyank Gupta Aug 21 '12 at 09:03
  • There is nothing to it, really. "variable == value" is the syntax. But it really doesn't matter if you use "==" or "equals", it's the same thing. So if you can't get it to work for some reason, just use "equals" instead. If you are having issues, set your log level to DEBUG and it will tell you what is going on... – Peter Kjaer Aug 21 '12 at 10:20
  • How do i set the DEBUG level in Tridion UGC? Could you please suggest. Thanks in advance. – Priyank Gupta Aug 21 '12 at 10:26
  • You do so in logback.xml. See the following page in the online documentation: http://sdllivecontent.sdl.com/LiveContent/content/en-US/SDL_Tridion_2011_SPONE/task_1D1C270013B644039FAA55752702E645 – Peter Kjaer Aug 21 '12 at 10:32
  • The logback is applied on the content delivery time. But UGC:Choose & ugc:When tag and condition work on the page load time. – Priyank Gupta Aug 21 '12 at 12:33
  • I don't know where you got that idea from, but I assure you that they all use the same logback.xml file :) And there is certainly logging in this area which could tell you why "==" doesn't work for you. – Peter Kjaer Aug 21 '12 at 12:37