WelcomeBlockModule::loadRoutes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees-lib: MyArtJaub library for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees
7
 * @subpackage WelcomeBlock
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2011-2022, Jonathan Jaubart
10
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
11
 */
12
13
declare(strict_types=1);
14
15
namespace MyArtJaub\Webtrees\Module\WelcomeBlock;
16
17
use Aura\Router\Map;
18
use Fisharebest\Webtrees\FlashMessages;
19
use Fisharebest\Webtrees\I18N;
20
use Fisharebest\Webtrees\Registry;
21
use Fisharebest\Webtrees\Tree;
22
use Fisharebest\Webtrees\Validator;
23
use Fisharebest\Webtrees\Module\AbstractModule;
24
use Fisharebest\Webtrees\Module\ModuleBlockInterface;
25
use Fisharebest\Webtrees\Module\ModuleBlockTrait;
26
use Illuminate\Support\Str;
27
use MyArtJaub\Webtrees\Module\ModuleMyArtJaubInterface;
28
use MyArtJaub\Webtrees\Module\ModuleMyArtJaubTrait;
29
use MyArtJaub\Webtrees\Module\WelcomeBlock\Http\RequestHandlers\MatomoStats;
30
use Psr\Http\Message\ServerRequestInterface;
31
32
/**
33
 * Welcome Block Module.
34
 */
35
class WelcomeBlockModule extends AbstractModule implements ModuleMyArtJaubInterface, ModuleBlockInterface
36
{
37
    use ModuleMyArtJaubTrait;
38
    use ModuleBlockTrait;
39
40
    /**
41
     * {@inheritDoc}
42
     * @see \Fisharebest\Webtrees\Module\AbstractModule::title()
43
     */
44
    public function title(): string
45
    {
46
        return /* I18N: Name of the “WelcomeBlock” module */ I18N::translate('MyArtJaub Welcome Block');
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     * @see \Fisharebest\Webtrees\Module\AbstractModule::description()
52
     */
53
    public function description(): string
54
    {
55
        //phpcs:ignore Generic.Files.LineLength.TooLong
56
        return /* I18N: Description of the “WelcomeBlock” module */ I18N::translate('The MyArtJaub Welcome block welcomes the visitor to the site, allows a quick login to the site, and displays statistics on visits.');
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     * @see \MyArtJaub\Webtrees\Module\ModuleMyArtJaubInterface::loadRoutes()
62
     */
63
    public function loadRoutes(Map $router): void
64
    {
65
        $router->attach('', '', static function (Map $router): void {
66
67
            $router->attach('', '/module-maj/welcomeblock/{block_id}', static function (Map $router): void {
68
                $router->tokens(['block_id' => '\d+']);
69
                $router->get(MatomoStats::class, '/matomostats', MatomoStats::class);
70
            });
71
        });
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     * @see \Fisharebest\Webtrees\Module\ModuleCustomInterface::customModuleVersion()
77
     */
78
    public function customModuleVersion(): string
79
    {
80
        return '2.1.3-v.1';
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     * @see \Fisharebest\Webtrees\Module\ModuleBlockInterface::getBlock()
86
     *
87
     * @param mixed[] $config
88
     */
89
    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
90
    {
91
        $fab_welcome_block_view = app(\Fisharebest\Webtrees\Module\WelcomeBlockModule::class)
92
            ->getBlock($tree, $block_id, ModuleBlockInterface::CONTEXT_EMBED);
93
94
        $fab_login_block_view = app(\Fisharebest\Webtrees\Module\LoginBlockModule::class)
95
            ->getBlock($tree, $block_id, ModuleBlockInterface::CONTEXT_EMBED);
96
97
        $content = view($this->name() . '::block-embed', [
98
            'block_id'                  =>  $block_id,
99
            'fab_welcome_block_view'    =>  $fab_welcome_block_view,
100
            'fab_login_block_view'      =>  $fab_login_block_view,
101
            'matomo_enabled'            =>  $this->isMatomoEnabled($block_id),
102
            'js_script_url'             =>  $this->assetUrl('js/welcomeblock.min.js')
103
        ]);
104
105
        if ($context !== self::CONTEXT_EMBED) {
106
            return view('modules/block-template', [
107
                'block'      => Str::kebab($this->name()),
108
                'id'         => $block_id,
109
                'config_url' => $this->configUrl($tree, $context, $block_id),
110
                'title'      => e($tree->title()),
111
                'content'    => $content,
112
            ]);
113
        }
114
115
        return $content;
116
    }
117
118
    /**
119
     * {@inheritDoc}
120
     * @see \Fisharebest\Webtrees\Module\ModuleBlockInterface::isTreeBlock()
121
     */
122
    public function isTreeBlock(): bool
123
    {
124
        return true;
125
    }
126
127
    /**
128
     * {@inheritDoc}
129
     * @see \Fisharebest\Webtrees\Module\ModuleBlockInterface::editBlockConfiguration()
130
     */
131
    public function editBlockConfiguration(Tree $tree, int $block_id): string
132
    {
133
        return view($this->name() . '::config', $this->matomoSettings($block_id));
134
    }
135
136
    /**
137
     * {@inheritDoc}
138
     * @see \Fisharebest\Webtrees\Module\ModuleBlockInterface::saveBlockConfiguration()
139
     */
140
    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
141
    {
142
        $matomo_enabled = Validator::parsedBody($request)->string('matomo_enabled', '') === 'yes';
143
        $this->setBlockSetting($block_id, 'matomo_enabled', $matomo_enabled ? 'yes' : 'no');
144
        if (!$matomo_enabled) {
145
            return;
146
        }
147
148
        $matomo_url = trim(Validator::parsedBody($request)->string('matomo_url', ''));
149
        if (filter_var($matomo_url, FILTER_VALIDATE_URL) === false) {
150
            FlashMessages::addMessage(I18N::translate('The Matomo URL provided is not valid.'), 'danger');
151
            return;
152
        }
153
154
        $matomo_siteid = Validator::parsedBody($request)->integer('matomo_siteid', -1);
155
        if ($matomo_siteid === -1) {
156
            FlashMessages::addMessage(I18N::translate('The Matomo Site ID provided is not valid.'), 'danger');
157
            return;
158
        }
159
160
        $matomo_token = trim(Validator::parsedBody($request)->string('matomo_token', ''));
161
162
        $this
163
            ->setBlockSetting($block_id, 'matomo_url', $matomo_url)
164
            ->setBlockSetting($block_id, 'matomo_token', $matomo_token)
165
            ->setBlockSetting($block_id, 'matomo_siteid', (string) $matomo_siteid);
166
167
        Registry::cache()->file()->forget($this->name() . '-matomovisits-yearly-' . $block_id);
168
    }
169
170
    /**
171
     * Returns whether Matomo statistics is enabled for a specific MyArtJaub WelcomeBlock block
172
     *
173
     * @param int $block_id
174
     * @return bool
175
     */
176
    public function isMatomoEnabled(int $block_id): bool
177
    {
178
        return $this->getBlockSetting($block_id, 'matomo_enabled', 'no') === 'yes';
179
    }
180
181
    /**
182
     * Returns settings for retrieving Matomo statistics for a specific MyArtJaub WelcomeBlock block
183
     *
184
     * @param int $block_id
185
     * @return array<string, mixed>
186
     */
187
    public function matomoSettings(int $block_id): array
188
    {
189
        return [
190
            'matomo_enabled' => $this->isMatomoEnabled($block_id),
191
            'matomo_url' => $this->getBlockSetting($block_id, 'matomo_url'),
192
            'matomo_token' => $this->getBlockSetting($block_id, 'matomo_token'),
193
            'matomo_siteid'  => (int) $this->getBlockSetting($block_id, 'matomo_siteid', '0')
194
        ];
195
    }
196
}
197