Browse Source

[Backend] Optimistation de FieldDefinition #169

develop
Fabien Normand 1 year ago
parent
commit
a2fd6916f4
4 changed files with 34 additions and 12 deletions
  1. +7
    -2
      Definition/Field/FieldDefinitionTrait.php
  2. +6
    -2
      Definition/Field/Product/ProductCategoryFieldDefinition.php
  3. +6
    -2
      Definition/Field/Site/NewsFieldDefinition.php
  4. +15
    -6
      Definition/Field/User/UserFieldDefinition.php

+ 7
- 2
Definition/Field/FieldDefinitionTrait.php View File

use MerchantContextTrait; use MerchantContextTrait;
use SectionContextTrait; use SectionContextTrait;


protected ?array $sectionArray = null;

public function configureFieldsBase(): array public function configureFieldsBase(): array
{ {
$sectionArray = $this->sectionStore->setMerchant($this->merchant)->get();
if(is_null($this->sectionArray)){
$this->sectionArray = $this->sectionStore->setMerchant($this->merchant)->get();
}



return array_merge(parent::configureFieldsBase(), [ return array_merge(parent::configureFieldsBase(), [
'section' => AssociationField::new('section') 'section' => AssociationField::new('section')
->setTemplatePath('@LcCaracole/admin/section/field/section.html.twig') ->setTemplatePath('@LcCaracole/admin/section/field/section.html.twig')
->setFormTypeOption('choices', $sectionArray)
->setFormTypeOption('choices', $this->sectionArray)
]); ]);
} }



+ 6
- 2
Definition/Field/Product/ProductCategoryFieldDefinition.php View File

protected SectionStore $sectionStore; protected SectionStore $sectionStore;
protected ProductCategoryStore $productCategoryStore; protected ProductCategoryStore $productCategoryStore;


protected ?array $productCategoryArray = null;

public function __construct( public function __construct(
TranslatorAdmin $translatorAdmin, TranslatorAdmin $translatorAdmin,
SectionStore $sectionStore, SectionStore $sectionStore,


public function configureFields(): array public function configureFields(): array
{ {
$productCategoryArray = $this->productCategoryStore
if(is_null($this->productCategoryArray)) {
$this->productCategoryArray = $this->productCategoryStore
->setSection($this->section) ->setSection($this->section)
->getParents(); ->getParents();
}


return [ return [
'title' => TextField::new('title')->setSortable(true), 'title' => TextField::new('title')->setSortable(true),
'position' => NumberField::new('position')->setSortable(true), 'position' => NumberField::new('position')->setSortable(true),
'parent' => AssociationField::new('parent') 'parent' => AssociationField::new('parent')
->setFormTypeOption('choices', $productCategoryArray)
->setFormTypeOption('choices', $this->productCategoryArray)
->setFormTypeOption( ->setFormTypeOption(
'choice_label', 'choice_label',
function ($productCategory) { function ($productCategory) {

+ 6
- 2
Definition/Field/Site/NewsFieldDefinition.php View File

{ {
use FieldDefinitionTrait; use FieldDefinitionTrait;


protected ?array $newsletterArray = null;

protected SectionStore $sectionStore; protected SectionStore $sectionStore;
protected NewsletterStore $newsletterStore; protected NewsletterStore $newsletterStore;


{ {
$fieldArray = parent::configureFields(); $fieldArray = parent::configureFields();


$newsletterArray = $this->newsletterStore
if(is_null($this->newsletterArray)){
$this->newsletterArray = $this->newsletterStore
->setSection($this->section) ->setSection($this->section)
->setMerchant($this->merchant) ->setMerchant($this->merchant)
->get(); ->get();
}


$fieldArray['newsletter']->setFormTypeOption('choices', $newsletterArray);
$fieldArray['newsletter']->setFormTypeOption('choices', $this->newsletterArray);


return $fieldArray; return $fieldArray;
} }

+ 15
- 6
Definition/Field/User/UserFieldDefinition.php View File

{ {
use MerchantContextTrait; use MerchantContextTrait;


protected ?array $newsletterArray = null;
protected ?array $groupUserArray = null;

protected GroupUserStore $groupUserStore; protected GroupUserStore $groupUserStore;
protected NewsletterStore $newsletterStore; protected NewsletterStore $newsletterStore;


public function __construct( public function __construct(
TranslatorAdmin $translatorAdmin, TranslatorAdmin $translatorAdmin,
RolesDefinition $rolesDefinition, RolesDefinition $rolesDefinition,
GroupUserStore $groupUserStore,
GroupUserStore $groupUserStore,
NewsletterStore $newsletterStore NewsletterStore $newsletterStore
) {
)
{
parent::__construct($translatorAdmin, $rolesDefinition); parent::__construct($translatorAdmin, $rolesDefinition);


$this->groupUserStore = $groupUserStore; $this->groupUserStore = $groupUserStore;


public function configureFields(): array public function configureFields(): array
{ {
$groupUsers = $this->groupUserStore->setMerchant($this->merchant)->getAll();
$newsletters = $this->newsletterStore->getAll();
if (is_null($this->newsletterArray)) {
$this->newsletterArray = $this->newsletterStore->getAll();
}

if (is_null($this->groupUserArray)) {
$this->groupUserArray = $this->groupUserStore->setMerchant($this->merchant)->getAll();
}


return array_merge(parent::configureFields(), [ return array_merge(parent::configureFields(), [
'isSaleAlwaysOpen' => BooleanField::new('isSaleAlwaysOpen'), 'isSaleAlwaysOpen' => BooleanField::new('isSaleAlwaysOpen'),
'newsletters' => AssociationField::new('newsletters') 'newsletters' => AssociationField::new('newsletters')
->setFormTypeOption('choices', $newsletters)
->setFormTypeOption('choices', $this->newsletterArray)
->setFormTypeOption('choice_label', function ($choice) { ->setFormTypeOption('choice_label', function ($choice) {
return $choice->getTitle() . '[' . $choice->getSection()->getMerchant() . ']'; return $choice->getTitle() . '[' . $choice->getSection()->getMerchant() . ']';
}) })
->setSortable(true), ->setSortable(true),
'groupUsers' => AssociationField::new('groupUsers') 'groupUsers' => AssociationField::new('groupUsers')
->setFormTypeOption('choices', $groupUsers)
->setFormTypeOption('choices', $this->groupUserArray)
->setCustomOption('class', GroupUser::class) ->setCustomOption('class', GroupUser::class)
->setTemplatePath('@LcSov/adminlte/crud/field/association_many.html.twig') ->setTemplatePath('@LcSov/adminlte/crud/field/association_many.html.twig')
->setFormTypeOption('choice_label', function ($choice) { ->setFormTypeOption('choice_label', function ($choice) {

Loading…
Cancel
Save