1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics) |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as published |
9
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace App\Services\LabelSystem\PlaceholderProviders; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
use App\Entity\UserSystem\User; |
25
|
|
|
use IntlDateFormatter; |
26
|
|
|
use Locale; |
27
|
|
|
use Symfony\Component\Security\Core\Security; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Provides Placeholders for infos about global infos like Installation name or datetimes. |
31
|
|
|
* @package App\Services\LabelSystem\PlaceholderProviders |
32
|
|
|
*/ |
33
|
|
|
final class GlobalProviders implements PlaceholderProviderInterface |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
private $partdb_title; |
37
|
|
|
private $security; |
38
|
|
|
|
39
|
|
|
public function __construct(string $partdb_title, Security $security) |
40
|
|
|
{ |
41
|
|
|
$this->partdb_title = $partdb_title; |
42
|
|
|
$this->security = $security; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritDoc |
47
|
|
|
*/ |
48
|
|
|
public function replace(string $placeholder, object $label_target, array $options = []): ?string |
49
|
|
|
{ |
50
|
|
|
if ($placeholder === "[[INSTALL_NAME]]") { |
51
|
|
|
return $this->partdb_title; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
$user = $this->security->getUser(); |
56
|
|
|
if ($placeholder === "[[USERNAME]]") { |
57
|
|
|
if ($user instanceof User) { |
58
|
|
|
return $user->getName(); |
59
|
|
|
} |
60
|
|
|
return 'anonymous'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($placeholder === "[[USERNAME_FULL]]") { |
64
|
|
|
if ($user instanceof User) { |
65
|
|
|
return $user->getFullName(true); |
66
|
|
|
} |
67
|
|
|
return 'anonymous'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$now = new \DateTime(); |
71
|
|
|
|
72
|
|
|
if ($placeholder === '[[DATETIME]]') { |
73
|
|
|
$formatter = IntlDateFormatter::create( |
74
|
|
|
Locale::getDefault(), |
75
|
|
|
IntlDateFormatter::SHORT, |
76
|
|
|
IntlDateFormatter::SHORT, |
77
|
|
|
$now->getTimezone() |
|
|
|
|
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
return $formatter->format($now); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($placeholder === '[[DATE]]') { |
84
|
|
|
$formatter = IntlDateFormatter::create( |
85
|
|
|
Locale::getDefault(), |
86
|
|
|
IntlDateFormatter::SHORT, |
87
|
|
|
IntlDateFormatter::NONE, |
88
|
|
|
$now->getTimezone() |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
return $formatter->format($now); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($placeholder === '[[TIME]]') { |
95
|
|
|
$formatter = IntlDateFormatter::create( |
96
|
|
|
Locale::getDefault(), |
97
|
|
|
IntlDateFormatter::NONE, |
98
|
|
|
IntlDateFormatter::SHORT, |
99
|
|
|
$now->getTimezone() |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
return $formatter->format($now); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return null; |
106
|
|
|
} |
107
|
|
|
} |