|
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\Http\RequestHandlers; |
|
16
|
|
|
|
|
17
|
|
|
use Fig\Http\Message\StatusCodeInterface; |
|
18
|
|
|
use Fisharebest\Webtrees\Auth; |
|
19
|
|
|
use Fisharebest\Webtrees\I18N; |
|
20
|
|
|
use Fisharebest\Webtrees\Validator; |
|
21
|
|
|
use Fisharebest\Webtrees\Http\ViewResponseTrait; |
|
22
|
|
|
use Fisharebest\Webtrees\Services\ModuleService; |
|
23
|
|
|
use MyArtJaub\Webtrees\Module\WelcomeBlock\WelcomeBlockModule; |
|
24
|
|
|
use MyArtJaub\Webtrees\Module\WelcomeBlock\Services\MatomoStatsService; |
|
25
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
26
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
27
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
28
|
|
|
use Throwable; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Request handler for retrieving Matomo statistics |
|
32
|
|
|
*/ |
|
33
|
|
|
class MatomoStats implements RequestHandlerInterface |
|
34
|
|
|
{ |
|
35
|
|
|
use ViewResponseTrait; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var WelcomeBlockModule|null $module |
|
39
|
|
|
*/ |
|
40
|
|
|
private $module; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var MatomoStatsService $matomo_service |
|
44
|
|
|
*/ |
|
45
|
|
|
private $matomo_service; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Constructor for MatomoStats request handler |
|
49
|
|
|
* @param ModuleService $module_service |
|
50
|
|
|
* @param MatomoStatsService $matomo_service |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct( |
|
53
|
|
|
ModuleService $module_service, |
|
54
|
|
|
MatomoStatsService $matomo_service |
|
55
|
|
|
) { |
|
56
|
|
|
$this->module = $module_service->findByInterface(WelcomeBlockModule::class)->first(); |
|
57
|
|
|
$this->matomo_service = $matomo_service; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritDoc} |
|
62
|
|
|
* @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
63
|
|
|
*/ |
|
64
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
|
65
|
|
|
{ |
|
66
|
|
|
$this->layout = 'layouts/ajax'; |
|
67
|
|
|
|
|
68
|
|
|
if ($this->module === null) { |
|
69
|
|
|
return $this->viewResponse('errors/unhandled-exception', [ |
|
70
|
|
|
'error' => 'The attached module could not be found.' |
|
71
|
|
|
], StatusCodeInterface::STATUS_NOT_FOUND); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$block_id = Validator::attributes($request)->integer('block_id', -1); |
|
75
|
|
|
$nb_visits_year = $nb_visits_today = null; |
|
76
|
|
|
|
|
77
|
|
|
try { |
|
78
|
|
|
if ($block_id !== -1 && $this->module->isMatomoEnabled($block_id)) { |
|
79
|
|
|
$nb_visits_today = $this->matomo_service->visitsToday($this->module, $block_id) ?? 0; |
|
80
|
|
|
$nb_visits_year = ($this->matomo_service->visitsThisYear($this->module, $block_id) ?? 0) |
|
81
|
|
|
+ $nb_visits_today; |
|
82
|
|
|
} |
|
83
|
|
|
} catch (Throwable $ex) { |
|
84
|
|
|
return $this->viewResponse('errors/unhandled-exception', [ |
|
85
|
|
|
'error' => I18N::translate('Error while retrieving Matomo statistics: ') . |
|
86
|
|
|
(Auth::isAdmin() ? $ex->getMessage() : I18N::translate('Log in as admin for error details')) |
|
87
|
|
|
], StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $this->viewResponse($this->module->name() . '::matomo-stats', [ |
|
91
|
|
|
'visits_year' => $nb_visits_year, |
|
92
|
|
|
'visits_today' => $nb_visits_today |
|
93
|
|
|
]); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|