Passed
Branch feature/2.0 (ef99fd)
by Jonathan
11:25
created

WelcomeBlockModule::saveBlockConfiguration()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 16
c 2
b 0
f 0
nc 4
nop 2
dl 0
loc 26
rs 9.4222
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-2020, 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\Tree;
21
use Fisharebest\Webtrees\Module\ModuleBlockInterface;
22
use Fisharebest\Webtrees\Module\ModuleBlockTrait;
23
use Illuminate\Support\Str;
24
use MyArtJaub\Webtrees\Module\AbstractModuleMaj;
25
use MyArtJaub\Webtrees\Module\WelcomeBlock\Http\RequestHandlers\MatomoStats;
26
use Psr\Http\Message\ServerRequestInterface;
27
28
/**
29
 * Welcome Block Module.
30
 */
31
class WelcomeBlockModule extends AbstractModuleMaj implements ModuleBlockInterface
32
{
33
    use ModuleBlockTrait;
34
35
    /**
36
     * {@inheritDoc}
37
     * @see \Fisharebest\Webtrees\Module\AbstractModule::title()
38
     */
39
    public function title(): string
40
    {
41
        return /* I18N: Name of the “WelcomeBlock” module */ I18N::translate('MyArtJaub Welcome Block');
42
    }
43
    
44
    /**
45
     * {@inheritDoc}
46
     * @see \Fisharebest\Webtrees\Module\AbstractModule::description()
47
     */
48
    public function description(): string
49
    {
50
        //phpcs:ignore Generic.Files.LineLength.TooLong
51
        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.');
52
    }
53
    
54
    /**
55
     * {@inheritDoc}
56
     * @see \MyArtJaub\Webtrees\Module\AbstractModuleMaj::loadRoutes()
57
     */
58
    public function loadRoutes(Map $router): void
59
    {
60
        $router->attach('', '', static function (Map $router) {
61
62
            $router->attach('', '/module-maj/welcomeblock/{block_id}', static function (Map $router) {
63
                
64
                $router->get(MatomoStats::class, '/matomostats', MatomoStats::class);
65
            });
66
        });
67
    }
68
    
69
    /**
70
     * {@inheritDoc}
71
     * @see \Fisharebest\Webtrees\Module\ModuleBlockInterface::getBlock()
72
     */
73
    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
74
    {
75
        $fab_welcome_block_view = app(\Fisharebest\Webtrees\Module\WelcomeBlockModule::class)
76
            ->getBlock($tree, $block_id, ModuleBlockInterface::CONTEXT_EMBED);
77
        
78
        $fab_login_block_view = app(\Fisharebest\Webtrees\Module\LoginBlockModule::class)
79
            ->getBlock($tree, $block_id, ModuleBlockInterface::CONTEXT_EMBED);
80
        
81
        $content = view($this->name() . '::block-embed', [
82
            'block_id'                  =>  $block_id,
83
            'fab_welcome_block_view'    =>  $fab_welcome_block_view,
84
            'fab_login_block_view'      =>  $fab_login_block_view,
85
            'matomo_enabled'            =>  $this->isMatomoEnabled($block_id)
86
        ]);
87
        
88
        if ($context !== self::CONTEXT_EMBED) {
89
            return view('modules/block-template', [
90
                'block'      => Str::kebab($this->name()),
91
                'id'         => $block_id,
92
                'config_url' => $this->configUrl($tree, $context, $block_id),
93
                'title'      => $tree->title(),
94
                'content'    => $content,
95
            ]);
96
        }
97
        
98
        return $content;
99
    }
100
    
101
    /**
102
     * {@inheritDoc}
103
     * @see \Fisharebest\Webtrees\Module\ModuleBlockInterface::isTreeBlock()
104
     */
105
    public function isTreeBlock(): bool
106
    {
107
        return true;
108
    }
109
    
110
    /**
111
     * {@inheritDoc}
112
     * @see \Fisharebest\Webtrees\Module\ModuleBlockInterface::editBlockConfiguration()
113
     */
114
    public function editBlockConfiguration(Tree $tree, int $block_id): string
115
    {
116
        return view($this->name() . '::config', $this->matomoSettings($block_id));
117
    }
118
    
119
    /**
120
     * {@inheritDoc}
121
     * @see \Fisharebest\Webtrees\Module\ModuleBlockInterface::saveBlockConfiguration()
122
     */
123
    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
124
    {
125
        $params = (array) $request->getParsedBody();
126
        
127
        $matomo_enabled = $params['matomo_enabled'] == 'yes';
128
        $this->setBlockSetting($block_id, 'matomo_enabled', $matomo_enabled ? 'yes' : 'no');
129
        if (!$matomo_enabled) {
130
            return;
131
        }
132
        
133
        if (filter_var($params['matomo_url'], FILTER_VALIDATE_URL) === false) {
134
            FlashMessages::addMessage(I18N::translate('The Matomo URL provided is not valid.'), 'danger');
135
            return;
136
        }
137
        
138
        if (filter_var($params['matomo_siteid'], FILTER_VALIDATE_INT) === false) {
139
            FlashMessages::addMessage(I18N::translate('The Matomo Site ID provided is not valid.'), 'danger');
140
            return;
141
        }
142
        
143
        $this
144
            ->setBlockSetting($block_id, 'matomo_url', trim($params['matomo_url']))
145
            ->setBlockSetting($block_id, 'matomo_token', trim($params['matomo_token']))
146
            ->setBlockSetting($block_id, 'matomo_siteid', $params['matomo_siteid']);
147
        
148
        app('cache.files')->forget($this->name() . '-matomovisits-yearly-' . $block_id);
149
    }
150
    
151
    /**
152
     * Returns whether Matomo statistics is enabled for a specific MyArtJaub WelcomeBlock block
153
     *
154
     * @param int $block_id
155
     * @return bool
156
     */
157
    public function isMatomoEnabled(int $block_id): bool
158
    {
159
        return $this->getBlockSetting($block_id, 'matomo_enabled', 'no') === 'yes';
160
    }
161
    
162
    /**
163
     * Returns settings for retrieving Matomo statistics for a specific MyArtJaub WelcomeBlock block
164
     *
165
     * @param int $block_id
166
     * @return array<string, mixed>
167
     */
168
    public function matomoSettings(int $block_id): array
169
    {
170
        return [
171
            'matomo_enabled' => $this->isMatomoEnabled($block_id),
172
            'matomo_url' => $this->getBlockSetting($block_id, 'matomo_url'),
173
            'matomo_token' => $this->getBlockSetting($block_id, 'matomo_token'),
174
            'matomo_siteid'  => (int) $this->getBlockSetting($block_id, 'matomo_siteid', '0')
175
        ];
176
    }
177
}
178