diff --git a/Controller/Admin/ConfigController.php b/Controller/Admin/ConfigController.php new file mode 100644 index 0000000..f17fae6 --- /dev/null +++ b/Controller/Admin/ConfigController.php @@ -0,0 +1,61 @@ +configRepository = $configRepository; + } + + /** + * @Route("/%eccube_admin_route%/recommend4/config", name="recommend4_admin_config") + * @Template("@Recommend4/admin/config.twig") + */ + public function index(Request $request) + { + $Config = $this->configRepository->get(); + $form = $this->createForm(ConfigType::class, $Config); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $Config = $form->getData(); + $this->entityManager->persist($Config); + $this->entityManager->flush($Config); + $this->addSuccess('登録しました。', 'admin'); + + return $this->redirectToRoute('recommend4_admin_config'); + } + + return [ + 'form' => $form->createView(), + ]; + } +} diff --git a/Entity/Config.php b/Entity/Config.php new file mode 100644 index 0000000..fa04cff --- /dev/null +++ b/Entity/Config.php @@ -0,0 +1,75 @@ +id; + } + + /** + * @return integer + */ + public function getOptionUseComment() + { + return $this->option_use_comment; + } + + /** + * @param integer $option_use_comment + * @return Config + */ + public function setOptionUseComment($option_use_comment) + { + $this->option_use_comment = $option_use_comment; + return $this; + } + + } +} diff --git a/Form/Type/Admin/ConfigType.php b/Form/Type/Admin/ConfigType.php new file mode 100644 index 0000000..fe7e8b5 --- /dev/null +++ b/Form/Type/Admin/ConfigType.php @@ -0,0 +1,53 @@ +add('option_use_comment', ChoiceType::class, [ + 'choices' => [ + trans('plugin_recommend.admin.config.option_use_comment.choice.default') => Config::OPTION_COMMENT_DEFAULT, + trans('plugin_recommend.admin.config.option_use_comment.choice.not_required') => Config::OPTION_COMMENT_NOT_REQUIRED, + trans('plugin_recommend.admin.config.option_use_comment.choice.not_use') => Config::OPTION_COMMENT_NOT_USE, + ], + 'constraints' => [ + new NotBlank(), + ], + ]); + } + + /** + * {@inheritdoc} + */ + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => Config::class, + ]); + } +} diff --git a/Form/Type/RecommendProductType.php b/Form/Type/RecommendProductType.php index c61a438..2c439ba 100644 --- a/Form/Type/RecommendProductType.php +++ b/Form/Type/RecommendProductType.php @@ -15,6 +15,8 @@ use Doctrine\ORM\EntityManagerInterface; use Eccube\Common\EccubeConfig; +use Plugin\Recommend4\Entity\Config; +use Plugin\Recommend4\Repository\ConfigRepository; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\TextareaType; @@ -42,16 +44,26 @@ class RecommendProductType extends AbstractType */ private $entityManager; + /** + * @var \Plugin\Recommend4\Entity\Config|null + */ + private $Config; + /** * RecommendProductType constructor. * * @param EccubeConfig $eccubeConfig * @param EntityManagerInterface $entityManager + * @param ConfigRepository $configRepository */ - public function __construct(EccubeConfig $eccubeConfig, EntityManagerInterface $entityManager) - { + public function __construct( + EccubeConfig $eccubeConfig, + EntityManagerInterface $entityManager, + ConfigRepository $configRepository + ) { $this->eccubeConfig = $eccubeConfig; $this->entityManager = $entityManager; + $this->Config = $configRepository->get(); } /** @@ -67,23 +79,34 @@ public function buildForm(FormBuilderInterface $builder, array $options) 'label' => 'plugin_recommend.admin.edit.product', 'required' => false, 'attr' => ['readonly' => 'readonly'], - ]) - ->add('comment', TextareaType::class, [ - 'label' => 'plugin_recommend.admin.edit.comment', - 'required' => true, - 'trim' => true, - 'constraints' => [ - new Assert\NotBlank(), - new Assert\Length([ - 'max' => $this->eccubeConfig['plugin_recommend.text_area_len'], - ]), - ], - 'attr' => [ - 'maxlength' => $this->eccubeConfig['plugin_recommend.text_area_len'], - 'placeholder' => 'plugin_recommend.admin.type.comment.placeholder', - ], ]); + if ($this->Config->getOptionUseComment() != Config::OPTION_COMMENT_NOT_USE) { + $required = false; + $constraints = [ + new Assert\Length([ + 'max' => $this->eccubeConfig['plugin_recommend.text_area_len'], + ]), + ]; + + if ($this->Config->getOptionUseComment() == Config::OPTION_COMMENT_DEFAULT) { + $required = true; + $constraints[] = new Assert\NotBlank(); + } + + $builder + ->add('comment', TextareaType::class, [ + 'label' => 'plugin_recommend.admin.edit.comment', + 'required' => $required, + 'trim' => true, + 'constraints' => $constraints, + 'attr' => [ + 'maxlength' => $this->eccubeConfig['plugin_recommend.text_area_len'], + 'placeholder' => 'plugin_recommend.admin.type.comment.placeholder', + ], + ]); + } + $builder->add( $builder ->create('Product', HiddenType::class) diff --git a/PluginManager.php b/PluginManager.php index 175fc1c..2fb9c97 100644 --- a/PluginManager.php +++ b/PluginManager.php @@ -23,6 +23,7 @@ use Eccube\Repository\BlockRepository; use Eccube\Repository\LayoutRepository; use Eccube\Repository\Master\DeviceTypeRepository; +use Plugin\Recommend4\Entity\Config; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Filesystem\Filesystem; @@ -83,6 +84,7 @@ public function enable(array $meta = null, ContainerInterface $container) // pagelayoutの作成 $this->createDataBlock($container); } + $this->createConfig($container); } /** @@ -101,6 +103,7 @@ public function disable(array $meta = null, ContainerInterface $container) public function update(array $meta = null, ContainerInterface $container) { $this->copyBlock($container); + $this->createConfig($container); } /** @@ -226,4 +229,23 @@ private function removeBlock(ContainerInterface $container) $file = new Filesystem(); $file->remove($templateDir.'/Block/'.$this->blockFileName.'.twig'); } + + /** + * @param ContainerInterface $container + */ + public function createConfig(ContainerInterface $container) + { + $entityManager = $container->get('doctrine.orm.entity_manager'); + $Config = $entityManager->find(Config::class, 1); + if ($Config) { + return; + } + + // プラグイン情報初期セット NULL + $Config = new Config(); + $Config->setOptionUseComment(true); + + $entityManager->persist($Config); + $entityManager->flush(); + } } diff --git a/Repository/ConfigRepository.php b/Repository/ConfigRepository.php new file mode 100644 index 0000000..1810879 --- /dev/null +++ b/Repository/ConfigRepository.php @@ -0,0 +1,47 @@ +find($id); + } +} diff --git a/Resource/locale/messages.ja.yml b/Resource/locale/messages.ja.yml index 4921e54..9435f50 100644 --- a/Resource/locale/messages.ja.yml +++ b/Resource/locale/messages.ja.yml @@ -27,3 +27,8 @@ plugin_recommend.admin.edit.existed: この商品はすでにおすすめ商品 plugin_recommend.admin.edit.search: 商品の追加 plugin_recommend.admin.edit.product_search.header: 商品検索 plugin_recommend.admin.edit.product_search.button: 検索 + +plugin_recommend.admin.config.option_use_comment.choice.default: 必須にする +plugin_recommend.admin.config.option_use_comment.choice.not_required: 任意項目にする +plugin_recommend.admin.config.option_use_comment.choice.not_use: 使用しない +plugin_recommend.admin.config.option_use_comment.label: 説明文を使用するか diff --git a/Resource/template/Block/recommend_product_block.twig b/Resource/template/Block/recommend_product_block.twig index 71cbbd3..bb5c185 100644 --- a/Resource/template/Block/recommend_product_block.twig +++ b/Resource/template/Block/recommend_product_block.twig @@ -7,6 +7,7 @@ * file that was distributed with this source code. #} {% set recommend_products = repository('Plugin\\Recommend4\\Entity\\RecommendProduct').getRecommendProduct %} +{% set Config = repository('Plugin\\Recommend4\\Entity\\Config').get() %}
@@ -113,7 +116,9 @@ {% endif %} + {% if Config.option_use_comment != constant('Plugin\\Recommend4\\Entity\\Config::OPTION_COMMENT_NOT_USE') %}{{ 'plugin_recommend.admin.edit.comment.help'|trans({'%number%' : eccube_config['plugin_recommend.text_area_len']}) }}