Skip to content

Commit

Permalink
Stacked 14: Allow to chose the photo layout per album (#2575)
Browse files Browse the repository at this point in the history
* Stacked 15: Show owner instead of just Shared albums (#2574)
* Stacked 16: Composer update + fixing phpstan / Ts checker (#2580)
* Stacked 17: Various fixes. (#2581)

---------

Co-authored-by: Martin Stone <1611702+d7415@users.noreply.github.com>
  • Loading branch information
ildyria and d7415 authored Oct 25, 2024
1 parent d0ed437 commit f63b586
Show file tree
Hide file tree
Showing 105 changed files with 942 additions and 621 deletions.
8 changes: 4 additions & 4 deletions app/Actions/Album/ListAlbums.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public function do(?int $lft, ?int $rgt, ?string $parent_id): array
$query = (new SortingDecorator($unfiltered))
->orderBy($sorting->column, $sorting->order);

/** @var NsCollection<int,string,Album> $albums */
/** @var NsCollection<Album> $albums */
$albums = $query->get();
/** @var NsCollection<int,string,Album> $tree */
/** @var NsCollection<Album> $tree */
$tree = $albums->toTree(null);

$flat_tree = $this->flatten($tree);
Expand All @@ -66,8 +66,8 @@ public function do(?int $lft, ?int $rgt, ?string $parent_id): array
/**
* Flatten the tree and create bread crumb paths.
*
* @param NsCollection<int,string,Album>|Collection<int,Album> $collection
* @param string $prefix
* @param NsCollection<Album>|Collection<int,Album> $collection
* @param string $prefix
*
* @return TAlbumSaved[]
*/
Expand Down
1 change: 1 addition & 0 deletions app/Actions/Album/PositionData.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function get(AbstractAlbum $album, bool $includeSubAlbums = false): Posit
$album->all_photos() :
$album->photos();

// @phpstan-ignore-next-line
$photoRelation
->with([
'album' => function (BelongsTo $b) {
Expand Down
13 changes: 13 additions & 0 deletions app/Contracts/Http/Requests/HasPhotoLayout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Contracts\Http\Requests;

use App\Enum\PhotoLayoutType;

interface HasPhotoLayout
{
/**
* @return PhotoLayoutType|null
*/
public function photoLayout(): ?PhotoLayoutType;
}
1 change: 1 addition & 0 deletions app/Contracts/Http/Requests/RequestAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class RequestAttribute
public const PHOTO_SORTING_ORDER_ATTRIBUTE = 'photo_sorting_order';
public const ALBUM_SORTING_COLUMN_ATTRIBUTE = 'album_sorting_column';
public const ALBUM_SORTING_ORDER_ATTRIBUTE = 'album_sorting_order';
public const ALBUM_PHOTO_LAYOUT = 'photo_layout';

public const PERMISSION_ID = 'perm_id';
public const IS_COMPACT_ATTRIBUTE = 'is_compact';
Expand Down
3 changes: 2 additions & 1 deletion app/Contracts/Models/AbstractAlbum.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Models\Photo;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;

/**
Expand All @@ -31,7 +32,7 @@
interface AbstractAlbum
{
/**
* @return Relation<Photo>|Builder<Photo>
* @return Relation<Photo,AbstractAlbum&Model,Collection<int,Photo>>|Builder<Photo>
*/
public function photos(): Relation|Builder;

Expand Down
16 changes: 8 additions & 8 deletions app/Eloquent/FixedQueryBuilderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace App\Eloquent;

use App\Exceptions\Internal\QueryBuilderException;
use Illuminate\Contracts\Database\Query\Expression;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Builder as BaseBuilder;
use Illuminate\Database\Query\Expression;

/**
* Fixed Eloquent query builder.
Expand Down Expand Up @@ -50,10 +50,10 @@ trait FixedQueryBuilderTrait
/**
* Add a basic where clause to the query.
*
* @param \Closure|string|array<string>|Expression $column
* @param mixed $operator
* @param mixed $value
* @param string $boolean
* @param \Closure|string|array<int|string,mixed>|Expression $column
* @param mixed $operator
* @param mixed $value
* @param string $boolean
*
* @return $this
*
Expand Down Expand Up @@ -268,9 +268,9 @@ public function addSelect($column): static
/**
* Add an "or where" clause to the query.
*
* @param \Closure|array<string>|string|Expression $column
* @param mixed $operator
* @param mixed $value
* @param \Closure|string|array<int|string,mixed>|Expression $column
* @param mixed $operator
* @param mixed $value
*
* @return $this
*
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Admin/Maintenance/Model/Album.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
* This is necessary to fix the tree of the album model.
* We do not want to have to deal with the relationships here.
*
* @implements Node<string,Album>
* @implements Node<Album>
*/
class Album extends Model implements Node
{
/** @phpstan-use NodeTrait<string,Album> */
/** @phpstan-use NodeTrait<Album> */
use NodeTrait;
public $timestamps = false;
}
2 changes: 2 additions & 0 deletions app/Http/Controllers/Gallery/AlbumController.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public function updateAlbum(UpdateAlbumRequest $request, SetHeader $setHeader):
$album->copyright = $request->copyright();
$album->photo_sorting = $request->photoSortingCriterion();
$album->album_sorting = $request->albumSortingCriterion();
$album->photo_layout = $request->photoLayout();

$album = $setHeader->do(
album: $album,
Expand All @@ -131,6 +132,7 @@ public function updateTagAlbum(UpdateTagAlbumRequest $request): EditableBaseAlbu
$album->show_tags = $request->tags();
$album->copyright = $request->copyright();
$album->photo_sorting = $request->photoSortingCriterion();
$album->photo_layout = $request->photoLayout();
$album->save();

return EditableBaseAlbumResource::fromModel($album);
Expand Down
8 changes: 7 additions & 1 deletion app/Http/Requests/Album/UpdateAlbumRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Contracts\Http\Requests\HasDescription;
use App\Contracts\Http\Requests\HasLicense;
use App\Contracts\Http\Requests\HasPhoto;
use App\Contracts\Http\Requests\HasPhotoLayout;
use App\Contracts\Http\Requests\HasPhotoSortingCriterion;
use App\Contracts\Http\Requests\HasTitle;
use App\Contracts\Http\Requests\RequestAttribute;
Expand All @@ -20,6 +21,7 @@
use App\Enum\ColumnSortingPhotoType;
use App\Enum\LicenseType;
use App\Enum\OrderSortingType;
use App\Enum\PhotoLayoutType;
use App\Http\Requests\BaseApiRequest;
use App\Http\Requests\Traits\HasAlbumSortingCriterionTrait;
use App\Http\Requests\Traits\HasAlbumTrait;
Expand All @@ -28,6 +30,7 @@
use App\Http\Requests\Traits\HasCopyrightTrait;
use App\Http\Requests\Traits\HasDescriptionTrait;
use App\Http\Requests\Traits\HasLicenseTrait;
use App\Http\Requests\Traits\HasPhotoLayoutTrait;
use App\Http\Requests\Traits\HasPhotoSortingCriterionTrait;
use App\Http\Requests\Traits\HasPhotoTrait;
use App\Http\Requests\Traits\HasTitleTrait;
Expand All @@ -42,7 +45,7 @@
use Illuminate\Validation\Rules\Enum;
use Illuminate\Validation\ValidationException;

class UpdateAlbumRequest extends BaseApiRequest implements HasAlbum, HasTitle, HasDescription, HasLicense, HasPhotoSortingCriterion, HasAlbumSortingCriterion, HasCopyright, HasPhoto, HasCompactBoolean
class UpdateAlbumRequest extends BaseApiRequest implements HasAlbum, HasTitle, HasDescription, HasLicense, HasPhotoSortingCriterion, HasAlbumSortingCriterion, HasCopyright, HasPhoto, HasCompactBoolean, HasPhotoLayout
{
use HasAlbumTrait;
use HasLicenseTrait;
Expand All @@ -54,6 +57,7 @@ class UpdateAlbumRequest extends BaseApiRequest implements HasAlbum, HasTitle, H
use HasPhotoSortingCriterionTrait;
use HasAlbumSortingCriterionTrait;
use HasCopyrightTrait;
use HasPhotoLayoutTrait;

public function authorize(): bool
{
Expand Down Expand Up @@ -84,6 +88,7 @@ public function rules(): array
'nullable', new Enum(OrderSortingType::class),
],
RequestAttribute::ALBUM_ASPECT_RATIO_ATTRIBUTE => ['present', 'nullable', new Enum(AspectRatioType::class)],
RequestAttribute::ALBUM_PHOTO_LAYOUT => ['present', 'nullable', new Enum(PhotoLayoutType::class)],
RequestAttribute::COPYRIGHT_ATTRIBUTE => ['present', 'nullable', new CopyrightRule()],
RequestAttribute::IS_COMPACT_ATTRIBUTE => ['required', 'boolean'],
RequestAttribute::HEADER_ID_ATTRIBUTE => ['present', new RandomIDRule(true)],
Expand Down Expand Up @@ -123,6 +128,7 @@ protected function processValidatedValues(array $values, array $files): void
new AlbumSortingCriterion($albumColumn->toColumnSortingType(), $albumOrder);

$this->aspectRatio = AspectRatioType::tryFrom($values[RequestAttribute::ALBUM_ASPECT_RATIO_ATTRIBUTE]);
$this->photoLayout = PhotoLayoutType::tryFrom($values[RequestAttribute::ALBUM_PHOTO_LAYOUT]);
$this->copyright = $values[RequestAttribute::COPYRIGHT_ATTRIBUTE];

$this->is_compact = static::toBoolean($values[RequestAttribute::IS_COMPACT_ATTRIBUTE]);
Expand Down
8 changes: 7 additions & 1 deletion app/Http/Requests/Album/UpdateTagAlbumRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Contracts\Http\Requests\HasCopyright;
use App\Contracts\Http\Requests\HasDescription;
use App\Contracts\Http\Requests\HasPhotoLayout;
use App\Contracts\Http\Requests\HasPhotoSortingCriterion;
use App\Contracts\Http\Requests\HasTagAlbum;
use App\Contracts\Http\Requests\HasTags;
Expand All @@ -12,10 +13,12 @@
use App\DTO\PhotoSortingCriterion;
use App\Enum\ColumnSortingPhotoType;
use App\Enum\OrderSortingType;
use App\Enum\PhotoLayoutType;
use App\Http\Requests\BaseApiRequest;
use App\Http\Requests\Traits\Authorize\AuthorizeCanEditAlbumTrait;
use App\Http\Requests\Traits\HasCopyrightTrait;
use App\Http\Requests\Traits\HasDescriptionTrait;
use App\Http\Requests\Traits\HasPhotoLayoutTrait;
use App\Http\Requests\Traits\HasPhotoSortingCriterionTrait;
use App\Http\Requests\Traits\HasTagAlbumTrait;
use App\Http\Requests\Traits\HasTagsTrait;
Expand All @@ -28,14 +31,15 @@
use Illuminate\Validation\Rules\Enum;
use Illuminate\Validation\ValidationException;

class UpdateTagAlbumRequest extends BaseApiRequest implements HasTagAlbum, HasTitle, HasDescription, HasPhotoSortingCriterion, HasCopyright, HasTags
class UpdateTagAlbumRequest extends BaseApiRequest implements HasTagAlbum, HasTitle, HasDescription, HasPhotoSortingCriterion, HasCopyright, HasTags, HasPhotoLayout
{
use HasTagAlbumTrait;
use HasTitleTrait;
use HasDescriptionTrait;
use HasPhotoSortingCriterionTrait;
use HasCopyrightTrait;
use HasTagsTrait;
use HasPhotoLayoutTrait;
use AuthorizeCanEditAlbumTrait;

/**
Expand All @@ -55,6 +59,7 @@ public function rules(): array
RequestAttribute::TAGS_ATTRIBUTE => 'required|array|min:1',
RequestAttribute::TAGS_ATTRIBUTE . '.*' => 'required|string|min:1',
RequestAttribute::COPYRIGHT_ATTRIBUTE => ['present', 'nullable', new CopyrightRule()],
RequestAttribute::ALBUM_PHOTO_LAYOUT => ['present', 'nullable', new Enum(PhotoLayoutType::class)],
];
}

Expand Down Expand Up @@ -82,6 +87,7 @@ protected function processValidatedValues(array $values, array $files): void
null :
new PhotoSortingCriterion($photoColumn->toColumnSortingType(), $photoOrder);

$this->photoLayout = PhotoLayoutType::tryFrom($values[RequestAttribute::ALBUM_PHOTO_LAYOUT]);
$this->copyright = $values[RequestAttribute::COPYRIGHT_ATTRIBUTE];
$this->tags = $values[RequestAttribute::TAGS_ATTRIBUTE];
}
Expand Down
18 changes: 18 additions & 0 deletions app/Http/Requests/Traits/HasPhotoLayoutTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Http\Requests\Traits;

use App\Enum\PhotoLayoutType;

trait HasPhotoLayoutTrait
{
protected ?PhotoLayoutType $photoLayout = null;

/**
* @return PhotoLayoutType|null
*/
public function photoLayout(): ?PhotoLayoutType
{
return $this->photoLayout;
}
}
3 changes: 3 additions & 0 deletions app/Http/Resources/Editable/EditableBaseAlbumResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\DTO\PhotoSortingCriterion;
use App\Enum\AspectRatioType;
use App\Enum\LicenseType;
use App\Enum\PhotoLayoutType;
use App\Models\Album;
use App\Models\TagAlbum;
use Spatie\LaravelData\Data;
Expand All @@ -22,6 +23,7 @@ class EditableBaseAlbumResource extends Data
public ?PhotoSortingCriterion $photo_sorting;
public ?AlbumSortingCriterion $album_sorting;
public ?AspectRatioType $aspect_ratio;
public ?PhotoLayoutType $photo_layout;
public ?string $header_id;
public ?string $cover_id;
/** @var string[] */
Expand All @@ -40,6 +42,7 @@ public function __construct(Album|TagAlbum $album)
$this->album_sorting = null;
$this->header_id = null;
$this->cover_id = null;
$this->photo_layout = $album->photo_layout;

if ($album instanceof Album) {
$this->is_model_album = true;
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Resources/GalleryConfigs/AlbumConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Contracts\Models\AbstractAlbum;
use App\Enum\AspectRatioCSSType;
use App\Enum\AspectRatioType;
use App\Enum\PhotoLayoutType;
use App\Models\Album;
use App\Models\Configs;
use App\Models\Extensions\BaseAlbum;
Expand All @@ -26,6 +27,7 @@ class AlbumConfig extends Data
public bool $is_search_accessible;
public bool $is_nsfw_warning_visible;
public AspectRatioCSSType $album_thumb_css_aspect_ratio;
public PhotoLayoutType $photo_layout;

public function __construct(AbstractAlbum $album)
{
Expand All @@ -49,6 +51,8 @@ public function __construct(AbstractAlbum $album)
} else {
$this->album_thumb_css_aspect_ratio = Configs::getValueAsEnum('default_album_thumb_aspect_ratio', AspectRatioType::class)->css();
}

$this->photo_layout = (($album instanceof BaseAlbum) ? $album->photo_layout : null) ?? Configs::getValueAsEnum('layout', PhotoLayoutType::class);
}

public function setIsMapAccessible(): void
Expand Down
3 changes: 0 additions & 3 deletions app/Http/Resources/GalleryConfigs/PhotoLayoutConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

namespace App\Http\Resources\GalleryConfigs;

use App\Enum\PhotoLayoutType;
use App\Models\Configs;
use Spatie\LaravelData\Data;
use Spatie\TypeScriptTransformer\Attributes\TypeScript;

#[TypeScript()]
class PhotoLayoutConfig extends Data
{
public PhotoLayoutType $photos_layout;
public int $photo_layout_justified_row_height;
public int $photo_layout_masonry_column_width;
public int $photo_layout_grid_column_width;
Expand All @@ -24,6 +22,5 @@ public function __construct()
$this->photo_layout_grid_column_width = Configs::getValueAsInt('photo_layout_grid_column_width');
$this->photo_layout_square_column_width = Configs::getValueAsInt('photo_layout_square_column_width');
$this->photo_layout_gap = Configs::getValueAsInt('photo_layout_gap');
$this->photos_layout = Configs::getValueAsEnum('layout', PhotoLayoutType::class);
}
}
2 changes: 2 additions & 0 deletions app/Http/Resources/Models/ThumbAlbumResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ThumbAlbumResource extends Data
private ?string $min_taken_at = null;
private ?string $max_taken_at = null;
public ?string $formatted_min_max = null;
public ?string $owner = null;

public AlbumRightsResource $rights;

Expand All @@ -60,6 +61,7 @@ public function __construct(AbstractAlbum $data)
$this->created_at = $data->created_at->format($date_format);
$policy = AlbumProtectionPolicy::ofBaseAlbum($data);
$this->description = Str::limit($data->description, 100);
$this->owner = $data->owner->username;
}

if ($data instanceof Album) {
Expand Down
8 changes: 4 additions & 4 deletions app/Legacy/V1/Actions/Albums/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public function get(): AlbumForestResource
}
$query->orderBy($this->sorting->column, $this->sorting->order);

/** @var NsCollection<int,string,Album> $albums */
/** @var NsCollection<Album> $albums */
$albums = $query->get();
/** @var ?NsCollection<int,string,Album> $sharedAlbums */
/** @var ?NsCollection<Album> $sharedAlbums */
$sharedAlbums = null;
$userID = Auth::id();
if ($userID !== null) {
Expand All @@ -75,8 +75,8 @@ public function get(): AlbumForestResource
// (sub)-tree and then `toTree` will return garbage as it does
// not find connected paths within `$albums` or `$sharedAlbums`,
// resp.
/** @var NsCollection<int,string,Album> $albums */
/** @var ?NsCollection<int,string,Album> $sharedAlbums */
/** @var NsCollection<Album> $albums */
/** @var ?NsCollection<Album> $sharedAlbums */
list($albums, $sharedAlbums) = $albums->partition(fn (Album $album) => $album->owner_id === $userID);
}

Expand Down
Loading

0 comments on commit f63b586

Please sign in to comment.