From 57c137ac49f640434db350cee323bca38cb2f816 Mon Sep 17 00:00:00 2001 From: ghost Date: Sat, 25 Nov 2023 13:53:40 +0200 Subject: [PATCH] add totals placeholder --- src/webui/index.php | 42 +++++++++++++++++++++++++++++++++++++++--- src/webui/search.php | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/src/webui/index.php b/src/webui/index.php index aff63dd..ff3c89a 100644 --- a/src/webui/index.php +++ b/src/webui/index.php @@ -8,6 +8,13 @@ error_reporting(E_ALL); // Load dependencies require_once __DIR__ . '/../../vendor/autoload.php'; +function plural(int $number, array $texts) +{ + $cases = [2, 0, 1, 1, 1, 2]; + + return $texts[(($number % 100) > 4 && ($number % 100) < 20) ? 2 : $cases[min($number % 10, 5)]]; +} + // Init config $config = json_decode( file_get_contents( @@ -15,7 +22,7 @@ $config = json_decode( ) ); -/* @TODO show totals in placeholder +// Show totals in placeholder // Init $client = new \Manticoresearch\Client( @@ -30,7 +37,36 @@ $index = $client->index( $config->manticore->index->document ); -*/ +// Get totals +$total = $index->search('') + ->option('cutoff', 0) + ->limit(0) + ->get() + ->getTotal(); + +$placeholder = plural( + $total, + [ + sprintf( + _('Over %s page or enter the new one...'), + number_format( + $total + ) + ), + sprintf( + _('Over %s pages or enter the new one...'), + number_format( + $total + ) + ), + sprintf( + _('Over %s pages or enter the new one...'), + number_format( + $total + ) + ), + ] +); ?> @@ -261,7 +297,7 @@ $index = $client->index(

- +
diff --git a/src/webui/search.php b/src/webui/search.php index 2f16900..801337f 100644 --- a/src/webui/search.php +++ b/src/webui/search.php @@ -8,6 +8,13 @@ error_reporting(E_ALL); // Load dependencies require_once __DIR__ . '/../../vendor/autoload.php'; +function plural(int $number, array $texts) +{ + $cases = [2, 0, 1, 1, 1, 2]; + + return $texts[(($number % 100) > 4 && ($number % 100) < 20) ? 2 : $cases[min($number % 10, 5)]]; +} + // Init config $config = json_decode( file_get_contents( @@ -28,6 +35,37 @@ $index = $client->index( $config->manticore->index->document ); +// Get totals +$total = $index->search('') + ->option('cutoff', 0) + ->limit(0) + ->get() + ->getTotal(); + +$placeholder = plural( + $total, + [ + sprintf( + _('Over %s page or enter the new one...'), + number_format( + $total + ) + ), + sprintf( + _('Over %s pages or enter the new one...'), + number_format( + $total + ) + ), + sprintf( + _('Over %s pages or enter the new one...'), + number_format( + $total + ) + ), + ] +); + // Request $q = !empty($_GET['q']) ? $_GET['q'] : ''; $p = !empty($_GET['p']) ? (int) $_GET['p'] : 1; @@ -214,7 +252,7 @@ $results = $index->search($q)

- +