store search in session (#2735)

* allow to detect used query filters
* allow to set label in action buttons
* allow to reset last search from session
* migrate invoice archive to new search system
* display number of used search filters
This commit is contained in:
Kevin Papst
2021-08-26 01:37:40 +02:00
committed by GitHub
parent 56524a9773
commit 98a3fc99a2
55 changed files with 365 additions and 81 deletions

View File

@@ -185,12 +185,19 @@ class InvoiceRepository extends EntityRepository
case 'date':
$orderBy = 'i.createdAt';
break;
case 'customer':
$orderBy = 'i.customer';
case 'number':
$orderBy = 'i.invoiceNumber';
break;
case 'total':
case 'payed':
$orderBy = 'i.paymentDate';
break;
case 'total_rate':
$orderBy = 'i.total';
break;
case 'tax':
case 'status':
$orderBy = 'i.' . $orderBy;
break;
}
$qb->addOrderBy($orderBy, $query->getOrder());

View File

@@ -19,7 +19,7 @@ class ActivityQuery extends ProjectQuery
public const ACTIVITY_ORDER_ALLOWED = ['id', 'name', 'comment', 'customer', 'project', 'budget', 'timeBudget', 'visible'];
/**
* @var Project[]|int[]
* @var array<Project|int>
*/
private $projects = [];
/**
@@ -36,6 +36,9 @@ class ActivityQuery extends ProjectQuery
parent::__construct();
$this->setDefaults([
'orderBy' => 'name',
'projects' => [],
'globalsOnly' => false,
'excludeGlobals' => false,
]);
}

View File

@@ -85,13 +85,17 @@ class BaseQuery
*/
private $searchTerm;
/**
* @var Bookmark
* @var Bookmark|null
*/
private $bookmark;
/**
* @var string|null
*/
private $name;
/**
* @var bool
*/
private $bookmarkSearch = false;
/**
* @param Team[] $teams
@@ -258,13 +262,44 @@ class BaseQuery
{
$method = 'set' . ucfirst($name);
if (method_exists($this, $method)) {
$this->{$method}($value);
} elseif (property_exists($this, $name)) {
$this->$name = $value;
\call_user_func([$this, $method], $value);
return;
}
if (substr($name, -1) === 's') {
$method = 'add' . ucfirst(substr($name, 0, \strlen($name) - 1));
if (method_exists($this, $method) && \is_array($value)) {
foreach ($value as $v) {
\call_user_func([$this, $method], $v);
}
return;
}
}
if (property_exists($this, $name)) {
$this->{$name} = $value;
}
}
protected function get($name)
{
$methods = ['get' . ucfirst($name), 'is' . ucfirst($name), 'has' . ucfirst($name)];
foreach ($methods as $method) {
if (method_exists($this, $method)) {
return \call_user_func([$this, $method]);
}
}
if (property_exists($this, $name)) {
return $this->{$name};
}
}
/**
* You have to add ALL user facing form fields as default!
*
* @param array $defaults
* @return self
*/
@@ -347,4 +382,66 @@ class BaseQuery
return $query;
}
public function isDefaultFilter(string $filter): bool
{
if (!\array_key_exists($filter, $this->defaults)) {
return false;
}
$expectedValue = $this->defaults[$filter];
return $this->matchesFilter($filter, $expectedValue);
}
public function matchesFilter(string $filter, $expectedValue): bool
{
$currentValue = $this->get($filter);
if (\is_object($currentValue)) {
if ($currentValue != $expectedValue) {
return false;
}
} else {
if ($currentValue !== $expectedValue) {
return false;
}
}
return true;
}
public function countFilter(): int
{
$filter = 0;
foreach (array_keys($this->defaults) as $key) {
if ($key === 'page') {
continue;
}
if (!$this->isDefaultFilter($key)) {
$filter++;
}
}
return $filter;
}
public function resetFilter(): void
{
foreach ($this->defaults as $key => $value) {
$this->set($key, $value);
}
}
public function flagAsBookmarkSearch(): void
{
$this->bookmarkSearch = true;
}
public function isBookmarkSearch(): bool
{
return $this->bookmarkSearch;
}
}

View File

@@ -25,6 +25,7 @@ class CustomerQuery extends BaseQuery implements VisibilityInterface
{
$this->setDefaults([
'orderBy' => 'name',
'visibility' => VisibilityInterface::SHOW_VISIBLE,
]);
}
}

View File

@@ -20,6 +20,16 @@ class ExportQuery extends TimesheetQuery
*/
private $markAsExported = false;
public function __construct()
{
parent::__construct();
$this->setDefaults([
'order' => ExportQuery::ORDER_ASC,
'state' => ExportQuery::STATE_STOPPED,
'exported' => ExportQuery::STATE_NOT_EXPORTED,
]);
}
public function getRenderer(): ?string
{
return $this->renderer;

View File

@@ -21,7 +21,9 @@ class InvoiceArchiveQuery extends BaseQuery
use DateRangeTrait;
public const INVOICE_ARCHIVE_ORDER_ALLOWED = [
'date', 'customer', 'total'
'date', 'total_rate',
// TODO other fields have a problem with translation
// 'number', 'tax', 'payed', 'status'
];
/**
@@ -40,6 +42,8 @@ class InvoiceArchiveQuery extends BaseQuery
'orderBy' => 'date',
'order' => self::ORDER_DESC,
'dateRange' => new DateRange(),
'customers' => [],
'status' => [],
]);
}

View File

@@ -29,6 +29,8 @@ class InvoiceQuery extends TimesheetQuery
{
parent::__construct();
$this->setDefaults([
'order' => InvoiceQuery::ORDER_ASC,
'exported' => InvoiceQuery::STATE_NOT_EXPORTED,
'state' => self::STATE_STOPPED,
'billable' => true,
'markAsExported' => false,

View File

@@ -23,15 +23,15 @@ class ProjectQuery extends BaseQuery implements VisibilityInterface
];
/**
* @var array
* @var array<Customer|int>
*/
private $customers = [];
/**
* @var \DateTime
* @var \DateTime|null
*/
private $projectStart;
/**
* @var \DateTime
* @var \DateTime|null
*/
private $projectEnd;
@@ -39,6 +39,10 @@ class ProjectQuery extends BaseQuery implements VisibilityInterface
{
$this->setDefaults([
'orderBy' => 'name',
'customers' => [],
'projectStart' => null,
'projectEnd' => null,
'visibility' => VisibilityInterface::SHOW_VISIBLE,
]);
}

View File

@@ -24,6 +24,7 @@ class TeamQuery extends BaseQuery
{
$this->setDefaults([
'orderBy' => 'name',
'users' => [],
]);
}

View File

@@ -69,7 +69,13 @@ class TimesheetQuery extends ActivityQuery implements BillableInterface
$this->setDefaults([
'order' => self::ORDER_DESC,
'orderBy' => 'begin',
'dateRange' => new DateRange($resetTimes)
'dateRange' => new DateRange($resetTimes),
'exported' => self::STATE_ALL,
'state' => self::STATE_ALL,
'billable' => null,
'tags' => [],
'users' => [],
'activities' => [],
]);
}

View File

@@ -24,12 +24,17 @@ class UserQuery extends BaseQuery implements VisibilityInterface
* @var string|null
*/
private $role;
/**
* @var Team[]
*/
private $searchTeams = [];
public function __construct()
{
$this->setDefaults([
'orderBy' => 'username',
'searchTeams' => [],
'visibility' => VisibilityInterface::SHOW_VISIBLE,
]);
}