|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Netgen\Bundle\EnhancedBinaryFileBundle\Form\FieldTypeHandler; |
| 4 | + |
| 5 | +use eZ\Publish\Core\MVC\ConfigResolverInterface; |
| 6 | +use Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler; |
| 7 | +use Symfony\Component\Form\FormBuilderInterface; |
| 8 | +use eZ\Publish\API\Repository\Values\Content\Content; |
| 9 | +use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition; |
| 10 | +use eZ\Publish\SPI\FieldType\Value; |
| 11 | +use Symfony\Component\Validator\Constraints; |
| 12 | +use Symfony\Component\HttpFoundation\File\UploadedFile; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class EnhancedFile |
| 16 | + */ |
| 17 | +class EnhancedFile extends FieldTypeHandler |
| 18 | +{ |
| 19 | + |
| 20 | + /** |
| 21 | + * @param \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver |
| 22 | + */ |
| 23 | + public function __construct( ConfigResolverInterface $configResolver ) |
| 24 | + { |
| 25 | + $this->configResolver = $configResolver; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * {@inheritdoc} |
| 30 | + * |
| 31 | + * @param \eZ\Publish\Core\FieldType\Image\Value $value |
| 32 | + */ |
| 33 | + public function convertFieldValueToForm( Value $value ) |
| 34 | + { |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * {@inheritdoc} |
| 40 | + * |
| 41 | + * @param UploadedFile $data |
| 42 | + */ |
| 43 | + public function convertFieldValueFromForm( $data ) |
| 44 | + { |
| 45 | + if ( $data === null ) |
| 46 | + { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + return array( |
| 51 | + "inputUri" => $data->getFileInfo()->getRealPath(), |
| 52 | + "fileName" => $data->getClientOriginalName(), |
| 53 | + "fileSize" => $data->getSize(), |
| 54 | + "mimeType" => $data->getClientMimeType(), |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * {@inheritdoc} |
| 60 | + */ |
| 61 | + protected function buildFieldForm( |
| 62 | + FormBuilderInterface $formBuilder, |
| 63 | + FieldDefinition $fieldDefinition, |
| 64 | + $languageCode, |
| 65 | + Content $content = null |
| 66 | + ) |
| 67 | + { |
| 68 | + $options = $this->getDefaultFieldOptions( $fieldDefinition, $languageCode, $content ); |
| 69 | + |
| 70 | + $maxFileSize = $fieldDefinition->validatorConfiguration["FileSizeValidator"]["maxFileSize"]; |
| 71 | + $allowedExtensions = $fieldDefinition->fieldSettings['allowedTypes']; |
| 72 | + |
| 73 | + if ( $maxFileSize !== false || !empty( $allowedExtensions ) ) |
| 74 | + { |
| 75 | + $constraints = array(); |
| 76 | + |
| 77 | + if ( $maxFileSize !== false ) |
| 78 | + { |
| 79 | + $constraints['maxSize'] = $maxFileSize; |
| 80 | + } |
| 81 | + |
| 82 | + if ( !empty( $allowedExtensions ) ) |
| 83 | + { |
| 84 | + $allowedExtensions = explode( '|', $allowedExtensions ); |
| 85 | + |
| 86 | + $allowedMimeTypes = array(); |
| 87 | + |
| 88 | + foreach( $allowedExtensions as $allowedExtension ) |
| 89 | + { |
| 90 | + if ( $this->configResolver->hasParameter( "{$allowedExtension}.Types", 'mime' ) ) |
| 91 | + { |
| 92 | + $allowedMimeTypes = array_merge( $allowedMimeTypes, $this->configResolver->getParameter( "{$allowedExtension}.Types", 'mime' ) ); |
| 93 | + } |
| 94 | + } |
| 95 | + $constraints['mimeTypes'] = $allowedMimeTypes; |
| 96 | + } |
| 97 | + |
| 98 | + $options["constraints"][] = new Constraints\File( $constraints ); |
| 99 | + } |
| 100 | + |
| 101 | + // EnhancedBinaryFile should not be erased (updated as empty) if nothing is selected in file input |
| 102 | + $this->skipEmptyUpdate( $formBuilder, $fieldDefinition->identifier ); |
| 103 | + |
| 104 | + $formBuilder->add( $fieldDefinition->identifier, "file", $options ); |
| 105 | + } |
| 106 | +} |
0 commit comments