Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

管理画面 商品のタグによる絞り込み #4975

Merged
merged 1 commit into from
Nov 25, 2021
Merged
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
26 changes: 24 additions & 2 deletions src/Eccube/Form/Type/Admin/SearchProductType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
use Eccube\Entity\Category;
use Eccube\Entity\Master\ProductStatus;
use Eccube\Entity\ProductStock;
use Eccube\Entity\Tag;
use Eccube\Form\Type\Master\CategoryType as MasterCategoryType;
use Eccube\Form\Type\Master\ProductStatusType;
use Eccube\Repository\CategoryRepository;
use Eccube\Repository\Master\ProductStatusRepository;
use Eccube\Repository\TagRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
Expand All @@ -39,16 +42,26 @@ class SearchProductType extends AbstractType
*/
protected $categoryRepository;

/**
* @var TagRepository
*/
protected $tagRepository;

/**
* SearchProductType constructor.
*
* @param ProductStatusRepository $productStatusRepository
* @param CategoryRepository $categoryRepository
* @param TagRepository $tagRepository
*/
public function __construct(ProductStatusRepository $productStatusRepository, CategoryRepository $categoryRepository)
{
public function __construct(
ProductStatusRepository $productStatusRepository,
CategoryRepository $categoryRepository,
TagRepository $tagRepository
) {
$this->productStatusRepository = $productStatusRepository;
$this->categoryRepository = $categoryRepository;
$this->tagRepository = $tagRepository;
}

/**
Expand Down Expand Up @@ -92,6 +105,15 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'expanded' => true,
'multiple' => true,
])
->add('tag_id', EntityType::class, [
'class' => Tag::class,
'label' => 'admin.product.tag',
'placeholder' => 'common.select__all_products',
'choice_label' => 'name',
'required' => false,
'multiple' => false,
'expanded' => false,
])
->add('create_date_start', DateType::class, [
'label' => 'admin.common.create_date__start',
'required' => false,
Expand Down
8 changes: 8 additions & 0 deletions src/Eccube/Repository/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,14 @@ public function getQueryBuilderBySearchDataForAdmin($searchData)
}
}

// tag
if (!empty($searchData['tag_id']) && $searchData['tag_id']) {
$qb
->innerJoin('p.ProductTag', 'pt')
->andWhere('pt.Tag = :tag_id')
->setParameter('tag_id', $searchData['tag_id']);
}

// crate_date
if (!empty($searchData['create_datetime_start']) && $searchData['create_datetime_start']) {
$date = $searchData['create_datetime_start'];
Expand Down
7 changes: 7 additions & 0 deletions src/Eccube/Resource/template/admin/Product/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ file that was distributed with this source code.
</div>
</div>
<div class="col-6">
<div class="form-row mb-2">
<div class="col-6">
<label class="col-form-label">{{ 'admin.product.tag'|trans }}</label>
{{ form_widget(searchForm.tag_id) }}
{{ form_errors(searchForm.tag_id) }}
</div>
</div>
<div class="mb-2">
<label class="col-form-label">
{{ 'admin.common.create_date'|trans }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
namespace Eccube\Tests\Repository;

use Eccube\Entity\CustomerFavoriteProduct;
use Eccube\Tests\EccubeTestCase;
use Eccube\Entity\Product;
use Eccube\Entity\ProductTag;
use Eccube\Repository\ProductRepository;
use Eccube\Repository\TagRepository;
use Eccube\Tests\EccubeTestCase;

/**
* ProductRepository test cases.
Expand All @@ -29,6 +32,11 @@ abstract class AbstractProductRepositoryTestCase extends EccubeTestCase
*/
protected $productRepository;

/**
* @var TagRepository
*/
protected $tagRepository;

/**
* {@inheritdoc}
*/
Expand All @@ -37,6 +45,7 @@ public function setUp()
parent::setUp();

$this->productRepository = $this->entityManager->getRepository(\Eccube\Entity\Product::class);
$this->tagRepository = $this->entityManager->getRepository(\Eccube\Entity\Tag::class);

$tables = [
'dtb_product_image',
Expand Down Expand Up @@ -67,4 +76,24 @@ protected function createFavorites($Customer)
}
$this->entityManager->flush();
}

/**
* 商品にタグをつける
*
* @param Product $Product
* @param array $tagIds
*/
protected function setProductTags(Product $Product, array $tagIds)
{
$Tags = $this->tagRepository->findBy(['id' => $tagIds]);
foreach ($Tags as $Tag) {
$ProductTag = new ProductTag();
$ProductTag
->setProduct($Product)
->setTag($Tag);
$Product->addProductTag($ProductTag);
$this->entityManager->persist($ProductTag);
}
$this->entityManager->flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,53 @@ public function testProductImage()
$this->verify();
}
}

public function testTagSearch()
{
// データの事前準備
// * 商品1 に タグ 1 を設定
// * 商品2 に タグ 1, 2 を設定
$Products = $this->productRepository->findAll();
$Products[0]->setName('りんご');
$this->setProductTags($Products[0], [1]);
$this->setProductTags($Products[1], [1, 2]);
$this->entityManager->flush();

// タグ 1 で検索
$this->searchData = [
'tag_id' => 1,
];
$this->scenario();
$this->assertCount(2, $this->Results);

// タグ 2 で検索
$this->searchData = [
'tag_id' => 2,
];
$this->scenario();
$this->assertCount(1, $this->Results);

// タグ 3 で検索
$this->searchData = [
'tag_id' => 3,
];
$this->scenario();
$this->assertCount(0, $this->Results);

// 文字列とのAND検索 → 結果あり
$this->searchData = [
'id' => 'りんご',
'tag_id' => 1,
];
$this->scenario();
$this->assertCount(1, $this->Results);

// 文字列とのAND検索 → 結果0件
$this->searchData = [
'id' => 'りんご',
'tag_id' => 2,
];
$this->scenario();
$this->assertCount(0, $this->Results);
}
}