Skip to content

Commit

Permalink
Fix sub-albums sorting not being respected per album (#2630)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
ildyria and d7415 authored Oct 30, 2024
1 parent e7bee3c commit 863eafe
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
20 changes: 20 additions & 0 deletions app/Assets/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Assets;

use App\Exceptions\Internal\ZeroModuloException;
use Exception;
use Illuminate\Support\Facades\File;
use function Safe\ini_get;

Expand Down Expand Up @@ -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());
}
}
10 changes: 10 additions & 0 deletions app/Models/Album.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
13 changes: 8 additions & 5 deletions app/Relations/HasManyChildAlbums.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,7 +18,6 @@
class HasManyChildAlbums extends HasManyBidirectionally
{
protected AlbumQueryPolicy $albumQueryPolicy;
private AlbumSortingCriterion $sorting;

public function __construct(Album $owningAlbum)
{
Expand All @@ -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,
Expand Down Expand Up @@ -85,11 +83,15 @@ public function getResults(): Collection
return $this->related->newCollection();
}

$albumSorting = $this->getParent()->getEffectiveAlbumSorting();

/** @var SortingDecorator<Album> */
$sortingDecorator = new SortingDecorator($this->query);

return $sortingDecorator
->orderBy($this->sorting->column, $this->sorting->order)
->orderBy(
$albumSorting->column,
$albumSorting->order)
->get();
}

Expand All @@ -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<int,Album> $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
Expand Down

0 comments on commit 863eafe

Please sign in to comment.