Uploaded image for project: 'CiviCRM'
  1. CiviCRM
  2. CRM-8548

Search builder IN operator don't work

    Details

    • Type: Bug
    • Status: Done/Fixed
    • Priority: Blocker
    • Resolution: Fixed/Completed
    • Affects Version/s: 4.0.0
    • Fix Version/s: 3.4.5
    • Component/s: Core CiviCRM
    • Labels:
      None

      Description

      This is a issue first discussed and investigated in the forum: http://forum.civicrm.org/index.php/topic,16249.msg87094.html

      For Civicrm 4.0/Drupal 7.0

      I am trying to search for contacts with addresses in Scandinavia (Sweden, Norway and Denmark) and my search criteria looks like this:
      Individual -> Country -> Home -> IN -> (Sweden,Norway,Denmark) results in error message: Please enter correct Data (in valid format)

      Individual -> Country -> Home -> IN -> ('Sweden','Norway','Denmark') results in no matches although I verified that my database contains contacts from Sweden (I used 'United States' at demo.civicrm.org and got no matches) and when I look in civicrm_saved_search.where_clause it has the following value:
      ( LOWER(civicrm_country.name) IN ('\\\'Sweden\\\'','\\\'Norway\\\'','\\\'Denmark\\\''))

      In the file CRM/Contact/Form/Search/Builder.php in the function formRule() there is a check for if the search word is in valid format. The regular expression expects to find apostrophe (') before and after every search word for the IN operator. But in the code that builds the database question there is an escape functions that adds backslashes before every SQL-dangerous character (e.g. apostrophes).

      So I simply removed the check for apostrophes (just for the IN operator) and my searches are now working. I also added the possibilty to write some scandinavian and german letters (å, ä, ö, æ, ø and ü). Here are the old and new code lines:

      if ( $v[1] == 'IN' ) {
      ...

      OLD
      if (!preg_match( '/^[(][\']([A-Za-z0-9\'\,\s]+)[\'][)]$/', $inVal) ) {

      NEW
      if (!preg_match( '/^[(]([A-Za-z0-9åäöÅÄÖæÆøØüÜœŒ\,\s]+)[)]$/', $inVal) ) {

      ...
      }

      I was also annoyed by the fact that I couldn't learn myself to not put spaces after the commas so I added the possibility to do so :0) That means I added a trim for each of the values found within the parentheses in the file CRM/Contact/BAO/Query.php in the function buildClause():

      Code: [Select] [nofollow]
      case 'IN'
      ...
      foreach ( $values as $v ) {
      $v = trim( $v, " " ); // NEW LINE OF CODE
      $val[] = "'" . CRM_Utils_Type::escape( $v, $dataType ) . "'";
      }

      I haven't tested this much but my searches works now and I have only changed code for the IN operator so I think this pretty safe to implement.

        Attachments

        1. Builder.php.patch
          1 kB
          Jonas Tano
        2. Query.php.patch
          0.5 kB
          Jonas Tano

          Activity

            People

            • Assignee:
              lobo Donald A. Lobo
              Reporter:
              gastrit Jonas Tano
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: