Skip to content

Commit

Permalink
Merge pull request #82 from okazy/feature/tag-search-admin
Browse files Browse the repository at this point in the history
管理画面 商品のタグでの絞り込み
okazy authored Apr 5, 2021
2 parents d3dde59 + 70813a4 commit 33e70c1
Showing 4 changed files with 88 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/Eccube/Form/Type/Admin/SearchProductType.php
Original file line number Diff line number Diff line change
@@ -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\DateType;
@@ -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;
}

/**
@@ -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,
8 changes: 8 additions & 0 deletions src/Eccube/Repository/ProductRepository.php
Original file line number Diff line number Diff line change
@@ -313,6 +313,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'];
7 changes: 7 additions & 0 deletions src/Eccube/Resource/template/admin/Product/index.twig
Original file line number Diff line number Diff line change
@@ -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 }}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 33e70c1

Please sign in to comment.