From b967de0fef7828e7cf314104ddd147394efb0060 Mon Sep 17 00:00:00 2001
From: Iain Patterson <me@iain.cx>
Date: Mon, 13 May 2013 06:12:27 -0400
Subject: [PATCH] Filter area and city searches.

Only show areas or city in dropdown for searches if there are
contacts, hubs etc in that area or city.
---
 lib/area.php     |  9 ++++++++-
 lib/contact.php  | 18 ++++++++++++++++--
 lib/donation.php | 19 +++++++++++++++++--
 lib/hub.php      | 18 ++++++++++++++++--
 lib/order.php    | 18 ++++++++++++++++--
 5 files changed, 73 insertions(+), 9 deletions(-)

diff --git a/lib/area.php b/lib/area.php
index 51f57d3..2aa5e2d 100644
--- a/lib/area.php
+++ b/lib/area.php
@@ -53,10 +53,17 @@
       return;
     }
 
+    $candidates = array();
+    foreach ($cities as $city) {
+      if (! count(get_city_areas($city->getId()))) continue;
+      $candidates[] = $city;
+    }
+    if (! count($candidates)) return;
+
     form("noprint standout");
     echo "<p>Show areas in\n";
     echo "<select name=\"city_id\">\n";
-    foreach ($cities as $city) {
+    foreach ($candidates as $city) {
       option("city_id", sprintf("%s/%s", $city->getName(), $city->getId()), get_city_displayname($city));
     }
     echo "</select>\n";
diff --git a/lib/contact.php b/lib/contact.php
index c457f4b..b33c97a 100644
--- a/lib/contact.php
+++ b/lib/contact.php
@@ -129,9 +129,16 @@
       return;
     }
 
+    $candidates = array();
+    foreach ($areas as $area) {
+      if (! count(get_area_contacts($area->getId()))) continue;
+      $candidates[] = $area;
+    }
+    if (! count($candidates)) return;
+
     echo "<p>Show contacts in area\n";
     echo "<select name=\"area_id\">\n";
-    foreach ($areas as $area) {
+    foreach ($candidates as $area) {
       option("area_id", $area->getId(), get_area_displayname($area));
     }
     echo "</select>\n";
@@ -147,9 +154,16 @@
       return;
     }
 
+    $candidates = array();
+    foreach ($cities as $city) {
+      if (! count(get_city_contacts($city->getId()))) continue;
+      $candidates[] = $city;
+    }
+    if (! count($candidates)) return;
+
     echo "<p>Show contacts in city\n";
     echo "<select name=\"city_id\">\n";
-    foreach ($cities as $city) {
+    foreach ($candidates as $city) {
       option("city_id", $city->getId(), get_city_displayname($city), $city_id);
     }
     echo "</select>\n";
diff --git a/lib/donation.php b/lib/donation.php
index e9c75da..be3710c 100644
--- a/lib/donation.php
+++ b/lib/donation.php
@@ -127,9 +127,17 @@
       return;
     }
 
+    form("noprint standout");
+    $candidates = array();
+    foreach ($areas as $area) {
+      if (! count(get_area_hubs($area->getId()))) continue;
+      $candidates[] = $area;
+    }
+    if (! count($candidates)) return;
+
     echo "<p>Show donations in area\n";
     echo "<select name=\"area_id\">\n";
-    foreach ($areas as $area) {
+    foreach ($candidates as $area) {
       option("area_id", $area->getId(), get_area_displayname($area));
     }
     echo "</select>\n";
@@ -145,9 +153,16 @@
       return;
     }
 
+    $candidates = array();
+    foreach ($cities as $city) {
+      if (! count(get_city_hubs($city->getId()))) continue;
+      $candidates[] = $city;
+    }
+    if (! count($candidates)) return;
+
     echo "<p>Show donations in city\n";
     echo "<select name=\"city_id\">\n";
-    foreach ($cities as $city) {
+    foreach ($candidates as $city) {
       option("city_id", $city->getId(), get_city_displayname($city), $city_id);
     }
     echo "</select>\n";
diff --git a/lib/hub.php b/lib/hub.php
index 79051f8..4e34c17 100644
--- a/lib/hub.php
+++ b/lib/hub.php
@@ -108,9 +108,16 @@
       return;
     }
 
+    $candidates = array();
+    foreach ($areas as $area) {
+      if (! count(get_area_hubs($area->getId()))) continue;
+      $candidates[] = $area;
+    }
+    if (! count($candidates)) return;
+
     echo "<p>Show hubs in area\n";
     echo "<select name=\"area_id\">\n";
-    foreach ($areas as $area) {
+    foreach ($candidates as $area) {
       option("area_id", $area->getId(), get_area_displayname($area));
     }
     echo "</select>\n";
@@ -126,9 +133,16 @@
       return;
     }
 
+    $candidates = array();
+    foreach ($cities as $city) {
+      if (! count(get_city_hubs($city->getId()))) continue;
+      $candidates[] = $city;
+    }
+    if (! count($candidates)) return;
+
     echo "<p>Show hubs in city\n";
     echo "<select name=\"city_id\">\n";
-    foreach ($cities as $city) {
+    foreach ($candidates as $city) {
       option("city_id", $city->getId(), get_city_displayname($city), $city_id);
     }
     echo "</select>\n";
diff --git a/lib/order.php b/lib/order.php
index 2eae9e0..d48f214 100644
--- a/lib/order.php
+++ b/lib/order.php
@@ -167,9 +167,16 @@
       return;
     }
 
+    $candidates = array();
+    foreach ($areas as $area) {
+      if (! count(get_area_contacts($area->getId()))) continue;
+      $candidates[] = $area;
+    }
+    if (! count($candidates)) return;
+
     echo "<p>Show orders in area\n";
     echo "<select name=\"area_id\">\n";
-    foreach ($areas as $area) {
+    foreach ($candidates as $area) {
       option("area_id", $area->getId(), get_area_displayname($area));
     }
     echo "</select>\n";
@@ -185,9 +192,16 @@
       return;
     }
 
+    $candidates = array();
+    foreach ($cities as $city) {
+      if (! count(get_city_contacts($city->getId()))) continue;
+      $candidates[] = $city;
+    }
+    if (! count($candidates)) return;
+
     echo "<p>Show orders in city\n";
     echo "<select name=\"city_id\">\n";
-    foreach ($cities as $city) {
+    foreach ($candidates as $city) {
       option("city_id", $city->getId(), get_city_displayname($city), $city_id);
     }
     echo "</select>\n";
-- 
2.20.1