Update wmta member search
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 19 Jun 2018 13:16:51 +0000 (09:16 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 19 Jun 2018 13:16:51 +0000 (09:16 -0400)
Break out words in the search and use each one for match.

functions.php

index d8554de..e30951d 100644 (file)
@@ -411,25 +411,42 @@ function new_mail_from_name($old) {
 add_filter('glm-member-db-front-search-query', function( $queryParts ){
     if ( isset( $_REQUEST['s'] ) && $_REQUEST['s'] ) {
         $textSearch = addslashes(filter_var($_REQUEST['s'], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES));
-        $queryParts[] = " (
+        // Split the search by spaces
+        $sub_parts = explode( ' ', $textSearch );
+        $name_part = $desc_part = $city_part = array();
+        foreach ( $sub_parts as $part ) {
+            $name_part[] = " name SOUNDS LIKE '%$part%' OR name LIKE '%$part%'";
+            $desc_part[] = " T.descr SOUNDS LIKE '%$part%' OR T.descr LIKE '%$part%'";
+            $city_part[] = " name SOUNDS LIKE '%$part%' OR name LIKE '%$part%'";
+        }
+        // echo '<pre>$sub_parts: ' . print_r( $sub_parts, true ) . '</pre>';
+        $query_parts = " (
                 (
                 SELECT true
                   FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX."members
                  WHERE id = T.member
-                   AND (
-                        name SOUNDS LIKE '%$textSearch%'
-                     OR name LIKE '%$textSearch%'
-                    )
-                )
-            OR T.descr SOUNDS LIKE '%$textSearch%'
-            OR T.descr LIKE '%$textSearch%'
-            OR T.city IN (
+                   AND (";
+        if ( $name_part ) {
+            $query_parts .= implode( ' OR ', $name_part );
+        }
+        $query_parts .= ")
+                ) OR ";
+        if ( $desc_part ) {
+            $query_parts .= implode( ' OR ', $desc_part );
+        }
+        $query_parts .= "OR T.city IN (
                 SELECT id
                   FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "cities
-                 WHERE name SOUNDS LIKE '%$textSearch%'
-                    OR name LIKE '%$textSearch%'
-            )
-        )";
+                 WHERE ";
+        if ( $city_part ) {
+            $query_parts .= implode( ' OR ', $city_part );
+        }
+        // $query_parts .= " name SOUNDS LIKE '%$textSearch%' OR name LIKE '%$textSearch%'";
+
+        $query_parts .= ")
+            )";
+        $queryParts[] = $query_parts;
     }
+    // echo '<pre>$queryParts: ' . print_r( $queryParts, true ) . '</pre>';
     return $queryParts;
 });