Skip to content

Commit

Permalink
Adding filter by tags into product listing in admin.
Browse files Browse the repository at this point in the history
remp/crm#985
  • Loading branch information
lubos-michalik committed Mar 24, 2020
1 parent f7d8bdb commit bbab446
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/lang/products.cs_CZ.neon
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ admin:
admin_filter_form:
text:
placeholder: 'Název / Identifikátor / Veřejný popis / Cena'
tags: Tagy
new:
title: Nový produkt
back: Spať na zoznam
Expand Down
1 change: 1 addition & 0 deletions src/lang/products.en_US.neon
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ admin:
admin_filter_form:
text:
placeholder: 'Name / Identifier / Label / Price'
tags: Tags

new:
title: New product
Expand Down
1 change: 1 addition & 0 deletions src/lang/products.sk_SK.neon
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ admin:
admin_filter_form:
text:
placeholder: 'Názov / Identifikátor / Verejný popis / Cena'
tags: Tagy

new:
title: Nový produkt
Expand Down
11 changes: 8 additions & 3 deletions src/models/Repository/ProductsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Crm\ProductsModule\Distribution\ProductShopCountsDistribution;
use Nette\Database\Context;
use Nette\Database\Table\IRow;
use Nette\Database\Table\Selection;

class ProductsRepository extends Repository
{
Expand Down Expand Up @@ -39,11 +40,11 @@ public function __construct(
$this->productShopCountsDistribution = $productShopCountsDistribution;
}

final public function all(string $search = null)
final public function all(string $search = null, array $tags = []): Selection
{
$all = $this->getTable()->order('-sorting DESC, name ASC');

if (is_null($search) || empty(trim($search))) {
if (empty($tags) && ($search === null || empty(trim($search)))) {
return $all;
}

Expand All @@ -57,13 +58,17 @@ final public function all(string $search = null)
// check if searched text is number (replace comma with period; otherwise is_numeric won't work)
$searchNum = str_replace(',', '.', $search);
if (is_numeric($searchNum)) {
$searchFloat = floatval($searchNum);
$searchFloat = (float) $searchNum;
$conditions = array_merge($conditions, [
'price = ?' => $searchFloat,
'catalog_price = ?' => $searchFloat,
]);
}

if (!empty($tags)) {
$all->where(':product_tags.tag_id IN (?)', $tags);
}

return $all->whereOr($conditions);
}

Expand Down
31 changes: 17 additions & 14 deletions src/presenters/ProductsAdminPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,41 @@
use Crm\ProductsModule\Forms\ProductsFormFactory;
use Crm\ProductsModule\PaymentItem\ProductPaymentItem;
use Crm\ProductsModule\Repository\ProductsRepository;
use Crm\ProductsModule\Repository\TagsRepository;
use Nette\Application\UI\Form;
use Nette\Database\Table\ActiveRow;
use Nette\Database\Table\IRow;
use Nette\Http\Request;
use Tomaj\Form\Renderer\BootstrapInlineRenderer;

class ProductsAdminPresenter extends AdminPresenter
{
private $request;

private $productsRepository;

private $productsFormFactory;

private $paymentItemsRepository;

private $tagsRepository;

/** @persistent */
public $tags = [];

public function __construct(
Request $request,
ProductsRepository $productsRepository,
ProductsFormFactory $productsFormFactory,
PaymentItemsRepository $paymentItemsRepository
PaymentItemsRepository $paymentItemsRepository,
TagsRepository $tagsRepository
) {
parent::__construct();
$this->request = $request;
$this->productsRepository = $productsRepository;
$this->productsFormFactory = $productsFormFactory;
$this->paymentItemsRepository = $paymentItemsRepository;
}

public function startup()
{
parent::startup();
$this->text = isset($this->params['text']) ? $this->params['text'] : null;
$this->tagsRepository = $tagsRepository;
}

public function renderDefault()
{
$products = $this->productsRepository->all($this->text);
$products = $this->productsRepository->all($this->text, $this->tags);
$filteredCount = $products->count();

$vp = new VisualPaginator();
Expand Down Expand Up @@ -187,14 +184,20 @@ public function createComponentAdminFilterForm()
->setAttribute('placeholder', $this->translator->translate('products.admin.products.default.admin_filter_form.text.placeholder'))
->setAttribute('autofocus');

$form->addMultiSelect(
'tags',
$this->translator->translate('products.admin.products.default.admin_filter_form.tags'),
$this->tagsRepository->all()->fetchPairs('id', 'code')
)->getControlPrototype()->addAttributes(['class' => 'select2']);

$form->addSubmit('send', $this->translator->translate('system.filter'))
->getControlPrototype()
->setName('button')
->setHtml('<i class="fa fa-filter"></i> ' . $this->translator->translate('system.filter'));
$presenter = $this;
$form->addSubmit('cancel', $this->translator->translate('system.cancel_filter'))
->onClick[] = function () use ($presenter) {
$presenter->redirect('ProductsAdmin:Default', ['text' => '']);
$presenter->redirect('ProductsAdmin:Default', ['text' => '', 'tags' => []]);
};

$form->setDefaults((array)$this->params);
Expand Down

0 comments on commit bbab446

Please sign in to comment.