1
0
mirror of https://github.com/freescout-helpdesk/freescout.git synced 2024-11-25 03:43:33 +01:00

Set timezone via Settings

This commit is contained in:
FreeScout 2018-11-24 23:16:20 -08:00
parent 42eff0f61b
commit cd88e1aa67
3 changed files with 290 additions and 151 deletions

View File

@ -48,16 +48,7 @@ class SettingsController extends Controller
public function getValidator($section)
{
switch ($section) {
case 'emails':
$rules = [
'settings.mail_from' => 'required|email',
];
break;
// default:
// $rules = \Event::fire('filter.settings_validate_rules');
// break;
}
$rules = $this->getSectionParams($section, 'validator_rules');
if (!empty($rules)) {
return Validator::make(request()->all(), $rules);
@ -66,22 +57,70 @@ class SettingsController extends Controller
public function getTemplateVars($section, $template_vars)
{
$section_vars = $this->getSectionParams($section, 'template_vars');
if ($section_vars && is_array($section_vars)) {
return array_merge($template_vars, $section_vars);
} else {
return $template_vars;
}
}
/**
* Parameters of the sections settings.
*
* If in settings parameter `env` is set, option will be saved into .env file
* instead of DB.
* @param [type] $section [description]
* @param string $param [description]
* @return [type] [description]
*/
public function getSectionParams($section, $param = '')
{
$params = [];
switch ($section) {
case 'emails':
$template_vars['sendmail_path'] = ini_get('sendmail_path');
$template_vars['mail_drivers'] = [
'mail' => __("PHP's mail() function"),
'sendmail' => __('Sendmail'),
'smtp' => 'SMTP',
$params = [
'template_vars' => [
'sendmail_path' => ini_get('sendmail_path'),
'mail_drivers' => [
'mail' => __("PHP's mail() function"),
'sendmail' => __('Sendmail'),
'smtp' => 'SMTP',
]
],
'validator_rules' => [
'settings.mail_from' => 'required|email',
],
];
break;
// default:
// $template_vars = \Event::fire('filter.settings_template_vars', [$template_vars]);
// break;
case 'general':
$params = [
'settings' => [
'locale' => [
'env' => 'APP_LOCALE'
],
'timezone' => [
'env' => 'APP_TIMEZONE'
],
],
];
break;
default:
$params = \Eventy::filter('settings.section_params', $params, $section);
break;
}
return $template_vars;
if ($param) {
if (isset($params[$param])) {
return $params[$param];
} else {
return null;
}
} else {
return $params;
}
}
public function getSectionSettings($section)
@ -98,6 +137,8 @@ class SettingsController extends Controller
'open_tracking' => Option::get('open_tracking'),
'enrich_customer_data' => Option::get('enrich_customer_data'),
'time_format' => Option::get('time_format', User::TIME_FORMAT_24),
'locale' => config('app.locale'),
'timezone' => config('app.timezone'),
];
break;
case 'emails':
@ -120,7 +161,7 @@ class SettingsController extends Controller
];
break;
default:
$settings = \Eventy::filter('settings.section_settings', $section, $settings);
$settings = \Eventy::filter('settings.section_settings', $settings, $section);
break;
}
@ -171,8 +212,17 @@ class SettingsController extends Controller
$request = \Eventy::filter('settings.before_save', $request, $section, $settings);
$cc_required = false;
$settings_params = $this->getSectionParams($section, 'settings');
foreach ($settings as $i => $option_name) {
// By some reason isset() does not work for empty elements
// Option has to be saved to .env file.
if (!empty($settings_params[$option_name]) && !empty($settings_params[$option_name]['env'])) {
\Helper::setEnvFileVar($settings_params[$option_name]['env'], $request->settings[$option_name] ?? '');
$cc_required = true;
continue;
}
// By some reason isset() does not work for empty elements.
if (array_key_exists($option_name, $request->settings)) {
$option_value = $request->settings[$option_name];
Option::set($option_name, $option_value);
@ -187,6 +237,11 @@ class SettingsController extends Controller
}
}
// Clear cache if some options have been saved to .env file.
if ($cc_required) {
\Helper::clearCache();
}
\Session::flash('flash_success_floating', __('Settings updated'));
return redirect()->route('settings', ['section' => $section]);

View File

@ -64,376 +64,380 @@ class Helper
'customers' => [],
];
/**
* Locales data.
* @var [type]
*/
public static $locales = [
'af' => ['name' => 'Afrikaans',
'en' => 'Afrikaans',
'af' => ['name' => 'Afrikaans',
'name_en' => 'Afrikaans',
],
'sq' => ['name' => 'Shqip',
'en' => 'Albanian',
'name_en' => 'Albanian',
],
'ar' => ['name' => 'العربية',
'en' => 'Arabic',
'name_en' => 'Arabic',
],
'ar_IQ' => ['name' => 'العربية',
'en' => 'Arabic (Iraq)',
'name_en' => 'Arabic (Iraq)',
],
'ar_LY' => ['name' => 'العربية',
'en' => 'Arabic (Libya)',
'name_en' => 'Arabic (Libya)',
],
'ar_MA' => ['name' => 'العربية',
'en' => 'Arabic (Morocco)',
'name_en' => 'Arabic (Morocco)',
],
'ar_OM' => ['name' => 'العربية',
'en' => 'Arabic (Oman)',
'name_en' => 'Arabic (Oman)',
],
'ar_SY' => ['name' => 'العربية',
'en' => 'Arabic (Syria)',
'name_en' => 'Arabic (Syria)',
],
'ar_LB' => ['name' => 'العربية',
'en' => 'Arabic (Lebanon)',
'name_en' => 'Arabic (Lebanon)',
],
'ar_AE' => ['name' => 'العربية',
'en' => 'Arabic (U.A.E.)',
'name_en' => 'Arabic (U.A.E.)',
],
'ar_QA' => ['name' => 'العربية',
'en' => 'Arabic (Qatar)',
'name_en' => 'Arabic (Qatar)',
],
'ar_SA' => ['name' => 'العربية',
'en' => 'Arabic (Saudi Arabia)',
'name_en' => 'Arabic (Saudi Arabia)',
],
'ar_EG' => ['name' => 'العربية',
'en' => 'Arabic (Egypt)',
'name_en' => 'Arabic (Egypt)',
],
'ar_DZ' => ['name' => 'العربية',
'en' => 'Arabic (Algeria)',
'name_en' => 'Arabic (Algeria)',
],
'ar_TN' => ['name' => 'العربية',
'en' => 'Arabic (Tunisia)',
'name_en' => 'Arabic (Tunisia)',
],
'ar_YE' => ['name' => 'العربية',
'en' => 'Arabic (Yemen)',
'name_en' => 'Arabic (Yemen)',
],
'ar_JO' => ['name' => 'العربية',
'en' => 'Arabic (Jordan)',
'name_en' => 'Arabic (Jordan)',
],
'ar_KW' => ['name' => 'العربية',
'en' => 'Arabic (Kuwait)',
'name_en' => 'Arabic (Kuwait)',
],
'ar_BH' => ['name' => 'العربية',
'en' => 'Arabic (Bahrain)',
'name_en' => 'Arabic (Bahrain)',
],
'eu' => ['name' => 'Euskara',
'en' => 'Basque',
'name_en' => 'Basque',
],
'be' => ['name' => 'Беларуская',
'en' => 'Belarusian',
'name_en' => 'Belarusian',
],
'bn' => ['name' => 'বাংলা',
'en' => 'Bengali',
'name_en' => 'Bengali',
],
'bg' => ['name' => 'Български език',
'en' => 'Bulgarian',
'name_en' => 'Bulgarian',
],
'ca' => ['name' => 'Català',
'en' => 'Catalan',
'name_en' => 'Catalan',
],
'zh_CN' => ['name' => '简体中文',
'en' => 'Chinese (Simplified)',
'name_en' => 'Chinese (Simplified)',
],
'zh_SG' => ['name' => '简体中文',
'en' => 'Chinese (Singapore)',
'name_en' => 'Chinese (Singapore)',
],
'zh_TW' => ['name' => '简体中文',
'en' => 'Chinese (Traditional)',
'name_en' => 'Chinese (Traditional)',
],
'zh_HK' => ['name' => '简体中文',
'en' => 'Chinese (Hong Kong SAR)',
'name_en' => 'Chinese (Hong Kong SAR)',
],
'hr' => ['name' => 'Hrvatski',
'en' => 'Croatian',
'name_en' => 'Croatian',
],
'cs' => ['name' => 'Čeština',
'en' => 'Czech',
'name_en' => 'Czech',
],
'da' => ['name' => 'Dansk',
'en' => 'Danish',
'name_en' => 'Danish',
],
'nl' => ['name' => 'Nederlands',
'en' => 'Dutch',
'name_en' => 'Dutch',
],
// 'nl_BE' => ['name' => 'Nederlands',
// 'en' => 'Dutch (Belgium)',
// 'name_en' => 'Dutch (Belgium)',
// ],
// 'en_US' => ['name' => 'English',
// 'en' => 'English (United States)',
// 'name_en' => 'English (United States)',
// ],
// 'en_AU' => ['name' => '',
// 'en' => 'English (Australia)',
// 'name_en' => 'English (Australia)',
// ],
// 'en_NZ' => ['name' => '',
// 'en' => 'English (New Zealand)',
// 'name_en' => 'English (New Zealand)',
// ],
// 'en_ZA' => ['name' => '',
// 'en' => 'English (South Africa)',
// 'name_en' => 'English (South Africa)',
// ],
'en' => ['name' => 'English',
'en' => 'English',
'name_en' => 'English',
],
// 'en_TT' => ['name' => '',
// 'en' => 'English (Trinidad)',
// 'name_en' => 'English (Trinidad)',
// ],
// 'en_GB' => ['name' => '',
// 'en' => 'English (United Kingdom)',
// 'name_en' => 'English (United Kingdom)',
// ],
// 'en_CA' => ['name' => '',
// 'en' => 'English (Canada)',
// 'name_en' => 'English (Canada)',
// ],
// 'en_IE' => ['name' => '',
// 'en' => 'English (Ireland)',
// 'name_en' => 'English (Ireland)',
// ],
// 'en_JM' => ['name' => '',
// 'en' => 'English (Jamaica)',
// 'name_en' => 'English (Jamaica)',
// ],
// 'en_BZ' => ['name' => '',
// 'en' => 'English (Belize)',
// 'name_en' => 'English (Belize)',
// ],
'et' => ['name' => 'Eesti',
'en' => 'Estonian',
'name_en' => 'Estonian',
],
'fo' => ['name' => 'Føroyskt',
'en' => 'Faeroese',
'name_en' => 'Faeroese',
],
'fa' => ['name' => 'فارسی',
'en' => 'Farsi',
'name_en' => 'Farsi',
],
'fi' => ['name' => 'Suomi',
'en' => 'Finnish',
'name_en' => 'Finnish',
],
'fr' => ['name' => 'Français',
'en' => 'French',
'name_en' => 'French',
],
// 'fr_CA' => ['name' => '',
// 'en' => 'French (Canada)',
// 'name_en' => 'French (Canada)',
// ],
// 'fr_LU' => ['name' => '',
// 'en' => 'French (Luxembourg)',
// 'name_en' => 'French (Luxembourg)',
// ],
// 'fr_BE' => ['name' => '',
// 'en' => 'French (Belgium)',
// 'name_en' => 'French (Belgium)',
// ],
// 'fr_CH' => ['name' => '',
// 'en' => 'French (Switzerland)',
// 'name_en' => 'French (Switzerland)',
// ],
'gd' => ['name' => 'Gàidhlig',
'en' => 'Gaelic (Scotland)',
'name_en' => 'Gaelic (Scotland)',
],
'de' => ['name' => 'Deutsch',
'en' => 'German',
'name_en' => 'German',
],
// 'de_CH' => ['name' => '',
// 'en' => 'German (Switzerland)',
// 'name_en' => 'German (Switzerland)',
// ],
// 'de_LU' => ['name' => '',
// 'en' => 'German (Luxembourg)',
// 'name_en' => 'German (Luxembourg)',
// ],
// 'de_AT' => ['name' => '',
// 'en' => 'German (Austria)',
// 'name_en' => 'German (Austria)',
// ],
// 'de_LI' => ['name' => '',
// 'en' => 'German (Liechtenstein)',
// 'name_en' => 'German (Liechtenstein)',
// ],
'el' => ['name' => 'Ελληνικά',
'en' => 'Greek',
'name_en' => 'Greek',
],
'he' => ['name' => 'עברית',
'en' => 'Hebrew',
'name_en' => 'Hebrew',
],
'hi' => ['name' => 'हिन्दी',
'en' => 'Hindi',
'name_en' => 'Hindi',
],
'hu' => ['name' => 'Magyar',
'en' => 'Hungarian',
'name_en' => 'Hungarian',
],
'is' => ['name' => 'Íslenska',
'en' => 'Icelandic',
'name_en' => 'Icelandic',
],
'id' => ['name' => 'Bahasa Indonesia',
'en' => 'Indonesian',
'name_en' => 'Indonesian',
],
'ga' => ['name' => 'Gaeilge',
'en' => 'Irish',
'name_en' => 'Irish',
],
'it' => ['name' => 'Italiano',
'en' => 'Italian',
'name_en' => 'Italian',
],
// 'it_CH' => ['name' => 'Italiano',
// 'en' => 'Italian (Switzerland)',
// 'name_en' => 'Italian (Switzerland)',
// ],
'ja' => ['name' => '日本語',
'en' => 'Japanese',
'name_en' => 'Japanese',
],
'ko' => ['name' => '한국어 (韓國語)',
'en' => 'Korean (Johab)',
'name_en' => 'Korean (Johab)',
],
'lv' => ['name' => 'Latviešu valoda',
'en' => 'Latvian',
'name_en' => 'Latvian',
],
'lt' => ['name' => 'Lietuvių kalba',
'en' => 'Lithuanian',
'name_en' => 'Lithuanian',
],
'mk' => ['name' => 'Македонски јазик',
'en' => 'Macedonian (FYROM)',
'name_en' => 'Macedonian (FYROM)',
],
'ms' => ['name' => 'Bahasa Melayu, بهاس ملايو',
'en' => 'Malay',
'name_en' => 'Malay',
],
'mt' => ['name' => 'Malti',
'en' => 'Maltese',
'name_en' => 'Maltese',
],
'ne' => ['name' => 'नेपाली',
'en' => 'Nepali',
'name_en' => 'Nepali',
],
'no' => ['name' => 'Norsk bokmål',
'en' => 'Norwegian (Bokmal)',
'name_en' => 'Norwegian (Bokmal)',
],
'pl' => ['name' => 'Polski',
'en' => 'Polish',
'name_en' => 'Polish',
],
'pt' => ['name' => 'Português',
'en' => 'Portuguese (Portugal)',
'name_en' => 'Portuguese (Portugal)',
],
'pt_BR' => ['name' => 'Português do Brasil',
'en' => 'Portuguese (Brazil)',
'name_en' => 'Portuguese (Brazil)',
],
'ro' => ['name' => 'Română',
'en' => 'Romanian',
'name_en' => 'Romanian',
],
// 'ro_MO' => ['name' => 'Română',
// 'en' => 'Romanian (Republic of Moldova)',
// 'name_en' => 'Romanian (Republic of Moldova)',
// ],
'rm' => ['name' => 'Rumantsch grischun',
'en' => 'Romansh',
'name_en' => 'Romansh',
],
'ru' => ['name' => 'Русский',
'en' => 'Russian',
'name_en' => 'Russian',
],
// 'ru_MO' => ['name' => '',
// 'en' => 'Russian (Republic of Moldova)',
// 'name_en' => 'Russian (Republic of Moldova)',
// ],
'sz' => ['name' => 'Davvisámegiella',
'en' => 'Sami (Lappish)',
'name_en' => 'Sami (Lappish)',
],
'sr' => ['name' => 'Српски језик',
'en' => 'Serbian (Latin)',
'name_en' => 'Serbian (Latin)',
],
'sk' => ['name' => 'Slovenčina',
'en' => 'Slovak',
'name_en' => 'Slovak',
],
'sl' => ['name' => 'Slovenščina',
'en' => 'Slovenian',
'name_en' => 'Slovenian',
],
/*'sb' => ['name' => 'Serbsce',
'en' => 'Sorbian',
'name_en' => 'Sorbian',
],*/
'es' => ['name' => 'Español',
'en' => 'Spanish',
'name_en' => 'Spanish',
],
// 'es_GT' => ['name' => '',
// 'en' => 'Spanish (Guatemala)',
// 'name_en' => 'Spanish (Guatemala)',
// ],
// 'es_PA' => ['name' => '',
// 'en' => 'Spanish (Panama)',
// 'name_en' => 'Spanish (Panama)',
// ],
// 'es_VE' => ['name' => '',
// 'en' => 'Spanish (Venezuela)',
// 'name_en' => 'Spanish (Venezuela)',
// ],
// 'es_PE' => ['name' => '',
// 'en' => 'Spanish (Peru)',
// 'name_en' => 'Spanish (Peru)',
// ],
// 'es_EC' => ['name' => '',
// 'en' => 'Spanish (Ecuador)',
// 'name_en' => 'Spanish (Ecuador)',
// ],
// 'es_UY' => ['name' => '',
// 'en' => 'Spanish (Uruguay)',
// 'name_en' => 'Spanish (Uruguay)',
// ],
// 'es_BO' => ['name' => '',
// 'en' => 'Spanish (Bolivia)',
// 'name_en' => 'Spanish (Bolivia)',
// ],
// 'es_HN' => ['name' => '',
// 'en' => 'Spanish (Honduras)',
// 'name_en' => 'Spanish (Honduras)',
// ],
// 'es_PR' => ['name' => '',
// 'en' => 'Spanish (Puerto Rico)',
// 'name_en' => 'Spanish (Puerto Rico)',
// ],
// 'es_MX' => ['name' => '',
// 'en' => 'Spanish (Mexico)',
// 'name_en' => 'Spanish (Mexico)',
// ],
// 'es_CR' => ['name' => '',
// 'en' => 'Spanish (Costa Rica)',
// 'name_en' => 'Spanish (Costa Rica)',
// ],
// 'es_DO' => ['name' => '',
// 'en' => 'Spanish (Dominican Republic)',
// 'name_en' => 'Spanish (Dominican Republic)',
// ],
// 'es_CO' => ['name' => '',
// 'en' => 'Spanish (Colombia)',
// 'name_en' => 'Spanish (Colombia)',
// ],
// 'es_AR' => ['name' => '',
// 'en' => 'Spanish (Argentina)',
// 'name_en' => 'Spanish (Argentina)',
// ],
// 'es_CL' => ['name' => '',
// 'en' => 'Spanish (Chile)',
// 'name_en' => 'Spanish (Chile)',
// ],
// 'es_PY' => ['name' => '',
// 'en' => 'Spanish (Paraguay)',
// 'name_en' => 'Spanish (Paraguay)',
// ],
// 'es_SV' => ['name' => '',
// 'en' => 'Spanish (El Salvador)',
// 'name_en' => 'Spanish (El Salvador)',
// ],
// 'es_NI' => ['name' => '',
// 'en' => 'Spanish (Nicaragua)',
// 'name_en' => 'Spanish (Nicaragua)',
// ],
'sv' => ['name' => 'Svenska',
'en' => 'Swedish',
'name_en' => 'Swedish',
],
// unknown
// 'sx' => ['name' => '',
// 'en' => 'Sutu',
// 'name_en' => 'Sutu',
// ],
// 'sv_FI' => ['name' => '',
// 'en' => 'Swedish (Finland)',
// 'name_en' => 'Swedish (Finland)',
// ],
'th' => ['name' => 'ไทย',
'en' => 'Thai',
'name_en' => 'Thai',
],
'ts' => ['name' => 'Xitsonga',
'en' => 'Tsonga',
'name_en' => 'Tsonga',
],
'tn' => ['name' => 'Setswana',
'en' => 'Tswana',
'name_en' => 'Tswana',
],
'tr' => ['name' => 'Türkçe',
'en' => 'Turkish',
'name_en' => 'Turkish',
],
'uk' => ['name' => 'українська',
'en' => 'Ukrainian',
'name_en' => 'Ukrainian',
],
'ur' => ['name' => 'اردو',
'en' => 'Urdu',
'name_en' => 'Urdu',
],
've' => ['name' => 'Tshivenḓa',
'en' => 'Venda',
'name_en' => 'Venda',
],
'vi' => ['name' => 'Tiếng Việt',
'en' => 'Vietnamese',
'name_en' => 'Vietnamese',
],
'xh' => ['name' => 'isiXhosa',
'en' => 'Xhosa',
'name_en' => 'Xhosa',
],
'ji' => ['name' => 'ייִדיש',
'en' => 'Yiddish',
'name_en' => 'Yiddish',
],
'zu' => ['name' => 'isiZulu',
'en' => 'Zulu',
'name_en' => 'Zulu',
],
];
@ -756,4 +760,68 @@ class Helper
return false;
}
}
/**
* Get locale's data.
* @param [type] $locale [description]
* @param string $param [description]
* @return [type] [description]
*/
public static function getLocaleData($locale, $param = '')
{
if (!empty(self::$locales[$locale])) {
$data = self::$locales[$locale];
} else {
return null;
}
if ($param) {
if (isset(self::$locales[$locale])) {
return self::$locales[$locale];
} else {
return null;
}
} else {
return $data;
}
}
/**
* Clear application cache.
* @return [type] [description]
*/
public static function clearCache()
{
\Artisan::call('freescout:clear-cache');
}
/**
* Set variable in .evn file.
*/
public static function setEnvFileVar($key, $value)
{
$env_path = app()->environmentFilePath();
$contents = file_get_contents($env_path);
// Maybe validate value and add quotes.
// str_contains($key, '=')
// !preg_match('/^[a-zA-Z_]+$/', $key)
$old_value = '';
// Match the given key at the beginning of a line
preg_match("/^{$key}=[^\r\n]*/m", $contents, $matches);
if (count($matches)) {
$old_value = substr($matches[0], strlen($key) + 1);
}
if ($old_value) {
// Replace.
$contents = str_replace("{$key}={$old_value}", "{$key}={$value}", $contents);
\File::put($env_path, $contents);
} else {
// Add.
$contents = $contents . "\n{$key}={$value}\n";
\File::put($env_path, $contents);
}
}
}

View File

@ -1,4 +1,4 @@
<form class="form-horizontal margin-top" method="POST" action="">
<form class="form-horizontal margin-top margin-bottom" method="POST" action="">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('settings[company_name]') ? ' has-error' : '' }}">
@ -51,7 +51,7 @@
</div>
</div>
</div>
<p class="help-block">
<p class="form-help">
{{ __('Add "Powered by :app_name" footer text to the outgoing emails to invite more developers to the project and make application better.', ['app_name' => \Config::get('app.name')]) }}
</p>
@include('partials/field_error', ['field'=>'settings.email_branding'])
@ -92,17 +92,33 @@
</div>
</div>
<div class="form-group{{ $errors->has('settings[locale]') ? ' has-error' : '' }}">
<label for="locale" class="col-sm-2 control-label">{{ __('Default Language') }}</label>
<div class="col-sm-6">
<select id="locale" class="form-control input-sized" name="settings[locale]" required autofocus>
@include('partials/locale_options', ['selected' => old('settings[locale]', \Config::get('app.locale'))])
</select>
{{--<div class="help-block">
{{ __('Value is set in .env file using APP_TIMEZONE parameter.') }}
</div>--}}
@include('partials/field_error', ['field'=>'settings.timezone'])
</div>
</div>
<div class="form-group{{ $errors->has('settings[timezone]') ? ' has-error' : '' }}">
<label for="timezone" class="col-sm-2 control-label">{{ __('Timezone') }}</label>
<div class="col-sm-6">
<select id="timezone" disabled="disabled" class="form-control input-sized" name="settings[timezone]" required autofocus>
<select id="timezone" class="form-control input-sized" name="settings[timezone]" required autofocus>
@include('partials/timezone_options', ['current_timezone' => old('settings[timezone]', \Config::get('app.timezone'))])
</select>
<div class="help-block">
{{--<div class="help-block">
{{ __('Value is set in .env file using APP_TIMEZONE parameter.') }}
</div>
</div>--}}
@include('partials/field_error', ['field'=>'settings.timezone'])
</div>