Browse Source

Merchant config : initialisation des options par défaut lors du persist/update

reduction
Guillaume 4 years ago
parent
commit
6bd57358c1
3 changed files with 88 additions and 38 deletions
  1. +35
    -0
      ShopBundle/Controller/Admin/MerchantController.php
  2. +11
    -37
      ShopBundle/Form/MerchantConfigType.php
  3. +42
    -1
      ShopBundle/Model/MerchantConfig.php

+ 35
- 0
ShopBundle/Controller/Admin/MerchantController.php View File

@@ -2,6 +2,7 @@

namespace Lc\ShopBundle\Controller\Admin;

use App\Entity\MerchantConfig;
use Lc\ShopBundle\Context\MerchantInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
@@ -9,6 +10,40 @@ use Symfony\Component\HttpFoundation\Request;
class MerchantController extends AdminController
{

public function persistEntity($entity)
{
$this->createAvailableOptions($entity) ;
parent::persistEntity($entity);
}

public function updateEntity($entity)
{
$this->createAvailableOptions($entity) ;
parent::updateEntity($entity);
}

public function createAvailableOptions($entity)
{
$merchantConfigs = $entity->getMerchantConfigs() ;
$availableOptions = MerchantConfig::getAvailableOptions() ;

foreach($availableOptions as $key => $option) {
$optionExist = false ;
foreach($merchantConfigs as $merchantConfig) {
if($merchantConfig->getName() == $key) {
$optionExist = true ;
}
}
if(!$optionExist) {
$merchantConfig = new MerchantConfig();
$merchantConfig->setName($key)->setValue($merchantConfig->getOptionValue('default'))->setMerchant($entity) ;
$this->em->persist($merchantConfig);
}
}

$this->em->flush() ;
}

public function editAction()
{
// paramètres (admin)

+ 11
- 37
ShopBundle/Form/MerchantConfigType.php View File

@@ -31,47 +31,21 @@ class MerchantConfigType extends AbstractType
$merchantConfig = $event->getData();

if ($merchantConfig) {
$form->add('name', HiddenType::class) ;

switch ($merchantConfig->getName()) {
case 'order-open-day' :
$label = 'Jour d\'ouverture des commandes';
break;
case 'order-open-time' :
$label = 'Heure d\'ouverture des commandes';
break;
case 'order-close-day' :
$label = 'Jour de fermeture des commandes';
break;
case 'order-close-time' :
$label = 'Heure de fermeture des commandes';
break;
if($merchantConfig->getFieldType() == 'choice') {
$form->add('value', ChoiceType::class, [
'label' => $merchantConfig->getLabel(),
'choices' => $merchantConfig->getChoices()
]);
}

$choicesArray = [] ;

if (strpos($merchantConfig->getName(), 'time') !== false) {
for($i = 0; $i < 24 ; $i++) {
$choicesArray[$i.'h'] = $i.':00' ;
else {
if($merchantConfig->getOption()) {
$form->add('value', TextType::class, [
'label' => $merchantConfig->getLabel(),
]);
}
}

if (strpos($merchantConfig->getName(), 'day') !== false) {
$choicesArray = [
'Lundi' => 1,
'Mardi' => 2,
'Mercredi' => 3,
'Jeudi' => 4,
'Vendredi' => 5,
'Samedi' => 6,
'Dimanche' => 7,
] ;
}

$form->add('name', HiddenType::class)
->add('value', ChoiceType::class, [
'label' => $label,
'choices' => $choicesArray
]);
}
});


+ 42
- 1
ShopBundle/Model/MerchantConfig.php View File

@@ -21,10 +21,11 @@ abstract class MerchantConfig
protected $name;

/**
* @ORM\Column(type="text")
* @ORM\Column(type="text", nullable=true)
*/
protected $value;

public static $availableOptions = [] ;

public function getMerchant(): ?Merchant
{
@@ -61,4 +62,44 @@ abstract class MerchantConfig

return $this;
}

public static function getAvailableOptions(): array
{
return static::$availableOptions ;
}

public function getOption()
{
if(isset(static::$availableOptions[$this->getName()])) {
return static::$availableOptions[$this->getName()] ;
}
return false ;
}

public function getOptionValue($key, $default = '')
{
$option = $this->getOption() ;
if($option) {
if(isset($option[$key])) {
return $option[$key] ;
}
}

return $default ;
}

public function getLabel()
{
return $this->getOptionValue('label') ;
}

public function getFieldType()
{
return $this->getOptionValue('type', 'text') ;
}

public function getDefaultValue()
{
return $this->getOptionValue('default') ;
}
}

Loading…
Cancel
Save