@c960657 opened this Pull Request on June 21st 2018 Contributor

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.

@tsteur commented on September 16th 2018 Member

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();
@c960657 commented on September 17th 2018 Contributor

@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 API::getInstance()->getDefaultTimezone() and ->getDefaultCurrency() and use that to bypass the detection logic?

@tsteur commented on September 17th 2018 Member

I was thinking of API::getInstance()->getDefaultTimezone() as well initially but then you need to know the default value as it always returns a value like (API::getInstance()->getDefaultTimezone() !== 'UTC') and if this value changes it could break etc. And the default value for currency is USD and you wouldn't know whether it was set manually before or whether it is the default value

The script would do eg

// php timezone might be "Europe/Berlin" for example
$sitesApi->setDefaultTimezone('abcdef...');
$sitesApi->setDefaultCurrency('USD');
$formWebsiteSetup = new FormFirstWebsiteSetup();
...
@c960657 commented on September 20th 2018 Contributor

Done.

@tsteur commented on September 20th 2018 Member

Cheers 👍

This Pull Request was closed on September 20th 2018
Powered by GitHub Issue Mirror