src/AdminBundle/Admin/Vehicles/VehicleItemAdmin.php line 19

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Admin\Vehicles;
  3. use AdminBundle\Admin\BaseAdmin;
  4. use CoreBundle\Entity\Vehicles\Equipment;
  5. use CoreBundle\Entity\Vehicles\Specification;
  6. use CoreBundle\Entity\Vehicles\Variation;
  7. use CoreBundle\Entity\Vehicles\VehicleItem;
  8. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  9. use Sonata\AdminBundle\Datagrid\ListMapper;
  10. use Sonata\AdminBundle\Form\FormMapper;
  11. use Sonata\AdminBundle\Form\Type\ModelType;
  12. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  13. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  14. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  15. use Doctrine\ORM\Query\Expr\Join;
  16. class VehicleItemAdmin extends BaseAdmin
  17. {
  18.     protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
  19.     {
  20.         $datagridMapper
  21.             ->add('vehicle.id'null, ['label' => 'id основної машинки'])
  22.             ->add('vehicle.vin'null, ['label' => 'ВІН'])
  23.             ->add('vehicle.url'null, ['label' => 'SEO url']);
  24.     }
  25.     /**
  26.      * @param ListMapper $listMapper
  27.      */
  28.     protected function configureListFields(ListMapper $listMapper): void
  29.     {
  30.         $listMapper
  31.             ->addIdentifier('id')
  32.             ->add('vehicle.model'null, ['label' => 'Підв\'язано до моделі',])
  33.             ->add('vehicle.model.brand'null, ['label' => 'Підв\'язано до бренда',])
  34.             ->add('vehicle.vin'null, ['label' => 'Він машинки',])
  35.             ->add('vehicle.id'null, ['label' => 'ID машинки',])
  36.             ->add('creator'null, ['label' => 'Створено адміном'])
  37.             ->add('price'null, ['label' => 'Ціна'])
  38.             ->add('sold''boolean', ['label' => 'Продана?'])
  39.             ->add('_action''actions', [
  40.                 'actions' => [
  41.                     'edit' => [],
  42.                 ]
  43.             ]);
  44.     }
  45.     /**
  46.      * @param FormMapper $formMapper
  47.      * @return false|void
  48.      */
  49.     protected function configureFormFields(FormMapper $formMapper): void
  50.     {
  51.         $User $this->getUser();
  52.         if (!$Dealer $User->getDealer()) {
  53.             throw new AccessDeniedException('User without dealer');
  54.         }
  55.         /** @var BaseVehicleAdmin $rootAdmin */
  56.         $rootAdmin $this->getRoot();
  57.         /** @var VehicleItem|null $parent */
  58.         $parent $this->getRoot()->getSubject();
  59.         if (!$parent || !$parent->getId()) {
  60.             return;
  61.         }
  62.         /** @var VehicleItem $parent */
  63.         $hasRentPrice false;
  64.         $hasPreOrderPrice false;
  65.         $priceParams $this->parameterBag->get('price-config');
  66.         if (isset($priceParams[$Dealer->getUniqueId()])) {
  67.             $hasPreOrderPrice =
  68.                 isset($priceParams[$Dealer->getUniqueId()]['has-preorder-price']) &&
  69.                 $priceParams[$Dealer->getUniqueId()]['has-preorder-price'];
  70.             $hasRentPrice =
  71.                 isset($priceParams[$Dealer->getUniqueId()]['has-rent-price']) &&
  72.                 $priceParams[$Dealer->getUniqueId()]['has-rent-price'];
  73.         }
  74.         $queryBuilderEquipment $this->getModelManager()
  75.             ->getEntityManager(Equipment::class)
  76.             ->createQueryBuilder('e')
  77.             ->select('e')
  78.             ->from(Equipment::class, 'e')
  79.             ->join('e.vehicle''v'Join::WITH'v.id = :id')
  80.             ->setParameter('id'$parent->getId());
  81.         $queryBuilderVariation $this->getModelManager()
  82.             ->getEntityManager(Variation::class)
  83.             ->createQueryBuilder('v')
  84.             ->select('v')
  85.             ->from(Variation::class, 'v')
  86.             ->join('v.vehicle''veh'Join::WITH'veh.id = :id')
  87.             ->andWhere('v.state = 1')
  88.             ->setParameter('id'$parent->getId());
  89.         $queryBuilderSpecification $this->getModelManager()
  90.             ->getEntityManager(Specification::class)
  91.             ->createQueryBuilder('s')
  92.             ->select('s')
  93.             ->from(Specification::class, 's')
  94.             ->join('s.vehicle''veh'Join::WITH'veh.id = :id')
  95.             ->setParameter('id'$parent->getId());
  96.         $specCount count($queryBuilderSpecification->getQuery()->getResult());
  97.         $formMapper
  98.             ->add(
  99.                 'equipment',
  100.                 ModelType::class,
  101.                 [
  102.                     'label' => 'Комплектація',
  103.                     'required' => true,
  104.                     'query' => $queryBuilderEquipment,
  105.                     'btn_add' => false,
  106.                     'empty_data' => null,
  107.                     'property' => 'adminName',
  108.                 ]
  109.             )
  110.             ->add(
  111.                 'variation',
  112.                 ModelType::class,
  113.                 [
  114.                     'label' => 'Варіація',
  115.                     'query' => $queryBuilderVariation,
  116.                     'btn_add' => false,
  117.                     'required' => true,
  118.                     'empty_data' => null,
  119.                 ],
  120.                 [
  121.                     'admin_code' => $rootAdmin->getVariationAdminCode(),
  122.                 ]
  123.             );
  124.         if ($specCount) {
  125.             $formMapper->add(
  126.                 'specification',
  127.                 ModelType::class,
  128.                 [
  129.                     'label' => 'Специфікація',
  130.                     'query' => $queryBuilderSpecification,
  131.                     'btn_add' => false,
  132.                     'required' => false,
  133.                     'empty_data' => null,
  134.                     'attr' => [
  135.                         'style' => 'width:150px'
  136.                     ]
  137.                 ],
  138.                 [
  139.                     'admin_code' => 'admin.vehicles.specification',
  140.                 ]
  141.             );
  142.         }
  143.         $formMapper
  144.             ->add('state'CheckboxType::class, ['label' => 'Активна''required' => false])
  145.             ->add('specify_price'CheckboxType::class, ['label' => 'Уточнить цену''required' => false])
  146.             ->add('year'NumberType::class, ['label' => 'Год выпуска''required' => true])
  147.             ->add('price'NumberType::class, ['label' => 'Цена''required' => true])
  148.             ->add('alt_price'NumberType::class, ['label' => 'Акционная цена, грн'])
  149.             ->add('alt_usd_price'NumberType::class, ['label' => 'Акционная цена, $'])
  150.             ->add('alt_rate'NumberType::class, ['label' => 'Акционный курс']);
  151.         if ($hasPreOrderPrice) {
  152.             $formMapper->add('preorder_price'NumberType::class, ['label' => 'Цена предзаказа']);
  153.         }
  154.         if ($hasRentPrice) {
  155.             $formMapper->add('rent_price'NumberType::class, ['label' => 'Цена аренды''required' => true]);
  156.         }
  157.         return;
  158.     }
  159.     protected function getVariationAdminCode(): string
  160.     {
  161.         return 'admin.vehicles.passenger_variation';
  162.     }
  163.     protected function getVehicleAdminCode(): string
  164.     {
  165.         return 'admin.vehicles.passenger_vehicle';
  166.     }
  167. }