Last active
June 6, 2017 17:54
-
-
Save miedzwin/fae1c934552bf0379c1d2a1dab83a18d to your computer and use it in GitHub Desktop.
Filters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use Gedmo\Mapping\Annotation as Gedmo; | |
/** | |
* Carrier | |
* | |
* @ORM\Table(name="carrier") | |
* @ORM\Entity(repositoryClass="AppBundle\Repository\CarrierRepository") | |
*/ | |
class Carrier { | |
/** | |
* | |
* @ORM\ManyToMany(targetEntity="TransportType", mappedBy="carriers") | |
* @ORM\JoinTable(name="carrier_has_transport_type") | |
*/ | |
private $transportTypes; | |
/** | |
* | |
* @ORM\ManyToMany(targetEntity="Region", inversedBy="carriers") | |
*/ | |
private $regions; | |
/** | |
* Constructor | |
*/ | |
public function __construct() { | |
$this->transportTypes = new \Doctrine\Common\Collections\ArrayCollection(); | |
$this->regions = new \Doctrine\Common\Collections\ArrayCollection(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Controller; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
use Symfony\Component\HttpFoundation\Request; | |
use AppBundle\Entity\Carrier; | |
use AppBundle\Form\CarrierType; | |
use AppBundle\Form\Filter\CarrierFilterType; | |
/** | |
* @Route("/carriers") | |
*/ | |
class CarrierController extends Controller { | |
/** | |
* @Route("/list", name="list_carrier") | |
* @param Request $request | |
* @return type | |
*/ | |
public function listAction(Request $request) { | |
/* @var $paginator \Knp\Component\Pager\Paginator */ | |
$em = $this->getDoctrine()->getManager(); | |
$session = $request->getSession(); | |
$paginator = $this->get('knp_paginator'); | |
$orderBy = $request->query->get('order') ? $request->query->get('order') : 'id'; | |
$arrange = $request->query->get('arrange') ? $request->query->get('arrange') : 'ASC'; | |
if ($request->query->get('reset_filters') === '1') { | |
$session->remove('carrier_list_filters'); | |
return $this->redirect($this->generateUrl('list_carrier')); | |
} | |
$filters = $session->has('carrier_list_filters') ? unserialize($session->get('carrier_list_filters')) : []; | |
foreach ($filters as $key => $filter) { | |
if (is_object($filter)) { | |
try { | |
$em->merge($filter); | |
} catch (\Exception $ex) { | |
dump($key); | |
dump($filter); | |
dump($ex); | |
die(); | |
} | |
} | |
} | |
$form = $this->createForm(CarrierFilterType::class, $filters); | |
$form->handleRequest($request); | |
if ($form->isSubmitted() && $form->isValid()) { | |
$filters = $form->getData(); | |
$session->set('carrier_list_filters', serialize($filters)); | |
} | |
$order = [ | |
$orderBy => $arrange, | |
]; | |
// dump($filters); | |
// die(); | |
$carrier = $em->getRepository(Carrier::class)->getCarriersList($order, $filters); | |
$pagination = $paginator->paginate($carrier, $request->query->getInt('page', 1), 10); | |
return $this->render('carrier/list.html.twig', [ | |
'order' => $order, | |
'arrange' => $arrange === 'ASC' ? 'DESC' : 'ASC', | |
'carriers' => $pagination, | |
'filters' => $filters, | |
'filter_form' => $form->createView(), | |
]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Form\Filter; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
use Symfony\Component\Form\Extension\Core\Type\TextType; | |
use Symfony\Bridge\Doctrine\Form\Type\EntityType; | |
use Doctrine\ORM\EntityRepository; | |
use AppBundle\Entity\Region; | |
use AppBundle\Entity\Country; | |
use AppBundle\Entity\TransportType; | |
use AppBundle\Entity\User; | |
class CarrierFilterType extends AbstractType { | |
public function buildForm(FormBuilderInterface $builder, array $options) { | |
$builder->add('transportTypes', EntityType::class, [ | |
'class' => TransportType::class, | |
'label' => 'carrier_form.transport_type', | |
'choice_label' => 'id', | |
'expanded' => true, | |
'multiple' => true, | |
'attr' => [ | |
'class' => 'form-control', | |
], | |
]); | |
} | |
public function configureOptions(OptionsResolver $resolver) { | |
$resolver->setDefaults([ | |
'validation_groups' => [ | |
'filtering', | |
] // avoid NotBlank() constraint-related message | |
]); | |
} | |
public function getBlockPrefix() { | |
return 'carrier_filter'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use Gedmo\Mapping\Annotation as Gedmo; | |
/** | |
* Region | |
* | |
* @ORM\Table(name="region") | |
* @ORM\Entity(repositoryClass="AppBundle\Repository\RegionRepository") | |
*/ | |
class Region { | |
/** | |
* @var int | |
* | |
* @ORM\Column(name="id", type="integer") | |
* @ORM\Id | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
private $id; | |
/** | |
* @var \DateTime | |
* @Gedmo\Timestampable(on="create") | |
* @ORM\Column(name="created", type="datetime") | |
*/ | |
private $created; | |
/** | |
* @var \DateTime | |
* @Gedmo\Timestampable(on="update") | |
* @ORM\Column(name="updated", type="datetime") | |
*/ | |
private $updated; | |
/** | |
* | |
* @ORM\ManyToMany(targetEntity="Country", mappedBy="regions") | |
*/ | |
private $countries; | |
/** | |
* | |
* @ORM\ManyToMany(targetEntity="Carrier", mappedBy="regions") | |
*/ | |
private $carriers; | |
/** | |
* Create name for select using countries codes | |
* @return type | |
*/ | |
public function getName() { | |
$countriesArr = []; | |
foreach ($this->countries as $country) { | |
$countriesArr[] = $country->getCode(); | |
} | |
return implode(",", $countriesArr); | |
} | |
/** | |
* Constructor | |
*/ | |
public function __construct() { | |
$this->countries = new \Doctrine\Common\Collections\ArrayCollection(); | |
$this->carriers = new \Doctrine\Common\Collections\ArrayCollection(); | |
} | |
/** | |
* Get id | |
* | |
* @return integer | |
*/ | |
public function getId() { | |
return $this->id; | |
} | |
/** | |
* Set created | |
* | |
* @param \DateTime $created | |
* | |
* @return Region | |
*/ | |
public function setCreated($created) { | |
$this->created = $created; | |
return $this; | |
} | |
/** | |
* Get created | |
* | |
* @return \DateTime | |
*/ | |
public function getCreated() { | |
return $this->created; | |
} | |
/** | |
* Set updated | |
* | |
* @param \DateTime $updated | |
* | |
* @return Region | |
*/ | |
public function setUpdated($updated) { | |
$this->updated = $updated; | |
return $this; | |
} | |
/** | |
* Get updated | |
* | |
* @return \DateTime | |
*/ | |
public function getUpdated() { | |
return $this->updated; | |
} | |
/** | |
* Add country | |
* | |
* @param \AppBundle\Entity\Country $country | |
* | |
* @return Region | |
*/ | |
public function addCountry(\AppBundle\Entity\Country $country) { | |
$this->countries[] = $country; | |
return $this; | |
} | |
/** | |
* Remove country | |
* | |
* @param \AppBundle\Entity\Country $country | |
*/ | |
public function removeCountry(\AppBundle\Entity\Country $country) { | |
$this->countries->removeElement($country); | |
} | |
/** | |
* Get countries | |
* | |
* @return \Doctrine\Common\Collections\Collection | |
*/ | |
public function getCountries() { | |
return $this->countries; | |
} | |
/** | |
* Add carrier | |
* | |
* @param \AppBundle\Entity\Carrier $carrier | |
* | |
* @return Region | |
*/ | |
public function addCarrier(\AppBundle\Entity\Carrier $carrier) { | |
$this->carriers[] = $carrier; | |
return $this; | |
} | |
/** | |
* Remove carrier | |
* | |
* @param \AppBundle\Entity\Carrier $carrier | |
*/ | |
public function removeCarrier(\AppBundle\Entity\Carrier $carrier) { | |
$this->carriers->removeElement($carrier); | |
} | |
/** | |
* Get carriers | |
* | |
* @return \Doctrine\Common\Collections\Collection | |
*/ | |
public function getCarriers() { | |
return $this->carriers; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use Gedmo\Mapping\Annotation as Gedmo; | |
/** | |
* TransportType | |
* | |
* @ORM\Table(name="transport_type") | |
* @ORM\Entity(repositoryClass="AppBundle\Repository\TransportTypeRepository") | |
*/ | |
class TransportType { | |
/** | |
* | |
* @ORM\ManyToMany(targetEntity="Carrier", inversedBy="transportTypes") | |
*/ | |
private $carriers; | |
** | |
* Constructor | |
*/ | |
public function __construct() | |
{ | |
$this->carriers = new \Doctrine\Common\Collections\ArrayCollection(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment