Issues (2559)

app/Module/HtmlBlockModule.php (2 issues)

Labels
Severity
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Module;
21
22
use Fisharebest\Webtrees\I18N;
0 ignored issues
show
The type Fisharebest\Webtrees\I18N was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Fisharebest\Webtrees\Registry;
24
use Fisharebest\Webtrees\Services\HtmlService;
25
use Fisharebest\Webtrees\Statistics;
26
use Fisharebest\Webtrees\Tree;
27
use Fisharebest\Webtrees\Validator;
28
use Illuminate\Support\Str;
29
use Psr\Http\Message\ServerRequestInterface;
30
31
use function in_array;
32
use function time;
33
34
class HtmlBlockModule extends AbstractModule implements ModuleBlockInterface
0 ignored issues
show
The type Fisharebest\Webtrees\Module\ModuleBlockInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
{
36
    use ModuleBlockTrait;
37
38
    private HtmlService $html_service;
39
40
    /**
41
     * HtmlBlockModule bootstrap.
42
     *
43
     * @param HtmlService $html_service
44
     */
45
    public function __construct(HtmlService $html_service)
46
    {
47
        $this->html_service = $html_service;
48
    }
49
50
    public function title(): string
51
    {
52
        /* I18N: Name of a module */
53
        return I18N::translate('HTML');
54
    }
55
56
    public function description(): string
57
    {
58
        /* I18N: Description of the “HTML” module */
59
        return I18N::translate('Add your own text and graphics.');
60
    }
61
62
    /**
63
     * Generate the HTML content of this block.
64
     *
65
     * @param Tree                 $tree
66
     * @param int                  $block_id
67
     * @param string               $context
68
     * @param array<string,string> $config
69
     *
70
     * @return string
71
     */
72
    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
73
    {
74
        $statistics = Registry::container()->get(Statistics::class);
75
76
        $title          = $this->getBlockSetting($block_id, 'title');
77
        $content        = $this->getBlockSetting($block_id, 'html');
78
        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp');
79
        $languages      = $this->getBlockSetting($block_id, 'languages');
80
81
        // Only show this block for certain languages
82
        if ($languages && !in_array(I18N::languageTag(), explode(',', $languages), true)) {
83
            return '';
84
        }
85
86
        // Retrieve text, process embedded variables
87
        $title   = $statistics->embedTags($title);
88
        $content = $statistics->embedTags($content);
89
90
        $block_timestamp = (int) $this->getBlockSetting($block_id, 'timestamp', (string) time());
91
92
        if ($show_timestamp === '1') {
93
            $content .= '<br>' . view('components/datetime', ['timestamp' => Registry::timestampFactory()->make($block_timestamp)]);
94
        }
95
96
        if ($context !== self::CONTEXT_EMBED) {
97
            return view('modules/block-template', [
98
                'block'      => Str::kebab($this->name()),
99
                'id'         => $block_id,
100
                'config_url' => $this->configUrl($tree, $context, $block_id),
101
                'title'      => e(strip_tags($title)),
102
                'content'    => $content,
103
            ]);
104
        }
105
106
        return $content;
107
    }
108
109
    /**
110
     * Should this block load asynchronously using AJAX?
111
     *
112
     * Simple blocks are faster in-line, more complex ones can be loaded later.
113
     *
114
     * @return bool
115
     */
116
    public function loadAjax(): bool
117
    {
118
        return false;
119
    }
120
121
    /**
122
     * Can this block be shown on the user’s home page?
123
     *
124
     * @return bool
125
     */
126
    public function isUserBlock(): bool
127
    {
128
        return true;
129
    }
130
131
    /**
132
     * Can this block be shown on the tree’s home page?
133
     *
134
     * @return bool
135
     */
136
    public function isTreeBlock(): bool
137
    {
138
        return true;
139
    }
140
141
    /**
142
     * Update the configuration for a block.
143
     *
144
     * @param ServerRequestInterface $request
145
     * @param int                    $block_id
146
     *
147
     * @return void
148
     */
149
    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
150
    {
151
        $title          = Validator::parsedBody($request)->string('title');
152
        $html           = Validator::parsedBody($request)->string('html');
153
        $show_timestamp = Validator::parsedBody($request)->boolean('show_timestamp');
154
        $languages      = Validator::parsedBody($request)->array('languages');
155
156
        $this->setBlockSetting($block_id, 'title', $title);
157
        $this->setBlockSetting($block_id, 'html', $this->html_service->sanitize($html));
158
        $this->setBlockSetting($block_id, 'show_timestamp', (string) $show_timestamp);
159
        $this->setBlockSetting($block_id, 'timestamp', (string) time());
160
        $this->setBlockSetting($block_id, 'languages', implode(',', $languages));
161
    }
162
163
    /**
164
     * An HTML form to edit block settings
165
     *
166
     * @param Tree $tree
167
     * @param int  $block_id
168
     *
169
     * @return string
170
     */
171
    public function editBlockConfiguration(Tree $tree, int $block_id): string
172
    {
173
        $title          = $this->getBlockSetting($block_id, 'title');
174
        $html           = $this->getBlockSetting($block_id, 'html');
175
        $show_timestamp = $this->getBlockSetting($block_id, 'show_timestamp', '0');
176
        $languages      = explode(',', $this->getBlockSetting($block_id, 'languages'));
177
178
        $templates = [
179
            $html                                                       => I18N::translate('Custom'),
180
            view('modules/html/template-keywords')                      => I18N::translate('Keyword examples'),
181
            view('modules/html/template-narrative')                     => I18N::translate('Narrative description'),
182
            view('modules/html/template-statistics', ['tree' => $tree]) => I18N::translate('Statistics'),
183
        ];
184
185
        return view('modules/html/config', [
186
            'html'           => $html,
187
            'languages'      => $languages,
188
            'show_timestamp' => $show_timestamp,
189
            'templates'      => $templates,
190
            'title'          => $title,
191
        ]);
192
    }
193
}
194