From 863eafe6c24679affbeef352edfeba98a79b9e2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Viguier?= Date: Wed, 30 Oct 2024 22:44:30 +0100 Subject: [PATCH] Fix sub-albums sorting not being respected per album (#2630) * fix sub-albums sorting not being respected per album * Update app/Models/Album.php Co-authored-by: Martin Stone <1611702+d7415@users.noreply.github.com> --------- Co-authored-by: Martin Stone <1611702+d7415@users.noreply.github.com> --- app/Assets/Helpers.php | 20 ++++++++++++++++++++ app/Models/Album.php | 10 ++++++++++ app/Relations/HasManyChildAlbums.php | 13 ++++++++----- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/app/Assets/Helpers.php b/app/Assets/Helpers.php index 331a8205919..956306182d7 100644 --- a/app/Assets/Helpers.php +++ b/app/Assets/Helpers.php @@ -3,6 +3,7 @@ namespace App\Assets; use App\Exceptions\Internal\ZeroModuloException; +use Exception; use Illuminate\Support\Facades\File; use function Safe\ini_get; @@ -261,4 +262,23 @@ public function censor(string $string, float $percentOfClear = 0.5): string return substr_replace($string, $replacement, $start, $censored_length); } + + /** + * Format exception trace as text. + * + * @param \Exception $e + * + * @return string + * + * @codeCoverageIgnore + */ + public function exceptionTraceToText(\Exception $e): string + { + $renderer = new ArrayToTextTable(); + + return $renderer->getTable(collect($e->getTrace())->map(fn (array $err) => [ + 'class' => $err['class'] ?? $err['file'] ?? '?', + 'line' => $err['line'] ?? '?', + 'function' => $err['function']])->all()); + } } diff --git a/app/Models/Album.php b/app/Models/Album.php index a48e894d7a0..18d2bddf9d7 100644 --- a/app/Models/Album.php +++ b/app/Models/Album.php @@ -462,4 +462,14 @@ protected function setAlbumSortingAttribute(?AlbumSortingCriterion $sorting): vo $this->attributes['album_sorting_col'] = $sorting?->column->value; $this->attributes['album_sorting_order'] = $sorting?->order->value; } + + /** + * Returns the criterion acc. to which **albums** inside the album shall be sorted. + * + * @return AlbumSortingCriterion + */ + public function getEffectiveAlbumSorting(): AlbumSortingCriterion + { + return $this->getAlbumSortingAttribute() ?? AlbumSortingCriterion::createDefault(); + } } diff --git a/app/Relations/HasManyChildAlbums.php b/app/Relations/HasManyChildAlbums.php index 129accd36d3..655ff4d8dfd 100644 --- a/app/Relations/HasManyChildAlbums.php +++ b/app/Relations/HasManyChildAlbums.php @@ -3,7 +3,6 @@ namespace App\Relations; use App\Contracts\Exceptions\InternalLycheeException; -use App\DTO\AlbumSortingCriterion; use App\Enum\OrderSortingType; use App\Exceptions\Internal\InvalidOrderDirectionException; use App\Models\Album; @@ -19,7 +18,6 @@ class HasManyChildAlbums extends HasManyBidirectionally { protected AlbumQueryPolicy $albumQueryPolicy; - private AlbumSortingCriterion $sorting; public function __construct(Album $owningAlbum) { @@ -28,7 +26,7 @@ public function __construct(Album $owningAlbum) // The parent constructor calls `addConstraints` and thus our own // attributes must be initialized by then $this->albumQueryPolicy = resolve(AlbumQueryPolicy::class); - $this->sorting = $owningAlbum->album_sorting ?? AlbumSortingCriterion::createDefault(); + parent::__construct( $owningAlbum->newQuery(), $owningAlbum, @@ -85,11 +83,15 @@ public function getResults(): Collection return $this->related->newCollection(); } + $albumSorting = $this->getParent()->getEffectiveAlbumSorting(); + /** @var SortingDecorator */ $sortingDecorator = new SortingDecorator($this->query); return $sortingDecorator - ->orderBy($this->sorting->column, $this->sorting->order) + ->orderBy( + $albumSorting->column, + $albumSorting->order) ->get(); } @@ -113,8 +115,9 @@ public function match(array $models, Collection $results, $relation): array if (isset($dictionary[$key = $this->getDictionaryKey($model->getAttribute($this->localKey))])) { /** @var Collection $childrenOfModel */ $childrenOfModel = $this->getRelationValue($dictionary, $key, 'many'); + $sorting = $model->getEffectiveAlbumSorting(); $childrenOfModel = $childrenOfModel - ->sortBy($this->sorting->column->value, SORT_NATURAL | SORT_FLAG_CASE, $this->sorting->order === OrderSortingType::DESC) + ->sortBy($sorting->column->value, SORT_NATURAL | SORT_FLAG_CASE, $sorting->order === OrderSortingType::DESC) ->values(); $model->setRelation($relation, $childrenOfModel); // This is the newly added code which sets this method apart