Advertisement
Zgragselus

Matching bottles DB

Dec 17th, 2022
880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.69 KB | None | 0 0
  1.     // Due to no product match - there is a problem, we have to attempt to match to bottles in db. This
  2.     // must be done in steps:
  3.     //   1. Find possible matches
  4.     //   2. Attempt to find full match (new product name in tokens must contain ALL tokens in DB bottle)
  5.     //
  6.     // If such match is found, we can't be sure it is certain, therefore so such product must be
  7.     // hand-checked
  8.  
  9.     // Tokenize input
  10.     $tokens = explode(' ', $record->name);
  11.  
  12.     // Filter out volume and alcohol volume
  13.     $tokens = array_filter($tokens, static function ($element) {
  14.         if (strlen(trim($element)) < 1)
  15.         {
  16.             return false;
  17.         }
  18.  
  19.         if (strpos($element, "%") !== false &&
  20.             (strpos($element, "0") !== false ||
  21.             strpos($element, "1") !== false ||
  22.             strpos($element, "2") !== false ||
  23.             strpos($element, "3") !== false ||
  24.             strpos($element, "4") !== false ||
  25.             strpos($element, "5") !== false ||
  26.             strpos($element, "6") !== false ||
  27.             strpos($element, "7") !== false ||
  28.             strpos($element, "8") !== false ||
  29.             strpos($element, "9") !== false))
  30.         {
  31.             return false;
  32.         }
  33.  
  34.         if ((strpos($element, "l") !== false || strpos($element, "m")) &&
  35.             (strpos($element, "0") !== false ||
  36.             strpos($element, "1") !== false ||
  37.             strpos($element, "2") !== false ||
  38.             strpos($element, "3") !== false ||
  39.             strpos($element, "4") !== false ||
  40.             strpos($element, "5") !== false ||
  41.             strpos($element, "6") !== false ||
  42.             strpos($element, "7") !== false ||
  43.             strpos($element, "8") !== false ||
  44.             strpos($element, "9") !== false))
  45.         {
  46.             return false;
  47.         }
  48.  
  49.         return true;
  50.     });
  51.  
  52.     // Sort by length decreasing
  53.     usort($tokens, function($a, $b)
  54.     {
  55.         return strlen($b) - strlen($a);
  56.     });
  57.  
  58.     // Loop through tokens
  59.     for ($j = 0; $j < count($tokens); $j++)
  60.     {
  61.         // For each try to find ALL candidate bottle records, from longest name
  62.         $query = "SELECT * FROM bottle WHERE LOWER(name) LIKE LOWER('" . $db->EscapeString($tokens[$j]) . "')";
  63.         if (isset($record->size))
  64.         {
  65.             $query .= " AND size = '" . $db->EscapeString($size) . "'";
  66.         }
  67.         $query .= " ORDER BY name DESC ";
  68.  
  69.         $result = $db->Query($query);
  70.         if ($result == false)
  71.         {
  72.             continue;
  73.         }
  74.         else
  75.         {
  76.             // In case of such records, attempt to do insensitive tokenized compare of record name against candidate name,
  77.             // this requires record name to have all tokens candidate name has (but can have more - like L.E., 45%, etc.)
  78.             $numRows = $db->GetRowsCount($result);
  79.  
  80.             for ($k = 0; $k < $numRows; $k++)
  81.             {
  82.                 $row = $db->GetRow($result, $k);
  83.  
  84.                 $candidateId = $row['id'];
  85.                 $candidateName = $row['name'];
  86.  
  87.                 if (StringUtil::InsensitiveTokenizedCompare($candidateName, $record->name) == true)
  88.                 {
  89.                     // We have a match, store bottle and break out
  90.                     $bottleId = $candidateId;
  91.                     break;
  92.                 }
  93.             }
  94.         }
  95.  
  96.         // In case of any match, break the loop
  97.         if ($bottleId > 0)
  98.         {
  99.             break;
  100.         }
  101.     }
  102.  
  103.     // In case we don't find any match, create a new bottle
  104.     if ($bottleId == 0 && $record->auction == 0)
  105.     {
  106.         $bottle->bottler_id = 0;
  107.         $bottleId = $bottle->Create();
  108.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement