Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: introduce ibexa 3 compatibility #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions bundle/Controller/DownloadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Netgen\Bundle\EnhancedBinaryFileBundle\Core\FieldType\DataTransformer;

use eZ\Publish\API\Repository\FieldType;
use eZ\Publish\Core\FieldType\Value;
use EzSystems\EzPlatformContentForms\FieldType\DataTransformer\AbstractBinaryBaseTransformer;
use EzSystems\EzPlatformContentForms\FieldType\DataTransformer\BinaryFileValueTransformer;
use Symfony\Component\Form\DataTransformerInterface;

class EnhancedBinaryFileValueTransformer extends BinaryFileValueTransformer
{
/**
* @param FieldType $fieldType
* @param Value|null $initialValue
* @param string $valueClass
*/
public function __construct(FieldType $fieldType, ?Value $initialValue, $valueClass)
{
$this->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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
66 changes: 66 additions & 0 deletions bundle/Core/FieldType/EnhancedBinaryFile/FieldValueFormMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Netgen\Bundle\EnhancedBinaryFileBundle\Core\FieldType\EnhancedBinaryFile;

use eZ\Publish\API\Repository\FieldTypeService;
use eZ\Publish\Core\MVC\ConfigResolverInterface;
use EzSystems\EzPlatformContentForms\Data\Content\FieldData;
use EzSystems\EzPlatformContentForms\FieldType\DataTransformer\BinaryFileValueTransformer;
use EzSystems\EzPlatformContentForms\FieldType\Mapper\BinaryFileFormMapper;
use Netgen\Bundle\EnhancedBinaryFileBundle\Core\FieldType\DataTransformer\EnhancedBinaryFileValueTransformer;
use Netgen\Bundle\EnhancedBinaryFileBundle\Form\Type\EnhancedBinaryFileFieldType;
use Symfony\Component\Form\FormInterface;
use Netgen\Bundle\EnhancedBinaryFileBundle\Core\FieldType\EnhancedBinaryFile\Value as FieldValue;

class FieldValueFormMapper extends BinaryFileFormMapper
{
/** @var FieldTypeService */
private $fieldTypeService;

private $configResolver;

public function __construct(FieldTypeService $fieldTypeService, ConfigResolverInterface $configResolver)
{
$this->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()
);
}
}
4 changes: 2 additions & 2 deletions bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
29 changes: 18 additions & 11 deletions bundle/DependencyInjection/NetgenEnhancedBinaryFileExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));

}

/**
Expand All @@ -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');
Expand Down
12 changes: 5 additions & 7 deletions bundle/FieldHandler/EnhancedBinaryFileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -30,28 +30,26 @@ public function __construct(IOServiceInterface $IOService)
/**
* {@inheritdoc}
*/
public function supports(Value $value)
public function supports(Value $value): bool
{
return $value instanceof EnhancedBinaryFileValue;
}

/**
* {@inheritdoc}
*/
public function toString(Value $value, FieldDefinition $fieldDefinition)
public function toString(Value $value, FieldDefinition $fieldDefinition): string
{
return (string) $value;
}

/**
* {@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)
);
}
Expand Down
Loading