<?php
namespace AdminBundle\Admin\DCAutoSite;
use AdminBundle\Admin\BaseAdmin;
use DcSiteBundle\Model\BoatEngine;
use FOS\CKEditorBundle\Form\Type\CKEditorType;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\Type\ModelListType;
use Sonata\AdminBundle\Form\Type\ModelType;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Sonata\Form\Validator\ErrorElement;
use Sonata\MediaBundle\Form\Type\MediaType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormError;
class BoatEngineAdmin extends BaseAdmin
{
/**
* @param RouteCollectionInterface $collection
*/
protected function configureRoutes(RouteCollectionInterface $collection): void
{
$collection->remove('view');
$collection->remove('delete');
}
public function validate(ErrorElement $errorElement, $object)
{
$hasEngine = $this->em->getRepository(\DcSiteBundle\Entity\BoatEngine::class)->createQueryBuilder('be')
->where('be.url = :url')
->setParameter('url', $object->getUrl())
->andWhere('be.id != :id')
->setParameter('id', $object->getId())
->getQuery()->getResult()
;
if ($hasEngine) {
$this->getForm()->addError(new FormError('Мотор з таким URL уже существует'));
}
}
/**
* @param ListMapper $listMapper
*/
public function configureListFields(ListMapper $listMapper): void
{
$listMapper
->addIdentifier('title_ru', null, ['label' => 'Название'])
->add('state','choice', ['label' => 'Показывать на сайте', 'editable' => true, 'choices' => [
1 => 'Да',
0 => 'Нет',
null => 'Нет',
]])
->add('_action','actions',[
'label' => 'Действия',
'actions' => [
'edit' => [],
]
])
;
}
/**
* @param $object
*/
public function prePersist($object): void
{
$dealer = $this->security->getUser()->getDealer();
$object->setDealer($dealer);
}
/**
* @param FormMapper $formMapper
*/
public function configureFormFields(FormMapper $formMapper): void
{
$dealer = $this->security->getUser()->getDealer();
$formMapper
->tab('Характеристики')
->with('Русский', ['class' => 'col-md-6'])
->add('title_ru', null, ['label' => 'Название RU'])
->add('description_ru', TextareaType::class, ['label' => 'Краткое описание RU', 'required' => false,])
->add('content_ru', CKEditorType::class, ['label' => 'Описание RU', 'required' => false])
->end()
->with('Украинский', ['class' => 'col-md-6'])
->add('title_ua', null, ['label' => 'Название UA'])
->add('description_ua', TextareaType::class, ['label' => 'Краткое описание UA', 'required' => false,])
->add('content_ua', CKEditorType::class, ['label' => 'Описание UA', 'required' => false])
->end()
->with('Общие')
->add('price', null, ['label' => 'Цена в валюте'])
->add('alt_price', null, ['label' => 'Акционная цена'])
->add('alt_rate', null, ['label' => 'Акционный курс'])
->add('state',ChoiceType::class, ['label' => 'Показывать на сайте', 'choices' => array_flip([
1 => 'Да',
0 => 'Нет',
null => 'Нет',
])])
->add('position',null, ['label' => 'Порядковый номер на сайте'])
->add('group', ModelType::class, [
'label' => 'Группа',
'btn_add' => false,
'required' => true,
])
->add('url', null, ['label' => 'URL', 'required' => true])
->end()
->with('Характеристики двигателя', ['class' => 'col-md-6'])
->add('engine_type', ChoiceType::class, [
'label' => 'Тип двигателя',
'required' => false,
'choices' => array_flip(BoatEngine::getEngineType()),
])
->add('engine_volume', null, [
'label' => 'Рабочий объем, куб. см',
'required' => false,
])
->add('cylinder_count', IntegerType::class, [
'label' => 'Количество цилиндров',
'required' => false,
])
->add('cylinder_diameter', IntegerType::class, [
'label' => 'Диаметр цилиндра, мм',
'required' => false,
])
->add('engine_configuration', ChoiceType::class, [
'label' => 'Конфигурация двигателя',
'required' => false,
'choices' => array_flip(BoatEngine::getEngineConfigType()),
])
->add('piston_stroke', IntegerType::class, [
'label' => 'Ход поршня, мм',
'required' => false,
])
->add('engine_power', null, [
'label' => 'Мощность, кВт / об/мин',
'required' => false,
])
->add('gear_ratio', null, [
'label' => 'Передаточное соотношение',
'required' => false,
])
->add('engine_rpm', null, [
'label' => 'Диапазон оборотов при полном газе, об/мин',
'required' => false,
])
->add('engine_lubrication', ChoiceType::class, [
'label' => 'Система смазки',
'required' => false,
'choices' => array_flip(BoatEngine::getLubricationType()),
])
->add('oil_tank_volume', null, [
'label' => 'Объем емкости масляного картера, л',
'required' => false,
])
->add('fuel_injection_system', ChoiceType::class, [
'label' => 'Система впрыска топлива',
'required' => false,
'choices' => array_flip(BoatEngine::getFuelInjectionType()),
])
->add('ignition_coil_generator', null, [
'label' => 'Катушка зажигания/генератор',
'required' => false,
])
->add('fastening', ChoiceType::class, [
'label' => 'Крепление',
'required' => false,
'choices' => array_flip(BoatEngine::getFasteningType()),
])
->end()
->with('Характеристики электродвигателя', ['class' => 'col-md-6'])
->add('max_amperage', null, [
'label' => 'Максимальный ток, А',
'required' => false,
])
->add('voltage', null, [
'label' => 'Напряжение, В',
'required' => false,
])
->add('traction_force', null, [
'label' => 'Тяговое усилие, кг',
'required' => false,
])
->add('speed_back_forth', ChoiceType::class, [
'label' => 'Скорости вперед/назад',
'required' => false,
'choices' => array_flip(BoatEngine::getSpeedBackForthType()),
])
->add('digital_extension_cord', ChoiceType::class, [
'label' => 'Цифровой удлинитель хода',
'required' => false,
'choices' => array_flip(BoatEngine::getAvailability()),
])
->add('leg_length', null, [
'label' => 'Длина ноги, мм',
'required' => false,
])
->add('battery_discharge_indicator', ChoiceType::class, [
'label' => 'Индикатор разряда батареи',
'required' => false,
'choices' => array_flip(BoatEngine::getAvailability()),
])
->end()
->with('Управление', ['class' => 'col-md-6'])
->add('start_system_type', ChoiceType::class, [
'label' => 'Система запуска',
'required' => false,
'choices' => array_flip(BoatEngine::getStartType()),
//'multiple' => true,
])
->add('steering_type', ChoiceType::class, [
'label' => 'Рулевое управление',
'required' => false,
'choices' => array_flip(BoatEngine::getSteeringTypeType()),
//'multiple' => true,
])
->end()
->with('Физические характеристики', ['class' => 'col-md-6'])
->add('recommended_boat_height', null, [
'label' => 'Рекомендуемая высота транца, мм',
'required' => false,
])
->add('weight', null, [
'label' => 'Вес, кг',
'required' => false,
])
->end()
->end()
->tab('Медиа контент')
->with('Изображения', ['class' => 'col-lg-6'])
->add('image', MediaType::class, [
'label' => 'Основное изображение',
'required' => false,
'provider' => 'sonata.media.provider.image',
'context' => 'dc_site'
])
->add('preview', MediaType::class, [
'label' => 'Превью',
'provider' => 'sonata.media.provider.image',
'context' => 'dc_site'
])
->end()
->with('Галереи', ['class' => 'col-lg-6'])
->add('gallery', ModelListType::class, [
'label' => 'Галерея изображений',
'btn_list' => false,
'required' => false,
], [
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
'link_parameters' => [
'context' => 'dc_car_gallery',
'provider' => 'sonata.media.provider.image'
],
'admin_code' => 'sonata.media.admin.gallery',
]
)
->add('video', ModelListType::class, [
'label' => 'Галерея видео',
'btn_list' => false,
'required' => false,
], [
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
'link_parameters' => [
'context' => 'dc_car_video',
'provider' => 'sonata.media.provider.youtube'
],
'admin_code' => 'sonata.media.admin.gallery',
])
->end()
->end()
->tab('SEO')
->with('RU', ['class' => 'col-md-6'])
->add('seo_title_ru', null, ['label' => 'Seo-title (RU)', 'required' => false])
->add('seo_description_ru', TextareaType::class, ['label' => 'Seo-description (RU)', 'required' => false])
->end()
->with('UA', ['class' => 'col-md-6'])
->add('seo_title_ua', null, ['label' => 'Seo-title (UA)', 'required' => false])
->add('seo_description_ua', TextareaType::class, ['label' => 'Seo-description (UA)', 'required' => false])
->end()
->end()
;
}
/**
* @param DatagridMapper $datagridMapper
*/
public function configureDatagridFilters(DatagridMapper $datagridMapper): void
{
$datagridMapper
->add('title_ru')
;
}
}