From 8efec57abde97617421b8e70c237aacdc311e722 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Thu, 21 May 2020 15:49:48 -0500 Subject: [PATCH] Sort collections by their sequence number --- lib/Collection.php | 1 + lib/Ebook.php | 6 +++++- lib/Library.php | 11 +++++++++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/Collection.php b/lib/Collection.php index 02cdc95e..857b3d05 100644 --- a/lib/Collection.php +++ b/lib/Collection.php @@ -2,6 +2,7 @@ class Collection{ public $Name; public $Url; + public $SequenceNumber = null; public function __construct(string $name){ $this->Name = $name; diff --git a/lib/Ebook.php b/lib/Ebook.php index 9b0206b7..4d1559e5 100644 --- a/lib/Ebook.php +++ b/lib/Ebook.php @@ -166,7 +166,11 @@ class Ebook{ // Get SE collections foreach($xml->xpath('/package/metadata/meta[@property="belongs-to-collection"]') ?: [] as $collection){ - $this->Collections[] = new Collection($collection); + $c = new Collection($collection); + foreach($xml->xpath('/package/metadata/meta[@refines="#' . $collection->attributes()->id . '"][@property="group-position"]') ?: [] as $s){ + $c->SequenceNumber = (int)$s; + } + $this->Collections[] = $c; } // Get LoC tags diff --git a/lib/Library.php b/lib/Library.php index 9a0ac837..e6e6f9b3 100644 --- a/lib/Library.php +++ b/lib/Library.php @@ -168,7 +168,12 @@ class Library{ $collections[$lcCollection] = []; } - $collections[$lcCollection][] = $ebook; + if($collection->SequenceNumber !== null){ + $collections[$lcCollection][$collection->SequenceNumber] = $ebook; + } + else{ + $collections[$lcCollection][] = $ebook; + } } // Create the tags cache @@ -266,7 +271,9 @@ class Library{ // Now store various collections apcu_delete(new APCUIterator('/^collection-/')); foreach($collections as $collection => $ebooks){ - apcu_store('collection-' . $collection, $ebooks); + // Sort the array by key, then reindex to 0 with array_values + ksort($ebooks); + apcu_store('collection-' . $collection, array_values($ebooks)); } apcu_delete(new APCUIterator('/^tag-/'));