@@ -426,4 +426,102 @@ private function buildBulkStatusForm(
426426
427427 return $ builder ->getForm ();
428428 }
429+
430+ /**
431+ * @throws RuntimeError
432+ */
433+ public function bulkUndeleteAction (Request $ request ): Response
434+ {
435+ if (empty ($ request ->get ('undeleteForm ' )['nodesIds ' ])) {
436+ throw new ResourceNotFoundException ();
437+ }
438+
439+ $ nodesIds = trim ((string ) $ request ->get ('undeleteForm ' )['nodesIds ' ]);
440+ $ nodesIds = \json_decode ($ nodesIds , true , flags: JSON_THROW_ON_ERROR );
441+ array_filter ($ nodesIds );
442+
443+ /** @var Node[] $nodes */
444+ $ nodes = $ this ->allStatusesNodeRepository ->findBy ([
445+ 'id ' => $ nodesIds ,
446+ ]);
447+
448+ if (0 === count ($ nodes )) {
449+ throw new ResourceNotFoundException ();
450+ }
451+
452+ $ items = [];
453+ foreach ($ nodes as $ node ) {
454+ $ this ->denyAccessUnlessGranted (NodeVoter::DELETE , $ node );
455+ $ items [] = $ this ->explorerItemFactory ->createForEntity ($ node )->toArray ();
456+ }
457+
458+ $ form = $ this ->buildBulkUndeleteForm (
459+ $ nodesIds
460+ );
461+ $ form ->handleRequest ($ request );
462+ if ($ request ->get ('confirm ' ) && $ form ->isSubmitted () && $ form ->isValid ()) {
463+ $ msg = $ this ->bulkUndeleteNodes ($ form ->getData ());
464+ $ this ->logTrail ->publishConfirmMessage ($ request , $ msg );
465+
466+ return $ this ->redirectToRoute ('nodesHomePage ' );
467+ }
468+
469+ return $ this ->render ('@RoadizRozier/admin/confirm_action.html.twig ' , [
470+ 'title ' => new UnicodeString ($ this ->translator ->trans ('undelete.nodes ' )),
471+ 'headPath ' => '@RoadizRozier/nodes/head.html.twig ' ,
472+ 'cancelPath ' => $ this ->generateUrl ('nodesHomePage ' ),
473+ 'alertMessage ' => 'are_you_sure.undelete.these.nodes ' ,
474+ 'action_label ' => 'undelete.nodes.all ' ,
475+ 'action_icon ' => 'rz-icon-ri--device-recover-line ' ,
476+ 'form ' => $ form ->createView (),
477+ 'items ' => $ items ,
478+ ]);
479+ }
480+
481+ private function buildBulkUndeleteForm (
482+ array $ nodesIds = [],
483+ ): FormInterface {
484+ /** @var FormBuilder $builder */
485+ $ builder = $ this ->formFactory
486+ ->createNamedBuilder ('undeleteForm ' )
487+ ->add ('nodesIds ' , HiddenType::class, [
488+ 'data ' => json_encode ($ nodesIds , flags: JSON_THROW_ON_ERROR ),
489+ 'attr ' => ['class ' => 'bulk-form-value ' ],
490+ 'constraints ' => [
491+ new NotNull (),
492+ new NotBlank (),
493+ ],
494+ ]);
495+
496+ $ builder ->setAction ('?confirm=1 ' );
497+
498+ return $ builder ->getForm ();
499+ }
500+
501+ private function bulkUndeleteNodes (array $ data ): string
502+ {
503+ if (!empty ($ data ['nodesIds ' ])) {
504+ $ nodesIds = trim ((string ) $ data ['nodesIds ' ]);
505+ $ nodesIds = \json_decode ($ nodesIds , true , flags: JSON_THROW_ON_ERROR );
506+ array_filter ($ nodesIds );
507+
508+ $ nodes = $ this ->allStatusesNodeRepository
509+ ->findBy ([
510+ 'id ' => $ nodesIds ,
511+ ]);
512+
513+ /** @var Node $node */
514+ foreach ($ nodes as $ node ) {
515+ /** @var NodeHandler $handler */
516+ $ handler = $ this ->handlerFactory ->getHandler ($ node );
517+ $ handler ->softUnremoveWithChildren ();
518+ }
519+
520+ $ this ->managerRegistry ->getManager ()->flush ();
521+
522+ return $ this ->translator ->trans ('nodes.bulk.undeleted ' );
523+ }
524+
525+ return $ this ->translator ->trans ('wrong.request ' );
526+ }
429527}
0 commit comments