Add customer functionality (#26)
* improved toolbar * added "create customer" functionality #22
This commit is contained in:
@@ -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'
|
||||
]
|
||||
);
|
||||
|
||||
95
src/TimesheetBundle/Model/Query/BaseQuery.php
Normal file
95
src/TimesheetBundle/Model/Query/BaseQuery.php
Normal 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;
|
||||
}
|
||||
}
|
||||
44
src/TimesheetBundle/Model/Query/CustomerQuery.php
Normal file
44
src/TimesheetBundle/Model/Query/CustomerQuery.php
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
'label.currency': 'hidden-xs',
|
||||
'label.visible': '',
|
||||
'label.actions': '',
|
||||
}) }}
|
||||
}, null, {'user-plus': path('admin_customer_create')}) }}
|
||||
|
||||
{% for entry in entries %}
|
||||
<tr>
|
||||
|
||||
@@ -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')
|
||||
}) }}
|
||||
|
||||
Reference in New Issue
Block a user