diff --git a/config/packages/app.yaml b/config/packages/app.yaml index 9a29569..4d9cf89 100644 --- a/config/packages/app.yaml +++ b/config/packages/app.yaml @@ -4,4 +4,4 @@ app: App\Entity\Page\SimplePage: name: 'Page simple' templates: - - {name: "Template 1", file: "site/page/simple/page.html.twig"} + - {name: "Template 1", file: "page/simple/page.html.twig"} diff --git a/core/Entity/Site/Page/Block.php b/core/Entity/Site/Page/Block.php index 5a21471..6180d75 100644 --- a/core/Entity/Site/Page/Block.php +++ b/core/Entity/Site/Page/Block.php @@ -8,6 +8,8 @@ use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass=BlockRepository::class) + * @ORM\DiscriminatorColumn(name="class_key", type="string") + * @ORM\InheritanceType("SINGLE_TABLE") * @ORM\HasLifecycleCallbacks */ class Block @@ -54,7 +56,7 @@ class Block return $this; } - public function getValue(): ?string + public function getValue() { return $this->value; } diff --git a/core/Entity/Site/Page/FileBlock.php b/core/Entity/Site/Page/FileBlock.php new file mode 100644 index 0000000..7f4d772 --- /dev/null +++ b/core/Entity/Site/Page/FileBlock.php @@ -0,0 +1,36 @@ +getValue() instanceof File && null === $value) { + return $this; + } + + return parent::setValue($value); + } +} diff --git a/core/Entity/Site/Page/Page.php b/core/Entity/Site/Page/Page.php index c0ccc6f..03ab38f 100644 --- a/core/Entity/Site/Page/Page.php +++ b/core/Entity/Site/Page/Page.php @@ -137,7 +137,7 @@ class Page implements EntityInterface { } - public function getBlock($name) + public function getBlock($name, string $className = null) { foreach ($this->getBlocks() as $block) { if ($block->getName() === $name) { @@ -145,7 +145,12 @@ class Page implements EntityInterface } } - $block = new Block(); + if ($className) { + $block = new $className(); + } else { + $block = new Block(); + } + $block->setName($name); $block->setPage($this); diff --git a/core/Entity/Site/Page/TextBlock.php b/core/Entity/Site/Page/TextBlock.php new file mode 100644 index 0000000..5b59861 --- /dev/null +++ b/core/Entity/Site/Page/TextBlock.php @@ -0,0 +1,14 @@ +getEntity(); + foreach ($event->getEntity()->getBlocks() as $block) { + if ($block instanceof FileBlock) { + if ($block->getValue() instanceof UploadedFile) { + $directory = 'uploads/page/block'; - if (!$block->getValue() instanceof UploadedFile) { - return; - } - - $directory = 'uploads/page/block'; - - $fileUpload->handleForm( - $block->getValue(), - $directory, - function ($filename) use ($block) { - $block->setValue($directory.'/'.$filename); + $this->fileUpload->handleForm( + $block->getValue(), + $directory, + function ($filename) use ($block, $directory) { + $block->setValue($directory.'/'.$filename); + } + ); + } } - ); + } } public function onPreCreate(EntityManagerEvent $event) diff --git a/core/Form/Site/Page/FileBlockType.php b/core/Form/Site/Page/FileBlockType.php new file mode 100644 index 0000000..47f94d1 --- /dev/null +++ b/core/Form/Site/Page/FileBlockType.php @@ -0,0 +1,36 @@ +add( + 'value', + FileType::class, + array_merge([ + 'required' => false, + 'label' => false, + ], $options['options']), + ); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => FileBlock::class, + 'options' => [], + ]); + } +} diff --git a/core/Form/Site/Page/ImageBlockType.php b/core/Form/Site/Page/ImageBlockType.php new file mode 100644 index 0000000..c52e8b8 --- /dev/null +++ b/core/Form/Site/Page/ImageBlockType.php @@ -0,0 +1,32 @@ +add( + 'value', + FileType::class, + array_merge([ + 'required' => false, + 'label' => false, + 'constraints' => [ + new Image(), + ], + ], $options['options']), + ); + } +} diff --git a/src/Controller/Blog/PostAdminController.php b/src/Controller/Blog/PostAdminController.php index 15addb2..684ad88 100644 --- a/src/Controller/Blog/PostAdminController.php +++ b/src/Controller/Blog/PostAdminController.php @@ -74,7 +74,7 @@ class PostAdminController extends AdminController $fileUpload->handleForm( $form->get('image')->getData(), $directory, - function ($filename) use ($entity) { + function ($filename) use ($entity, $directory) { $entity->setImage($directory.'/'.$filename); } ); diff --git a/src/Entity/Page/SimplePage.php b/src/Entity/Page/SimplePage.php index cc12454..db55fdb 100644 --- a/src/Entity/Page/SimplePage.php +++ b/src/Entity/Page/SimplePage.php @@ -9,6 +9,8 @@ use App\Core\Form\Site\Page\TextareaBlockType; use App\Core\Form\Site\Page\TextBlockType; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Form\FormBuilderInterface; +use App\Core\Entity\Site\Page\FileBlock; +use App\Core\Form\Site\Page\ImageBlockType; /** * @ORM\Entity @@ -46,19 +48,19 @@ class SimplePage extends Page ] ); - // $builder->add( - // 'file', - // FileBlockType::class, - // [ - // 'label' => 'Fichier', - // 'options' => [ - // 'attr' => [ - // ], - // 'constraints' => [ - // ], - // ], - // ] - // ); + $builder->add( + 'image', + ImageBlockType::class, + [ + 'label' => 'Image', + 'options' => [ + 'attr' => [ + ], + 'constraints' => [ + ], + ], + ] + ); } public function setTitle(Block $block) @@ -81,13 +83,13 @@ class SimplePage extends Page return $this->getBlock('content'); } - public function setFile(Block $block) + public function setImage(Block $block) { return $this->setBlock($block); } - public function getFile() + public function getImage() { - return $this->getBlock('file'); + return $this->getBlock('image', FileBlock::class); } } diff --git a/templates/site/page/simple/page.html.twig b/templates/page/simple/page.html.twig similarity index 94% rename from templates/site/page/simple/page.html.twig rename to templates/page/simple/page.html.twig index 4668034..c1b5509 100644 --- a/templates/site/page/simple/page.html.twig +++ b/templates/page/simple/page.html.twig @@ -50,6 +50,12 @@
{{ _page.content.value }}
+{% set image = _page.image.value %} + +{% if image %} + +{% endif %} +