diff --git a/bundle/Controller/DownloadController.php b/bundle/Controller/DownloadController.php index a30a714..3786080 100644 --- a/bundle/Controller/DownloadController.php +++ b/bundle/Controller/DownloadController.php @@ -2,42 +2,38 @@ namespace Netgen\Bundle\EnhancedBinaryFileBundle\Controller; +use eZ\Bundle\EzPublishCoreBundle\Controller; use eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse; +use eZ\Publish\API\Repository\Repository; use eZ\Publish\Core\IO\IOServiceInterface; -use eZ\Publish\Core\SignalSlot\Repository; -use Netgen\Bundle\InformationCollectionBundle\Entity\EzInfoCollection; -use Netgen\Bundle\InformationCollectionBundle\Entity\EzInfoCollectionAttribute; -use Netgen\Bundle\InformationCollectionBundle\Repository\EzInfoCollectionAttributeRepository; -use Netgen\Bundle\InformationCollectionBundle\Repository\EzInfoCollectionRepository; -use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait; +use Netgen\InformationCollection\Doctrine\Entity\EzInfoCollection; +use Netgen\InformationCollection\Doctrine\Entity\EzInfoCollectionAttribute; +use Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionAttributeRepository; +use Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionRepository; +use Symfony\Component\DependencyInjection\ContainerAwareInterface; +use Symfony\Component\DependencyInjection\ContainerAwareTrait; use Symfony\Component\HttpFoundation\ResponseHeaderBag; +use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Security\Core\Exception\AccessDeniedException; -class DownloadController +class DownloadController extends Controller { - use ControllerTrait; - private $infocollectionAttributeRepository; - private $infocollectionRepository; - private $ioService; - private $repository; - public function __construct( EzInfoCollectionAttributeRepository $infocollectionAttributeRepository, EzInfoCollectionRepository $infocollectionRepository, - IOServiceInterface $ioService, - Repository $repository + IOServiceInterface $ioService ) { $this->infocollectionAttributeRepository = $infocollectionAttributeRepository; $this->infocollectionRepository = $infocollectionRepository; $this->ioService = $ioService; - $this->repository = $repository; } /** + * @Route(methods={"GET"}, name="netgen_enhancedezbinaryfile.route.download_binary_file", path="/netgen/enhancedezbinaryfile/download/{infocollectionAttributeId}") * @param int $infocollectionAttributeId * * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue @@ -60,9 +56,9 @@ public function downloadCollectedEnhancedEzBinaryFileAction($infocollectionAttri $infocollection = $this->infocollectionRepository->find($infocollectionAttribute->getInformationCollectionId()); $contentId = $infocollection->getContentObjectId(); - $content = $this->repository->getContentService()->loadContent($contentId); + $content = $this->getRepository()->getContentService()->loadContent($contentId); - if (!$this->repository->getPermissionResolver()->canUser('infocollector', 'read', $content)) { + if (!$this->getRepository()->getPermissionResolver()->canUser('infocollector', 'read', $content)) { throw new AccessDeniedException('Access denied.'); } diff --git a/bundle/Core/FieldType/DataTransformer/EnhancedBinaryFileValueTransformer.php b/bundle/Core/FieldType/DataTransformer/EnhancedBinaryFileValueTransformer.php new file mode 100644 index 0000000..de6d077 --- /dev/null +++ b/bundle/Core/FieldType/DataTransformer/EnhancedBinaryFileValueTransformer.php @@ -0,0 +1,33 @@ +fieldType = $fieldType; + $this->initialValue = $initialValue; + $this->valueClass = $valueClass; + } + + public function reverseTransform($value) + { + if (null === $value['file']) { + return null; + } + + return parent::reverseTransform($value); // TODO: Change the autogenerated stub + } +} \ No newline at end of file diff --git a/bundle/Core/FieldType/EnhancedBinaryFile/FormMapper.php b/bundle/Core/FieldType/EnhancedBinaryFile/FieldTypeFormMapper.php similarity index 82% rename from bundle/Core/FieldType/EnhancedBinaryFile/FormMapper.php rename to bundle/Core/FieldType/EnhancedBinaryFile/FieldTypeFormMapper.php index c047769..5302e78 100644 --- a/bundle/Core/FieldType/EnhancedBinaryFile/FormMapper.php +++ b/bundle/Core/FieldType/EnhancedBinaryFile/FieldTypeFormMapper.php @@ -2,14 +2,14 @@ namespace Netgen\Bundle\EnhancedBinaryFileBundle\Core\FieldType\EnhancedBinaryFile; -use EzSystems\RepositoryForms\Data\FieldDefinitionData; -use EzSystems\RepositoryForms\FieldType\Mapper\BinaryFileFormMapper; +use EzSystems\EzPlatformAdminUi\FieldType\Mapper\BinaryFileFormMapper; +use EzSystems\EzPlatformAdminUi\Form\Data\FieldDefinitionData; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormInterface; -class FormMapper extends BinaryFileFormMapper +class FieldTypeFormMapper extends BinaryFileFormMapper { - public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, FieldDefinitionData $data) + public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, FieldDefinitionData $data): void { parent::mapFieldDefinitionForm($fieldDefinitionForm, $data); diff --git a/bundle/Core/FieldType/EnhancedBinaryFile/FieldValueFormMapper.php b/bundle/Core/FieldType/EnhancedBinaryFile/FieldValueFormMapper.php new file mode 100644 index 0000000..7852f6f --- /dev/null +++ b/bundle/Core/FieldType/EnhancedBinaryFile/FieldValueFormMapper.php @@ -0,0 +1,66 @@ +fieldTypeService = $fieldTypeService; + $this->configResolver = $configResolver; + } + + public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data) + { + $fieldDefinition = $data->fieldDefinition; + $formConfig = $fieldForm->getConfig(); + $fieldType = $this->fieldTypeService->getFieldType($fieldDefinition->fieldTypeIdentifier); + $allowedFileExtensions = $fieldDefinition->fieldSettings['allowedTypes'] ?? []; + $mimeTypesMessage = $fieldDefinition->fieldSettings['mimeTypesMessage'] ?? null; + + $allowedMimeTypes = []; + if (!empty($allowedFileExtensions)) { + $allowedExtensions = explode('|', $allowedFileExtensions); + + foreach ($allowedExtensions as $allowedExtension) { + if ($this->configResolver->hasParameter("{$allowedExtension}.Types", 'mime')) { + $allowedMimeTypes = array_merge($allowedMimeTypes, $this->configResolver->getParameter("{$allowedExtension}.Types", 'mime')); + } + } + + } + + $fieldForm + ->add( + $formConfig->getFormFactory()->createBuilder() + ->create( + 'value', + EnhancedBinaryFileFieldType::class, + [ + 'required' => $fieldDefinition->isRequired, + 'label' => $fieldDefinition->getName(), + 'mime_types' => array_unique($allowedMimeTypes), + 'mime_types_message' => $mimeTypesMessage + ] + ) + ->addModelTransformer(new EnhancedBinaryFileValueTransformer($fieldType, $data->value, FieldValue::class)) + ->setAutoInitialize(false) + ->getForm() + ); + } +} \ No newline at end of file diff --git a/bundle/DependencyInjection/Configuration.php b/bundle/DependencyInjection/Configuration.php index 4a7ec0c..7a57236 100644 --- a/bundle/DependencyInjection/Configuration.php +++ b/bundle/DependencyInjection/Configuration.php @@ -12,8 +12,8 @@ class Configuration implements ConfigurationInterface */ public function getConfigTreeBuilder() { - $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('netgen_enhanced_ez_binary_file'); + $treeBuilder = new TreeBuilder("netgen_enhanced_ez_binary_file"); + $rootNode = $treeBuilder->getRootNode(); return $treeBuilder; } diff --git a/bundle/DependencyInjection/NetgenEnhancedBinaryFileExtension.php b/bundle/DependencyInjection/NetgenEnhancedBinaryFileExtension.php index 82c1a26..267a78a 100644 --- a/bundle/DependencyInjection/NetgenEnhancedBinaryFileExtension.php +++ b/bundle/DependencyInjection/NetgenEnhancedBinaryFileExtension.php @@ -2,6 +2,7 @@ namespace Netgen\Bundle\EnhancedBinaryFileBundle\DependencyInjection; +use Netgen\Bundle\EnhancedBinaryFileBundle\NetgenEnhancedBinaryFileBundle; use Symfony\Component\Config\FileLocator; use Symfony\Component\Config\Resource\FileResource; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -13,21 +14,27 @@ class NetgenEnhancedBinaryFileExtension extends Extension implements PrependExtensionInterface { /** - * Preprend ezpublish configuration to make the field templates + * Preprend ezplatform configuration to make the field templates * visibile to the admin template engine. * * @param ContainerBuilder $container */ public function prepend(ContainerBuilder $container) { - if (class_exists(\EzSystems\RepositoryFormsBundle\EzSystemsRepositoryFormsBundle::class)) { - $fileName = 'ez_field_templates.yml'; - $configFile = __DIR__ . '/../Resources/config/' . $fileName; - $config = Yaml::parse(file_get_contents($configFile)); + $refl = new \ReflectionClass(NetgenEnhancedBinaryFileBundle::class); + $path = \dirname($refl->getFileName()).'/Resources/views'; - $container->prependExtensionConfig('ezpublish', $config); - $container->addResource(new FileResource($configFile)); - } + $container->prependExtensionConfig('twig', ['paths' => [ + $path => 'NetgenEnhancedBinaryFileBundle' + ]]); + + $fileName = 'ez_field_templates.yml'; + $configFile = __DIR__ . '/../Resources/config/' . $fileName; + $config = Yaml::parse(file_get_contents($configFile)); + + $container->prependExtensionConfig('ezpublish', $config); + $container->addResource(new FileResource($configFile)); + } /** @@ -45,9 +52,9 @@ public function load(array $configs, ContainerBuilder $container) $loader->load('fieldtypes_before_611.yml'); } - if (class_exists(\EzSystems\RepositoryFormsBundle\EzSystemsRepositoryFormsBundle::class)) { - $loader->load('repository_forms.yml'); - } + + $loader->load('repository_forms.yml'); + $loader->load('fieldtypes.yml'); $loader->load('field_type_handlers.yml'); $loader->load('storage_engines.yml'); diff --git a/bundle/FieldHandler/EnhancedBinaryFileHandler.php b/bundle/FieldHandler/EnhancedBinaryFileHandler.php index 6189e27..2a1aae6 100644 --- a/bundle/FieldHandler/EnhancedBinaryFileHandler.php +++ b/bundle/FieldHandler/EnhancedBinaryFileHandler.php @@ -7,8 +7,8 @@ use eZ\Publish\Core\FieldType\Value; use eZ\Publish\Core\IO\IOServiceInterface; use Netgen\Bundle\EnhancedBinaryFileBundle\Core\FieldType\EnhancedBinaryFile\Value as EnhancedBinaryFileValue; -use Netgen\Bundle\InformationCollectionBundle\FieldHandler\Custom\CustomLegacyFieldHandlerInterface; -use Netgen\Bundle\InformationCollectionBundle\Value\LegacyData; +use Netgen\InformationCollection\API\FieldHandler\CustomLegacyFieldHandlerInterface; +use Netgen\InformationCollection\API\Value\Legacy\FieldValue as LegacyData; class EnhancedBinaryFileHandler implements CustomLegacyFieldHandlerInterface { @@ -30,7 +30,7 @@ public function __construct(IOServiceInterface $IOService) /** * {@inheritdoc} */ - public function supports(Value $value) + public function supports(Value $value): bool { return $value instanceof EnhancedBinaryFileValue; } @@ -38,7 +38,7 @@ public function supports(Value $value) /** * {@inheritdoc} */ - public function toString(Value $value, FieldDefinition $fieldDefinition) + public function toString(Value $value, FieldDefinition $fieldDefinition): string { return (string) $value; } @@ -46,12 +46,10 @@ public function toString(Value $value, FieldDefinition $fieldDefinition) /** * {@inheritdoc} */ - public function getLegacyValue(Value $value, FieldDefinition $fieldDefinition) + public function getLegacyValue(Value $value, FieldDefinition $fieldDefinition): LegacyData { return new LegacyData( $fieldDefinition->id, - 0, - 0, $this->store($value, $fieldDefinition) ); } diff --git a/bundle/Form/FieldTypeHandler/EnhancedFile.php b/bundle/Form/FieldTypeHandler/EnhancedFile.php deleted file mode 100644 index b08cef1..0000000 --- a/bundle/Form/FieldTypeHandler/EnhancedFile.php +++ /dev/null @@ -1,105 +0,0 @@ -configResolver = $configResolver; - } - - /** - * {@inheritdoc} - */ - public function convertFieldValueToForm(Value $value, FieldDefinition $fieldDefinition = null) - { - return null; - } - - /** - * {@inheritdoc} - * - * @param UploadedFile $data - */ - public function convertFieldValueFromForm($data) - { - if (null === $data) { - return null; - } - - return new EnhancedFileValue( - [ - 'path' => $data->getFileInfo()->getRealPath(), - 'fileName' => $data->getClientOriginalName(), - 'fileSize' => $data->getSize(), - 'mimeType' => $data->getClientMimeType(), - ] - ); - } - - /** - * {@inheritdoc} - */ - protected function buildFieldForm( - FormBuilderInterface $formBuilder, - FieldDefinition $fieldDefinition, - $languageCode, - Content $content = null - ) { - $options = $this->getDefaultFieldOptions($fieldDefinition, $languageCode, $content); - - $maxFileSize = $fieldDefinition->validatorConfiguration['FileSizeValidator']['maxFileSize']; - $allowedExtensions = $fieldDefinition->fieldSettings['allowedTypes']; - $mimeTypesMessage = $fieldDefinition->fieldSettings['mimeTypesMessage']; - - if (null !== $maxFileSize || !empty($allowedExtensions)) { - $constraints = []; - - if (null !== $maxFileSize && !empty($maxFileSize)) { - $constraints['maxSize'] = (string) $maxFileSize . 'M'; - } - - if (!empty($allowedExtensions)) { - $allowedExtensions = explode('|', $allowedExtensions); - - $allowedMimeTypes = []; - - foreach ($allowedExtensions as $allowedExtension) { - if ($this->configResolver->hasParameter("{$allowedExtension}.Types", 'mime')) { - $allowedMimeTypes = array_merge($allowedMimeTypes, $this->configResolver->getParameter("{$allowedExtension}.Types", 'mime')); - } - } - $constraints['mimeTypes'] = $allowedMimeTypes; - } - - if (!empty($mimeTypesMessage)) { - $constraints['mimeTypesMessage'] = $mimeTypesMessage; - } - - $options['constraints'][] = new Constraints\File($constraints); - } - - // EnhancedBinaryFile should not be erased (updated as empty) if nothing is selected in file input - $this->skipEmptyUpdate($formBuilder, $fieldDefinition->identifier); - - $formBuilder->add($fieldDefinition->identifier, FileType::class, $options); - } -} diff --git a/bundle/Form/Type/EnhancedBinaryFileFieldType.php b/bundle/Form/Type/EnhancedBinaryFileFieldType.php new file mode 100644 index 0000000..1d8f632 --- /dev/null +++ b/bundle/Form/Type/EnhancedBinaryFileFieldType.php @@ -0,0 +1,71 @@ +maxUploadSize = $maxUploadSize; + } + + public function getName() + { + return $this->getBlockPrefix(); + } + + public function getBlockPrefix() + { + return 'ezplatform_fieldtype_enhancedbinaryfile'; + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults( + [ + 'translation_domain' => 'ezplatform_content_forms_fieldtype', + 'mime_types' => [], + 'mime_types_message' => null + ] + ); + } + + public function buildForm(FormBuilderInterface $builder, array $options) + { + $constraints = ['maxSize' => $this->maxUploadSize->get()]; + + if ($options['mime_types']) + { + $constraints['mimeTypes'] = $options['mime_types']; + if ($options['mime_types_message']) + { + $constraints['mimeTypesMessage'] = $options['mime_types_message']; + } + } + + $builder + ->add( + 'file', + FileType::class, + [ + 'label' => false, + 'required' => $options['required'], + 'constraints' => [ + new Assert\File( + $constraints + ), + ], + ] + ); + } +} \ No newline at end of file diff --git a/bundle/Resources/config/ez_field_templates.yml b/bundle/Resources/config/ez_field_templates.yml index 6413a57..7d6388e 100644 --- a/bundle/Resources/config/ez_field_templates.yml +++ b/bundle/Resources/config/ez_field_templates.yml @@ -1,6 +1,8 @@ system: default: fielddefinition_edit_templates: - - template: "NetgenEnhancedBinaryFileBundle:admin:field_types.html.twig" + - + template: "@NetgenEnhancedBinaryFileBundle/admin/field_types.html.twig" field_templates: - - template: "NetgenEnhancedBinaryFileBundle::content_fields.html.twig" + - + template: "@NetgenEnhancedBinaryFileBundle/content_fields.html.twig" diff --git a/bundle/Resources/config/fieldtypes.yml b/bundle/Resources/config/fieldtypes.yml index 73c518d..c0c0d56 100644 --- a/bundle/Resources/config/fieldtypes.yml +++ b/bundle/Resources/config/fieldtypes.yml @@ -3,7 +3,7 @@ services: class: Netgen\Bundle\EnhancedBinaryFileBundle\Core\FieldType\EnhancedBinaryFile\Type parent: ezpublish.fieldType arguments: - - "@ezpublish.core.io.mimetypedetector" + - "@ezpublish.core.io.mimeTypeDetector" - "@ezpublish.config.resolver" tags: - { name: ezpublish.fieldType, alias: enhancedezbinaryfile } diff --git a/bundle/Resources/config/information_collection.yml b/bundle/Resources/config/information_collection.yml index 056b0a6..5499a35 100644 --- a/bundle/Resources/config/information_collection.yml +++ b/bundle/Resources/config/information_collection.yml @@ -1,6 +1,6 @@ services: - ezpublish.enhancedezbinaryfile.information_collection.field_handler: - class: Netgen\Bundle\EnhancedBinaryFileBundle\FieldHandler\EnhancedBinaryFileHandler + Netgen\Bundle\EnhancedBinaryFileBundle\FieldHandler\EnhancedBinaryFileHandler: + autowire: true arguments: - "@ezpublish.core.io.service" tags: diff --git a/bundle/Resources/config/repository_forms.yml b/bundle/Resources/config/repository_forms.yml index 476aa2e..bfdb8f8 100644 --- a/bundle/Resources/config/repository_forms.yml +++ b/bundle/Resources/config/repository_forms.yml @@ -1,8 +1,17 @@ services: - ezpublish.fieldType.enhancedezbinaryfile.form_mapper: - class: Netgen\Bundle\EnhancedBinaryFileBundle\Core\FieldType\EnhancedBinaryFile\FormMapper - arguments: - - "@ezpublish.api.service.field_type" - - "@ezrepoforms.config_resolver.max_upload_size" + _defaults: + autowire: true + autoconfigure: true + public: false + + Netgen\Bundle\EnhancedBinaryFileBundle\Core\FieldType\EnhancedBinaryFile\FieldTypeFormMapper: tags: - - { name: ez.fieldFormMapper.definition, fieldType: enhancedezbinaryfile } + - { name: ezplatform.field_type.form_mapper.definition, fieldType: enhancedezbinaryfile } + + Netgen\Bundle\EnhancedBinaryFileBundle\Core\FieldType\EnhancedBinaryFile\FieldValueFormMapper: + tags: + - { name: ezplatform.field_type.form_mapper.value, fieldType: enhancedezbinaryfile } + + Netgen\Bundle\EnhancedBinaryFileBundle\Form\Type\EnhancedBinaryFileFieldType: + tags: + - { name: 'form.type' } \ No newline at end of file diff --git a/bundle/Resources/config/routing.yml b/bundle/Resources/config/routing.yml index c956d4b..8ec05b5 100644 --- a/bundle/Resources/config/routing.yml +++ b/bundle/Resources/config/routing.yml @@ -1,5 +1,3 @@ -netgen_enhancedezbinaryfile.route.download_binary_file: - path: /netgen/enhancedezbinaryfile/download/{infocollectionAttributeId} - defaults: { _controller: netgen_enhancedezbinaryfile.controller.download:downloadCollectedEnhancedEzBinaryFileAction } - methods: [GET] - +netgen_enhancedy_binary_file_controllers: + resource: ../../Controller/ + type: annotation diff --git a/bundle/Resources/config/services.yml b/bundle/Resources/config/services.yml index 6ba2b2f..8b87543 100644 --- a/bundle/Resources/config/services.yml +++ b/bundle/Resources/config/services.yml @@ -1,9 +1,10 @@ services: - netgen_enhancedezbinaryfile.controller.download: - class: Netgen\Bundle\EnhancedBinaryFileBundle\Controller\DownloadController + Netgen\Bundle\EnhancedBinaryFileBundle\Controller\DownloadController: + autoconfigure: true + tags: [controller.service_arguments] + calls: + - [ setContainer, [ "@service_container" ] ] arguments: - "@netgen_information_collection.repository.ez_info_collection_attribute" - "@netgen_information_collection.repository.ez_info_collection" - "@ezpublish.fieldType.ezbinaryfile.io_service" - - "@ezpublish.api.repository" - diff --git a/bundle/Resources/views/admin/field_types.html.twig b/bundle/Resources/views/admin/field_types.html.twig index 10def7b..2ff7a80 100644 --- a/bundle/Resources/views/admin/field_types.html.twig +++ b/bundle/Resources/views/admin/field_types.html.twig @@ -1,20 +1,11 @@ -{% extends 'EzSystemsRepositoryFormsBundle:ContentType:field_types.html.twig' %} {% block enhancedezbinaryfile_field_definition_edit %}
- {{- form_label(form.maxSize) -}} - {{- form_errors(form.maxSize) -}} - {{- form_widget(form.maxSize) -}} + {{- form_row(form.maxSize, {'label_attr': {'class': 'ez-label'}}) -}}
-
- {{- form_label(form.allowedTypes) -}} - {{- form_errors(form.allowedTypes) -}} - {{- form_widget(form.allowedTypes) -}} + {{- form_row(form.allowedTypes, {'label_attr': {'class': 'ez-label'}}) -}}
-
- {{- form_label(form.mimeTypesMessage) -}} - {{- form_errors(form.mimeTypesMessage) -}} - {{- form_widget(form.mimeTypesMessage) -}} + {{- form_row(form.mimeTypesMessage, {'label_attr': {'class': 'ez-label'}}) -}}
-{% endblock %} +{% endblock %} \ No newline at end of file diff --git a/bundle/Resources/views/content_fields.html.twig b/bundle/Resources/views/content_fields.html.twig index 7280ca7..fa09ac9 100644 --- a/bundle/Resources/views/content_fields.html.twig +++ b/bundle/Resources/views/content_fields.html.twig @@ -1,4 +1,4 @@ -{% extends "EzPublishCoreBundle::content_fields.html.twig" %} +{% extends "@EzPublishCore/content_fields.html.twig" %} {% block enhancedbinaryfile_field %} {{ block('ezbinaryfile_field') }} {% endblock %} diff --git a/composer.json b/composer.json index 262265c..34ce0ca 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ ], "require": { "ezsystems/ezpublish-kernel": "*", - "netgen/information-collection-bundle": "^1.1" + "netgen/information-collection-bundle": "^2.0" }, "require-dev": { "ezsystems/ezpublish-kernel": "~6.0",