You are here:  » Filter to overwrite contents of a field with another if not empty


Filter to overwrite contents of a field with another if not empty

Submitted by ChrisNBC on Mon, 2015-05-11 17:28 in

Hi David,

Hope all is going well.

I wondered if you could possibly tell me if there are any existing filters which would allow me to overwrite the contents of a field(1) with the contents of a second field(2) but leave field (1) intact if field(2) was empty?

Thanks in advance.
Regards
Chris

Submitted by support on Tue, 2015-05-12 08:07

Hi Chris,

An "Alternative If Set" filter, using the same placeholders as Text Before / Text after
would be straight forward.

Have a go with the following code, add to includes/filter.php at the end
of the file, just before the closing PHP tag:

  /*************************************************/
  /* alternativeIfSet */
  /*************************************************/
  $filter_names["alternativeIfSet"] = "Alternative If Set";
  function filter_alternativeIfSetConfigure($filter_data)
  {
    print "Text:<br />";
    print "<input type='text' size='40' name='text' value='".widget_safe($filter_data["text"])."' />";
    widget_errorGet("text");
  }
  function filter_alternativeIfSetValidate($filter_data)
  {
    if (!$filter_data["text"])
    {
      widget_errorSet("text","required field");
    }
  }
  function filter_alternativeIfSetExec($filter_data,$text)
  {
    $alternative = filter_recordPlaceholders($filter_data["text"]);
    if ($alternative)
    {
      return $alternative;
    }
    else
    {
      return $text;
    }
  }

So for example, if you add an instance of the filter to the Category field, using a value of

%otherfield%

...then if a value exists in "otherfield" (with reference to the field names shown in the sample data below the form on Feed Registration Step 2) then it will REPLACE the Category value. Otherwise, if blank the Category will remain unchanged.

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Tue, 2015-05-12 11:51

Hi David,
Thanks as always for your quick reply and for the new filter.
Apologies, I should have explained more clearly what I need to use the filter for....I’m currently breaking down the product names from my merchant feeds and rebuilding them using a series of filters. This works well except that I now have some merchants who only sell own label. Since there is no real comparison for these merchants products, I would like to just use the product names provided in the feed. I have registered these feeds so that the original product name is imported into two separate PT fields, one gets broken down and rebuilt and the other is retained in its raw format. At the end of all the filters running (i’m using global filters, merchant filters and then finally zzglobal filters) I would like to replace the contents of the original ‘productname’ field with the contents of the field holding the raw product name. If the raw product name does not contain anything the productname (which has been altered by the filters) will be left as it is. Would it be possible to modify the filter you kindly wrote to use the pt product field name rather the field name from the feed?
Thanks in advance.
Regards
Chris

Submitted by support on Tue, 2015-05-12 12:16

Hi Chris,

The following filter - "Alternative If Set DB" - will work in exactly the same way except that the placeholders in the input string need to refer to the `pt_products` table fields instead of the feed field name, in other words, for the main product name field use %name%. The values can be any of the keys in the $config_fieldSet array from config.advanced.php:

  /*************************************************/
  /* alternativeIfSetDB */
  /*************************************************/
  $filter_names["alternativeIfSetDB"] = "Alternative If Set DB";
  function filter_alternativeIfSetDBConfigure($filter_data)
  {
    print "Text:<br />";
    print "<input type='text' size='40' name='text' value='".widget_safe($filter_data["text"])."' />";
    widget_errorGet("text");
  }
  function filter_alternativeIfSetDBValidate($filter_data)
  {
    if (!$filter_data["text"])
    {
      widget_errorSet("text","required field");
    }
  }
  function filter_alternativeIfSetDBExec($filter_data,$text)
  {
    global $importRecord;
    foreach($importRecord as $k => $v)
    {
      $filter_data["text"] = str_replace("%".$k."%",$v,$filter_data["text"]);
    }
    $alternative = $filter_data["text"];
    if ($alternative)
    {
      return $alternative;
    }
    else
    {
      return $text;
    }
  }

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Tue, 2015-05-12 15:28

Hi David,

Thanks for the above filter...it works a treat!

Best regards
Chris

Submitted by ChrisNBC on Thu, 2015-05-14 12:56

Hi David,

Thanks for all your help with these filters. I wondered if you could suggest if there is a way to easily create another filter based on the above but which would only be applied if the contents of another field matched a specified regex condition?

Thanks in advance.

Regards
Chris

Submitted by support on Thu, 2015-05-14 13:57

Hi Chris,

Something like this should do it;

  /*************************************************/
  /* alternativeIfSetDBTriggerTrigger */
  /*************************************************/
  $filter_names["alternativeIfSetDBTrigger"] = "Alternative If Set DB Trigger";
  function filter_alternativeIfSetDBTriggerConfigure($filter_data)
  {
    print "Trigger Field:<br />";
    print "<input type='text' size='40' name='trigger_field' value='".widget_safe($filter_data["trigger_field"])."' />";
    widget_errorGet("trigger_field");
    print "Trigger Regexp:<br />";
    print "<input type='text' size='40' name='trigger_regexp' value='".widget_safe($filter_data["trigger_regexp"])."' />";
    widget_errorGet("trigger_regexp");
    print "Text:<br />";
    print "<input type='text' size='40' name='text' value='".widget_safe($filter_data["text"])."' />";
    widget_errorGet("text");
  }
  function filter_alternativeIfSetDBTriggerValidate($filter_data)
  {
    if (!$filter_data["text"])
    {
      widget_errorSet("text","required field");
    }
    if (!$filter_data["trigger_field"])
    {
      widget_errorSet("trigger","required field");
    }
    if (!$filter_data["trigger_regexp"])
    {
      widget_errorSet("trigger_regexp","required field");
    }
  }
  function filter_alternativeIfSetDBTriggerExec($filter_data,$text)
  {
    global $importRecord;
    if (!preg_match($filter_data["trigger_regexp"],$importRecord[$filter_data["trigger_field"]]))
    {
      return $text;
    }
    foreach($importRecord as $k => $v)
    {
      $filter_data["text"] = str_replace("%".$k."%",$v,$filter_data["text"]);
    }
    $alternative = $filter_data["text"];
    if ($alternative)
    {
      return $alternative;
    }
    else
    {
      return $text;
    }
  }

This new filter - "Alternative If Set DB Trigger" adds 2 new configuration fields - Trigger Field - which can be anything from $config_fieldSet (the keys, e.g. "name", "category" etc.) and then Trigger Regexp - the regular expression to test that field against. If the regexp does not match then the filter will return the input text unchanged.

Cheers,
David.
--
PriceTapestry.com