* added base classes for repositories and queries #40 * prepared controller and repositories to use fetchByQuery() #40 * removed findAll() which has added pagination to the original doctrine method #40
This commit is contained in:
@@ -14,6 +14,7 @@ namespace AppBundle\Controller\Admin;
|
||||
use AppBundle\Controller\AbstractController;
|
||||
use AppBundle\Entity\User;
|
||||
use AppBundle\Form\UserCreateType;
|
||||
use AppBundle\Repository\Query\UserQuery;
|
||||
use Pagerfanta\Pagerfanta;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
||||
@@ -40,8 +41,12 @@ class UserController extends AbstractController
|
||||
*/
|
||||
public function indexAction($page)
|
||||
{
|
||||
$query = new UserQuery();
|
||||
$query->setVisibility(UserQuery::SHOW_BOTH);
|
||||
$query->setPage($page);
|
||||
|
||||
/* @var $entries Pagerfanta */
|
||||
$entries = $this->getDoctrine()->getRepository(User::class)->findAll($page);
|
||||
$entries = $this->getDoctrine()->getRepository(User::class)->findByQuery($query);
|
||||
|
||||
return $this->render('admin/user.html.twig', ['entries' => $entries]);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace TimesheetBundle\Repository;
|
||||
namespace AppBundle\Repository;
|
||||
|
||||
use AppBundle\Entity\User;
|
||||
use TimesheetBundle\Entity\Activity;
|
||||
@@ -28,31 +28,6 @@ use TimesheetBundle\Model\ActivityStatistic;
|
||||
abstract class AbstractRepository extends EntityRepository
|
||||
{
|
||||
|
||||
/**
|
||||
* @param string $orderBy
|
||||
* @return Query
|
||||
*/
|
||||
protected function queryAll($orderBy = 'id')
|
||||
{
|
||||
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||
|
||||
$qb->select('a')
|
||||
->from($this->getEntityName(), 'a')
|
||||
->orderBy('a.' . $orderBy, 'ASC');
|
||||
|
||||
return $qb->getQuery();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $page
|
||||
*
|
||||
* @return Pagerfanta
|
||||
*/
|
||||
public function findAll($page = 1)
|
||||
{
|
||||
return $this->getPager($this->queryAll(), $page);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Query $query
|
||||
* @param int $page
|
||||
@@ -9,7 +9,7 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace TimesheetBundle\Model\Query;
|
||||
namespace AppBundle\Repository\Query;
|
||||
|
||||
/**
|
||||
* Base class for advanced Repository queries.
|
||||
@@ -34,6 +34,10 @@ class BaseQuery
|
||||
* @var string
|
||||
*/
|
||||
protected $orderBy = 'id';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $order = 'ASC';
|
||||
|
||||
/**
|
||||
* @return int
|
||||
@@ -92,4 +96,24 @@ class BaseQuery
|
||||
$this->orderBy = $orderBy;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getOrder()
|
||||
{
|
||||
return $this->order;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $order
|
||||
* @return BaseQuery
|
||||
*/
|
||||
public function setOrder($order)
|
||||
{
|
||||
if (in_array($order, ['ASC', 'DESC'])) {
|
||||
$this->order = $order;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
22
src/AppBundle/Repository/Query/UserQuery.php
Normal file
22
src/AppBundle/Repository/Query/UserQuery.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai package.
|
||||
*
|
||||
* (c) Kevin Papst <kevin@kevinpapst.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace AppBundle\Repository\Query;
|
||||
|
||||
/**
|
||||
* Can be used for advanced queries with the: UserRepository
|
||||
*
|
||||
* @author Kevin Papst <kevin@kevinpapst.de>
|
||||
*/
|
||||
class UserQuery extends BaseQuery implements VisibilityInterface
|
||||
{
|
||||
use VisibilityTrait;
|
||||
}
|
||||
24
src/AppBundle/Repository/Query/VisibilityInterface.php
Normal file
24
src/AppBundle/Repository/Query/VisibilityInterface.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai package.
|
||||
*
|
||||
* (c) Kevin Papst <kevin@kevinpapst.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace AppBundle\Repository\Query;
|
||||
|
||||
/**
|
||||
* Can be used for advanced queries with the: UserRepository
|
||||
*
|
||||
* @author Kevin Papst <kevin@kevinpapst.de>
|
||||
*/
|
||||
interface VisibilityInterface
|
||||
{
|
||||
const SHOW_VISIBLE = 1;
|
||||
const SHOW_HIDDEN = 0;
|
||||
const SHOW_BOTH = 2;
|
||||
}
|
||||
@@ -9,19 +9,15 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace TimesheetBundle\Model\Query;
|
||||
namespace AppBundle\Repository\Query;
|
||||
|
||||
/**
|
||||
* Can be used for advanced queries with the: CustomerRepository
|
||||
* Can be used for advanced queries with the: UserRepository
|
||||
*
|
||||
* @author Kevin Papst <kevin@kevinpapst.de>
|
||||
*/
|
||||
class CustomerQuery extends BaseQuery
|
||||
trait VisibilityTrait
|
||||
{
|
||||
const SHOW_VISIBLE = 1;
|
||||
const SHOW_HIDDEN = 0;
|
||||
const SHOW_BOTH = 2;
|
||||
|
||||
protected $visibility = self::SHOW_VISIBLE;
|
||||
|
||||
/**
|
||||
@@ -34,7 +30,7 @@ class CustomerQuery extends BaseQuery
|
||||
|
||||
/**
|
||||
* @param int $visibility
|
||||
* @return CustomerQuery
|
||||
* @return ProjectQuery
|
||||
*/
|
||||
public function setVisibility($visibility)
|
||||
{
|
||||
@@ -12,17 +12,14 @@
|
||||
namespace AppBundle\Repository;
|
||||
|
||||
use AppBundle\Model\UserStatistic;
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Pagerfanta\Adapter\DoctrineORMAdapter;
|
||||
use Pagerfanta\Pagerfanta;
|
||||
use AppBundle\Repository\Query\UserQuery;
|
||||
|
||||
/**
|
||||
* Class UserRepository
|
||||
*
|
||||
* @author Kevin Papst <kevin@kevinpapst.de>
|
||||
*/
|
||||
class UserRepository extends EntityRepository
|
||||
class UserRepository extends AbstractRepository
|
||||
{
|
||||
|
||||
/**
|
||||
@@ -42,46 +39,30 @@ class UserRepository extends EntityRepository
|
||||
return $stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Query
|
||||
*/
|
||||
protected function queryAll()
|
||||
{
|
||||
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||
|
||||
$qb->select('u')
|
||||
->from('AppBundle:User', 'u')
|
||||
->orderBy('u.id', 'ASC');
|
||||
|
||||
return $qb->getQuery();
|
||||
}
|
||||
|
||||
public function findByUsername($username)
|
||||
{
|
||||
return $this->findOneBy(['username' => $username]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $page
|
||||
*
|
||||
* @return Pagerfanta
|
||||
* @param UserQuery $query
|
||||
* @return \Pagerfanta\Pagerfanta
|
||||
*/
|
||||
public function findAll($page = 1)
|
||||
public function findByQuery(UserQuery $query)
|
||||
{
|
||||
return $this->getPager($this->queryAll(), $page);
|
||||
}
|
||||
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||
|
||||
/**
|
||||
* @param Query $query
|
||||
* @param int $page
|
||||
* @return Pagerfanta
|
||||
*/
|
||||
protected function getPager(Query $query, $page = 1)
|
||||
{
|
||||
$paginator = new Pagerfanta(new DoctrineORMAdapter($query, false));
|
||||
$paginator->setMaxPerPage(25);
|
||||
$paginator->setCurrentPage($page);
|
||||
// if we join activities, the maxperpage limit will limit the list to the amount or projects + activties
|
||||
$qb->select('u')
|
||||
->from('AppBundle:User', 'u')
|
||||
->orderBy('u.' . $query->getOrderBy(), $query->getOrder());
|
||||
|
||||
return $paginator;
|
||||
if ($query->getVisibility() === UserQuery::SHOW_VISIBLE) {
|
||||
$qb->andWhere('u.visible = 1');
|
||||
} elseif ($query->getVisibility() === UserQuery::SHOW_HIDDEN) {
|
||||
$qb->andWhere('u.visible = 0');
|
||||
}
|
||||
|
||||
return $this->getPager($qb->getQuery(), $query->getPage(), $query->getPageSize());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
|
||||
use TimesheetBundle\Form\ActivityEditForm;
|
||||
use TimesheetBundle\Repository\ActivityRepository;
|
||||
use TimesheetBundle\Repository\Query\ActivityQuery;
|
||||
|
||||
/**
|
||||
* Controller used to manage activities in the admin part of the site.
|
||||
@@ -41,8 +42,12 @@ class ActivityController extends AbstractController
|
||||
*/
|
||||
public function indexAction($page)
|
||||
{
|
||||
$query = new ActivityQuery();
|
||||
$query->setVisibility(ActivityQuery::SHOW_BOTH);
|
||||
$query->setPage($page);
|
||||
|
||||
/* @var $entries Pagerfanta */
|
||||
$entries = $this->getDoctrine()->getRepository(Activity::class)->findAll($page);
|
||||
$entries = $this->getDoctrine()->getRepository(Activity::class)->findByQuery($query);
|
||||
|
||||
return $this->render('TimesheetBundle:admin:activity.html.twig', ['entries' => $entries]);
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
|
||||
use TimesheetBundle\Form\CustomerEditForm;
|
||||
use TimesheetBundle\Model\Query\CustomerQuery;
|
||||
use TimesheetBundle\Repository\Query\CustomerQuery;
|
||||
|
||||
/**
|
||||
* Controller used to manage activities in the admin part of the site.
|
||||
|
||||
@@ -21,7 +21,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
|
||||
use TimesheetBundle\Form\ProjectEditForm;
|
||||
use TimesheetBundle\Repository\ProjectRepository;
|
||||
use TimesheetBundle\Repository\Query\ProjectQuery;
|
||||
|
||||
/**
|
||||
* Controller used to manage projects in the admin part of the site.
|
||||
@@ -41,8 +41,12 @@ class ProjectController extends AbstractController
|
||||
*/
|
||||
public function indexAction($page)
|
||||
{
|
||||
$query = new ProjectQuery();
|
||||
$query->setVisibility(ProjectQuery::SHOW_BOTH);
|
||||
$query->setPage($page);
|
||||
|
||||
/* @var $entries Pagerfanta */
|
||||
$entries = $this->getDoctrine()->getRepository(Project::class)->findAll($page);
|
||||
$entries = $this->getDoctrine()->getRepository(Project::class)->findByQuery($query);
|
||||
|
||||
return $this->render('TimesheetBundle:admin:project.html.twig', ['entries' => $entries]);
|
||||
}
|
||||
|
||||
@@ -17,8 +17,8 @@ use TimesheetBundle\Entity\Project;
|
||||
use TimesheetBundle\Entity\Timesheet;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use TimesheetBundle\Form\TimesheetToolbarForm;
|
||||
use TimesheetBundle\Repository\Query\TimesheetQuery;
|
||||
use TimesheetBundle\Repository\TimesheetRepository;
|
||||
use TimesheetBundle\Model\Query\TimesheetQuery;
|
||||
|
||||
/**
|
||||
* Helper functions for Timesheet controller
|
||||
|
||||
@@ -18,7 +18,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use TimesheetBundle\Form\Type\ActivityType;
|
||||
use TimesheetBundle\Form\Type\CustomerType;
|
||||
use TimesheetBundle\Form\Type\ProjectType;
|
||||
use TimesheetBundle\Model\Query\TimesheetQuery;
|
||||
use TimesheetBundle\Repository\Query\TimesheetQuery;
|
||||
|
||||
/**
|
||||
* Defines the form used for filtering the timesheet.
|
||||
|
||||
@@ -12,20 +12,18 @@
|
||||
namespace TimesheetBundle\Repository;
|
||||
|
||||
use AppBundle\Entity\User;
|
||||
use Doctrine\ORM\Query;
|
||||
use AppBundle\Repository\AbstractRepository;
|
||||
use TimesheetBundle\Entity\Activity;
|
||||
use TimesheetBundle\Entity\Timesheet;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Pagerfanta\Adapter\DoctrineORMAdapter;
|
||||
use Pagerfanta\Pagerfanta;
|
||||
use TimesheetBundle\Model\ActivityStatistic;
|
||||
use TimesheetBundle\Repository\Query\ActivityQuery;
|
||||
|
||||
/**
|
||||
* Class ActivityRepository
|
||||
*
|
||||
* @author Kevin Papst <kevin@kevinpapst.de>
|
||||
*/
|
||||
class ActivityRepository extends EntityRepository
|
||||
class ActivityRepository extends AbstractRepository
|
||||
{
|
||||
|
||||
/**
|
||||
@@ -95,67 +93,29 @@ class ActivityRepository extends EntityRepository
|
||||
return $stats;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @return Query
|
||||
* @param ActivityQuery $query
|
||||
* @return \Pagerfanta\Pagerfanta
|
||||
*/
|
||||
protected function queryLatest(User $user = null)
|
||||
public function findByQuery(ActivityQuery $query)
|
||||
{
|
||||
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||
|
||||
$qb->select('a')
|
||||
$qb->select('a', 'p', 'c')
|
||||
->from('TimesheetBundle:Activity', 'a')
|
||||
->orderBy('a.id', 'DESC');
|
||||
->join('a.project', 'p')
|
||||
->join('p.customer', 'c')
|
||||
->orderBy('a.' . $query->getOrderBy(), $query->getOrder());
|
||||
|
||||
return $qb->getQuery();
|
||||
}
|
||||
if ($query->getVisibility() === ActivityQuery::SHOW_VISIBLE) {
|
||||
$qb->andWhere('a.visible = 1');
|
||||
// TODO check for visibility of customer and project
|
||||
} elseif ($query->getVisibility() === ActivityQuery::SHOW_HIDDEN) {
|
||||
$qb->andWhere('a.visible = 0');
|
||||
// TODO check for visibility of customer and project
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $orderBy
|
||||
* @return Query
|
||||
*/
|
||||
protected function queryAll($orderBy = 'id')
|
||||
{
|
||||
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||
|
||||
$qb->select('a')
|
||||
->from('TimesheetBundle:Activity', 'a')
|
||||
->orderBy('a.' . $orderBy, 'ASC');
|
||||
|
||||
return $qb->getQuery();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @param int $page
|
||||
* @return Pagerfanta
|
||||
*/
|
||||
public function findLatest(User $user, $page = 1)
|
||||
{
|
||||
return $this->getPager($this->queryLatest($user), $page);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $page
|
||||
*
|
||||
* @return Pagerfanta
|
||||
*/
|
||||
public function findAll($page = 1)
|
||||
{
|
||||
return $this->getPager($this->queryAll(), $page);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Query $query
|
||||
* @param int $page
|
||||
* @return Pagerfanta
|
||||
*/
|
||||
protected function getPager(Query $query, $page = 1)
|
||||
{
|
||||
$paginator = new Pagerfanta(new DoctrineORMAdapter($query, false));
|
||||
$paginator->setMaxPerPage(25);
|
||||
$paginator->setCurrentPage($page);
|
||||
|
||||
return $paginator;
|
||||
return $this->getPager($qb->getQuery(), $query->getPage(), $query->getPageSize());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,9 +11,10 @@
|
||||
|
||||
namespace TimesheetBundle\Repository;
|
||||
|
||||
use AppBundle\Repository\AbstractRepository;
|
||||
use TimesheetBundle\Entity\Customer;
|
||||
use TimesheetBundle\Model\CustomerStatistic;
|
||||
use TimesheetBundle\Model\Query\CustomerQuery;
|
||||
use TimesheetBundle\Repository\Query\CustomerQuery;
|
||||
|
||||
/**
|
||||
* Class CustomerRepository
|
||||
@@ -41,7 +42,7 @@ class CustomerRepository extends AbstractRepository
|
||||
public function getGlobalStatistics()
|
||||
{
|
||||
$countAll = $this->getEntityManager()
|
||||
->createQuery('SELECT COUNT(a.id) FROM TimesheetBundle:Customer c')
|
||||
->createQuery('SELECT COUNT(c.id) FROM TimesheetBundle:Customer c')
|
||||
->getSingleScalarResult();
|
||||
|
||||
$stats = new CustomerStatistic();
|
||||
@@ -59,7 +60,7 @@ class CustomerRepository extends AbstractRepository
|
||||
|
||||
$qb->select('c')
|
||||
->from('TimesheetBundle:Customer', 'c')
|
||||
->orderBy('c.' . $query->getOrderBy(), 'ASC');
|
||||
->orderBy('c.' . $query->getOrderBy(), $query->getOrder());
|
||||
|
||||
if ($query->getVisibility() === CustomerQuery::SHOW_VISIBLE) {
|
||||
$qb->andWhere('c.visible = 1');
|
||||
|
||||
@@ -11,20 +11,17 @@
|
||||
|
||||
namespace TimesheetBundle\Repository;
|
||||
|
||||
use AppBundle\Entity\User;
|
||||
use AppBundle\Repository\AbstractRepository;
|
||||
use TimesheetBundle\Entity\Project;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Doctrine\ORM\Query;
|
||||
use Pagerfanta\Adapter\DoctrineORMAdapter;
|
||||
use Pagerfanta\Pagerfanta;
|
||||
use TimesheetBundle\Model\ProjectStatistic;
|
||||
use TimesheetBundle\Repository\Query\ProjectQuery;
|
||||
|
||||
/**
|
||||
* Class ProjectRepository
|
||||
*
|
||||
* @author Kevin Papst <kevin@kevinpapst.de>
|
||||
*/
|
||||
class ProjectRepository extends EntityRepository
|
||||
class ProjectRepository extends AbstractRepository
|
||||
{
|
||||
|
||||
/**
|
||||
@@ -54,66 +51,27 @@ class ProjectRepository extends EntityRepository
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @return Query
|
||||
* @param ProjectQuery $query
|
||||
* @return \Pagerfanta\Pagerfanta
|
||||
*/
|
||||
protected function queryLatest(User $user = null)
|
||||
public function findByQuery(ProjectQuery $query)
|
||||
{
|
||||
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||
|
||||
$qb->select('p')
|
||||
// if we join activities, the maxperpage limit will limit the list due to the raised amount of rows by projects * activities
|
||||
$qb->select('p', 'c')
|
||||
->from('TimesheetBundle:Project', 'p')
|
||||
->orderBy('p.id', 'DESC');
|
||||
->join('p.customer', 'c')
|
||||
->orderBy('p.' . $query->getOrderBy(), $query->getOrder());
|
||||
|
||||
return $qb->getQuery();
|
||||
}
|
||||
if ($query->getVisibility() === ProjectQuery::SHOW_VISIBLE) {
|
||||
$qb->andWhere('p.visible = 1');
|
||||
// TODO check for visibility of customer
|
||||
} elseif ($query->getVisibility() === ProjectQuery::SHOW_HIDDEN) {
|
||||
$qb->andWhere('p.visible = 0');
|
||||
// TODO check for visibility of customer
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $orderBy
|
||||
* @return Query
|
||||
*/
|
||||
protected function queryAll($orderBy = 'id')
|
||||
{
|
||||
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||
|
||||
$qb->select('p')
|
||||
->from('TimesheetBundle:Project', 'p')
|
||||
->orderBy('p.' . $orderBy, 'ASC');
|
||||
|
||||
return $qb->getQuery();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @param int $page
|
||||
* @return Pagerfanta
|
||||
*/
|
||||
public function findLatest(User $user, $page = 1)
|
||||
{
|
||||
return $this->getPager($this->queryLatest($user), $page);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $page
|
||||
*
|
||||
* @return Pagerfanta
|
||||
*/
|
||||
public function findAll($page = 1)
|
||||
{
|
||||
return $this->getPager($this->queryAll(), $page);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Query $query
|
||||
* @param int $page
|
||||
* @return Pagerfanta
|
||||
*/
|
||||
protected function getPager(Query $query, $page = 1)
|
||||
{
|
||||
$paginator = new Pagerfanta(new DoctrineORMAdapter($query, false));
|
||||
$paginator->setMaxPerPage(25);
|
||||
$paginator->setCurrentPage($page);
|
||||
|
||||
return $paginator;
|
||||
return $this->getPager($qb->getQuery(), $query->getPage(), $query->getPageSize());
|
||||
}
|
||||
}
|
||||
|
||||
26
src/TimesheetBundle/Repository/Query/ActivityQuery.php
Normal file
26
src/TimesheetBundle/Repository/Query/ActivityQuery.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai package.
|
||||
*
|
||||
* (c) Kevin Papst <kevin@kevinpapst.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace TimesheetBundle\Repository\Query;
|
||||
|
||||
use AppBundle\Repository\Query\BaseQuery;
|
||||
use AppBundle\Repository\Query\VisibilityInterface;
|
||||
use AppBundle\Repository\Query\VisibilityTrait;
|
||||
|
||||
/**
|
||||
* Can be used for advanced queries with the: ActivityRepository
|
||||
*
|
||||
* @author Kevin Papst <kevin@kevinpapst.de>
|
||||
*/
|
||||
class ActivityQuery extends BaseQuery implements VisibilityInterface
|
||||
{
|
||||
use VisibilityTrait;
|
||||
}
|
||||
26
src/TimesheetBundle/Repository/Query/CustomerQuery.php
Normal file
26
src/TimesheetBundle/Repository/Query/CustomerQuery.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai package.
|
||||
*
|
||||
* (c) Kevin Papst <kevin@kevinpapst.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace TimesheetBundle\Repository\Query;
|
||||
|
||||
use AppBundle\Repository\Query\BaseQuery;
|
||||
use AppBundle\Repository\Query\VisibilityInterface;
|
||||
use AppBundle\Repository\Query\VisibilityTrait;
|
||||
|
||||
/**
|
||||
* Can be used for advanced queries with the: CustomerRepository
|
||||
*
|
||||
* @author Kevin Papst <kevin@kevinpapst.de>
|
||||
*/
|
||||
class CustomerQuery extends BaseQuery implements VisibilityInterface
|
||||
{
|
||||
use VisibilityTrait;
|
||||
}
|
||||
26
src/TimesheetBundle/Repository/Query/ProjectQuery.php
Normal file
26
src/TimesheetBundle/Repository/Query/ProjectQuery.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai package.
|
||||
*
|
||||
* (c) Kevin Papst <kevin@kevinpapst.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace TimesheetBundle\Repository\Query;
|
||||
|
||||
use AppBundle\Repository\Query\BaseQuery;
|
||||
use AppBundle\Repository\Query\VisibilityInterface;
|
||||
use AppBundle\Repository\Query\VisibilityTrait;
|
||||
|
||||
/**
|
||||
* Can be used for advanced queries with the: ProjectRepository
|
||||
*
|
||||
* @author Kevin Papst <kevin@kevinpapst.de>
|
||||
*/
|
||||
class ProjectQuery extends BaseQuery implements VisibilityInterface
|
||||
{
|
||||
use VisibilityTrait;
|
||||
}
|
||||
@@ -9,9 +9,10 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace TimesheetBundle\Model\Query;
|
||||
namespace TimesheetBundle\Repository\Query;
|
||||
|
||||
use AppBundle\Entity\User;
|
||||
use AppBundle\Repository\Query\BaseQuery;
|
||||
use TimesheetBundle\Entity\Activity;
|
||||
use TimesheetBundle\Entity\Customer;
|
||||
use TimesheetBundle\Entity\Project;
|
||||
@@ -21,16 +22,22 @@ use TimesheetBundle\Entity\Project;
|
||||
*
|
||||
* @author Kevin Papst <kevin@kevinpapst.de>
|
||||
*/
|
||||
class TimesheetQuery
|
||||
class TimesheetQuery extends BaseQuery
|
||||
{
|
||||
|
||||
const DEFAULT_PAGESIZE = 25;
|
||||
const DEFAULT_PAGE = 1;
|
||||
|
||||
const STATE_ALL = 0;
|
||||
const STATE_RUNNING = 1;
|
||||
const STATE_STOPPED = 2;
|
||||
|
||||
/**
|
||||
* Overwritten for different default order
|
||||
* @var string
|
||||
*/
|
||||
protected $order = 'DESC';
|
||||
/**
|
||||
* Overwritten for different default order
|
||||
* @var string
|
||||
*/
|
||||
protected $orderBy = 'begin';
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
@@ -47,14 +54,6 @@ class TimesheetQuery
|
||||
* @var Customer
|
||||
*/
|
||||
protected $customer;
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $page = self::DEFAULT_PAGE;
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $pageSize = self::DEFAULT_PAGESIZE;
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
@@ -70,7 +69,7 @@ class TimesheetQuery
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @return Timesheet
|
||||
* @return TimesheetQuery
|
||||
*/
|
||||
public function setUser(User $user = null)
|
||||
{
|
||||
@@ -90,7 +89,7 @@ class TimesheetQuery
|
||||
|
||||
/**
|
||||
* @param Activity $activity
|
||||
* @return Timesheet
|
||||
* @return TimesheetQuery
|
||||
*/
|
||||
public function setActivity(Activity $activity = null)
|
||||
{
|
||||
@@ -111,7 +110,7 @@ class TimesheetQuery
|
||||
* Is overwritten by: setActivity()
|
||||
*
|
||||
* @param Project $project
|
||||
* @return Timesheet
|
||||
* @return TimesheetQuery
|
||||
*/
|
||||
public function setProject(Project $project = null)
|
||||
{
|
||||
@@ -132,7 +131,7 @@ class TimesheetQuery
|
||||
* Is overwritten by: setActivity() and setProject()
|
||||
*
|
||||
* @param Customer $customer
|
||||
* @return Timesheet
|
||||
* @return TimesheetQuery
|
||||
*/
|
||||
public function setCustomer(Customer $customer = null)
|
||||
{
|
||||
@@ -140,44 +139,6 @@ class TimesheetQuery
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getPage()
|
||||
{
|
||||
return $this->page;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $page
|
||||
* @return Timesheet
|
||||
*/
|
||||
public function setPage($page)
|
||||
{
|
||||
$this->page = $page;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getPageSize()
|
||||
{
|
||||
return $this->pageSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $pageSize
|
||||
* @return Timesheet
|
||||
*/
|
||||
public function setPageSize($pageSize)
|
||||
{
|
||||
if (!empty($pageSize) && (int) $pageSize > 0) {
|
||||
$this->pageSize = (int) $pageSize;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
@@ -188,7 +149,7 @@ class TimesheetQuery
|
||||
|
||||
/**
|
||||
* @param int $state
|
||||
* @return Timesheet
|
||||
* @return TimesheetQuery
|
||||
*/
|
||||
public function setState($state)
|
||||
{
|
||||
@@ -22,9 +22,9 @@ use Pagerfanta\Pagerfanta;
|
||||
use TimesheetBundle\Model\Statistic\Month;
|
||||
use TimesheetBundle\Model\Statistic\Year;
|
||||
use TimesheetBundle\Model\TimesheetGlobalStatistic;
|
||||
use TimesheetBundle\Model\Query\TimesheetQuery;
|
||||
use TimesheetBundle\Model\TimesheetStatistic;
|
||||
use DateTime;
|
||||
use TimesheetBundle\Repository\Query\TimesheetQuery;
|
||||
|
||||
/**
|
||||
* Class TimesheetRepository
|
||||
@@ -245,6 +245,8 @@ class TimesheetRepository extends EntityRepository
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO replace me by a findByQuery() call
|
||||
*
|
||||
* @param User $user
|
||||
* @return Timesheet[]|null
|
||||
*/
|
||||
@@ -272,26 +274,9 @@ class TimesheetRepository extends EntityRepository
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @return Query
|
||||
* @param TimesheetQuery $query
|
||||
* @return Pagerfanta
|
||||
*/
|
||||
public function queryLatest(User $user = null)
|
||||
{
|
||||
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||
|
||||
$qb->select('t', 'a')
|
||||
->from('TimesheetBundle:Timesheet', 't')
|
||||
->join('t.activity', 'a')
|
||||
->orderBy('t.begin', 'DESC');
|
||||
|
||||
if (null !== $user) {
|
||||
$qb->where('t.user = :user')
|
||||
->setParameter('user', $user);
|
||||
}
|
||||
|
||||
return $qb->getQuery();
|
||||
}
|
||||
|
||||
public function findByQuery(TimesheetQuery $query)
|
||||
{
|
||||
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||
@@ -301,7 +286,7 @@ class TimesheetRepository extends EntityRepository
|
||||
->join('t.activity', 'a')
|
||||
->join('a.project', 'p')
|
||||
->join('p.customer', 'c')
|
||||
->orderBy('t.begin', 'DESC');
|
||||
->orderBy('t.' . $query->getOrderBy(), $query->getOrder());
|
||||
|
||||
if ($query->getUser() !== null) {
|
||||
$qb->andWhere('t.user = :user')
|
||||
@@ -328,26 +313,6 @@ class TimesheetRepository extends EntityRepository
|
||||
return $this->getPager($qb->getQuery(), $query->getPage(), $query->getPageSize());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @param int $page
|
||||
* @return Pagerfanta
|
||||
*/
|
||||
public function findLatest(User $user, $page = 1)
|
||||
{
|
||||
return $this->getPager($this->queryLatest($user), $page);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $page
|
||||
*
|
||||
* @return Pagerfanta
|
||||
*/
|
||||
public function findAll($page = 1)
|
||||
{
|
||||
return $this->getPager($this->queryLatest(), $page);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Query $query
|
||||
* @param int $page
|
||||
|
||||
Reference in New Issue
Block a user