New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto-detect timezone and currency in installer #13092
Conversation
// Use server timezone as default. If server timezone is UTC, it is likely | ||
// a default not specified explicitly by the sysadm, so ignore this. | ||
$timezone = PIWIK_DEFAULT_TIMEZONE; | ||
if (in_array($timezone, array('UTC', 'Etc/UTC', 'GMT', 'Etc/GMT'))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it worth it to compare this all Lowercase
? Or maybe they cannot be set in different cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. It seems that PHP does indeed treat timezones as case-insensitive. I didn't know that.
$timezone = PIWIK_DEFAULT_TIMEZONE; | ||
if (in_array($timezone, array('UTC', 'Etc/UTC', 'GMT', 'Etc/GMT'))) { | ||
$timezone = null; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we need to check whether Matomo "knows" the timezone (present in $timezones
)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The value determines the default selected timezone in the select field. If an invalid timezone is submitted, QuickForm will just ignore it, so there is no need for further validation.
Awesome 👍 I've just installed the Intl extension, debugged it step by step and worked 👍 so valuable 👍 Be all good to merge but there is one problem... in some of our scripts (they are not in this repository) we are setting the default timezone and currency before creating the website. By applying this PR it would no longer work and they would be overwritten. We would need to add some checks to skip the detection if for some reason a default currency or timezone is already set. Like this: diff --git a/plugins/Installation/FormFirstWebsiteSetup.php b/plugins/Installation/FormFirstWebsiteSetup.php
index 13d4dbc4e3..e83a246b8f 100644
--- a/plugins/Installation/FormFirstWebsiteSetup.php
+++ b/plugins/Installation/FormFirstWebsiteSetup.php
@@ -15,6 +15,7 @@ use HTML_QuickForm2_Factory;
use HTML_QuickForm2_Rule;
use NumberFormatter;
use Piwik\Access;
+use Piwik\Option;
use Piwik\Piwik;
use Piwik\Plugins\SitesManager\API;
use Piwik\QuickForm2;
@@ -46,6 +47,11 @@ class FormFirstWebsiteSetup extends QuickForm2
$timezone = null;
}
+ if (Option::get(API::OPTION_DEFAULT_TIMEZONE)) {
+ // a default timezone was already configured
+ $timezone = null;
+ }
+
$this->addElement('text', 'siteName')
->setLabel(Piwik::translate('Installation_SetupWebSiteName'))
->addRule('required', Piwik::translate('General_Required', Piwik::translate('Installation_SetupWebSiteName')));
@@ -99,7 +105,7 @@ class Rule_isValidTimezone extends HTML_QuickForm2_Rule
}
// If intl extension is installed, get default currency from timezone country.
- if ($timezone && class_exists('NumberFormatter')) {
+ if (!Option::get(API::OPTION_DEFAULT_CURRENCY) && $timezone && class_exists('NumberFormatter')) {
try {
$zone = new DateTimeZone($timezone);
$location = $zone->getLocation(); |
@tsteur Can you provide an example of such a script? If the default timezone/currency is already stored in the database at this point, I might be able to retrieve it using |
I was thinking of The script would do eg
|
Done. |
Cheers 👍 |
* Auto-detect timezone and currency * Compare timezone case-insentively * Preserve existing defaults
To make it easier to install Matomo, the installer should try to auto-detect the timezone and currency.
If PHP or the operating system is configured to use a timezone other than UTC, it is highly likely that this timezone is relevant to use as the default timezone for Matomo. We should preselect this timezone in the timezone selector in the installer, so the user does not have to look for the proper timezone in the huge list of timezones.
Once we have the timezone, we can find the corresponding country and (if the Intl extension is installed) currency. This is likely a better default than the hard-coded default, USD.