Skip to content
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

User can choose language and timezone #60

Merged
merged 7 commits into from
Feb 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions resources/lang/en/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
'new_password' => 'New password',
'next' => 'Next',
'nick_name' => 'Nick name',
'no_option_chosen' => 'No option chosen',
'no_ratings' => 'No ratings',
'no_translation' => 'No translation',
'not_a_member' => 'Not a member yet?',
Expand Down
18 changes: 10 additions & 8 deletions resources/lang/en/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
# modified at 13-06-2014 12:53

return [
'admin_panel' => 'Admin panel',
'edit_account' => 'Edit Account',
'get_help' => 'Get Help',
'my_account' => 'My Account',
'oauth' => 'OAuth',
'user' => 'User',
'invalid_language' => 'This language is invalid',
'invalid_timezone' => 'This timezone does not exist',
'admin_panel' => 'Admin panel',
'edit_account' => 'Edit Account',
'get_help' => 'Get Help',
'my_account' => 'My Account',
'oauth' => 'OAuth',
'user' => 'User',
'invalid_language' => 'This language is invalid',
'invalid_timezone' => 'This timezone does not exist',
'choose_preferred_language' => 'Preferred language',
'choose_preferred_timezone' => 'Preferred timezone',
];
1 change: 1 addition & 0 deletions resources/lang/pl/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
'new_password' => 'Nowe hasło',
'next' => 'Następny',
'nick_name' => 'Nick',
'no_option_chosen' => 'Nie wybrano',
'no_ratings' => 'Brak ocen',
'no_translation' => 'Brak tłumaczenia',
'not_a_member' => 'Nie masz jeszcze konta?',
Expand Down
18 changes: 10 additions & 8 deletions resources/lang/pl/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
# modified at 13-06-2014 12:53

return [
'admin_panel' => 'Zarządzaj',
'edit_account' => 'Edytuj Konto',
'get_help' => 'Pomoc',
'my_account' => 'Moje Konto',
'oauth' => 'OAuth',
'user' => 'Użytkownik',
'invalid_language' => 'Ten język nie istnieje',
'invalid_timezone' => 'Ta strefa czasowa jest niepoprawna',
'admin_panel' => 'Zarządzaj',
'edit_account' => 'Edytuj Konto',
'get_help' => 'Pomoc',
'my_account' => 'Moje Konto',
'oauth' => 'OAuth',
'user' => 'Użytkownik',
'invalid_language' => 'Ten język nie istnieje',
'invalid_timezone' => 'Ta strefa czasowa jest niepoprawna',
'choose_preferred_language' => 'Preferowany język',
'choose_preferred_timezone' => 'Preferowana strefa czasowa',
];
62 changes: 56 additions & 6 deletions resources/views/account/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,55 @@ class="form-control{{ $errors->first('last_name') ? ' is-invalid' : '' }}"
<p class="form-control-static">{{ $user->lastName() }}</p>
</div>
@endif

<div class="form-group">
<label class="control-label" for="language_code">
@lang('gzero-core::user.choose_preferred_language')
</label>
<select id="language_code" name="language_code"
class="form-control{{ $errors->first('language_code') ? ' is-invalid' : '' }}">
@if($user->languageCode() === null)
<option value="" selected>
@lang('gzero-core::common.no_option_chosen')
</option>
@endif
@foreach($languages as $language)
<option value="{{ $language->code }}"
@if($language->code === $user->languageCode())selected @endif>
@lang('gzero-core::language_names.' . $language->code)
</option>
@endforeach
</select>
@if($errors->first('language_code'))
<div class="invalid-feedback">{{ $errors->first('language_code') }}</div>
@endif
</div>

<div class="form-group">
<label class="control-label" for="timezone">
@lang('gzero-core::user.choose_preferred_timezone')
</label>
<select id="timezone" name="timezone"
class="form-control{{ $errors->has('timezone') ? ' is-invalid' : '' }}"
>
<option value="" @if(empty($user->timezone)) selected @endif>
@lang('gzero-core::common.no_option_chosen')
</option>
@foreach($timezones as $timezone)
<option value="{{ $timezone['name'] }}"
@if($user->timezone() == $timezone['name'])
selected
@endif
>
{{ ($timezone['offset'] ? 'GMT ' . $timezone['offset'] . ' ' : 'GMT ') . $timezone['name'] }}
</option>
@endforeach
</select>
@if($errors->first('timezone'))
<div class="invalid-feedback">{{ $errors->first('timezone') }}</div>
@endif
</div>

@if($isUserEmailSet)
@if($user->password())
<div class="separator">
Expand All @@ -98,7 +147,8 @@ class="form-control{{ $errors->first('password') ? ' is-invalid' : '' }}"
@endif
</div>
<div class="form-group">
<label class="control-label" for="passwordConfirmation">@lang('gzero-core::common.password_repeat')</label>
<label class="control-label"
for="passwordConfirmation">@lang('gzero-core::common.password_repeat')</label>
<input type="password" id="passwordConfirmation" name="password_confirmation"
class="form-control{{ $errors->first('password_confirmation') ? ' is-invalid' : '' }}"
placeholder="@lang('gzero-core::common.password_repeat')">
Expand Down Expand Up @@ -137,13 +187,13 @@ class="form-control{{ $errors->first('password_confirmation') ? ' is-invalid' :
type: 'PATCH',
success: function(xhr) {
@if($isUserEmailSet)
Loading.stop();
Loading.stop();
// set success message
setGlobalMessage('success', "@lang('gzero-core::common.changes_saved_message')");
hideMessages();
clearFormValidationErrors();
@else
location.reload();
location.reload();
@endif
},
error: function(xhr) {
Expand All @@ -154,11 +204,11 @@ class="form-control{{ $errors->first('password_confirmation') ? ' is-invalid' :
$.each(xhr.responseJSON.error.errors, function(index, error) {
// set form errors
setFormValidationErrors(index, error);
});
})
}
}
});
})
})
});
})
</script>
@stop
14 changes: 14 additions & 0 deletions resources/views/account/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@
</p>
@endif

<p>
<strong>@lang('gzero-core::user.choose_preferred_language'):</strong>
@lang('gzero-core::' . ($user->languageCode() ? 'language_names.' . $user->languageCode() : 'common.no_option_chosen'))
</p>

<p>
<strong>@lang('gzero-core::user.choose_preferred_timezone'):</strong>
@if(empty($user->timezone()))
@lang('gzero-core::common.no_option_chosen')
@else
{{ $timezone = $user->timezone() }}
@endif
</p>

<a href="{{ route('account.edit') }}" title="@lang('gzero-core::user.edit_account')" class="btn btn-outline-primary">
@lang('gzero-core::user.edit_account')
</a>
Expand Down
19 changes: 17 additions & 2 deletions resources/views/auth/register.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class="form-control{{ $errors->has('last_name') ? ' is-invalid' : '' }}"
<div class="invalid-feedback">{{ $errors->first('last_name') }}</div>
@endif
</div>

<input type="hidden" name="language_code" value="{{ $language->code }}">
<input type="hidden" name="timezone" value="">

<div class="form-group">
<label class="control-label" for="password">@lang('gzero-core::common.password')</label>
<input id="password" type="password" name="password"
Expand All @@ -57,8 +61,6 @@ class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}"
@endif
</div>

<input type="hidden" name="language_code" value="{{ app()->getLocale() }}">

<div class="form-group">
<button type="submit" class="btn btn-primary btn-lg btn-block">
@lang('gzero-core::common.register')
Expand All @@ -72,3 +74,16 @@ class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}"
</div>
</div>
@endsection

@section('footerScripts')
@parent
<script type="text/javascript">
$(function() {
userTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
if(typeof userTimezone !== undefined) {
timezoneField = $('#register-account-form').find('input[name="timezone"]');
timezoneField.attr('value', userTimezone);
}
});
</script>
@stop
2 changes: 1 addition & 1 deletion resources/views/layouts/_navbar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</a>
<div class="dropdown-menu" aria-labelledby="languageDropdown">
@foreach($languages as $language)
<a href="{{ route('home', $language->code) }}"
<a href="{{ routeMl('home', $language->code) }}"
class="dropdown-item {{ (app()->getLocale() == $language->code) ? 'active' : '' }}"
title="{{$language->code}}">
@lang('gzero-core::language_names.' . $language->code)
Expand Down
19 changes: 18 additions & 1 deletion src/Gzero/Core/Http/Controllers/AccountController.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
<?php namespace Gzero\Core\Http\Controllers;

use Gzero\Core\Services\TimezoneService;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class AccountController extends Controller {

/** @var TimezoneService */
protected $timezones;

/**
* AccountController constructor.
*
* @param TimezoneService $timezones timezone service
*/
public function __construct(TimezoneService $timezones)
{
$this->timezones = $timezones;
}

/**
* Show account main page
*
Expand All @@ -24,7 +38,10 @@ public function index()
*/
public function edit(Request $request)
{
return view('gzero-core::account.edit', ['isUserEmailSet' => strpos($request->user()->email, '@')]);
return view('gzero-core::account.edit', [
'isUserEmailSet' => strpos($request->user()->email, '@'),
'timezones' => $this->timezones->getAvailableTimezones()
]);
}

/**
Expand Down
12 changes: 11 additions & 1 deletion src/Gzero/Core/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@ class LoginController extends Controller {
*/
protected function redirectTo()
{
return route('home');
return routeMl('home', $this->getEffectiveLocale());
}

/**
* Returns user's set language or otherwise the application default language
*
* @return string
*/
protected function getEffectiveLocale()
{
return $this->guard()->user()->language_code ?: app()->getLocale();
}

/**
Expand Down
23 changes: 15 additions & 8 deletions src/Gzero/Core/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Gzero\Core\Http\Controllers\Controller;
use Gzero\Core\Jobs\CreateUser;
use Gzero\Core\Jobs\SendWelcomeEmail;
use Gzero\Core\Services\TimezoneService;
use Gzero\Core\Services\UserService;
use Gzero\Core\Validators\BaseUserValidator;
use Gzero\Core\Validators\UserValidator;
Expand All @@ -28,26 +29,31 @@ class RegisterController extends Controller {
use RedirectsUsers;

/**
* Where to redirect users after registration.
* Where to redirect users after login / registration.
*
* @return string
*/
protected function redirectTo()
protected function getEffectiveLocale()
{
return route('account.welcome');
return $this->guard()->user()->language_code ?: app()->getLocale();
}

/** @var UserValidator */
protected $validator;

/** @var TimezoneService */
protected $timezones;

/**
* Create a new controller instance.
*
* @param BaseUserValidator $validator Validator
* @param UserValidator $validator Validator
* @param TimezoneService $timezones timezone service
*/
public function __construct(UserValidator $validator)
public function __construct(UserValidator $validator, TimezoneService $timezones)
{
$this->validator = $validator;
$this->timezones = $timezones;
$this->middleware('guest');
}

Expand All @@ -58,7 +64,7 @@ public function __construct(UserValidator $validator)
*/
public function showRegistrationForm()
{
return view('gzero-core::auth.register');
return view('gzero-core::auth.register', ['timezones' => $this->timezones->getAvailableTimezones()]);
}

/**
Expand All @@ -77,14 +83,15 @@ public function register(Request $request)

$this->validator->setData($request->all());
$input = $this->validator->validate('register');
$user = dispatch_now(new CreateUser($input));
$user = dispatch_now(new CreateUser($input));
event(new Registered($user));

$this->guard()->login($user);

dispatch(new SendWelcomeEmail($user));
session()->put('showWelcomePage', true);

return redirect($this->redirectPath());
return redirect(routeMl('account.welcome', $this->getEffectiveLocale()));
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Gzero/Core/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Gzero\Core\Policies\UserPolicy;
use Gzero\Core\Policies\RoutePolicy;
use Gzero\Core\Services\RoutesService;
use Gzero\Core\Services\TimezoneService;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Database\Eloquent\Factory;
Expand All @@ -43,7 +44,8 @@ class ServiceProvider extends AbstractServiceProvider {
* @var array
*/
protected $aliases = [
'options' => OptionService::class
'options' => OptionService::class,
'timezones' => TimezoneService::class
];

/**
Expand Down
Loading