xref: /webtrees/app/Http/RequestHandlers/UserPageBlock.php (revision 6f4ec3cadc983f0a7294108c634bef48846b4311)
18e0e1b25SGreg Roach<?php
28e0e1b25SGreg Roach
38e0e1b25SGreg Roach/**
48e0e1b25SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
68e0e1b25SGreg Roach * This program is free software: you can redistribute it and/or modify
78e0e1b25SGreg Roach * it under the terms of the GNU General Public License as published by
88e0e1b25SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98e0e1b25SGreg Roach * (at your option) any later version.
108e0e1b25SGreg Roach * This program is distributed in the hope that it will be useful,
118e0e1b25SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128e0e1b25SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138e0e1b25SGreg Roach * GNU General Public License for more details.
148e0e1b25SGreg 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/>.
168e0e1b25SGreg Roach */
178e0e1b25SGreg Roach
188e0e1b25SGreg Roachdeclare(strict_types=1);
198e0e1b25SGreg Roach
208e0e1b25SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
218e0e1b25SGreg Roach
22*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
238e0e1b25SGreg Roachuse Fisharebest\Webtrees\Module\ModuleBlockInterface;
248e0e1b25SGreg Roachuse Fisharebest\Webtrees\Services\HomePageService;
25b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
268e0e1b25SGreg Roachuse Psr\Http\Message\ResponseInterface;
278e0e1b25SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
288e0e1b25SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
298e0e1b25SGreg Roach
308e0e1b25SGreg Roachuse function response;
318e0e1b25SGreg Roachuse function view;
328e0e1b25SGreg Roach
338e0e1b25SGreg Roach/**
347893c81aSGreg Roach * Load block asynchronously.
357893c81aSGreg Roach *
368e0e1b25SGreg Roach */
378e0e1b25SGreg Roachclass UserPageBlock implements RequestHandlerInterface
388e0e1b25SGreg Roach{
39c4943cffSGreg Roach    private HomePageService $home_page_service;
408e0e1b25SGreg Roach
418e0e1b25SGreg Roach    /**
428e0e1b25SGreg Roach     * @param HomePageService $home_page_service
438e0e1b25SGreg Roach     */
448e0e1b25SGreg Roach    public function __construct(HomePageService $home_page_service)
458e0e1b25SGreg Roach    {
468e0e1b25SGreg Roach        $this->home_page_service = $home_page_service;
478e0e1b25SGreg Roach    }
488e0e1b25SGreg Roach
498e0e1b25SGreg Roach    /**
508e0e1b25SGreg Roach     * @param ServerRequestInterface $request
518e0e1b25SGreg Roach     *
528e0e1b25SGreg Roach     * @return ResponseInterface
538e0e1b25SGreg Roach     */
548e0e1b25SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
558e0e1b25SGreg Roach    {
56b55cbc6bSGreg Roach        $tree     = Validator::attributes($request)->tree();
57b55cbc6bSGreg Roach        $user     = Validator::attributes($request)->user();
58748dbe15SGreg Roach        $block_id = Validator::queryParams($request)->integer('block_id');
598e0e1b25SGreg Roach
608e0e1b25SGreg Roach        $block_id = (int) DB::table('block')
618e0e1b25SGreg Roach            ->where('block_id', '=', $block_id)
628e0e1b25SGreg Roach            ->where('user_id', '=', $user->id())
638e0e1b25SGreg Roach            ->value('block_id');
648e0e1b25SGreg Roach
658e0e1b25SGreg Roach        $module = $this->home_page_service->getBlockModule($tree, $block_id);
668e0e1b25SGreg Roach
678e0e1b25SGreg Roach        $html = view('layouts/ajax', [
688e0e1b25SGreg Roach            'content' => $module->getBlock($tree, $block_id, ModuleBlockInterface::CONTEXT_USER_PAGE),
698e0e1b25SGreg Roach        ]);
708e0e1b25SGreg Roach
718e0e1b25SGreg Roach        return response($html);
728e0e1b25SGreg Roach    }
738e0e1b25SGreg Roach}
74