mirror of
https://github.com/wallabag/wallabag.git
synced 2024-10-31 22:28:54 +00:00
Refactorize the way to retrieve entries
One place to retrieve entries in Entry & Rss controller. More simple and easy to maintain.
This commit is contained in:
parent
4fcb7eaf13
commit
0ab7404f93
3 changed files with 124 additions and 115 deletions
|
@ -113,34 +113,7 @@ class EntryController extends Controller
|
||||||
*/
|
*/
|
||||||
public function showUnreadAction(Request $request, $page)
|
public function showUnreadAction(Request $request, $page)
|
||||||
{
|
{
|
||||||
$form = $this->get('form.factory')->create(new EntryFilterType());
|
return $this->showEntries('unread', $request, $page);
|
||||||
|
|
||||||
$filterBuilder = $this->getDoctrine()
|
|
||||||
->getRepository('WallabagCoreBundle:Entry')
|
|
||||||
->findUnreadByUser($this->getUser()->getId());
|
|
||||||
|
|
||||||
if ($request->query->has($form->getName())) {
|
|
||||||
// manually bind values from the request
|
|
||||||
$form->submit($request->query->get($form->getName()));
|
|
||||||
|
|
||||||
// build the query from the given form object
|
|
||||||
$this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $filterBuilder);
|
|
||||||
}
|
|
||||||
|
|
||||||
$pagerAdapter = new DoctrineORMAdapter($filterBuilder->getQuery());
|
|
||||||
$entries = new Pagerfanta($pagerAdapter);
|
|
||||||
|
|
||||||
$entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
|
|
||||||
$entries->setCurrentPage($page);
|
|
||||||
|
|
||||||
return $this->render(
|
|
||||||
'WallabagCoreBundle:Entry:entries.html.twig',
|
|
||||||
array(
|
|
||||||
'form' => $form->createView(),
|
|
||||||
'entries' => $entries,
|
|
||||||
'currentPage' => $page,
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -155,34 +128,7 @@ class EntryController extends Controller
|
||||||
*/
|
*/
|
||||||
public function showArchiveAction(Request $request, $page)
|
public function showArchiveAction(Request $request, $page)
|
||||||
{
|
{
|
||||||
$form = $this->get('form.factory')->create(new EntryFilterType());
|
return $this->showEntries('archive', $request, $page);
|
||||||
|
|
||||||
$filterBuilder = $this->getDoctrine()
|
|
||||||
->getRepository('WallabagCoreBundle:Entry')
|
|
||||||
->findArchiveByUser($this->getUser()->getId());
|
|
||||||
|
|
||||||
if ($request->query->has($form->getName())) {
|
|
||||||
// manually bind values from the request
|
|
||||||
$form->submit($request->query->get($form->getName()));
|
|
||||||
|
|
||||||
// build the query from the given form object
|
|
||||||
$this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $filterBuilder);
|
|
||||||
}
|
|
||||||
|
|
||||||
$pagerAdapter = new DoctrineORMAdapter($filterBuilder->getQuery());
|
|
||||||
$entries = new Pagerfanta($pagerAdapter);
|
|
||||||
|
|
||||||
$entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
|
|
||||||
$entries->setCurrentPage($page);
|
|
||||||
|
|
||||||
return $this->render(
|
|
||||||
'WallabagCoreBundle:Entry:entries.html.twig',
|
|
||||||
array(
|
|
||||||
'form' => $form->createView(),
|
|
||||||
'entries' => $entries,
|
|
||||||
'currentPage' => $page,
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -197,11 +143,64 @@ class EntryController extends Controller
|
||||||
*/
|
*/
|
||||||
public function showStarredAction(Request $request, $page)
|
public function showStarredAction(Request $request, $page)
|
||||||
{
|
{
|
||||||
|
return $this->showEntries('starred', $request, $page);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Global method to retrieve entries depending on the given type
|
||||||
|
* It returns the response to be send.
|
||||||
|
*
|
||||||
|
* @param string $type Entries type: unread, starred or archive
|
||||||
|
* @param Request $request
|
||||||
|
* @param int $page
|
||||||
|
*
|
||||||
|
* @return \Symfony\Component\HttpFoundation\Response
|
||||||
|
*/
|
||||||
|
private function showEntries($type, Request $request, $page)
|
||||||
|
{
|
||||||
|
$repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
|
||||||
|
|
||||||
|
switch ($type) {
|
||||||
|
case 'starred':
|
||||||
|
$qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'archive':
|
||||||
|
$qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'unread':
|
||||||
|
$qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
|
||||||
|
}
|
||||||
|
|
||||||
$form = $this->get('form.factory')->create(new EntryFilterType());
|
$form = $this->get('form.factory')->create(new EntryFilterType());
|
||||||
|
|
||||||
$filterBuilder = $this->getDoctrine()
|
if ($request->query->has($form->getName())) {
|
||||||
->getRepository('WallabagCoreBundle:Entry')
|
// manually bind values from the request
|
||||||
->findStarredByUser($this->getUser()->getId());
|
$form->submit($request->query->get($form->getName()));
|
||||||
|
|
||||||
|
// build the query from the given form object
|
||||||
|
$this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
|
||||||
|
}
|
||||||
|
|
||||||
|
$pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
|
||||||
|
$entries = new Pagerfanta($pagerAdapter);
|
||||||
|
|
||||||
|
$entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage());
|
||||||
|
$entries->setCurrentPage($page);
|
||||||
|
|
||||||
|
return $this->render(
|
||||||
|
'WallabagCoreBundle:Entry:entries.html.twig',
|
||||||
|
array(
|
||||||
|
'form' => $form->createView(),
|
||||||
|
'entries' => $entries,
|
||||||
|
'currentPage' => $page,
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
if ($request->query->has($form->getName())) {
|
if ($request->query->has($form->getName())) {
|
||||||
// manually bind values from the request
|
// manually bind values from the request
|
||||||
|
|
|
@ -22,22 +22,7 @@ class RssController extends Controller
|
||||||
*/
|
*/
|
||||||
public function showUnreadAction(User $user)
|
public function showUnreadAction(User $user)
|
||||||
{
|
{
|
||||||
$qb = $this->getDoctrine()
|
return $this->showEntries('unread', $user);
|
||||||
->getRepository('WallabagCoreBundle:Entry')
|
|
||||||
->findUnreadByUser(
|
|
||||||
$user->getId()
|
|
||||||
);
|
|
||||||
|
|
||||||
$pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
|
|
||||||
$entries = new Pagerfanta($pagerAdapter);
|
|
||||||
|
|
||||||
$perPage = $user->getConfig()->getRssLimit() ?: $this->container->getParameter('rss_limit');
|
|
||||||
$entries->setMaxPerPage($perPage);
|
|
||||||
|
|
||||||
return $this->render('WallabagCoreBundle:Entry:entries.xml.twig', array(
|
|
||||||
'type' => 'unread',
|
|
||||||
'entries' => $entries,
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -50,22 +35,7 @@ class RssController extends Controller
|
||||||
*/
|
*/
|
||||||
public function showArchiveAction(User $user)
|
public function showArchiveAction(User $user)
|
||||||
{
|
{
|
||||||
$qb = $this->getDoctrine()
|
return $this->showEntries('archive', $user);
|
||||||
->getRepository('WallabagCoreBundle:Entry')
|
|
||||||
->findArchiveByUser(
|
|
||||||
$user->getId()
|
|
||||||
);
|
|
||||||
|
|
||||||
$pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
|
|
||||||
$entries = new Pagerfanta($pagerAdapter);
|
|
||||||
|
|
||||||
$perPage = $user->getConfig()->getRssLimit() ?: $this->container->getParameter('rss_limit');
|
|
||||||
$entries->setMaxPerPage($perPage);
|
|
||||||
|
|
||||||
return $this->render('WallabagCoreBundle:Entry:entries.xml.twig', array(
|
|
||||||
'type' => 'archive',
|
|
||||||
'entries' => $entries,
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -78,11 +48,38 @@ class RssController extends Controller
|
||||||
*/
|
*/
|
||||||
public function showStarredAction(User $user)
|
public function showStarredAction(User $user)
|
||||||
{
|
{
|
||||||
$qb = $this->getDoctrine()
|
return $this->showEntries('starred', $user);
|
||||||
->getRepository('WallabagCoreBundle:Entry')
|
}
|
||||||
->findStarredByUser(
|
|
||||||
$user->getId()
|
/**
|
||||||
);
|
* Global method to retrieve entries depending on the given type
|
||||||
|
* It returns the response to be send.
|
||||||
|
*
|
||||||
|
* @param string $type Entries type: unread, starred or archive
|
||||||
|
* @param User $user
|
||||||
|
*
|
||||||
|
* @return \Symfony\Component\HttpFoundation\Response
|
||||||
|
*/
|
||||||
|
private function showEntries($type, User $user)
|
||||||
|
{
|
||||||
|
$repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
|
||||||
|
|
||||||
|
switch ($type) {
|
||||||
|
case 'starred':
|
||||||
|
$qb = $repository->getBuilderForStarredByUser($user->getId());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'archive':
|
||||||
|
$qb = $repository->getBuilderForArchiveByUser($user->getId());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'unread':
|
||||||
|
$qb = $repository->getBuilderForUnreadByUser($user->getId());
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
|
||||||
|
}
|
||||||
|
|
||||||
$pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
|
$pagerAdapter = new DoctrineORMAdapter($qb->getQuery());
|
||||||
$entries = new Pagerfanta($pagerAdapter);
|
$entries = new Pagerfanta($pagerAdapter);
|
||||||
|
@ -91,7 +88,7 @@ class RssController extends Controller
|
||||||
$entries->setMaxPerPage($perPage);
|
$entries->setMaxPerPage($perPage);
|
||||||
|
|
||||||
return $this->render('WallabagCoreBundle:Entry:entries.xml.twig', array(
|
return $this->render('WallabagCoreBundle:Entry:entries.xml.twig', array(
|
||||||
'type' => 'starred',
|
'type' => $type,
|
||||||
'entries' => $entries,
|
'entries' => $entries,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,22 @@ use Pagerfanta\Pagerfanta;
|
||||||
|
|
||||||
class EntryRepository extends EntityRepository
|
class EntryRepository extends EntityRepository
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Return a query builder to used by other getBuilderFor* method.
|
||||||
|
*
|
||||||
|
* @param int $userId
|
||||||
|
*
|
||||||
|
* @return QueryBuilder
|
||||||
|
*/
|
||||||
|
private function getBuilderByUser($userId)
|
||||||
|
{
|
||||||
|
return $this->createQueryBuilder('e')
|
||||||
|
->leftJoin('e.user', 'u')
|
||||||
|
->andWhere('u.id = :userId')->setParameter('userId', $userId)
|
||||||
|
->orderBy('e.id', 'desc')
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves unread entries for a user.
|
* Retrieves unread entries for a user.
|
||||||
*
|
*
|
||||||
|
@ -15,13 +31,12 @@ class EntryRepository extends EntityRepository
|
||||||
*
|
*
|
||||||
* @return QueryBuilder
|
* @return QueryBuilder
|
||||||
*/
|
*/
|
||||||
public function findUnreadByUser($userId)
|
public function getBuilderForUnreadByUser($userId)
|
||||||
{
|
{
|
||||||
return $this->createQueryBuilder('e')
|
return $this
|
||||||
->leftJoin('e.user', 'u')
|
->getBuilderByUser($userId)
|
||||||
->where('e.isArchived = false')
|
->andWhere('e.isArchived = false')
|
||||||
->andWhere('u.id =:userId')->setParameter('userId', $userId)
|
;
|
||||||
->orderBy('e.id', 'desc');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -31,13 +46,12 @@ class EntryRepository extends EntityRepository
|
||||||
*
|
*
|
||||||
* @return QueryBuilder
|
* @return QueryBuilder
|
||||||
*/
|
*/
|
||||||
public function findArchiveByUser($userId)
|
public function getBuilderForArchiveByUser($userId)
|
||||||
{
|
{
|
||||||
return $this->createQueryBuilder('e')
|
return $this
|
||||||
->leftJoin('e.user', 'u')
|
->getBuilderByUser($userId)
|
||||||
->where('e.isArchived = true')
|
->andWhere('e.isArchived = true')
|
||||||
->andWhere('u.id =:userId')->setParameter('userId', $userId)
|
;
|
||||||
->orderBy('e.id', 'desc');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -47,13 +61,12 @@ class EntryRepository extends EntityRepository
|
||||||
*
|
*
|
||||||
* @return QueryBuilder
|
* @return QueryBuilder
|
||||||
*/
|
*/
|
||||||
public function findStarredByUser($userId)
|
public function getBuilderForStarredByUser($userId)
|
||||||
{
|
{
|
||||||
return $this->createQueryBuilder('e')
|
return $this
|
||||||
->leftJoin('e.user', 'u')
|
->getBuilderByUser($userId)
|
||||||
->where('e.isStarred = true')
|
->andWhere('e.isStarred = true')
|
||||||
->andWhere('u.id =:userId')->setParameter('userId', $userId)
|
;
|
||||||
->orderBy('e.id', 'desc');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue