xref: /webtrees/app/Module/MediaListModule.php (revision 52550490b7095dd69811f3ec21ed5a3ca1a8968d)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fig\Http\Message\RequestMethodInterface;
23use Fisharebest\Webtrees\Auth;
24use Fisharebest\Webtrees\DB;
25use Fisharebest\Webtrees\GedcomRecord;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\Media;
28use Fisharebest\Webtrees\Registry;
29use Fisharebest\Webtrees\Services\LinkedRecordService;
30use Fisharebest\Webtrees\Tree;
31use Fisharebest\Webtrees\Validator;
32use Illuminate\Database\Query\Builder;
33use Illuminate\Database\Query\JoinClause;
34use Illuminate\Support\Collection;
35use Psr\Http\Message\ResponseInterface;
36use Psr\Http\Message\ServerRequestInterface;
37use Psr\Http\Server\RequestHandlerInterface;
38
39use function addcslashes;
40use function array_combine;
41use function array_unshift;
42use function dirname;
43use function max;
44use function min;
45use function redirect;
46use function route;
47
48/**
49 * Class MediaListModule
50 */
51class MediaListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface
52{
53    use ModuleListTrait;
54
55    protected const ROUTE_URL = '/tree/{tree}/media-list';
56
57    private LinkedRecordService $linked_record_service;
58
59    /**
60     * @param LinkedRecordService $linked_record_service
61     */
62    public function __construct(LinkedRecordService $linked_record_service)
63    {
64        $this->linked_record_service = $linked_record_service;
65    }
66
67    /**
68     * Initialization.
69     *
70     * @return void
71     */
72    public function boot(): void
73    {
74        Registry::routeFactory()->routeMap()
75            ->get(static::class, static::ROUTE_URL, $this)
76            ->allows(RequestMethodInterface::METHOD_POST);
77    }
78
79    /**
80     * How should this module be identified in the control panel, etc.?
81     *
82     * @return string
83     */
84    public function title(): string
85    {
86        /* I18N: Name of a module/list */
87        return I18N::translate('Media objects');
88    }
89
90    /**
91     * A sentence describing what this module does.
92     *
93     * @return string
94     */
95    public function description(): string
96    {
97        /* I18N: Description of the “Media objects” module */
98        return I18N::translate('A list of media objects.');
99    }
100
101    /**
102     * CSS class for the URL.
103     *
104     * @return string
105     */
106    public function listMenuClass(): string
107    {
108        return 'menu-list-obje';
109    }
110
111    /**
112     * @param Tree                                      $tree
113     * @param array<bool|int|string|array<string>|null> $parameters
114     *
115     * @return string
116     */
117    public function listUrl(Tree $tree, array $parameters = []): string
118    {
119        $parameters['tree'] = $tree->name();
120
121        return route(static::class, $parameters);
122    }
123
124    /**
125     * @return array<string>
126     */
127    public function listUrlAttributes(): array
128    {
129        return [];
130    }
131
132    /**
133     * @param Tree $tree
134     *
135     * @return bool
136     */
137    public function listIsEmpty(Tree $tree): bool
138    {
139        return !DB::table('media')
140            ->where('m_file', '=', $tree->id())
141            ->exists();
142    }
143
144    /**
145     * @param ServerRequestInterface $request
146     *
147     * @return ResponseInterface
148     */
149    public function handle(ServerRequestInterface $request): ResponseInterface
150    {
151        $tree = Validator::attributes($request)->tree();
152        $user = Validator::attributes($request)->user();
153
154        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
155
156        $formats = Registry::elementFactory()->make('OBJE:FILE:FORM:TYPE')->values();
157
158        // Convert POST requests into GET requests for pretty URLs.
159        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
160            $params = [
161                'go'      => true,
162                'page'    => Validator::parsedBody($request)->integer('page'),
163                'max'     => Validator::parsedBody($request)->integer('max'),
164                'folder'  => Validator::parsedBody($request)->string('folder'),
165                'filter'  => Validator::parsedBody($request)->string('filter'),
166                'subdirs' => Validator::parsedBody($request)->boolean('subdirs', false),
167                'format'  => Validator::parsedBody($request)->isInArrayKeys($formats)->string('format'),
168            ];
169
170            return redirect($this->listUrl($tree, $params));
171        }
172
173        $folders = $this->allFolders($tree);
174        $go      = Validator::queryParams($request)->boolean('go', false);
175        $page    = Validator::queryParams($request)->integer('page', 1);
176        $max     = Validator::queryParams($request)->integer('max', 20);
177        $folder  = Validator::queryParams($request)->string('folder', '');
178        $filter  = Validator::queryParams($request)->string('filter', '');
179        $subdirs = Validator::queryParams($request)->boolean('subdirs', false);
180        $format  = Validator::queryParams($request)->isInArrayKeys($formats)->string('format', '');
181
182        if ($go) {
183            $media_objects = $this->allMedia($tree, $folder, $subdirs, 'title', $filter, $format);
184        } else {
185            $media_objects = new Collection();
186        }
187
188        // Pagination
189        $count = $media_objects->count();
190        $pages = (int) (($count + $max - 1) / $max);
191        $page  = max(min($page, $pages), 1);
192
193        $media_objects = $media_objects->slice(($page - 1) * $max, $max);
194
195        return $this->viewResponse('modules/media-list/page', [
196            'count'                 => $count,
197            'filter'                => $filter,
198            'folder'                => $folder,
199            'folders'               => $folders,
200            'format'                => $format,
201            'formats'               => $formats,
202            'linked_record_service' => $this->linked_record_service,
203            'max'                   => $max,
204            'media_objects'         => $media_objects,
205            'page'                  => $page,
206            'pages'                 => $pages,
207            'subdirs'               => $subdirs,
208            'module'                => $this,
209            'title'                 => I18N::translate('Media'),
210            'tree'                  => $tree,
211        ]);
212    }
213
214    /**
215     * Generate a list of all the folders in a current tree.
216     *
217     * @param Tree $tree
218     *
219     * @return array<string>
220     */
221    private function allFolders(Tree $tree): array
222    {
223        $folders = DB::table('media_file')
224            ->where('m_file', '=', $tree->id())
225            ->where('multimedia_file_refn', 'NOT LIKE', 'http:%')
226            ->where('multimedia_file_refn', 'NOT LIKE', 'https:%')
227            ->where('multimedia_file_refn', 'LIKE', '%/%')
228            ->pluck('multimedia_file_refn', 'multimedia_file_refn')
229            ->map(static fn (string $path): string => dirname($path))
230            ->uniqueStrict()
231            ->sort()
232            ->all();
233
234        // Ensure we have an empty (top level) folder.
235        array_unshift($folders, '');
236
237        return array_combine($folders, $folders);
238    }
239
240    /**
241     * Generate a list of all the media objects matching the criteria in a current tree.
242     *
243     * @param Tree   $tree       find media in this tree
244     * @param string $folder     folder to search
245     * @param bool   $subfolders
246     * @param string $sort       either "file" or "title"
247     * @param string $filter     optional search string
248     * @param string $format     option OBJE/FILE/FORM/TYPE
249     *
250     * @return Collection<int,Media>
251     */
252    private function allMedia(Tree $tree, string $folder, bool $subfolders, string $sort, string $filter, string $format): Collection
253    {
254        $query = DB::table('media')
255            ->join('media_file', static function (JoinClause $join): void {
256                $join
257                    ->on('media_file.m_file', '=', 'media.m_file')
258                    ->on('media_file.m_id', '=', 'media.m_id');
259            })
260            ->where('media.m_file', '=', $tree->id());
261
262        if ($folder === '') {
263            // Include external URLs in the root folder.
264            if (!$subfolders) {
265                $query->where(static function (Builder $query): void {
266                    $query
267                        ->where('multimedia_file_refn', 'NOT LIKE', '%/%')
268                        ->orWhere('multimedia_file_refn', 'LIKE', 'http:%')
269                        ->orWhere('multimedia_file_refn', 'LIKE', 'https:%');
270                });
271            }
272        } else {
273            // Exclude external URLs from the root folder.
274            $query
275                ->where('multimedia_file_refn', 'LIKE', $folder . '/%')
276                ->where('multimedia_file_refn', 'NOT LIKE', 'http:%')
277                ->where('multimedia_file_refn', 'NOT LIKE', 'https:%');
278
279            if (!$subfolders) {
280                $query->where('multimedia_file_refn', 'NOT LIKE', $folder . '/%/%');
281            }
282        }
283
284        // Apply search terms
285        if ($filter !== '') {
286            $query->where(static function (Builder $query) use ($filter): void {
287                $like = '%' . addcslashes($filter, '\\%_') . '%';
288                $query
289                    ->where('multimedia_file_refn', 'LIKE', $like)
290                    ->orWhere('descriptive_title', 'LIKE', $like);
291            });
292        }
293
294        if ($format !== '') {
295            $query->where('source_media_type', '=', $format);
296        }
297
298        switch ($sort) {
299            case 'file':
300                $query->orderBy('multimedia_file_refn');
301                break;
302            case 'title':
303                $query->orderBy('descriptive_title');
304                break;
305        }
306
307        return $query
308            ->get()
309            ->map(Registry::mediaFactory()->mapper($tree))
310            ->uniqueStrict()
311            ->filter(GedcomRecord::accessFilter());
312    }
313}
314