From 19a3387f2cae0411e591070ba728eedacea2335b Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Fri, 05 Jul 2019 22:27:13 +0000
Subject: [PATCH] Use CSS icon for geolocation button, wrap it in #menu

---
 stops.php |   27 +++++++++++++++++----------
 1 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/stops.php b/stops.php
index b9b0132..16eab82 100644
--- a/stops.php
+++ b/stops.php
@@ -8,16 +8,16 @@
 	if(empty($_GET['query'])) throw new UnexpectedValueException();
 	if(strlen($_GET['query']) > 50) throw new UnexpectedValueException();
 	
-	// Initialize DB connection an query
+	// Initialize a DB connection an a query
 	$pdo = new PDO('sqlite:stops/stops.db', NULL, NULL, array(
 		PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
 	));
-	$st = $pdo->prepare('SELECT DISTINCT id FROM stop_search WHERE word LIKE ?');
+	$st = $pdo->prepare('SELECT DISTINCT id FROM stop_search WHERE word LIKE ? AND id LIKE \'t%\'');
 	
 	// Split stop name into words
 	$words = split_stop_name($_GET['query']);
 	
-	// Find relevant stop IDs
+	// Find matching stops (their IDs)
 	$ids = NULL;
 	foreach($words as $word) {
 		if(empty($word)) continue;
@@ -27,10 +27,11 @@
 		$results = $st->fetchAll(PDO::FETCH_COLUMN);
 		$st->closeCursor();
 		
-		// Merge results with previous searches
 		if(is_array($ids)) {
+			// Merge results with list for previous words
 			$ids = array_intersect($ids, $results);
 		} else {
+			// First search - initialize results list
 			$ids = $results;
 		}
 		
@@ -38,26 +39,32 @@
 		if(count($ids) == 0) break;
 	}
 	
-	// Close DB connection
+	// Close a DB connection
 	unset($st, $pdo);
 	
-	// No query was executed
+	// No query was executed - return empty list
 	if(!is_array($ids)) throw new UnexpectedValueException();
 	
-	// Build structure for UI
+	// Build a structure for the UI
 	$stop_list = [];
+	$query_lower = mb_strtolower($_GET['query'], 'UTF-8');
 	foreach($ids as $id) {
 		$stop_list[] = [
 			'id' => $id,
 			'name' => $stops[$id],
 			'type' => 'stop',
-			'relevance' => similar_text($_GET['query'], $stops[$id])
+			'relevance' => similar_text(
+				$query_lower,
+				mb_strtolower($stops[$id], 'UTF-8')
+			)
 		];
 	}
 	
-	// Sort stops by relevence
+	// Sort stops by relevance
 	usort($stop_list, function($a, $b) {
-		return $b['relevance'] - $a['relevance'];
+		$rel = $b['relevance'] - $a['relevance'];
+		if($rel == 0) return strcasecmp($a['name'], $b['name']);
+		return $rel;
 	});
 	
 	// Return JSON

--
Gitblit v1.9.1