<?php
namespace AdminBundle\Admin\SubAutoSite;
use AdminBundle\Form\Type\ContentType;
use CoreBundle\Entity\User;
use DateTime;
use Exception;
use ImporterBundle\Entity\Post;
use ImporterBundle\Entity\PostContent;
use ImporterBundle\Entity\PostLog;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Form\Type\ModelListType;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Symfony\Component\Finder\Exception\AccessDeniedException;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
class NewsAdmin extends BaseImporterAdmin
{
protected $baseRouteName = 'admin_vendor_adminbundle_importer_newsadmin';
protected $baseRoutePattern = 'importer/news';
/**
* @return string
*/
public function getImageContext() {
return 'default';
}
/**
* @return string
*/
public function getGalleryContext() {
return 'default';
}
/**
* @param RouteCollectionInterface $collection
*/
protected function configureRoutes(RouteCollectionInterface $collection): void
{
$collection->remove('view');
}
/**
* @param DatagridMapper $datagridMapper
*/
protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
{
$datagridMapper->add('id');
}
/**
* @param $post Post
* @param $user
* @throws Exception
*/
public function log($post, $user)
{
$oldPost = $this->em->getRepository(PostLog::class)->findOneBy(['post' => $post], ['update_date' => 'DESC']);
if ($oldPost) {
$oldPost = $oldPost->getNewValue();
}
$postLog = new PostLog();
$postLog->setUpdateDate(new DateTime());
$postLog->setPost($post);
$postLog->setUser($user);
$postLog->setOldValue($oldPost);
$postLog->setNewValue($this->prepareForLog($post));
$this->em->persist($postLog);
$this->em->flush();
}
/**
* @param $post Post
* @return false|string
*/
public function prepareForLog($post)
{
return json_encode([
'state' => $post->getState(),
'url' => $post->getUrl(),
'date_start' => $post->getDateStart() ? $post->getDateStart()->getTimestamp() : '',
'date_end' => $post->getDateEnd() ? $post->getDateEnd()->getTimestamp() : '',
'action_chapter' => $post->getActionChapter(),
'post_type' => $post->getPostType(),
'content.title' => ['ru' => $post->getTitle('ru'), 'ua' => $post->getTitle('ua')],
'content.description' => ['ru' => $post->getDescription('ru'), 'ua' => $post->getDescription('ua')],
'content.content' => ['ru' => $post->getContentHtml('ru'), 'ua' => $post->getContentHtml('ua')],
'content.seo_title' => ['ru' => $post->getContentByLocale('ru')->getSeoTitle(), 'ua' => $post->getContentByLocale('ua')->getSeoTitle()],
'content.seo_description' => ['ru' => $post->getContentByLocale('ru')->getSeoDescription(), 'ua' => $post->getContentByLocale('ua')->getSeoDescription()],
]);
}
/**
* @param Post $object
*/
public function prePersist($object): void
{
/** @var User $User */
$User = $this->security->getUser();
if(!$User->getSubDealer() && !$object->getDealer()) {
throw new AccessDeniedException('User without sub dealer');
}
/** @var PostContent $content */
foreach ($object->getContent() as $content) {
$content->setPost($object);
}
/** @var Post $object */
$object->setDealer($User->getSubDealer());
$object->setState((int) $object->getState());
$object->setPostType(\ImporterBundle\Model\Post::POST_TYPE_NEWS);
}
/**
* @param object $post
* @throws Exception
*/
public function postPersist($post): void
{
$user = $this->security->getUser();
$this->log($post, $user);
}
/**
* @param object $post
* @throws Exception
*/
public function postUpdate($post): void
{
$user = $this->security->getUser();
$this->log($post, $user);
}
/**
* @param Post $object
*/
public function preUpdate($object): void
{
$object->setState((int) $object->getState());
parent::preUpdate($object);
}
/**
* @param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper): void
{
$formMapper
->tab('Основная информация')
->with(' ', ['class' => 'col-lg-6 without-box-heder'])
->add('state', CheckboxType::class, ['label' => 'Показывать на сайте', 'required' => false])
->add('url', null, ['label' => 'URL'])
->add('date_create', DateType::class , ['widget' => 'single_text', 'label' => 'Дата публикации'])
->add('content', ContentType::class, ['label' => false], [
'edit' => 'inline',
'sortable' => 'position',
])
->add('gallery', ModelListType::class, [
'label' => 'Галерея изображений',
'btn_list' => false,
'required' => false,
], [
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
'link_parameters' => [
'context' => $this->getGalleryContext(),
'provider' => 'sonata.media.provider.image'
],
'admin_code' => 'sonata.media.admin.gallery',
]
)
->end()
->end()
;
}
/**
* @param string $context
* @return ProxyQueryInterface
*/
public function configureQuery($context = 'list'): ProxyQueryInterface
{
$query = parent::configureQuery($context);
$query->where($query->getRootAliases()[0].'.post_type = '.\ImporterBundle\Model\Post::POST_TYPE_NEWS);
/** @var User $User */
$User = $this->security->getUser();
if(!$User->getSubDealer()) {
throw new AccessDeniedException('User without sub dealer');
}
$query->andWhere($query->getRootAliases()[0].'.dealer = '.$User->getSubDealer()->getId());
return $query;
}
/**
* @param ListMapper $listMapper
*/
protected function configureListFields(ListMapper $listMapper): void
{
$listMapper->addIdentifier('id')
->add('title',null, ['label' => 'Заголовок'])
->add('dealer',null, ['label' => 'Дилер', 'admin_code' => 'admin.sub.contact'])
->add('state','choice', ['label' => 'Показывать на сайте', 'editable' => true, 'choices' => [
1 => 'Да',
0 => 'Нет',
]])
->add('_action', 'actions', [
'label' => 'Действия',
'actions' => [
'edit' => [],
]
])
;
}
}