-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSystemSettings.php
More file actions
139 lines (122 loc) · 5.27 KB
/
SystemSettings.php
File metadata and controls
139 lines (122 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\MicrosoftTeams;
use Piwik\Piwik;
use Piwik\Plugins\Marketplace\Api\Exception;
use Piwik\Settings\Setting;
use Piwik\Settings\FieldConfig;
class SystemSettings extends \Piwik\Settings\Plugin\SystemSettings
{
/** @var Setting */
public $clientID;
/** @var Setting */
public $clientSecret;
/** @var Setting */
public $clientSecretExpiryDate;
/** @var Setting */
public $tenantID;
/** @var Setting */
public $teamID;
protected function init()
{
// System setting --> allows selection of a single value
$this->clientID = $this->createClientIdSetting();
$this->clientSecret = $this->createClientSecretSetting();
$this->clientSecretExpiryDate = $this->createClientSecretExpiryDateSetting();
$this->tenantID = $this->createTenantIdSetting();
$this->teamID = $this->createTeamIdSetting();
}
private function createClientIdSetting()
{
return $this->makeSetting('teamsClientID', $default = '', FieldConfig::TYPE_STRING, function (FieldConfig $field) {
$field->title = Piwik::translate('MicrosoftTeams_ClientIdTitle');
$field->uiControl = FieldConfig::UI_CONTROL_PASSWORD;
$field->inlineHelp = Piwik::translate('MicrosoftTeams_ClientIdDescription', $this->getRequiredFieldsLearnMoreTranslation());
$field->transform = function ($value) {
return trim($value);
};
});
}
private function createClientSecretSetting()
{
return $this->makeSetting('teamsClientSecret', $default = '', FieldConfig::TYPE_STRING, function (FieldConfig $field) {
$field->title = Piwik::translate('MicrosoftTeams_ClientSecretTitle');
$field->uiControl = FieldConfig::UI_CONTROL_PASSWORD;
$field->inlineHelp = Piwik::translate('MicrosoftTeams_ClientSecretDescription', $this->getRequiredFieldsLearnMoreTranslation());
$field->transform = function ($value) {
return trim($value);
};
});
}
private function createClientSecretExpiryDateSetting()
{
return $this->makeSetting('teamsClientSecretExpiryDate', $default = '', FieldConfig::TYPE_STRING, function (FieldConfig $field) {
$field->title = Piwik::translate('MicrosoftTeams_ClientSecretExpiryDateTitle');
$field->uiControl = FieldConfig::UI_CONTROL_TEXT;
$field->uiControlAttributes = ['placeholder' => 'YYYY-MM-DD'];
$field->description = Piwik::translate('MicrosoftTeams_ClientSecretExpiryDateDescription');
$field->validate = function ($value) {
if (!empty(trim($value))) {
$date = trim($value);
$dateFormat = \DateTime::createFromFormat('Y-m-d', $date);
if (!$dateFormat || $dateFormat->format('Y-m-d') !== $date) {
throw new Exception('Invalid date format, allowed date format is YYYY-MM-DD');
}
}
};
$field->transform = function ($value) {
return trim($value);
};
});
}
private function createTenantIdSetting()
{
return $this->makeSetting('teamsTenantID', $default = '', FieldConfig::TYPE_STRING, function (FieldConfig $field) {
$field->title = Piwik::translate('MicrosoftTeams_TenantIdTitle');
$field->uiControl = FieldConfig::UI_CONTROL_PASSWORD;
$field->inlineHelp = Piwik::translate('MicrosoftTeams_TenantIdDescription', $this->getRequiredFieldsLearnMoreTranslation());
$field->transform = function ($value) {
return trim($value);
};
});
}
private function createTeamIdSetting()
{
return $this->makeSetting('teamsTeamID', $default = '', FieldConfig::TYPE_STRING, function (FieldConfig $field) {
$field->title = Piwik::translate('MicrosoftTeams_TeamIdTitle');
$field->uiControl = FieldConfig::UI_CONTROL_PASSWORD;
$field->inlineHelp = Piwik::translate('MicrosoftTeams_TeamIdDescription', $this->getRequiredFieldsLearnMoreTranslation());
$field->transform = function ($value) {
return trim($value);
};
});
}
public function isRequiredFieldsSet(): bool
{
return !empty($this->clientID->getValue())
&& !empty($this->clientSecret->getValue())
&& !empty($this->tenantID->getValue())
&& !empty($this->teamID->getValue());
}
public function getRequiredFieldsWithValue()
{
return [
'clientID' => $this->clientID->getValue(),
'clientSecret' => $this->clientSecret->getValue(),
'tenantID' => $this->tenantID->getValue(),
'teamID' => $this->teamID->getValue(),
];
}
private function getRequiredFieldsLearnMoreTranslation(): array
{
return [
'<a href="https://matomo.org/faq/reports/how-to-get-microsoft-teams-client-id-client-secret-tenant-id-and-teams-id/" target="_blank" rel="noopener noreferrer">',
'</a>',
];
}
}