<?php
namespace DM\KlienciBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use DM\KlienciBundle\Entity\ClientSource;
use DM\KlienciBundle\Form\ClientSource\ClientSourceType;
use DM\KlienciBundle\Form\ClientSource\ClientSourceFilterType;
class ClientSourceController extends Controller {
const TEMPLATE_PREFIX = 'DMKlienciBundle:ClientSource:';
const ACTIVE_BUNDLE = 'klienci';
const PAGINATION_PER_PAGE = 100;
/**
* Client Source list
*
* @param Request $request
*
* @return Response
*/
public function indexAction(Request $request) {
$em = $this->getDoctrine()->getManager();
$paginationLimitPerPage = self::PAGINATION_PER_PAGE;
// form data
$data = [];
$form = $this->createForm(ClientSourceFilterType::class, $data);
$resetPaginationPage = false;
if ($request->isMethod('POST')) {
$form->bind($request);
$data = $form->getData();
if ($form->get('submit')->isClicked()) {
$resetPaginationPage = true;
}
}
$paginationPage = (!$resetPaginationPage && isset($data['paginationPage']) ? $data['paginationPage'] : 1);
$itemsCount = $em->getRepository(ClientSource::class)->findByMultiParameters(true, $data, []);
$items = $em->getRepository(ClientSource::class)->findByMultiParameters(false, $data, ['t.id' => 'DESC'], (($paginationPage - 1) * $paginationLimitPerPage), $paginationLimitPerPage);
// return view
return $this->render(self::TEMPLATE_PREFIX . 'list.html.twig', array(
'active_bundle' => self::ACTIVE_BUNDLE,
'form' => $form->createView(),
'items' => $items,
'itemsCount' => $itemsCount,
'paginationPage' => $paginationPage,
'paginationCount' => ceil($itemsCount / $paginationLimitPerPage),
));
}
/**
* add new client source
*
* @param Request $request
*
* @return Response
*/
public function addAction(Request $request) {
return $this->addeditAction($request, 'add');
}
/**
* edit client source
*
* @param Request $request
* @param IframeLink|null $item
*
* @return Response
*/
public function editAction(Request $request, ClientSource $item) {
return $this->addeditAction($request, 'edit', $item);
}
/**
* crud add/edit
*
* @param Request $request
* @param string $mode
* @param IframeLink $clientSource
*
* @return Resposne|Redirect
*/
public function addeditAction(Request $request, $mode, ClientSource $clientSource = null) {
$em = $this->getDoctrine()->getManager();
//edit or add
if ($mode == 'add') {
$clientSource = new ClientSource();
} elseif ($mode == 'edit' && !$clientSource) {
$this->addFlash('error', 'Wystąpił błąd, nie można znaleźć wybranej pozycji w systemie. Spróbuj ponownie.');
return $this->redirectToRoute('dm_client_source_homepage');
}
$form = $this->createForm(ClientSourceType::class, $clientSource);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$em->persist($clientSource);
$em->flush();
if ($mode == 'add') {
$this->addFlash('success', 'Klient został pomyślnie dodany.');
} elseif ($mode == 'edit') {
$this->addFlash('success', 'Klient został pomyślnie zapisany.');
}
return $this->redirect($this->generateUrl('dm_client_source_homepage'));
} catch (\Exception $ex) {
$this->addFlash('error', 'Wystąpił błąd (ex: ' . $ex->getMessage() . ')');
}
}
return $this->render(self::TEMPLATE_PREFIX . 'addEdit.html.twig', [
'active_bundle' => self::ACTIVE_BUNDLE,
'form' => $form->createView(),
'mode' => $mode,
]);
}
/**
* remove client source
*
* @param Request $request
* @param ClientSource $item
*
* @return redirect
*/
public function removeAction(Request $request, ClientSource $item) {
$em = $this->getDoctrine()->getManager();
$session = $this->getRequest()->getSession();
if (!$item) {
$session->getFlashBag()->add('error', 'Nie znaleziono pozycji w systemie.');
return $this->redirect($this->generateUrl('dm_client_source_homepage'));
}
$em->remove($item);
$em->flush();
$session->getFlashBag()->add('success', 'Klient został pomyślnie usunięty.');
return $this->redirect($this->generateUrl('dm_client_source_homepage'));
}
}
//repository
<?php
namespace DM\KlienciBundle\Repository;
use DM\KlienciBundle\Entity\ClientSource;
class ClientSourceRepository extends \Doctrine\ORM\EntityRepository {
/**
* find items by mylti parameters with pagination
*
* @param boolean $onlyCount - true if return only count
* @param array $filter - data array with filter parameters
* @param array $orderBy - order param (key is field, value is direction, defulat id DESC)
* @param integer $offset - pagination offset
* @param integer $limit - pagination limit
* @param string $groupBy
*
* @return array
*/
public function findByMultiParameters($onlyCount, array $filter = [], array $orderBy = ['t.id' => 'DESC'], $offset = null, $limit = null, $groupBy = null) {
$select = ($onlyCount ? 'COUNT(t.id)' . ($groupBy ? ', ' . $groupBy : '') : ($groupBy ? $groupBy : 't'));
$query = $this->getEntityManager()->createQueryBuilder()
->from(ClientSource::class, 't')
->select($select)
;
// name
if (isset($filter['name']) && $filter['name'] != null) {
$query->andWhere('t.name LIKE :name')->setParameter('name', '%'.$filter['name'].'%');
}
if ($groupBy) {
$query = $query->groupBy($groupBy);
}
if (!empty($orderBy)) {
foreach ($orderBy as $orderField => $orderDirection) {
$query->addOrderBy($orderField, $orderDirection);
}
}
if (!$onlyCount) {
$query = $query->setFirstResult($offset)
->setMaxResults($limit);
}
try {
if ($onlyCount) {
if ($groupBy) {
return $query->getQuery()->getResult();
} else {
return $query->getQuery()->getSingleScalarResult();
}
} else {
return $query->getQuery()->getResult();
}
} catch (\Doctrine\ORM\NoResultException $e) {
if ($onlyCount) {
return 0;
} else {
return array();
}
}
}
}