Features / Usability

Features / Usability

How to check if a string exist in 2 smarty variables

posts: 8512 Israel


I have a tracker with a relation tracker field that look like this: "Banked ,July - Bernard | $720". I'm looking for a way to detect when the month in the field's value more than 1 month in the past.

We are in August;
if value is "Banked ,July - Bernard | $720" do nothing
if value is "Banked ,June - Bernard | $720" do something

From a wiki page I have a plugin list and then a smarty template.
In the foreach loop I grab the variable "$row.tracker_field_productsboughtPaymentId_text" that output the field _text value ("Banked ,July - Bernard | $720"). So the following comparison between a variable and a string is working just fine:

{$paymentref = $row.tracker_field_productsboughtPaymentId_text} {if $paymentref|strstr:"July"}Say hi{/if}

However I don't want to hardcode the month of the year and want to have it based on the smarty.now date. So I created a variable for "month minus 1" and now try to compare 2 variables the same ways before.
But this is not working (I tried with single quote, no quotes, no bracket parentheses...)

{$month = "-1 month"|date_format:"%B"} {$paymentref = $row.tracker_field_productsboughtPaymentId_text} {if $paymentref|strstr:"{$month}"}Say hi{/if}

I am missing something there or I should use something else to check if a variable contain something from another variable ?

posts: 126825 United Kingdom

Surely you would need to convert both month names into numbers (using %m format) and then do something like {if $thatMonthNumber lt $thisMonthNumber}{do stuff...

If you know the month you are looking at is always the 2nd word in the string then some regex should do it with preg_match_all ?

Does that help?

posts: 8512 Israel

Thank you dear jonny.

Ok I got your idea... I tried to create a conversion additional code but though I was overdoing something for what we may have a solution.

So for the relation field value only, grab the second word with a "preg_match_all"
preg_match_all ("\s+^\s+", $paymentref, $paymentrefmonth)

and convert each month (string) by a number.
{if $paymentrefmonth eq "January"}{$paymentrefmonth = 01}{elseif $paymentrefmonth eq "February"} ....

Then do the comparison...