src/AdminBundle/Admin/SubAutoSite/NewsAdmin.php line 24

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Admin\SubAutoSite;
  3. use AdminBundle\Form\Type\ContentType;
  4. use CoreBundle\Entity\User;
  5. use DateTime;
  6. use Exception;
  7. use ImporterBundle\Entity\Post;
  8. use ImporterBundle\Entity\PostContent;
  9. use ImporterBundle\Entity\PostLog;
  10. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  11. use Sonata\AdminBundle\Datagrid\ListMapper;
  12. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  13. use Sonata\AdminBundle\Form\FormMapper;
  14. use Sonata\AdminBundle\Form\Type\ModelListType;
  15. use Sonata\AdminBundle\Route\RouteCollection;
  16. use Sonata\AdminBundle\Route\RouteCollectionInterface;
  17. use Symfony\Component\Finder\Exception\AccessDeniedException;
  18. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  19. use Symfony\Component\Form\Extension\Core\Type\DateType;
  20. class NewsAdmin extends BaseImporterAdmin
  21. {
  22.     protected $baseRouteName 'admin_vendor_adminbundle_importer_newsadmin';
  23.     protected $baseRoutePattern 'importer/news';
  24.     /**
  25.      * @return string
  26.      */
  27.     public function getImageContext() {
  28.         return 'default';
  29.     }
  30.     /**
  31.      * @return string
  32.      */
  33.     public function getGalleryContext() {
  34.         return 'default';
  35.     }
  36.     /**
  37.      * @param RouteCollectionInterface $collection
  38.      */
  39.     protected function configureRoutes(RouteCollectionInterface $collection): void
  40.     {
  41.         $collection->remove('view');
  42.     }
  43.     /**
  44.      * @param DatagridMapper $datagridMapper
  45.      */
  46.     protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
  47.     {
  48.         $datagridMapper->add('id');
  49.     }
  50.     /**
  51.      * @param $post Post
  52.      * @param $user
  53.      * @throws Exception
  54.      */
  55.     public function log($post$user)
  56.     {
  57.         $oldPost $this->em->getRepository(PostLog::class)->findOneBy(['post' => $post], ['update_date' => 'DESC']);
  58.         if ($oldPost) {
  59.             $oldPost $oldPost->getNewValue();
  60.         }
  61.         $postLog = new PostLog();
  62.         $postLog->setUpdateDate(new DateTime());
  63.         $postLog->setPost($post);
  64.         $postLog->setUser($user);
  65.         $postLog->setOldValue($oldPost);
  66.         $postLog->setNewValue($this->prepareForLog($post));
  67.         $this->em->persist($postLog);
  68.         $this->em->flush();
  69.     }
  70.     /**
  71.      * @param $post Post
  72.      * @return false|string
  73.      */
  74.     public function prepareForLog($post)
  75.     {
  76.         return json_encode([
  77.             'state' => $post->getState(),
  78.             'url' => $post->getUrl(),
  79.             'date_start' => $post->getDateStart() ? $post->getDateStart()->getTimestamp() : '',
  80.             'date_end' => $post->getDateEnd() ? $post->getDateEnd()->getTimestamp() : '',
  81.             'action_chapter' => $post->getActionChapter(),
  82.             'post_type' => $post->getPostType(),
  83.             'content.title' => ['ru' => $post->getTitle('ru'), 'ua' => $post->getTitle('ua')],
  84.             'content.description' => ['ru' => $post->getDescription('ru'), 'ua' => $post->getDescription('ua')],
  85.             'content.content' => ['ru' => $post->getContentHtml('ru'), 'ua' => $post->getContentHtml('ua')],
  86.             'content.seo_title' => ['ru' => $post->getContentByLocale('ru')->getSeoTitle(), 'ua' => $post->getContentByLocale('ua')->getSeoTitle()],
  87.             'content.seo_description' => ['ru' => $post->getContentByLocale('ru')->getSeoDescription(), 'ua' => $post->getContentByLocale('ua')->getSeoDescription()],
  88.         ]);
  89.     }
  90.     /**
  91.      * @param Post $object
  92.      */
  93.     public function prePersist($object): void
  94.     {
  95.         /** @var User $User */
  96.         $User $this->security->getUser();
  97.         if(!$User->getSubDealer() && !$object->getDealer()) {
  98.             throw new AccessDeniedException('User without sub dealer');
  99.         }
  100.         /** @var PostContent $content */
  101.         foreach ($object->getContent() as $content) {
  102.             $content->setPost($object);
  103.         }
  104.         /** @var Post $object */
  105.         $object->setDealer($User->getSubDealer());
  106.         $object->setState((int) $object->getState());
  107.         $object->setPostType(\ImporterBundle\Model\Post::POST_TYPE_NEWS);
  108.     }
  109.     /**
  110.      * @param object $post
  111.      * @throws Exception
  112.      */
  113.     public function postPersist($post): void
  114.     {
  115.         $user $this->security->getUser();
  116.         $this->log($post$user);
  117.     }
  118.     /**
  119.      * @param object $post
  120.      * @throws Exception
  121.      */
  122.     public function postUpdate($post): void
  123.     {
  124.         $user $this->security->getUser();
  125.         $this->log($post$user);
  126.     }
  127.     /**
  128.      * @param Post $object
  129.      */
  130.     public function preUpdate($object): void
  131.     {
  132.         $object->setState((int) $object->getState());
  133.         parent::preUpdate($object);
  134.     }
  135.     /**
  136.      * @param FormMapper $formMapper
  137.      */
  138.     protected function configureFormFields(FormMapper $formMapper): void
  139.     {
  140.         $formMapper
  141.             ->tab('Основная информация')
  142.             ->with(' ', ['class' => 'col-lg-6 without-box-heder'])
  143.             ->add('state'CheckboxType::class, ['label' => 'Показывать на сайте''required' => false])
  144.             ->add('url'null, ['label' => 'URL'])
  145.             ->add('date_create'DateType::class , ['widget' => 'single_text''label' => 'Дата публикации'])
  146.             ->add('content'ContentType::class, ['label' => false], [
  147.                 'edit' => 'inline',
  148.                 'sortable' => 'position',
  149.             ])
  150.             ->add('gallery'ModelListType::class, [
  151.                 'label' => 'Галерея изображений',
  152.                 'btn_list' => false,
  153.                 'required' => false,
  154.             ], [
  155.                     'edit' => 'inline',
  156.                     'inline' => 'table',
  157.                     'sortable' => 'position',
  158.                     'link_parameters' => [
  159.                         'context' => $this->getGalleryContext(),
  160.                         'provider' => 'sonata.media.provider.image'
  161.                     ],
  162.                     'admin_code' => 'sonata.media.admin.gallery',
  163.                 ]
  164.             )
  165.             ->end()
  166.             ->end()
  167.         ;
  168.     }
  169.     /**
  170.      * @param string $context
  171.      * @return ProxyQueryInterface
  172.      */
  173.     public function configureQuery($context 'list'): ProxyQueryInterface
  174.     {
  175.         $query parent::configureQuery($context);
  176.         $query->where($query->getRootAliases()[0].'.post_type = '.\ImporterBundle\Model\Post::POST_TYPE_NEWS);
  177.         /** @var User $User */
  178.         $User $this->security->getUser();
  179.         if(!$User->getSubDealer()) {
  180.             throw new AccessDeniedException('User without sub dealer');
  181.         }
  182.         $query->andWhere($query->getRootAliases()[0].'.dealer = '.$User->getSubDealer()->getId());
  183.         return $query;
  184.     }
  185.     /**
  186.      * @param ListMapper $listMapper
  187.      */
  188.     protected function configureListFields(ListMapper $listMapper): void
  189.     {
  190.         $listMapper->addIdentifier('id')
  191.             ->add('title',null, ['label' => 'Заголовок'])
  192.             ->add('dealer',null, ['label' => 'Дилер''admin_code' => 'admin.sub.contact'])
  193.             ->add('state','choice', ['label' => 'Показывать на сайте''editable' => true'choices' => [
  194.                 => 'Да',
  195.                 => 'Нет',
  196.             ]])
  197.             ->add('_action''actions', [
  198.                 'label' => 'Действия',
  199.                 'actions' => [
  200.                     'edit' => [],
  201.                 ]
  202.             ])
  203.         ;
  204.     }
  205. }