xref: /webtrees/app/Module/ModuleBlockTrait.php (revision b6017f990d38d8c56e04c0096ce9a7e8745ad4ba)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Http\RequestHandlers\TreePageBlockEdit;
24use Fisharebest\Webtrees\Http\RequestHandlers\UserPageBlockEdit;
25use Fisharebest\Webtrees\Tree;
26use Psr\Http\Message\ServerRequestInterface;
27
28use function route;
29
30/**
31 * Trait ModuleBlockTrait - default implementation of ModuleBlockInterface
32 */
33trait ModuleBlockTrait
34{
35    /**
36     * Generate the HTML content of this block.
37     *
38     * @param Tree   $tree
39     * @param int    $block_id
40     * @param string $context
41     * @param array  $config
42     *
43     * @return string
44     */
45    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
46    {
47        return '';
48    }
49
50    /**
51     * Should this block load asynchronously using AJAX?
52     *
53     * Simple blocks are faster in-line, more complex ones can be loaded later.
54     *
55     * @return bool
56     */
57    public function loadAjax(): bool
58    {
59        return false;
60    }
61
62    /**
63     * Can this block be shown on the user’s home page?
64     *
65     * @return bool
66     */
67    public function isUserBlock(): bool
68    {
69        return false;
70    }
71
72    /**
73     * Can this block be shown on the tree’s home page?
74     *
75     * @return bool
76     */
77    public function isTreeBlock(): bool
78    {
79        return false;
80    }
81
82    /**
83     * @param Tree   $tree
84     * @param string $context
85     * @param int    $block_id
86     *
87     * @return string
88     */
89    protected function configUrl(Tree $tree, string $context, int $block_id): string
90    {
91        if ($context === ModuleBlockInterface::CONTEXT_TREE_PAGE && Auth::isManager($tree)) {
92            return route(TreePageBlockEdit::class, [
93                'block_id' => $block_id,
94                'tree'     => $tree->name(),
95            ]);
96        }
97
98        if ($context === ModuleBlockInterface::CONTEXT_USER_PAGE && Auth::check()) {
99            return route(UserPageBlockEdit::class, [
100                'block_id' => $block_id,
101                'tree'     => $tree->name(),
102            ]);
103        }
104
105        return '';
106    }
107
108    /**
109     * Update the configuration for a block.
110     *
111     * @param ServerRequestInterface $request
112     * @param int                    $block_id
113     *
114     * @return void
115     */
116    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
117    {
118    }
119
120    /**
121     * An HTML form to edit block settings
122     *
123     * @param Tree $tree
124     * @param int  $block_id
125     *
126     * @return string
127     */
128    public function editBlockConfiguration(Tree $tree, int $block_id): string
129    {
130        return '';
131    }
132}
133