xref: /webtrees/app/Http/RequestHandlers/AutoCompleteFolder.php (revision d11be7027e34e3121be11cc025421873364403f9)
163763244SGreg Roach<?php
263763244SGreg Roach
363763244SGreg Roach/**
463763244SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
663763244SGreg Roach * This program is free software: you can redistribute it and/or modify
763763244SGreg Roach * it under the terms of the GNU General Public License as published by
863763244SGreg Roach * the Free Software Foundation, either version 3 of the License, or
963763244SGreg Roach * (at your option) any later version.
1063763244SGreg Roach * This program is distributed in the hope that it will be useful,
1163763244SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1263763244SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1363763244SGreg Roach * GNU General Public License for more details.
1463763244SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
1663763244SGreg Roach */
1763763244SGreg Roach
1863763244SGreg Roachdeclare(strict_types=1);
1963763244SGreg Roach
2063763244SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
2163763244SGreg Roach
2239b93305SGreg Roachuse Fisharebest\Webtrees\I18N;
23f9b64f46SGreg Roachuse Fisharebest\Webtrees\Services\MediaFileService;
24f9b64f46SGreg Roachuse Fisharebest\Webtrees\Services\SearchService;
25b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
2663763244SGreg Roachuse Illuminate\Support\Collection;
27f0448b68SGreg Roachuse League\Flysystem\FilesystemException;
2863763244SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
2963763244SGreg Roach
3063763244SGreg Roach/**
3163763244SGreg Roach * Autocomplete handler for media folders
3263763244SGreg Roach */
3363763244SGreg Roachclass AutoCompleteFolder extends AbstractAutocompleteHandler
3463763244SGreg Roach{
35f9b64f46SGreg Roach    private MediaFileService $media_file_service;
36f9b64f46SGreg Roach
3792a78a2fSGreg Roach    /**
3861351a03SGreg Roach     * @param MediaFileService $media_file_service
3992a78a2fSGreg Roach     * @param SearchService    $search_service
4092a78a2fSGreg Roach     */
41f9b64f46SGreg Roach    public function __construct(MediaFileService $media_file_service, SearchService $search_service)
42f9b64f46SGreg Roach    {
43f9b64f46SGreg Roach        parent::__construct($search_service);
44f9b64f46SGreg Roach
45f9b64f46SGreg Roach        $this->media_file_service = $media_file_service;
46f9b64f46SGreg Roach    }
47f9b64f46SGreg Roach
48f0448b68SGreg Roach    /**
49f0448b68SGreg Roach     * @param ServerRequestInterface $request
50f0448b68SGreg Roach     *
5136779af1SGreg Roach     * @return Collection<int,string>
52f0448b68SGreg Roach     */
5363763244SGreg Roach    protected function search(ServerRequestInterface $request): Collection
5463763244SGreg Roach    {
55b55cbc6bSGreg Roach        $tree  = Validator::attributes($request)->tree();
56bb5c0006SGreg Roach        $query = Validator::queryParams($request)->string('query');
5763763244SGreg Roach
58f0448b68SGreg Roach        try {
59f9b64f46SGreg Roach            return $this->media_file_service->mediaFolders($tree)
6039b93305SGreg Roach                ->filter(fn (string $path): bool => stripos($path, $query) !== false)
6139b93305SGreg Roach                ->sort(I18N::comparator())
6239b93305SGreg Roach                ->values();
6328d026adSGreg Roach        } catch (FilesystemException) {
64f0448b68SGreg Roach            return new Collection();
65f0448b68SGreg Roach        }
6663763244SGreg Roach    }
6763763244SGreg Roach}
68