You are here:  » New Global Filter - Search and Add

Support Forum



New Global Filter - Search and Add

Submitted by peterb on Fri, 2012-02-24 12:16 in

Hi David,
I would like to add another 'Global Filter' called 'Search & Add' which will basically allow me to search one existing field and add a keyword to a different existing field on exact matches, ie

-----------------------------------
Example 1
----------------------------------

#Search field:
Product Name

#Keywords:
(Women|women|Women's|women's)

#Add to field:
Gender

#Keywords
f

-----------------------------------
Example 2
----------------------------------

#Search field:
Product Name

#Keywords:
(Boy|boy|Boy's|boy's)

#Add to field:
Age

#Keywords
youth
-----------------------------------

This new global filter will allow me to automatically add key matching data on feed importing so I can filter results better.

Thankyou for your help.

Submitted by support on Fri, 2012-02-24 12:53

Hello Peter,

For this to work first of all the $importRecord variable would need to be declared global in order that it can be modified by filters. To do this, look for the following code at line 147 of includes/admin.php

    $importRecord = array();

...and REPLACE with:

    global $importRecord;
    $importRecord = array();

With that in place, have a go with the following as your new filter - paste into the end of includes/filter.php just before the closing PHP tag:

  /*************************************************/
  /* searchadd */
  /*************************************************/
  $filter_names["searchAdd"] = "Search and Add";
  function filter_searchAddConfigure($filter_data)
  {
    print "Search (RegExp):<br />";
    print "<input type='text' name='search' value='".widget_safe($filter_data["search"])."' />";
    widget_errorGet("search");
    print "<br /><br />";
    print "Field:<br />";
    print "<input type='text' name='field' value='".widget_safe($filter_data["field"])."' />";
    widget_errorGet("field");
    print "<br /><br />";
    print "Add:<br />";
    print "<input type='text' name='add' value='".widget_safe($filter_data["add"])."' />";
    widget_errorGet("add");
  }
  function filter_searchAddValidate($filter_data)
  {
    if (!$filter_data["search"])
    {
      widget_errorSet("search","required field");
    }
    if (!$filter_data["field"])
    {
      widget_errorSet("field","required field");
    }
    if (!$filter_data["add"])
    {
      widget_errorSet("add","required field");
    }
  }
  function filter_searchAddExec($filter_data,$text)
  {
    global $importRecord;
    if (preg_match($filter_data["search"],$text))
    {
      $importRecord[$filter_data["field"]] .= $filter_data["add"];
    }
    return $text;
  }

The field to search will be the actual field you apply the filter to (so isn't part of the configuration), and then on the configuration page you can enter Search - as a regular expression so you can use (this|that|other) - Field - the field name as defined in $config_fieldSet in config.advanced.php, and Add - the text to add to the field name if the regular expression is a match...

Hope this helps!

Cheers,
David.
--
PriceTapestry.com