插件设置

设置类型

Matomo(以前的Piwik)平台区分“系统设置”,“用户设置”和“可测量设置”:

用户设置可以由任何登录用户配置,每个用户可以独立配置设置。Matomo平台确保每个用户都存储了设置,并且用户不能看到其他用户的配置。用户可以在“个人设置”页面更改设置。

系统设置适用于所有用户。只能由具有超级用户访问权限的用户配置。系统设置将出现在“常规设置”页面。

可测量的设置添加新字段时,创建或编辑一个网站或另一个可衡量的,如移动应用程序。这些设置的值可以由任何用户对特定网站的管理访问更改,设置分别保存为每个网站。网站管理器中显示的所有字段实际上都是可测量的设置,这些可以用来创建全新的类型,如“移动应用程序”,“汽车”,“嵌入式设备”等。

所有这些类都扩展了设置类。

添加设置

创建设置类

Matomo可以创造设置类的控制台

$ ./console generate:设置

该命令将要求您输入插件的名称和您想要创建的设置类型。根据所选的类型,它将创建一个名为UserSettings.phpSystemSettings.phpMeasurableSettings.php例如,插件/编写MyPlugin / SystemSettings.php.这个创建的文件包含一些示例供您入门。设置的创建和定义在不同类型中是相同的。

要查看操作中的设置,请转到管理>常规设置在你的Matomo安装。

添加一个或多个设置

设置将在init ()设置类的方法。要做到这一点,请调用makeSetting ()并传递设置的内部名称、默认值(设置应该返回的PHP类型)和一个回调来配置表单字段的UI表示。

例如:

SystemSettings类扩展\Piwik\Settings\Plugin\SystemSettings {/** @var Setting */ public $autoRefresh;/** @var设置*/ public $refreshInterval;protected函数init() {$this->autoRefresh = $this->createAutoRefreshSetting();$this->refreshInterval = $this-> createrrefreshintervalsetting ();}私有函数createAutoRefreshSetting(){返回$this-> makessetting ('autoRefresh', $default = false, FieldConfig::TYPE_BOOL,函数(FieldConfig $field) {$field->title = '自动刷新';$field->uiControl = FieldConfig::UI_CONTROL_CHECKBOX;$field->description = '如果启用,该值将根据指定的时间间隔自动刷新';});}私有函数createrrefreshintervalsetting(){返回$this-> makessetting ('refreshInterval', $default = '3', FieldConfig::TYPE_INT, function (FieldConfig $field) {$field->title = '刷新间隔';$field->uiControl = FieldConfig::UI_CONTROL_TEXT; $field->uiControlAttributes = array('size' => 3); $field->inlineHelp = 'Enter a number which is >= 15'; $field->introduction = 'New group of settings'; $field->description = 'Defines how often the value should be updated'; $field->validate = function ($value, $setting) { if ($value < 15) { throw new \Exception('Value is invalid'); } }; }); } }

有关每个设置的可能属性的列表,请参阅设置而且FieldConfigAPI参考。另请参阅ExampleSettingsPlugin看看还有什么可能。

字段配置

您可能想知道为什么在进行设置时将一些属性配置为参数,而在回调方法中配置一些属性。这样做的原因是性能,因为我们通常为每个请求创建所有设置。的回调中配置的所有内容美元FieldConfig领域仅当设置将在UI中显示时才需要。在其他所有时候,字段配置都是无关紧要的,我们通过不执行这些操作来节省时间。特别是因为一些设置可能会在这个回调中执行API请求来获得可用值列表等。

在配置文件中配置系统设置的值

系统设置不能仅通过UI进行配置,也可以通过配置/ config.ini.php文件。例如插件并且可以为设置配置值refreshInterval是这样的:

[MyPlugin] refreshInterval = 15

一旦配置文件中的值被配置,就不可能再在UI中更改该设置的值,设置甚至不会显示。

限制谁可以在UI中配置设置

例如,系统设置只能由默认具有超级用户访问权限的用户配置。方法自定义此行为setIsWritableByCurrentUser方法。例如,您可以定义为只允许名为“MyRootUser”的用户更改设置。所有其他用户将无法看到该设置的值,也无法更改它。

$this->autoRefresh = $this->createAutoRefreshSetting();$login = \Piwik\Piwik::getCurrentUserLogin();$this->autoRefresh->setIsWritableByCurrentUser($login == 'MyRootUser');

从UI中移除一个设置

有时你可能想要创建一个设置,但在UI中根本不可见。例如,如果您使一个设置的可见性依赖于另一个设置,或者您只希望用户通过配置/ config.ini.php.确保不能通过UI调用更改设置setIsWritableByCurrentUser(假)

$this->autoRefresh = $this->createAutoRefreshSetting();$login = \Piwik\Piwik::getCurrentUserLogin();$ this - > autoRefresh - > setIsWritableByCurrentUser(假);

动态地在UI中显示或隐藏设置

有时您可能会有一些更复杂的表单,其中一个设置应该仅在以某种方式配置另一个设置时才可见。Matomo可以动态地显示或隐藏设置,而无需根据特定条件重新加载。假设我们想要这样的设定refreshInterval只有在以下情况下才可见autoRefresh启用后,我们可以这样做:

$this-> makessetting ('refreshInterval', $default = '3', FieldConfig::TYPE_INT, function (FieldConfig $field) {$field->条件= 'autoRefresh';//代替它也可以写eg 'autoRefresh == 1'或'autoRefresh == true' //多个条件可以组合,如'autoRefresh == 1 && anotherSetting == "foobar"'});

读取设置值

您可以在小部件、控制器、报告或任何您想要的地方访问设置的值。要访问该值,请创建设置类的实例并获得如下所示的值。

系统设置

$settings = new \Piwik\Plugins\MyPlugin\SystemSettings();$interval = $settings->refreshInterval->getValue()

用户设置

$settings = new \Piwik\Plugins\MyPlugin\UserSettings();$interval = $settings->refreshInterval->getValue()

可测量的设置

$settings = new \Piwik\Plugins\MyPlugin\MeasurableSettings($idSite);$interval = $settings->refreshInterval->getValue()
Baidu