0

在上下文中,有一个编辑器可以根据他的 id 修改他的信息,并且他的信息必须在持久化到数据库之前由管理员验证。

我想创建一个临时表来存储编辑器将修改的信息,并且管理员可以通过 EasyAdmin 的界面访问他的信息,然后一旦管理员验证了信息,它将保留在原始表中。

我在文档中没有找到恢复编辑页面表单信息的方法。

我加入了我想要定位的 crud 控制器:

类 OperateurCrudController 扩展 AbstractCrudController {

private $crudUrlGenerator;
private $adminContextProvider;

public function __construct(AdminUrlGenerator $adminUrlGenerator, AdminContextProvider $adminContextProvider)
{
    $this->adminUrlGenerator = $adminUrlGenerator;
    $this->adminContextProvider = $adminContextProvider;

}

public static function getEntityFqcn(): string
{
    return Operateur::class;
}

public function configureFields(string $pageName): iterable
{
        return [
            IdField::new('id')->hideOnForm(),
            TextField::new('name', 'Nom'),
            TextEditorField::new('description', 'Description')->hideOnIndex(),
            TextareaField::new('address', 'Adresse'),
            TextField::new('city', 'Ville')->hideOnIndex(),
            TextField::new('postal_code', 'Code Postal')->hideOnIndex(),
            TextField::new('email', 'Email'),
            TelephoneField::new('phone', 'Téléphone'),
            UrlField::new('website', 'Site Web')->hideOnIndex(),
            TextEditorField::new('opening_hours', 'Horaires d\'Ouverture'),
            NumberField::new('latitude', 'Latitude')->hideOnIndex()->setNumDecimals(15),
            NumberField::new('longitude', 'Longitude')->hideOnIndex()->setNumDecimals(15),
            TextField::new('slug', 'Slug')->hideOnIndex()->setPermission('ROLE_ADMIN'),
            DateTimeField::new('created_at', 'Date Création')->onlyOnIndex(),
            DateTimeField::new('updated_at', 'Date Modification')->onlyOnIndex(),
            AssociationField::new('thematiques', 'Thématiques')

        ];

}

public function configureActions(Actions $actions): Actions
{
    $batchAction = Action::new('approve', 'Approuver', 'fa fa-user-check')
        ->linkToUrl('approveOperators');

    $updateOperator = Action::new('update', 'Enregistrer les modifications', 'fa fa-save')
        ->linkToCrudAction('saveOperator');


    if($this->isGranted('ROLE_ADMIN')){
        return $actions
            ->add(Crud::PAGE_INDEX, $batchAction)
            ->add(Crud:: PAGE_INDEX, Action::DETAIL)
            ->setPermission(Action::DELETE, 'ROLE_ADMIN')
            ->setPermission(Action::NEW, 'ROLE_ADMIN')
            ->setPermission(Action::EDIT, 'ROLE_ADMIN')
            ->setPermission($batchAction, 'ROLE_ADMIN');
    }

    if($this->isGranted('ROLE_EDITOR')){
        return $actions
            ->add(Crud:: PAGE_INDEX, Action::DETAIL)
            ->add(Crud::PAGE_EDIT, $updateOperator)
            ->setPermission(Action::DELETE, 'ROLE_ADMIN')
            ->setPermission(Action::NEW, 'ROLE_ADMIN')
            ->setPermission($updateOperator, 'ROLE_EDITOR')
        ->disable( Action::SAVE_AND_RETURN, Action::SAVE_AND_CONTINUE);
    }


}

public function approveOperators(): Response{

    $this->addFlash('notice', '<span style="color: green"><i class="fa fa-check"></i>Modification effecuté </span>');

    $url = $this->adminUrlGenerator
        ->setAction(Action::INDEX)
        ->generateUrl();


    return $this->redirect($url);


}



public function saveOperator(){
    //$this->addFlash('notice', '<span style="color: green"><i class="fa fa-check"></i>Modification pris en compte ! </span>');

    //Create my own save button in page edit

}


public function configureCrud(Crud $crud): Crud
{
    return $crud
        ->setEntityPermission('ROLE_EDITOR');
}



public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder
{

    $response = parent::createIndexQueryBuilder($searchDto, $entityDto, $fields, $filters);
    if(!$this->isGranted('ROLE_ADMIN')){
        $response->where('entity.id = :id');
        $response->setParameter('id', $this->getUser()->getOperateur());
    }


    return $response;

}

public function updateEntity(EntityManagerInterface $entityManager, $entityInstance): void
{
    parent::updateEntity($entityManager, $entityInstance); // TODO: Change the autogenerated stub
}

感谢您阅读

4

1 回答 1

-1

你应该使用 BeforeEntityPersistedEvent::class

它将在数据库中持久化元素之前自动触发

class EasyAdminSubscriber extends AbstractController implements EventSubscriberInterface
{

  public function __construct( )
      {

      }

  public static function getSubscribedEvents(){

    return [
      BeforeEntityPersistedEvent::class => ['functionName']
    ];
  }

  public function functionName(BeforeEntityPersistedEvent $event){

    $entity=$event->getEntityInstance();

    $user = $this->getUser();
    $userRole = $user->getRole();
    $check = $this->checkRole($userRole);

    if ($entity instanceof yourEntity && $check== true) {

    
           //Do your magic with changing the value you want in the table you
       }
    }

}

抱歉没有评论,我没有足够的声誉,所以我在那里回答。

于 2021-07-21T11:41:48.690 回答