xref: /webtrees/app/Http/RequestHandlers/TreePage.php (revision 52550490b7095dd69811f3ec21ed5a3ca1a8968d)
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
226f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
238e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
248e0e1b25SGreg Roachuse Fisharebest\Webtrees\Module\ModuleBlockInterface;
258e0e1b25SGreg Roachuse Fisharebest\Webtrees\Services\HomePageService;
26b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
278e0e1b25SGreg Roachuse Illuminate\Database\Query\Builder;
288e0e1b25SGreg Roachuse Illuminate\Database\Query\Expression;
298e0e1b25SGreg Roachuse Psr\Http\Message\ResponseInterface;
308e0e1b25SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
318e0e1b25SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
328e0e1b25SGreg Roach
338e0e1b25SGreg Roachuse function e;
348e0e1b25SGreg Roach
358e0e1b25SGreg Roach/**
367893c81aSGreg Roach * Show a tree's page.
378e0e1b25SGreg Roach */
388e0e1b25SGreg Roachclass TreePage implements RequestHandlerInterface
398e0e1b25SGreg Roach{
408e0e1b25SGreg Roach    use ViewResponseTrait;
418e0e1b25SGreg Roach
42c4943cffSGreg Roach    private HomePageService $home_page_service;
438e0e1b25SGreg Roach
448e0e1b25SGreg Roach    /**
458e0e1b25SGreg Roach     * @param HomePageService $home_page_service
468e0e1b25SGreg Roach     */
478e0e1b25SGreg Roach    public function __construct(HomePageService $home_page_service)
488e0e1b25SGreg Roach    {
498e0e1b25SGreg Roach        $this->home_page_service = $home_page_service;
508e0e1b25SGreg Roach    }
518e0e1b25SGreg Roach
528e0e1b25SGreg Roach    /**
538e0e1b25SGreg Roach     * @param ServerRequestInterface $request
548e0e1b25SGreg Roach     *
558e0e1b25SGreg Roach     * @return ResponseInterface
568e0e1b25SGreg Roach     */
578e0e1b25SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
588e0e1b25SGreg Roach    {
59b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
60b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
61f6924bc8SGreg Roach
628e0e1b25SGreg Roach        $has_blocks = DB::table('block')
638e0e1b25SGreg Roach            ->where('gedcom_id', '=', $tree->id())
648e0e1b25SGreg Roach            ->exists();
658e0e1b25SGreg Roach
668e0e1b25SGreg Roach        if (!$has_blocks) {
678e0e1b25SGreg Roach            $this->home_page_service->checkDefaultTreeBlocksExist();
688e0e1b25SGreg Roach
698e0e1b25SGreg Roach            // Copy the defaults
70*52550490SGreg Roach            DB::query()->from('block')->insertUsing(
718e0e1b25SGreg Roach                ['gedcom_id', 'location', 'block_order', 'module_name'],
728e0e1b25SGreg Roach                static function (Builder $query) use ($tree): void {
738e0e1b25SGreg Roach                    $query
748e0e1b25SGreg Roach                        ->select([new Expression($tree->id()), 'location', 'block_order', 'module_name'])
758e0e1b25SGreg Roach                        ->from('block')
768e0e1b25SGreg Roach                        ->where('gedcom_id', '=', -1);
778e0e1b25SGreg Roach                }
788e0e1b25SGreg Roach            );
798e0e1b25SGreg Roach        }
808e0e1b25SGreg Roach
818e0e1b25SGreg Roach        return $this->viewResponse('tree-page', [
82f6924bc8SGreg Roach            'main_blocks'      => $this->home_page_service->treeBlocks($tree, $user, ModuleBlockInterface::MAIN_BLOCKS),
83f6924bc8SGreg Roach            'side_blocks'      => $this->home_page_service->treeBlocks($tree, $user, ModuleBlockInterface::SIDE_BLOCKS),
848e0e1b25SGreg Roach            'title'            => e($tree->title()),
858e0e1b25SGreg Roach            'tree'             => $tree,
868e0e1b25SGreg Roach            'meta_robots'      => 'index,follow',
872406e0e0SGreg Roach            'meta_description' => e($tree->getPreference('META_DESCRIPTION'))
888e0e1b25SGreg Roach        ]);
898e0e1b25SGreg Roach    }
908e0e1b25SGreg Roach}
91