Add customer functionality (#26)

* improved toolbar

* added "create customer" functionality #22
This commit is contained in:
Kevin Papst
2018-01-05 21:21:22 +01:00
committed by GitHub
parent 6b9fed69f1
commit 591a3cb11a
10 changed files with 215 additions and 21 deletions

View File

@@ -26,6 +26,10 @@
<source>This is a mandatory field</source>
<target>Pflichtfeld</target>
</trans-unit>
<trans-unit id="create">
<source>create</source>
<target>Erstellen</target>
</trans-unit>
<!--
Login / Security

View File

@@ -1,24 +1,24 @@
{% macro data_table_header(entries, form, tools) %}
<div class="box">
<div class="box data_table">
{% if form or tools %}
<div class="box-header with-border">
{% if form %}
<div class="box-title toolbar">
<div class="box-title toolbar">
{% if form %}
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
</div>
{% endif %}
{% if tools %}
<div class="box-tools">
{% endif %}
</div>
<div class="box-tools">
{% if tools %}
<div class="btn-group">
{% for url,icon in tools %}
{% for icon,url in tools %}
<a class="btn btn-default" href="{{ url }}"><i class="fa fa-{{ icon }}"></i></a>
{% endfor %}
</div>
</div>
{% endif %}
{% endif %}
</div>
</div>
{% endif %}
<div class="box-body no-padding">

View File

@@ -14,14 +14,13 @@ namespace TimesheetBundle\Controller\Admin;
use AppBundle\Controller\AbstractController;
use Pagerfanta\Pagerfanta;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use TimesheetBundle\Entity\Customer;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use TimesheetBundle\Form\CustomerEditForm;
use TimesheetBundle\Repository\CustomerRepository;
use TimesheetBundle\Model\Query\CustomerQuery;
/**
* Controller used to manage activities in the admin part of the site.
@@ -41,21 +40,40 @@ class CustomerController extends AbstractController
*/
public function indexAction($page)
{
$query = new CustomerQuery();
$query->setVisibility(CustomerQuery::SHOW_BOTH);
$query->setPage($page);
/* @var $entries Pagerfanta */
$entries = $this->getDoctrine()->getRepository(Customer::class)->findAll($page);
$entries = $this->getDoctrine()->getRepository(Customer::class)->findByQuery($query);
return $this->render('TimesheetBundle:admin:customer.html.twig', ['entries' => $entries]);
}
/**
* @Route("/create", name="admin_customer_create")
* @Method({"GET", "POST"})
*/
public function createAction(Request $request)
{
return $this->renderCustomerForm(new Customer(), $request);
}
/**
* @Route("/{id}/edit", name="admin_customer_edit")
* @Method({"GET", "POST"})
*
*/
public function editAction(Customer $customer, Request $request)
{
return $this->renderCustomerForm($customer, $request);
}
/**
* @param Customer $customer
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function editAction(Customer $customer, Request $request)
protected function renderCustomerForm(Customer $customer, Request $request)
{
$editForm = $this->createEditForm($customer);
@@ -84,15 +102,21 @@ class CustomerController extends AbstractController
/**
* @param Customer $customer
* @return \Symfony\Component\Form\Form
* @return \Symfony\Component\Form\FormInterface
*/
private function createEditForm(Customer $customer)
{
if ($customer->getId() === null) {
$url = $this->generateUrl('admin_customer_create');
} else {
$url = $this->generateUrl('admin_customer_edit', ['id' => $customer->getId()]);
}
return $this->createForm(
CustomerEditForm::class,
$customer,
[
'action' => $this->generateUrl('admin_customer_edit', ['id' => $customer->getId()]),
'action' => $url,
'method' => 'POST'
]
);

View File

@@ -0,0 +1,95 @@
<?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\Model\Query;
/**
* Base class for advanced Repository queries.
*
* @author Kevin Papst <kevin@kevinpapst.de>
*/
class BaseQuery
{
const DEFAULT_PAGESIZE = 25;
const DEFAULT_PAGE = 1;
/**
* @var int
*/
protected $page = self::DEFAULT_PAGE;
/**
* @var int
*/
protected $pageSize = self::DEFAULT_PAGESIZE;
/**
* @var string
*/
protected $orderBy = 'id';
/**
* @return int
*/
public function getPage()
{
return $this->page;
}
/**
* @param int $page
* @return BaseQuery
*/
public function setPage($page)
{
$this->page = $page;
return $this;
}
/**
* @return int
*/
public function getPageSize()
{
return $this->pageSize;
}
/**
* @param int $pageSize
* @return BaseQuery
*/
public function setPageSize($pageSize)
{
if (!empty($pageSize) && (int)$pageSize > 0) {
$this->pageSize = (int)$pageSize;
}
return $this;
}
/**
* @return string
*/
public function getOrderBy()
{
return $this->orderBy;
}
/**
* You need to validate carefully if this value is used from a user-input.
*
* @param string $orderBy
* @return BaseQuery
*/
public function setOrderBy($orderBy)
{
$this->orderBy = $orderBy;
return $this;
}
}

View File

@@ -0,0 +1,44 @@
<?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\Model\Query;
/**
* Can be used for advanced queries with the: CustomerRepository
*
* @author Kevin Papst <kevin@kevinpapst.de>
*/
class CustomerQuery extends BaseQuery
{
const SHOW_VISIBLE = 1;
const SHOW_HIDDEN = 0;
const SHOW_BOTH = 2;
protected $visibility = self::SHOW_VISIBLE;
/**
* @return int
*/
public function getVisibility()
{
return $this->visibility;
}
/**
* @param int $visibility
* @return CustomerQuery
*/
public function setVisibility($visibility)
{
$this->visibility = $visibility;
return $this;
}
}

View File

@@ -56,12 +56,13 @@ abstract class AbstractRepository extends EntityRepository
/**
* @param Query $query
* @param int $page
* @param int $maxPerPage
* @return Pagerfanta
*/
protected function getPager(Query $query, $page = 1)
protected function getPager(Query $query, $page = 1, $maxPerPage = 25)
{
$paginator = new Pagerfanta(new DoctrineORMAdapter($query, false));
$paginator->setMaxPerPage(25);
$paginator->setMaxPerPage($maxPerPage);
$paginator->setCurrentPage($page);
return $paginator;

View File

@@ -13,6 +13,7 @@ namespace TimesheetBundle\Repository;
use TimesheetBundle\Entity\Customer;
use TimesheetBundle\Model\CustomerStatistic;
use TimesheetBundle\Model\Query\CustomerQuery;
/**
* Class CustomerRepository
@@ -47,4 +48,25 @@ class CustomerRepository extends AbstractRepository
$stats->setTotalAmount($countAll);
return $stats;
}
/**
* @param CustomerQuery $query
* @return \Pagerfanta\Pagerfanta
*/
public function findByQuery(CustomerQuery $query)
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('c')
->from('TimesheetBundle:Customer', 'c')
->orderBy('c.' . $query->getOrderBy(), 'ASC');
if ($query->getVisibility() === CustomerQuery::SHOW_VISIBLE) {
$qb->andWhere('c.visible = 1');
} elseif ($query->getVisibility() === CustomerQuery::SHOW_HIDDEN) {
$qb->andWhere('c.visible = 0');
}
return $this->getPager($qb->getQuery(), $query->getPage(), $query->getPageSize());
}
}

View File

@@ -21,7 +21,7 @@
'label.currency': 'hidden-xs',
'label.visible': '',
'label.actions': '',
}) }}
}, null, {'user-plus': path('admin_customer_create')}) }}
{% for entry in entries %}
<tr>

View File

@@ -7,7 +7,7 @@
{% block main %}
{{ include('default/_form.html.twig', {
'title': customer.name,
'title': customer.name|default('create'|trans),
'form': form,
'back': path('admin_customer')
}) }}

View File

@@ -33,6 +33,10 @@ li.open .ticktac i.running{
/* ================================ TOOLBAR ================================ */
.data_table .box-header>.box-tools {
top: 3px;
}
.box-header .box-title.toolbar form {
font-size: 13px;
}