added tags for timesheets (#604)
This commit is contained in:
14
src/Repository/Query/TagQuery.php
Normal file
14
src/Repository/Query/TagQuery.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Repository\Query;
|
||||
|
||||
class TagQuery extends BaseQuery
|
||||
{
|
||||
}
|
||||
@@ -54,6 +54,10 @@ class TimesheetQuery extends ActivityQuery
|
||||
* @var DateRange
|
||||
*/
|
||||
protected $dateRange;
|
||||
/**
|
||||
* @var iterable
|
||||
*/
|
||||
protected $tags;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
@@ -208,4 +212,31 @@ class TimesheetQuery extends ActivityQuery
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable
|
||||
*/
|
||||
public function getTags()
|
||||
{
|
||||
return $this->tags;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param iterable $tags
|
||||
* @return $this
|
||||
*/
|
||||
public function setTags(iterable $tags)
|
||||
{
|
||||
$this->tags = $tags;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasTags()
|
||||
{
|
||||
return !empty($this->tags) && count($this->tags) > 0;
|
||||
}
|
||||
}
|
||||
|
||||
83
src/Repository/TagRepository.php
Normal file
83
src/Repository/TagRepository.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Repository\Query\TagQuery;
|
||||
|
||||
class TagRepository extends AbstractRepository
|
||||
{
|
||||
/**
|
||||
* Find ids of the given tagNames separated by comma
|
||||
* @param string $tagNames
|
||||
* @return array
|
||||
*/
|
||||
public function findIdsByTagNameList($tagNames)
|
||||
{
|
||||
$qb = $this
|
||||
->createQueryBuilder('t')
|
||||
->select('t.id');
|
||||
$list = array_filter(array_unique(array_map('trim', explode(',', $tagNames))));
|
||||
$cnt = 0;
|
||||
foreach ($list as $listElem) {
|
||||
$qb
|
||||
->orWhere('t.name like :elem' . $cnt)
|
||||
->setParameter('elem' . $cnt, '%' . $listElem . '%');
|
||||
$cnt++;
|
||||
}
|
||||
|
||||
return array_column($qb->getQuery()->getScalarResult(), 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all tag names in an alphabetical order
|
||||
*
|
||||
* @param string $filter
|
||||
* @return array
|
||||
*/
|
||||
public function findAllTagNames($filter = null)
|
||||
{
|
||||
$qb = $this->createQueryBuilder('t');
|
||||
|
||||
$qb
|
||||
->select('t.name')
|
||||
->addOrderBy('t.name', 'ASC');
|
||||
|
||||
if (null !== $filter) {
|
||||
$qb
|
||||
->andWhere('t.name LIKE :filter')
|
||||
->setParameter('filter', '%' . $filter . '%');
|
||||
}
|
||||
|
||||
return array_column($qb->getQuery()->getScalarResult(), 'name');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of arrays with each inner array having the structure:
|
||||
* - id
|
||||
* - name
|
||||
* - amount
|
||||
*
|
||||
* @param TagQuery $query
|
||||
* @return array|\Doctrine\ORM\QueryBuilder|\Pagerfanta\Pagerfanta
|
||||
*/
|
||||
public function getTagCount(TagQuery $query)
|
||||
{
|
||||
$qb = $this->createQueryBuilder('tag');
|
||||
|
||||
$qb
|
||||
->select('tag.id, tag.name, count(timesheets.id) as amount')
|
||||
->leftJoin('tag.timesheets', 'timesheets')
|
||||
->addGroupBy('tag.id')
|
||||
->orderBy('tag.name')
|
||||
;
|
||||
|
||||
return $this->getBaseQueryResult($qb, $query);
|
||||
}
|
||||
}
|
||||
@@ -332,12 +332,13 @@ class TimesheetRepository extends AbstractRepository
|
||||
{
|
||||
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||
|
||||
$qb->select('t', 'a', 'p', 'c', 'u')
|
||||
$qb->select('t', 'a', 'p', 'c', 'u', 'tags')
|
||||
->from(Timesheet::class, 't')
|
||||
->leftJoin('t.activity', 'a')
|
||||
->leftJoin('t.user', 'u')
|
||||
->leftJoin('t.project', 'p')
|
||||
->leftJoin('p.customer', 'c')
|
||||
->leftJoin('t.tags', 'tags')
|
||||
->orderBy('t.' . $query->getOrderBy(), $query->getOrder());
|
||||
|
||||
if (null !== $query->getUser()) {
|
||||
@@ -382,6 +383,11 @@ class TimesheetRepository extends AbstractRepository
|
||||
}
|
||||
}
|
||||
|
||||
if ($query->hasTags()) {
|
||||
$qb->andWhere('tags.id IN (:tags)')
|
||||
->setParameter('tags', $query->getTags()->toArray());
|
||||
}
|
||||
|
||||
return $this->getBaseQueryResult($qb, $query);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user