|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * Copyright (c) 2017-2018 Tobias Reich |
| 6 | + * Copyright (c) 2018-2026 LycheeOrg. |
| 7 | + */ |
| 8 | + |
| 9 | +namespace App\Actions\Admin; |
| 10 | + |
| 11 | +use App\Actions\Album\SetProtectionPolicy; |
| 12 | +use App\DTO\BulkAlbumPatchData; |
| 13 | +use App\Http\Resources\Models\Utils\AlbumProtectionPolicy; |
| 14 | +use App\Models\Album; |
| 15 | +use App\Models\BaseAlbumImpl; |
| 16 | + |
| 17 | +/** |
| 18 | + * Applies a partial set of metadata and/or visibility changes to a batch of albums. |
| 19 | + * |
| 20 | + * Changes are applied within the caller's DB transaction. |
| 21 | + * Fields absent from the payload are left unchanged. |
| 22 | + * |
| 23 | + * Three groups are processed separately: |
| 24 | + * 1. base_albums columns → chunked mass UPDATE via BaseAlbumImpl |
| 25 | + * 2. albums columns → chunked mass UPDATE via Album |
| 26 | + * 3. Visibility fields → per-album via SetProtectionPolicy::do() |
| 27 | + */ |
| 28 | +class BulkEditAlbumsAction |
| 29 | +{ |
| 30 | + private SetProtectionPolicy $set_protection_policy; |
| 31 | + |
| 32 | + public function __construct() |
| 33 | + { |
| 34 | + $this->set_protection_policy = new SetProtectionPolicy(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Apply the partial payload to all specified album IDs. |
| 39 | + * |
| 40 | + * Only fields that were present in the original request (tracked via |
| 41 | + * {@see BulkAlbumPatchData::has()}) are updated; absent fields are left |
| 42 | + * unchanged. |
| 43 | + * |
| 44 | + * @param BulkAlbumPatchData $data validated, typed patch payload |
| 45 | + */ |
| 46 | + public function do(BulkAlbumPatchData $data): void |
| 47 | + { |
| 48 | + $album_ids = $data->album_ids; |
| 49 | + |
| 50 | + // ── Group 1: base_albums columns ───────────────────────────────────── |
| 51 | + $base_data = []; |
| 52 | + |
| 53 | + if ($data->has('description')) { |
| 54 | + $base_data['description'] = $data->description; |
| 55 | + } |
| 56 | + if ($data->has('copyright')) { |
| 57 | + $base_data['copyright'] = $data->copyright; |
| 58 | + } |
| 59 | + if ($data->has('photo_layout')) { |
| 60 | + $base_data['photo_layout'] = $data->photo_layout?->value; |
| 61 | + } |
| 62 | + if ($data->has('photo_sorting_col')) { |
| 63 | + $base_data['sorting_col'] = $data->photo_sorting_col?->value; |
| 64 | + } |
| 65 | + if ($data->has('photo_sorting_order')) { |
| 66 | + $base_data['sorting_order'] = $data->photo_sorting_order?->value; |
| 67 | + } |
| 68 | + if ($data->has('photo_timeline')) { |
| 69 | + $base_data['photo_timeline'] = $data->photo_timeline?->value; |
| 70 | + } |
| 71 | + if ($data->has('is_nsfw')) { |
| 72 | + $base_data['is_nsfw'] = $data->is_nsfw; |
| 73 | + } |
| 74 | + |
| 75 | + if ($base_data !== []) { |
| 76 | + BaseAlbumImpl::query() |
| 77 | + ->whereIn('id', $album_ids) |
| 78 | + ->update($base_data); |
| 79 | + } |
| 80 | + |
| 81 | + // ── Group 2: albums columns ─────────────────────────────────────────── |
| 82 | + $album_data = []; |
| 83 | + |
| 84 | + if ($data->has('license')) { |
| 85 | + $album_data['license'] = $data->license?->value; |
| 86 | + } |
| 87 | + if ($data->has('album_thumb_aspect_ratio')) { |
| 88 | + $album_data['album_thumb_aspect_ratio'] = $data->album_thumb_aspect_ratio?->value; |
| 89 | + } |
| 90 | + if ($data->has('album_timeline')) { |
| 91 | + $album_data['album_timeline'] = $data->album_timeline?->value; |
| 92 | + } |
| 93 | + if ($data->has('album_sorting_col')) { |
| 94 | + $album_data['album_sorting_col'] = $data->album_sorting_col?->value; |
| 95 | + } |
| 96 | + if ($data->has('album_sorting_order')) { |
| 97 | + $album_data['album_sorting_order'] = $data->album_sorting_order?->value; |
| 98 | + } |
| 99 | + |
| 100 | + if ($album_data !== []) { |
| 101 | + Album::query() |
| 102 | + ->whereIn('id', $album_ids) |
| 103 | + ->update($album_data); |
| 104 | + } |
| 105 | + |
| 106 | + // ── Group 3: Visibility fields ──────────────────────────────────────── |
| 107 | + $has_visibility = $data->has('is_public') || |
| 108 | + $data->has('is_link_required') || |
| 109 | + $data->has('grants_full_photo_access') || |
| 110 | + $data->has('grants_download') || |
| 111 | + $data->has('grants_upload'); |
| 112 | + |
| 113 | + if ($has_visibility) { |
| 114 | + /** @var Album[] $albums */ |
| 115 | + $albums = Album::query() |
| 116 | + ->with('base_class.access_permissions') |
| 117 | + ->whereIn('id', $album_ids) |
| 118 | + ->get() |
| 119 | + ->all(); |
| 120 | + |
| 121 | + foreach ($albums as $album) { |
| 122 | + $existing = $album->public_permissions(); |
| 123 | + |
| 124 | + // Derive current values as defaults, then overlay payload |
| 125 | + $is_public = $data->has('is_public') |
| 126 | + ? ($data->is_public === true) |
| 127 | + : ($existing !== null); |
| 128 | + $is_link_required = $data->has('is_link_required') |
| 129 | + ? ($data->is_link_required === true) |
| 130 | + : ($existing?->is_link_required === true); |
| 131 | + $grants_full_photo_access = $data->has('grants_full_photo_access') |
| 132 | + ? ($data->grants_full_photo_access === true) |
| 133 | + : ($existing?->grants_full_photo_access === true); |
| 134 | + $grants_download = $data->has('grants_download') |
| 135 | + ? ($data->grants_download === true) |
| 136 | + : ($existing?->grants_download === true); |
| 137 | + $grants_upload = $data->has('grants_upload') |
| 138 | + ? ($data->grants_upload === true) |
| 139 | + : ($existing?->grants_upload === true); |
| 140 | + |
| 141 | + // is_nsfw may have been updated in group 1 via mass-update; |
| 142 | + // use the payload value if present, else the model value. |
| 143 | + $is_nsfw = $data->has('is_nsfw') |
| 144 | + ? ($data->is_nsfw === true) |
| 145 | + : ($album->is_nsfw === true); |
| 146 | + |
| 147 | + $protection_policy = new AlbumProtectionPolicy( |
| 148 | + is_public: $is_public, |
| 149 | + is_link_required: $is_link_required, |
| 150 | + is_nsfw: $is_nsfw, |
| 151 | + grants_full_photo_access: $grants_full_photo_access, |
| 152 | + grants_download: $grants_download, |
| 153 | + grants_upload: $grants_upload, |
| 154 | + ); |
| 155 | + |
| 156 | + $this->set_protection_policy->do($album, $protection_policy, false, null); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments