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

Fix ord by id filter interface #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"nette/di": "^3.0",
"nette/utils": "^3.0 | ^4.0",
"nette/application": "*",
"adt/base-query": "^1.7"
"adt/doctrine-components": "^2.9"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 3 additions & 1 deletion src/Components/AjaxSelect/DI/AjaxSelectExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ public static function register(\Nette\DI\Container $container, $globalConfig) {

// if $items are not array of values, we have received query object
if ($items instanceof \ADT\BaseQuery\BaseQuery) {
AjaxSelect\Traits\OrByIdFilterTrait::applyOrByIdFilter($config, $container, $name, $items);
if ($items instanceof AjaxSelect\Interfaces\OrdByIdFilterInterface) {
$items->applyOrByIdFilter($config, $container, $name);
}

if ($items->callSelectPairsAuto()) {
$items->selectPairs();
Expand Down
40 changes: 15 additions & 25 deletions src/Components/AjaxSelect/Entities/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
namespace ADT\Components\AjaxSelect\Entities;

use ADT\Components\AjaxSelect;
use ADT\DoctrineComponents\QueryObject;
use Nette\Application\UI\Presenter;
use Nette\SmartObject;

abstract class AbstractEntity {

abstract class AbstractEntity
{
use SmartObject;

const DATA_ATTRIBUTE_NAME = 'data-adt-ajax-select';

const OPTION_QUERY = 'q';

public abstract function formatValues($values): array;
public abstract function hydrateValues($values): array;

protected abstract function createQueryObject(): QueryObject;

/** @var string */
protected $name;

Expand Down Expand Up @@ -69,9 +74,13 @@ public function areValidValues(array $values) {
$query = $this->createQueryObject()
->byId($values);

$this->filterQueryObject($query);
if (method_exists($this, 'filterQueryObject')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proč tohle není abstraktní funkce a nebo teda prázdná protected?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no protoze pak nemuzes definovat tridu u parametru... a musis to delat pres anotaci... tezko rict co je lepsi...

$this->filterQueryObject($query);
}

AjaxSelect\Traits\OrByIdFilterTrait::applyOrByIdFilter($this->config, $form, $control->getName(), $query);
if ($query instanceof AjaxSelect\Interfaces\OrdByIdFilterInterface) {
$query->applyOrByIdFilter($this->config, $form, $control->getName());
}

foreach ($query->fetch() as $row) {
// pouze selectnuté jsou validní
Expand Down Expand Up @@ -105,18 +114,9 @@ public function findValues($limit) {
->applyPaging(0, $limit)
->toArray();

return array_map(function ($row) {
return $row->getId();
}, $rows);
return $rows;
}

/**
* @internal
* @param array $values
* @return array List of items.
*/
public abstract function formatValues($values);

/**
* @internal
* @param array $values
Expand All @@ -140,16 +140,6 @@ public function formatJsonValues($values) {
return $result;
}

/**
* @return \ADT\BaseQuery\BaseQuery freshly created QO without filters
*/
protected abstract function createQueryObject();

/**
* @param \ADT\BaseQuery\BaseQuery $query
*/
protected abstract function filterQueryObject($query);

/**
* @param string $option
* @param mixed $value
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace ADT\Components\AjaxSelect\Interfaces;

interface OrByIdFilterInterface
{
public function orById($id): static;
public function applyOrByIdFilter(array $config, \Nette\Forms\Container $form, string $attributeName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected function processValues($values) {
$validValues = array_merge($validValues, $invalidValues);
}

$validItems = $this->getAjaxEntity()->formatValues($validValues);
$validItems = $this->getAjaxEntity()->formatValues($this->getAjaxEntity()->hydrateValues($validValues));

// add to list of valid values
$this->setItems($this->getItems() + $validItems);
Expand Down
15 changes: 5 additions & 10 deletions src/Components/AjaxSelect/Traits/OrByIdFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@

use ADT\Components\AjaxSelect;

trait OrByIdFilterTrait {
trait OrByIdFilterTrait
{
public abstract function orById($id): static;

/**
* @param array $config
* @param \Nette\Forms\Container $form
* @param string $attributeName
* @param \ADT\BaseQuery\BaseQuery $query
*/
public static function applyOrByIdFilter(array $config, \Nette\Forms\Container $form, string $attributeName, \ADT\BaseQuery\BaseQuery $query)
public function applyOrByIdFilter(array $config, \Nette\Forms\Container $form, string $attributeName)
{
// if orByIdFilter is active and it is entity form, the value, which is set in entity->inputName is included in items.
if ($config[AjaxSelect\DI\AjaxSelectExtension::CONFIG_OR_BY_ID_FILTER] && method_exists($form, 'getEntity') && !empty($form->getEntity())) {
Expand All @@ -24,9 +20,8 @@ public static function applyOrByIdFilter(array $config, \Nette\Forms\Container $
}

if (!empty($defaultValue)) {
$query->orById($defaultValue);
$this->orById($defaultValue);
}
}
}

}