<?php
declare(strict_types=1);
namespace App\Admin;
use App\Entity\PieceJointe;
use App\Form\BlockType;
use App\Form\ImageAdminType;
use App\Form\PieceJointeType;
use App\Form\PlanLotType;
use Sonata\AdminBundle\Form\Type\TemplateType;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Symfony\Component\Validator\Constraints\File;
use Sonata\AdminBundle\Datagrid\DatagridInterface;
use Symfony\Component\Form\Extension\Core\Type\RadioType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
final class PieceJointeAdmin extends AbstractAdmin
{
protected $datagridValues = [
'_page' => 1,
'_sort_order' => 'DESC',
'_sort_by' => 'id',
'_per_page' => 250,
];
protected function configureDefaultSortValues(array &$sortValues): void
{
$sortValues[DatagridInterface::PAGE] = 1;
$sortValues[DatagridInterface::SORT_ORDER] = 'DESC';
$sortValues[DatagridInterface::SORT_BY] = 'id';
$sortValues[DatagridInterface::PER_PAGE] = 250;
}
protected function configureDatagridFilters(DatagridMapper $filter): void
{
$filter
->add('filename')
->add('descriptionCourte')
;
}
protected function configureListFields(ListMapper $list): void
{
$list
->add('filename')
->add('descriptionCourte')
->add(ListMapper::NAME_ACTIONS, null, [
'actions' => [
'show' => [],
'edit' => [],
'delete' => [],
],
]);
}
protected function configureFormFields(FormMapper $form): void
{
$lot = $this->getSubject()->getLot();
$isInProgramme = $lot === null;
$classDescriptionCourte = 'pj-description-courte-field';
$pieceJointe = $this->getSubject();
if($pieceJointe->getPieceJointeMere()){
$classDescriptionCourte .= ' pj-of-programme';
}
$isLotContext = $this->hasParentFieldDescription() && $this->getParentFieldDescription()->getAdmin() instanceof LotAdmin;
if ($isLotContext) {
$form->add('showPublic', null, [
'required' => false,
'label' => 'Visibilité Client',
'row_attr' => [
'class' => 'showPublic'
]
]);
}
$form
// ->add('_rotate', PieceJointeType::class, [
// 'mapped' => false,
// 'label' => 'Faire pivoter',
// 'required' => false
// ])
->add('ordre', null, [
'required' => false,
'label' => 'Ordre'
])
->add('descriptionCourte', null, [
'attr' => [
'class' => $classDescriptionCourte,
'oninput' => 'this.value = this.value.toUpperCase()'
]
])
->add('fichier', PieceJointeType::class, [// unmapped means that this field is not associated to any entity property
'mapped' => false,
'save_path' => PieceJointe::SAVE_PATH,
'attr' => [
'accept' => 'application/pdf,application/x-pdf,image/jpeg,image/gif,image/png',
],
'label' => 'Fichier',
// make it optional so you don't have to re-upload the PDF file
// every time you edit the Product details
'required' => false,
// unmapped fields can't define their validation using annotations
// in the associated entity, so you can use the PHP constraint classes
'constraints' => [
new File([
'maxSize' => '32768k',
'mimeTypes' => [
'application/pdf',
'application/x-pdf',
'image/jpeg',
'image/png',
'image/gif',
],
'mimeTypesMessage' => 'Vous devez choisir un fichier au format PDF ou image.',
])
]
])
;
if (!$isInProgramme) {
$form
->add('estUnPlan', PlanLotType::class, [
'label' => 'Plan du lot',
'attr' => [
'class' => 'checkbox-plan',
'data-sonata-icheck' => 'false'
],
'help' => 'Cocher pour faire apparaitre ce doc dans recap',
'required' => false
])
->add('estImageAdmin', ImageAdminType::class, [
'label' => 'Image pour l\'administrateur du lot',
'attr' => [
'class' => 'checkbox-image-admin',
'data-sonata-icheck' => 'false'
],
'help' => 'Cocher pour faire apparaitre ce doc dans la fiche administrateur du lot',
'required' => false
])
;
}
}
protected function configureShowFields(ShowMapper $show): void
{
$show
->add('filename')
->add('descriptionCourte')
;
}
}