forked from hyva-themes/magento2-hyva-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
537 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Hyva\Admin\Api; | ||
|
||
use Hyva\Admin\ViewModel\HyvaGrid\ColumnDefinitionInterface; | ||
use Hyva\Admin\ViewModel\HyvaGrid\GridFilter; | ||
use Hyva\Admin\ViewModel\HyvaGrid\GridFilterInterface; | ||
use Magento\Framework\Api\SearchCriteriaBuilder; | ||
use Magento\Framework\View\Element\Template; | ||
|
||
interface GridFilterTypeInterface | ||
{ | ||
public function getRenderer(ColumnDefinitionInterface $columnDefinition): Template; | ||
|
||
public function apply( | ||
SearchCriteriaBuilder $searchCriteriaBuilder, | ||
GridFilterInterface $gridFilter, | ||
$filterValue | ||
): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Hyva\Admin\Model\DataType; | ||
|
||
use Hyva\Admin\Api\DataTypeInterface; | ||
|
||
class IntDataType implements DataTypeInterface | ||
{ | ||
const TYPE_INT = 'int'; | ||
|
||
public function valueToTypeCode($value): ?string | ||
{ | ||
return is_int($value) || (is_string($value) && preg_match('/^\d+$/', $value)) | ||
? self::TYPE_INT | ||
: null; | ||
} | ||
|
||
public function typeToTypeCode(string $type): ?string | ||
{ | ||
return $type === self::TYPE_INT | ||
? self::TYPE_INT | ||
: null; | ||
} | ||
|
||
public function toString($value): ?string | ||
{ | ||
return $this->valueToTypeCode($value) | ||
? (string) $value | ||
: null; | ||
} | ||
|
||
public function toHtmlRecursive($value, $maxRecursionDepth = self::UNLIMITED_RECURSION): ?string | ||
{ | ||
return $this->toString($value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Hyva\Admin\Model\GridFilter; | ||
|
||
use Hyva\Admin\Model\DataType\BooleanDataType; | ||
use Hyva\Admin\ViewModel\HyvaGrid\ColumnDefinitionInterface; | ||
use Hyva\Admin\ViewModel\HyvaGrid\GridFilterInterface; | ||
use Magento\Framework\Api\SearchCriteriaBuilder; | ||
use Magento\Framework\View\Element\Template; | ||
use Magento\Framework\View\LayoutInterface; | ||
|
||
class BooleanFilter implements ColumnDefinitionMatchingFilterInterface | ||
{ | ||
private LayoutInterface $layout; | ||
|
||
public function __construct(LayoutInterface $layout) | ||
{ | ||
$this->layout = $layout; | ||
} | ||
|
||
public function isMatchingFilter(GridFilterInterface $filter): bool | ||
{ | ||
return $filter->getColumnDefinition()->getType() === BooleanDataType::TYPE_BOOL; | ||
} | ||
|
||
public function getRenderer(ColumnDefinitionInterface $columnDefinition): Template | ||
{ | ||
/** @var Template $templateBlock */ | ||
$templateBlock = $this->layout->createBlock(Template::class); | ||
$templateBlock->setTemplate('Hyva_Admin::filter/bool.phtml'); | ||
|
||
return $templateBlock; | ||
} | ||
|
||
public function apply( | ||
SearchCriteriaBuilder $searchCriteriaBuilder, | ||
GridFilterInterface $gridFilter, | ||
$filterValue | ||
): void { | ||
if ($this->isValue($filterValue)) { | ||
$key = $gridFilter->getColumnDefinition()->getKey(); | ||
$searchCriteriaBuilder->addFilter($key, (int) $filterValue, 'eq'); | ||
} | ||
} | ||
|
||
private function isValue($value): bool | ||
{ | ||
return isset($value) && '' !== $value; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
Model/GridFilter/ColumnDefinitionMatchingFilterInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Hyva\Admin\Model\GridFilter; | ||
|
||
use Hyva\Admin\Api\GridFilterTypeInterface; | ||
use Hyva\Admin\ViewModel\HyvaGrid\ColumnDefinitionInterface; | ||
use Hyva\Admin\ViewModel\HyvaGrid\GridFilterInterface; | ||
|
||
interface ColumnDefinitionMatchingFilterInterface extends GridFilterTypeInterface | ||
{ | ||
public function isMatchingFilter(GridFilterInterface $filter): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Hyva\Admin\Model\GridFilter; | ||
|
||
use Hyva\Admin\Model\DataType\DateTimeDataType; | ||
use Hyva\Admin\ViewModel\HyvaGrid\ColumnDefinitionInterface; | ||
use Hyva\Admin\ViewModel\HyvaGrid\GridFilterInterface; | ||
use Magento\Framework\Api\SearchCriteriaBuilder; | ||
use Magento\Framework\View\Element\Template; | ||
use Magento\Framework\View\LayoutInterface; | ||
|
||
class DateRangeFilter implements ColumnDefinitionMatchingFilterInterface | ||
{ | ||
private LayoutInterface $layout; | ||
|
||
public function __construct(LayoutInterface $layout) | ||
{ | ||
$this->layout = $layout; | ||
} | ||
|
||
public function isMatchingFilter(GridFilterInterface $filter): bool | ||
{ | ||
return $filter->getColumnDefinition()->getType() === DateTimeDataType::TYPE_DATETIME; | ||
} | ||
|
||
public function getRenderer(ColumnDefinitionInterface $columnDefinition): Template | ||
{ | ||
/** @var Template $templateBlock */ | ||
$templateBlock = $this->layout->createBlock(Template::class); | ||
$templateBlock->setTemplate('Hyva_Admin::filter/date-range.phtml'); | ||
|
||
return $templateBlock; | ||
} | ||
|
||
public function apply( | ||
SearchCriteriaBuilder $searchCriteriaBuilder, | ||
GridFilterInterface $gridFilter, | ||
$filterValue | ||
): void { | ||
$key = $gridFilter->getColumnDefinition()->getKey(); | ||
if ($this->isValue($from = $filterValue['from'] ?? '')) { | ||
$searchCriteriaBuilder->addFilter($key, $from, 'from'); | ||
} | ||
if ($this->isValue($to = $filterValue['to'] ?? '')) { | ||
$searchCriteriaBuilder->addFilter($key, $to, 'to'); | ||
} | ||
} | ||
|
||
private function isValue($value): bool | ||
{ | ||
return isset($value) && '' !== $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Hyva\Admin\Model\GridFilter; | ||
|
||
use Hyva\Admin\Api\GridFilterTypeInterface; | ||
use Hyva\Admin\ViewModel\HyvaGrid\ColumnDefinitionInterface; | ||
use Hyva\Admin\ViewModel\HyvaGrid\GridFilterInterface; | ||
use Magento\Framework\ObjectManagerInterface; | ||
|
||
class GridFilterTypeLocator | ||
{ | ||
private ObjectManagerInterface $objectManager; | ||
|
||
private array $columnTypeMatchingFilterTypeMap; | ||
|
||
public function __construct(ObjectManagerInterface $objectManager, array $columnTypeMatchingFilterMap) | ||
{ | ||
$this->objectManager = $objectManager; | ||
$this->columnTypeMatchingFilterTypeMap = $columnTypeMatchingFilterMap; | ||
} | ||
|
||
public function findFilterForColumn( | ||
GridFilterInterface $gridFilter, | ||
ColumnDefinitionInterface $columnDefinition | ||
): GridFilterTypeInterface { | ||
foreach ($this->columnTypeMatchingFilterTypeMap as $type) { | ||
$filterType = $this->get($type); | ||
if ($this->canMatchColumn($filterType) && $filterType->isMatchingFilter($gridFilter, $columnDefinition)) { | ||
return $filterType; | ||
} | ||
} | ||
|
||
$msg = sprintf('Unable to determine filter type for column "%s"', $columnDefinition->getKey()); | ||
throw new \OutOfBoundsException($msg); | ||
} | ||
|
||
private function canMatchColumn(GridFilterTypeInterface $filterType): bool | ||
{ | ||
return $filterType instanceof ColumnDefinitionMatchingFilterInterface; | ||
} | ||
|
||
public function get(string $filterType): GridFilterTypeInterface | ||
{ | ||
return $this->objectManager->get($filterType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Hyva\Admin\Model\GridFilter; | ||
|
||
use Hyva\Admin\ViewModel\HyvaGrid\ColumnDefinitionInterface; | ||
use Hyva\Admin\ViewModel\HyvaGrid\FilterOptionInterface; | ||
use Hyva\Admin\ViewModel\HyvaGrid\GridFilterInterface; | ||
use Magento\Framework\Api\SearchCriteriaBuilder; | ||
use Magento\Framework\View\Element\Template; | ||
use Magento\Framework\View\LayoutInterface; | ||
|
||
use function array_filter as filter; | ||
use function array_values as values; | ||
|
||
class SelectFilter implements ColumnDefinitionMatchingFilterInterface | ||
{ | ||
private LayoutInterface $layout; | ||
|
||
public function __construct(LayoutInterface $layout) | ||
{ | ||
$this->layout = $layout; | ||
} | ||
|
||
public function isMatchingFilter(GridFilterInterface $filter): bool | ||
{ | ||
return (bool) $filter->getOptions(); | ||
} | ||
|
||
public function getRenderer(ColumnDefinitionInterface $columnDefinition): Template | ||
{ | ||
/** @var Template $templateBlock */ | ||
$templateBlock = $this->layout->createBlock(Template::class); | ||
$templateBlock->setTemplate('Hyva_Admin::filter/select.phtml'); | ||
|
||
return $templateBlock; | ||
} | ||
|
||
public function apply( | ||
SearchCriteriaBuilder $searchCriteriaBuilder, | ||
GridFilterInterface $gridFilter, | ||
$filterValue | ||
): void { | ||
if ($option = $this->getSelectedOption($gridFilter->getOptions(), $filterValue)) { | ||
$key = $gridFilter->getColumnDefinition()->getKey(); | ||
$searchCriteriaBuilder->addFilter($key, $option->getValues(), 'finset'); | ||
} | ||
} | ||
|
||
private function getSelectedOption(array $options, $value): ?FilterOptionInterface | ||
{ | ||
return values(filter($options, function (FilterOptionInterface $option) use ($value): bool { | ||
return $option->getValueId() === $value; | ||
}))[0] ?? null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Hyva\Admin\Model\GridFilter; | ||
|
||
use Hyva\Admin\ViewModel\HyvaGrid\ColumnDefinitionInterface; | ||
use Hyva\Admin\ViewModel\HyvaGrid\GridFilterInterface; | ||
use Magento\Framework\Api\SearchCriteriaBuilder; | ||
use Magento\Framework\View\Element\Template; | ||
use Magento\Framework\View\LayoutInterface; | ||
|
||
class TextFilter implements ColumnDefinitionMatchingFilterInterface | ||
{ | ||
private LayoutInterface $layout; | ||
|
||
public function __construct(LayoutInterface $layout) | ||
{ | ||
$this->layout = $layout; | ||
} | ||
|
||
public function isMatchingFilter(GridFilterInterface $filter): bool | ||
{ | ||
// default filter type | ||
return true; | ||
} | ||
|
||
public function getRenderer(ColumnDefinitionInterface $columnDefinition): Template | ||
{ | ||
/** @var Template $templateBlock */ | ||
$templateBlock = $this->layout->createBlock(Template::class); | ||
$templateBlock->setTemplate('Hyva_Admin::filter/text.phtml'); | ||
|
||
return $templateBlock; | ||
} | ||
|
||
public function apply( | ||
SearchCriteriaBuilder $searchCriteriaBuilder, | ||
GridFilterInterface $gridFilter, | ||
$filterValue | ||
): void { | ||
if ($this->isValue($filterValue)) { | ||
$key = $gridFilter->getColumnDefinition()->getKey(); | ||
$searchCriteriaBuilder->addFilter($key, '%' . $filterValue . '%', 'like'); | ||
} | ||
} | ||
|
||
private function isValue($value): bool | ||
{ | ||
return isset($value) && '' !== $value; | ||
} | ||
} |
Oops, something went wrong.