src/App/Form/Type/ReactMediaManagerType.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Form\Type;
  3. use App\Model\Content\Media\AbstractMedia;
  4. use App\Model\Content\Media\AbstractLooseMedia;
  5. use App\Model\Content\Media\MediaCollection;
  6. use App\Service\MediaDecorator;
  7. use App\Util\Arrays;
  8. use App\Util\Json;
  9. use Symfony\Component\Form\DataTransformerInterface;
  10. use Symfony\Component\Form\FormBuilderInterface;
  11. use Symfony\Component\Form\FormInterface;
  12. use Symfony\Component\Form\FormView;
  13. use Symfony\Component\OptionsResolver\OptionsResolver;
  14. use Symfony\Component\Routing\RouterInterface;
  15. /**
  16.  *
  17.  */
  18. final class ReactMediaManagerType extends AbstractReactType implements DataTransformerInterface
  19. {
  20.     protected const REACT_APP 'media-manager';
  21.     // DI
  22.     protected MediaDecorator $decorator;
  23.     protected RouterInterface $router;
  24.     /**
  25.      *
  26.      */
  27.     public function __construct(
  28.         MediaDecorator $decorator,
  29.         RouterInterface $router
  30.     )
  31.     {
  32.         $this->decorator $decorator;
  33.         $this->router $router;
  34.     }
  35.     /**
  36.      * {@inheritDoc}
  37.      */
  38.     public function buildForm(FormBuilderInterface $builder, array $options): void
  39.     {
  40.         $builder->addModelTransformer($this);
  41.     }
  42.     /**
  43.      * {@inheritDoc}
  44.      */
  45.     public function buildView(FormView $viewFormInterface $form, array $options): void
  46.     {
  47.         $view->vars['react_config'] = [
  48.             'filetypes' => $options['filetypes'] ?: [],
  49.             'limit' => $options['limit'] ?: 0,
  50.         ];
  51.         $view->vars['react_routing'] = Arrays::array_map_assoc(
  52.             function (string $route, ?array $params) {
  53.                 return $this->router->generate(
  54.                     $route,
  55.                     $params ?: [],
  56.                 );
  57.             },
  58.             [
  59.                 'app.ajax.filesystem.allocate_files' => null,
  60.                 'app.ajax.filesystem.uploaded' => null,
  61.                 'app.ajax.filesystem.check' => null,
  62.                 'app.notifications.dashboard.recording.phone_wait' => [
  63.                     'recording' => '~recording~',
  64.                 ],
  65.             ],
  66.         );
  67.     }
  68.     /**
  69.      * {@inheritdoc}
  70.      */
  71.     public function configureOptions(OptionsResolver $resolver): void
  72.     {
  73.         parent::configureOptions($resolver);
  74.         $resolver->setDefaults([
  75.             'filetypes' => [],
  76.             'limit' => 0,
  77.         ]);
  78.         $resolver->setAllowedTypes('filetypes''array');
  79.         $resolver->setAllowedTypes('limit''int');
  80.     }
  81.     /**
  82.      * {@inheritdoc}
  83.      */
  84.     public function transform($value)
  85.     {
  86.         if ( ! $value instanceof MediaCollection) {
  87.             return Json::encode([]);
  88.         }
  89.         $this->decorator
  90.             ->entities($value)
  91.             ->uploads($value)
  92.             ->meta($value)
  93.             ->urls($value);
  94.         $folder null;
  95.         foreach (($medias $value->toArray()) as $media) {
  96.             if ($media instanceof AbstractLooseMedia && $media->getFile() && $media->getFile()->getFolder()) {
  97.                 $folder $media->getFile()->getFolder()->getId();
  98.             }
  99.         }
  100.         return Json::encode([
  101.             'storage' => 'temporary',
  102.             'location' => $folder,
  103.             'media' => array_map(
  104.                 static function (AbstractMedia $media) {
  105.                     return array_merge(
  106.                         $media->jsonSerialize(),
  107.                         [
  108.                             'uploaded' => true,
  109.                             'processed' => true,
  110.                         ]
  111.                     );
  112.                 },
  113.                 $medias
  114.             )
  115.         ]);
  116.     }
  117.     /**
  118.      * {@inheritdoc}
  119.      */
  120.     public function reverseTransform($value)
  121.     {
  122.         if (empty($value)) {
  123.             return new MediaCollection();
  124.         }
  125.         $value Json::decode($valuetrue);
  126.         if (empty($value)) {
  127.             return new MediaCollection();
  128.         }
  129.         return new MediaCollection($value['media'] ?: []);
  130.     }
  131. }