src/Admin/SparePartAdmin.php line 21

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Admin;
  4. use App\Entity\Motorcycle;
  5. use App\Entity\SparePart;
  6. use App\Repository\SparePartRepository;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Sonata\AdminBundle\Admin\AbstractAdmin;
  9. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  10. use Sonata\AdminBundle\Datagrid\ListMapper;
  11. use Sonata\AdminBundle\Form\FormMapper;
  12. use Sonata\AdminBundle\Form\Type\ModelType;
  13. use Sonata\AdminBundle\Show\ShowMapper;
  14. use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  15. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  16. use Symfony\Component\Form\Extension\Core\Type\TextType;
  17. final class SparePartAdmin extends AbstractAdmin
  18. {
  19.     private ?EntityManagerInterface $entityManager null;
  20.     protected $datagridValues = [
  21.         '_sort_order' => 'DESC',
  22.         '_sort_by' => 'id',
  23.     ];
  24.     public function configure(): void
  25.     {
  26.         // Pink row when quantity is 0
  27.         $this->setTemplate('outer_list_rows_list''admin/SparePart/list_outer_rows_list.html.twig');
  28.     }
  29.     protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
  30.     {
  31.         $datagridMapper
  32.             ->add('name'null, ['label' => 'Название'])
  33.             ->add('partNumber'null, ['label' => 'Номер детали'])
  34.             ->add('motorcycles'null, ['label' => 'Мотоциклы'])
  35.             ->add('quantity'null, ['label' => 'Количество'])
  36.             ->add('cost'null, ['label' => 'Стоимость'])
  37.         ;
  38.     }
  39.     protected function configureListFields(ListMapper $listMapper): void
  40.     {
  41.         $listMapper
  42.             ->add('name'null, ['label' => 'Название'])
  43.             ->add('partNumber'null, ['label' => 'Номер детали'])
  44.             ->add('motorcycles'null, ['label' => 'Мотоциклы'])
  45.             ->add('quantity'null, ['label' => 'Количество'])
  46.             ->add('cost'null, ['label' => 'Стоимость'])
  47.             ->add('createdAt'null, ['label' => 'Добавлено'])
  48.             ->add('_action'null, [
  49.                 'actions' => [
  50.                     'show' => [],
  51.                     'edit' => [],
  52.                     'delete' => [],
  53.                 ],
  54.             ])
  55.         ;
  56.     }
  57.     protected function configureFormFields(FormMapper $formMapper): void
  58.     {
  59.         $formMapper
  60.             ->add('name'TextType::class, [
  61.                 'required' => true,
  62.                 'label' => 'Название',
  63.             ])
  64.             ->add('partNumber'TextType::class, [
  65.                 'required' => false,
  66.                 'label' => 'Номер детали',
  67.             ])
  68.             ->add('motorcycles'ModelType::class, [
  69.                 'class' => Motorcycle::class,
  70.                 'required' => false,
  71.                 'multiple' => true,
  72.                 'btn_add' => false,
  73.                 'label' => 'Мотоциклы',
  74.                 'help' => 'Необязательно. Можно оставить пустым, если неизвестно, к какому мотоциклу относится запчасть.',
  75.             ])
  76.             ->add('quantity'IntegerType::class, [
  77.                 'required' => true,
  78.                 'label' => 'Количество',
  79.                 'attr' => ['min' => 0],
  80.                 'help' => 'Если запчасть с такими же полями уже есть — количество будет добавлено к существующей записи.',
  81.             ])
  82.             ->add('cost'NumberType::class, [
  83.                 'required' => false,
  84.                 'label' => 'Стоимость',
  85.                 'scale' => 2,
  86.             ])
  87.         ;
  88.     }
  89.     protected function configureShowFields(ShowMapper $showMapper): void
  90.     {
  91.         $showMapper
  92.             ->add('id')
  93.             ->add('name'null, ['label' => 'Название'])
  94.             ->add('partNumber'null, ['label' => 'Номер детали'])
  95.             ->add('motorcycles'null, ['label' => 'Мотоциклы'])
  96.             ->add('quantity'null, ['label' => 'Количество'])
  97.             ->add('cost'null, ['label' => 'Стоимость'])
  98.             ->add('createdAt'null, ['label' => 'Добавлено'])
  99.         ;
  100.     }
  101.     /**
  102.      * If a twin already exists (same name, part number, cost, motorcycles),
  103.      * increase its quantity instead of creating a duplicate row.
  104.      *
  105.      * @param SparePart $object
  106.      *
  107.      * @return SparePart
  108.      */
  109.     public function create($object)
  110.     {
  111.         if ($this->entityManager) {
  112.             /** @var SparePartRepository $repo */
  113.             $repo $this->entityManager->getRepository(SparePart::class);
  114.             $twin $repo->findTwin($object);
  115.             if ($twin !== null) {
  116.                 $addQty max(0$object->getQuantity());
  117.                 $twin->setQuantity($twin->getQuantity() + $addQty);
  118.                 $this->getModelManager()->update($twin);
  119.                 $this->postPersist($twin);
  120.                 foreach ($this->getExtensions() as $extension) {
  121.                     $extension->postPersist($this$twin);
  122.                 }
  123.                 return $twin;
  124.             }
  125.         }
  126.         $object->setName(trim($object->getName()));
  127.         return parent::create($object);
  128.     }
  129.     /**
  130.      * @required
  131.      */
  132.     public function setEntityManager(EntityManagerInterface $entityManager): void
  133.     {
  134.         $this->entityManager $entityManager;
  135.     }
  136. }