From 62b8c853f519c8cb45a7b61782cb82bd92eb41ed Mon Sep 17 00:00:00 2001
From: Erik Thuning <boooink@gmail.com>
Date: Thu, 14 Nov 2024 15:44:04 +0100
Subject: [PATCH] Products now get sorted in alphabetical name order in most
 (all?) views

---
 include/Inventory.php | 2 ++
 include/Page.php      | 3 +++
 include/Product.php   | 5 +++++
 3 files changed, 10 insertions(+)

diff --git a/include/Inventory.php b/include/Inventory.php
index 09fb001..eb6a33b 100644
--- a/include/Inventory.php
+++ b/include/Inventory.php
@@ -105,6 +105,7 @@ class Inventory {
         foreach($this->seen_products as $prodid) {
             $out[] = new Product($prodid);
         }
+        usort($out, ['Product', 'compare']);
         return $out;
     }
 
@@ -146,6 +147,7 @@ class Inventory {
                 $out[] = $product;
             }
         }
+        usort($out, ['Product', 'compare']);
         return $out;
     }
 }
diff --git a/include/Page.php b/include/Page.php
index 66b0d71..c4227ec 100644
--- a/include/Page.php
+++ b/include/Page.php
@@ -133,6 +133,8 @@ abstract class Page extends Responder {
     }
 
     final protected function build_product_table($products) {
+        usort($products, ['Product', 'compare']);
+
         $rows = '';
         foreach($products as $product) {
             $rows .= $this->build_product_row($product);
@@ -305,6 +307,7 @@ abstract class Page extends Responder {
                     $unseen[] = $product;
                 }
             }
+            usort($unseen, ['Product', 'compare']);
         } else {
             $unseen = $inventory->get_unseen_products();
             $total_count = count($unseen) + count($seen);
diff --git a/include/Product.php b/include/Product.php
index 461a73c..9d0d4ac 100644
--- a/include/Product.php
+++ b/include/Product.php
@@ -42,6 +42,11 @@ class Product extends Entity {
         }
     }
 
+    public static function sort($prod1, $prod2) {
+        return strcmp(strtolower($prod1->get_name()),
+                      strtolower($prod2->get_name()));
+    }
+
     public function __construct($clue, $type = 'id') {
         parent::__construct();
         $search = null;