Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions Controller/Admin/ConfigController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Plugin\Recommend4\Controller\Admin;

use Eccube\Controller\AbstractController;
use Plugin\Recommend4\Form\Type\Admin\ConfigType;
use Plugin\Recommend4\Repository\ConfigRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class ConfigController extends AbstractController
{
/**
* @var ConfigRepository
*/
protected $configRepository;

/**
* ConfigController constructor.
*/
public function __construct(ConfigRepository $configRepository)
{
$this->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(),
];
}
}
75 changes: 75 additions & 0 deletions Entity/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Plugin\Recommend4\Entity;

use Doctrine\ORM\Mapping as ORM;
use Eccube\Entity\AbstractEntity;

if (!class_exists('\Plugin\Recommend4\Entity\Config', false)) {
/**
* Config
*
* @ORM\Table(name="plg_recommend_config")
* @ORM\Entity(repositoryClass="Plugin\Recommend4\Repository\ConfigRepository")
*/
class Config extends AbstractEntity
{
const OPTION_COMMENT_DEFAULT = 0;
const OPTION_COMMENT_NOT_REQUIRED = 1;
const OPTION_COMMENT_NOT_USE = 2;

/**
* @var int
*
* @ORM\Column(name="id", type="integer", options={"unsigned":true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;

/**
* @var string
*
* @ORM\Column(type="smallint", options={"default": 0})
*/
private $option_use_comment = 0;

/**
* @return int
*/
public function getId()
{
return $this->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;
}

}
}
53 changes: 53 additions & 0 deletions Form/Type/Admin/ConfigType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Plugin\Recommend4\Form\Type\Admin;

use Plugin\Recommend4\Entity\Config;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;

class ConfigType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->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,
]);
}
}
57 changes: 40 additions & 17 deletions Form/Type/RecommendProductType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

/**
Expand All @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -83,6 +84,7 @@ public function enable(array $meta = null, ContainerInterface $container)
// pagelayoutの作成
$this->createDataBlock($container);
}
$this->createConfig($container);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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();
}
}
47 changes: 47 additions & 0 deletions Repository/ConfigRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Plugin\Recommend4\Repository;

use Eccube\Repository\AbstractRepository;
use Plugin\Recommend4\Entity\Config;
use Symfony\Bridge\Doctrine\RegistryInterface;

/**
* ConfigRepository.
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class ConfigRepository extends AbstractRepository
{
/**
* CouponRepository constructor.
*
* @param RegistryInterface $registry
*/
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, Config::class);
}

/**
* @param int $id
*
* @return Config|null
*/
public function get($id = 1)
{
return $this->find($id);
}
}
5 changes: 5 additions & 0 deletions Resource/locale/messages.ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: 説明文を使用するか
Loading