allow to configure default values for users (#919)

This commit is contained in:
Kevin Papst
2019-07-05 02:42:50 +02:00
committed by GitHub
parent 3457f93ed0
commit 19843760e5
12 changed files with 142 additions and 29 deletions

View File

@@ -18,7 +18,7 @@ class FormConfiguration implements SystemBundleConfiguration
return 'defaults';
}
public function getCustomerDefaultTimezone(): string
public function getCustomerDefaultTimezone(): ?string
{
return $this->find('customer.timezone');
}
@@ -32,4 +32,19 @@ class FormConfiguration implements SystemBundleConfiguration
{
return $this->find('customer.country');
}
public function getUserDefaultTimezone(): ?string
{
return $this->find('user.timezone');
}
public function getUserDefaultTheme(): ?string
{
return $this->find('user.theme');
}
public function getUserDefaultLanguage(): string
{
return $this->find('user.language');
}
}

View File

@@ -111,10 +111,15 @@ class CustomerController extends AbstractController
*/
public function createAction(Request $request)
{
$timezone = date_default_timezone_get();
if (null !== $this->configuration->getCustomerDefaultTimezone()) {
$timezone = $this->configuration->getCustomerDefaultTimezone();
}
$customer = new Customer();
$customer->setCountry($this->configuration->getCustomerDefaultCountry());
$customer->setCurrency($this->configuration->getCustomerDefaultCurrency());
$customer->setTimezone($this->configuration->getCustomerDefaultTimezone());
$customer->setTimezone($timezone);
return $this->renderCustomerForm($customer, $request);
}

View File

@@ -15,6 +15,8 @@ use App\Form\Model\Configuration;
use App\Form\Model\SystemConfiguration as SystemConfigurationModel;
use App\Form\SystemConfigurationForm;
use App\Form\Type\EnhancedSelectboxType;
use App\Form\Type\LanguageType;
use App\Form\Type\SkinType;
use App\Form\Type\TrackingModeType;
use App\Repository\ConfigurationRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
@@ -171,7 +173,10 @@ class SystemConfigurationController extends AbstractController
foreach ($event->getConfigurations() as $configs) {
foreach ($configs->getConfiguration() as $config) {
$config->setValue($this->configurations->find($config->getName()));
$configValue = $this->configurations->find($config->getName());
if (null !== $configValue) {
$config->setValue($configValue);
}
}
}
@@ -216,7 +221,8 @@ class SystemConfigurationController extends AbstractController
(new Configuration())
->setName('defaults.customer.timezone')
->setLabel('timezone')
->setType(TimezoneType::class),
->setType(TimezoneType::class)
->setValue(date_default_timezone_get()),
(new Configuration())
->setName('defaults.customer.country')
->setLabel('country')
@@ -226,6 +232,23 @@ class SystemConfigurationController extends AbstractController
->setLabel('currency')
->setType(CurrencyType::class),
]),
(new SystemConfigurationModel())
->setSection(SystemConfigurationModel::SECTION_FORM_USER)
->setConfiguration([
(new Configuration())
->setName('defaults.user.timezone')
->setLabel('timezone')
->setType(TimezoneType::class)
->setValue(date_default_timezone_get()),
(new Configuration())
->setName('defaults.user.language')
->setLabel('language')
->setType(LanguageType::class),
(new Configuration())
->setName('defaults.user.theme')
->setLabel('skin')
->setType(SkinType::class),
]),
(new SystemConfigurationModel())
->setSection(SystemConfigurationModel::SECTION_THEME)
->setConfiguration([

View File

@@ -9,6 +9,7 @@
namespace App\DependencyInjection;
use App\Entity\User;
use App\Timesheet\Rounding\RoundingInterface;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
@@ -435,11 +436,20 @@ class Configuration implements ConfigurationInterface
->arrayNode('customer')
->addDefaultsIfNotSet()
->children()
->scalarNode('timezone')->defaultValue('Europe/Berlin')->end()
->scalarNode('timezone')->defaultNull()->end()
->scalarNode('country')->defaultValue('DE')->end()
->scalarNode('currency')->defaultValue('EUR')->end()
->end()
->end()
->arrayNode('user')
->addDefaultsIfNotSet()
->children()
->scalarNode('timezone')->defaultNull()->end()
->scalarNode('language')->defaultValue(User::DEFAULT_LANGUAGE)->end()
->scalarNode('theme')->defaultNull()->end()
->end()
->end()
->end()
;

View File

@@ -9,6 +9,7 @@
namespace App\EventSubscriber;
use App\Configuration\FormConfiguration;
use App\Entity\User;
use App\Entity\UserPreference;
use App\Event\PrepareUserEvent;
@@ -32,32 +33,27 @@ class UserPreferenceSubscriber implements EventSubscriberInterface
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* @var AuthorizationCheckerInterface
*/
protected $voter;
/**
* @var TokenStorageInterface
*/
protected $storage;
/**
* @param EventDispatcherInterface $dispatcher
* @param TokenStorageInterface $storage
* @param AuthorizationCheckerInterface $voter
* @var FormConfiguration
*/
public function __construct(EventDispatcherInterface $dispatcher, TokenStorageInterface $storage, AuthorizationCheckerInterface $voter)
protected $formConfig;
public function __construct(EventDispatcherInterface $dispatcher, TokenStorageInterface $storage, AuthorizationCheckerInterface $voter, FormConfiguration $formConfig)
{
$this->eventDispatcher = $dispatcher;
$this->storage = $storage;
$this->voter = $voter;
$this->formConfig = $formConfig;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
@@ -65,6 +61,26 @@ class UserPreferenceSubscriber implements EventSubscriberInterface
];
}
private function getDefaultTheme(): ?string
{
return $this->formConfig->getUserDefaultTheme();
}
private function getDefaultLanguage(): string
{
return $this->formConfig->getUserDefaultLanguage();
}
private function getDefaultTimezone(): string
{
$timezone = $this->formConfig->getUserDefaultTimezone();
if (null === $timezone) {
$timezone = date_default_timezone_get();
}
return $timezone;
}
/**
* @param User $user
* @return UserPreference[]
@@ -87,16 +103,17 @@ class UserPreferenceSubscriber implements EventSubscriberInterface
(new UserPreference())
->setName(UserPreference::TIMEZONE)
->setValue(date_default_timezone_get())
->setValue($this->getDefaultTimezone())
->setType(TimezoneType::class),
(new UserPreference())
->setName(UserPreference::LOCALE)
->setValue(User::DEFAULT_LANGUAGE)
->setValue($this->getDefaultLanguage())
->setType(LanguageType::class),
(new UserPreference())
->setName(UserPreference::SKIN)
->setValue($this->getDefaultTheme())
->setType(SkinType::class),
(new UserPreference())

View File

@@ -13,6 +13,7 @@ class SystemConfiguration
{
public const SECTION_TIMESHEET = 'timesheet';
public const SECTION_FORM_CUSTOMER = 'form_customer';
public const SECTION_FORM_USER = 'form_user';
public const SECTION_THEME = 'theme';
public const SECTION_CALENDAR = 'calendar';