Add filter to Timesheet admin #3

Add stop action to Timesheet admin
This commit is contained in:
Kevin Papst
2018-01-05 11:28:21 +01:00
parent 43f6a9cced
commit ea030ca199
4 changed files with 57 additions and 13 deletions

View File

@@ -11,9 +11,11 @@
namespace TimesheetBundle\Controller\Admin; namespace TimesheetBundle\Controller\Admin;
use AppBundle\Controller\AbstractController;
use Pagerfanta\Pagerfanta; use Pagerfanta\Pagerfanta;
use Symfony\Component\HttpFoundation\Request;
use TimesheetBundle\Controller\TimesheetControllerTrait;
use TimesheetBundle\Entity\Timesheet; use TimesheetBundle\Entity\Timesheet;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
@@ -27,19 +29,51 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
* *
* @author Kevin Papst <kevin@kevinpapst.de> * @author Kevin Papst <kevin@kevinpapst.de>
*/ */
class TimesheetController extends Controller class TimesheetController extends AbstractController
{ {
use TimesheetControllerTrait;
/** /**
* @Route("/", defaults={"page": 1}, name="admin_timesheet") * @Route("/", defaults={"page": 1}, name="admin_timesheet")
* @Route("/page/{page}", requirements={"page": "[1-9]\d*"}, name="admin_timesheet_paginated") * @Route("/page/{page}", requirements={"page": "[1-9]\d*"}, name="admin_timesheet_paginated")
* @Method("GET") * @Method("GET")
* @Cache(smaxage="10") * @Cache(smaxage="10")
*/ */
public function indexAction($page) public function indexAction($page, Request $request)
{ {
/* @var $entries Pagerfanta */ $query = $this->getQueryForRequest($request);
$entries = $this->getDoctrine()->getRepository(Timesheet::class)->findAll($page); $query->setPage($page);
return $this->render('TimesheetBundle:admin:timesheet.html.twig', ['entries' => $entries]); /* @var $entries Pagerfanta */
$entries = $this->getRepository()->findByQuery($query);
return $this->render('TimesheetBundle:admin:timesheet.html.twig', [
'entries' => $entries,
'page' => $page,
'query' => $query,
'toolbarForm' => $this->getToolbarForm($query, 'admin_timesheet')->createView(),
]);
}
/**
* The route to stop a running entry.
*
* @Route("/{id}/stop", name="admin_timesheet_stop")
* @Method({"GET"})
*
* @param Timesheet $entry
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function stopAction(Timesheet $entry, Request $request)
{
try {
$this->getRepository()->stopRecording($entry);
$this->flashSuccess('timesheet.stop.success');
} catch (\Exception $ex) {
$this->flashError('timesheet.stop.error', ['%reason%' => $ex->getMessage()]);
}
return $this->redirectToRoute('admin_timesheet');
} }
} }

View File

@@ -296,9 +296,11 @@ class TimesheetRepository extends EntityRepository
{ {
$qb = $this->getEntityManager()->createQueryBuilder(); $qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('t', 'a') $qb->select('t', 'a', 'p', 'c')
->from('TimesheetBundle:Timesheet', 't') ->from('TimesheetBundle:Timesheet', 't')
->join('t.activity', 'a') ->join('t.activity', 'a')
->join('a.project', 'p')
->join('p.customer', 'c')
->orderBy('t.begin', 'DESC'); ->orderBy('t.begin', 'DESC');
if ($query->getUser() !== null) { if ($query->getUser() !== null) {
@@ -319,9 +321,7 @@ class TimesheetRepository extends EntityRepository
$qb->andWhere('a.project = :project') $qb->andWhere('a.project = :project')
->setParameter('project', $query->getProject()); ->setParameter('project', $query->getProject());
} elseif ($query->getCustomer() !== null) { } elseif ($query->getCustomer() !== null) {
$qb->join('a.project', 'p') $qb->andWhere('p.customer = :customer')
->join('p.customer', 'c')
->andWhere('p.customer = :customer')
->setParameter('customer', $query->getCustomer()); ->setParameter('customer', $query->getCustomer());
} }

View File

@@ -4,8 +4,13 @@
{% block page_title %}{{ 'admin_timesheet.title'|trans }}{% endblock %} {% block page_title %}{{ 'admin_timesheet.title'|trans }}{% endblock %}
{% block page_subtitle %}{{ 'admin_timesheet.subtitle'|trans }}{% endblock %} {% block page_subtitle %}{{ 'admin_timesheet.subtitle'|trans }}{% endblock %}
{% block javascript_imports %}<script src="{{ asset('js/timesheet.js') }}"></script>{% endblock %}
{% block main %} {% block main %}
{% if toolbarForm %}
{{ include('default/_toolbar_form.html.twig', {'form': toolbarForm}) }}
{% endif %}
{% if entries.count > 0 %} {% if entries.count > 0 %}
{{ datatables.data_table_header({ {{ datatables.data_table_header({
'label.date': '', 'label.date': '',
@@ -25,7 +30,7 @@
{% if entry.end %} {% if entry.end %}
<td class="hidden-xs">{{ entry.end|date("H:i") }}</td> <td class="hidden-xs">{{ entry.end|date("H:i") }}</td>
<td>{{ entry.duration|duration }}</td> <td>{{ entry.duration|duration }}</td>
<td>{{ entry.rate|money }}</td> <td>{{ entry.rate|money(entry.activity.project.customer.currency) }}</td>
{% else %} {% else %}
<td class="hidden-xs">&dash;</td> <td class="hidden-xs">&dash;</td>
<td>&dash;</td> <td>&dash;</td>
@@ -34,7 +39,12 @@
<td class="hidden-xs"><a href="{{ path('profile'|route_alias, {'username' : entry.user.username}) }}">{{ entry.user.username }}</a></td> <td class="hidden-xs"><a href="{{ path('profile'|route_alias, {'username' : entry.user.username}) }}">{{ entry.user.username }}</a></td>
<td class="hidden-xs hidden-sm">{{ entry.description }}</td> <td class="hidden-xs hidden-sm">{{ entry.description }}</td>
<td> <td>
{{ widgets.button_group({'edit': '#', 'trash': '#'}) }} {% if entry.end %}
{{ widgets.button_group({'edit': '#', 'trash': '#'}) }}
{% else %}
{{ widgets.button_group({'stop': path('admin_timesheet_stop', {'id' : entry.id}), 'edit': '#', 'trash': '#'}) }}
{% endif %}
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}

View File

@@ -30,7 +30,7 @@
{% if entry.end %} {% if entry.end %}
<td>{{ entry.end|date("H:i") }}</td> <td>{{ entry.end|date("H:i") }}</td>
<td class="hidden-xs">{{ entry.duration|duration }}</td> <td class="hidden-xs">{{ entry.duration|duration }}</td>
<td class="hidden-xs">{{ entry.rate|money }}</td> <td class="hidden-xs">{{ entry.rate|money(entry.activity.project.customer.currency) }}</td>
{% else %} {% else %}
<td>&dash;</td> <td>&dash;</td>
<td class="hidden-xs"><i>{{ entry.duration|duration }}</i></td> <td class="hidden-xs"><i>{{ entry.duration|duration }}</i></td>