Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sub-albums sorting not being respected per album #2630

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used in the PR, but quite useful when debugging:

dd($var, Helpers::exceptionTraceToText(new Exception))

Will dump the value of $var in a 500 and followed by the trace of where the dump happened.
The front-end is already taking care of making it a nice visual :)

{
$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();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getAlbumSortingAttribute() is defined on line 448 of the same file.

}
}
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