Skip to content

Commit 01f5693

Browse files
committed
Added information collection handler
1 parent 604dea8 commit 01f5693

File tree

4 files changed

+124
-2
lines changed

4 files changed

+124
-2
lines changed

bundle/DependencyInjection/NetgenEnhancedBinaryFileExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ class NetgenEnhancedBinaryFileExtension extends Extension
1515
public function load(array $configs, ContainerBuilder $container)
1616
{
1717
$configuration = new Configuration();
18-
$config = $this->processConfiguration($configuration, $configs);
18+
$this->processConfiguration($configuration, $configs);
1919

2020
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
2121
$loader->load('fieldtypes.yml');
2222
$loader->load('field_type_handlers.yml');
2323
$loader->load('storage_engines.yml');
2424
$loader->load('mime.yml');
25+
$loader->load('information_collection.yml');
2526
}
2627
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
namespace Netgen\Bundle\EnhancedBinaryFileBundle\FieldHandler;
4+
5+
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
6+
use eZ\Publish\Core\FieldType\Value;
7+
use Netgen\Bundle\EnhancedBinaryFileBundle\Core\FieldType\EnhancedBinaryFile\Value as EnhancedBinaryFileValue;
8+
use eZ\Publish\Core\IO\IOServiceInterface;
9+
use Netgen\Bundle\InformationCollectionBundle\FieldHandler\Custom\CustomLegacyFieldHandlerInterface;
10+
use Netgen\Bundle\InformationCollectionBundle\Value\LegacyData;
11+
use DOMDocument;
12+
13+
class EnhancedBinaryFileHandler implements CustomLegacyFieldHandlerInterface
14+
{
15+
/**
16+
* @var IOServiceInterface
17+
*/
18+
protected $IOService;
19+
20+
/**
21+
* EnhancedBinaryFileHandler constructor.
22+
*
23+
* @param IOServiceInterface $IOService
24+
*/
25+
public function __construct(IOServiceInterface $IOService)
26+
{
27+
$this->IOService = $IOService;
28+
}
29+
30+
/**
31+
* @inheritDoc
32+
*/
33+
public function supports(Value $value)
34+
{
35+
return $value instanceof EnhancedBinaryFileValue;
36+
}
37+
38+
/**
39+
* @inheritDoc
40+
*/
41+
public function toString(Value $value, FieldDefinition $fieldDefinition)
42+
{
43+
return (string)$value;
44+
}
45+
46+
/**
47+
* @inheritDoc
48+
*/
49+
public function getLegacyValue(Value $value, FieldDefinition $fieldDefinition)
50+
{
51+
return new LegacyData(
52+
$fieldDefinition->id,
53+
0,
54+
0,
55+
$this->store($value, $fieldDefinition)
56+
);
57+
}
58+
59+
/**
60+
* Create XML doc string
61+
* and save file to filesystem
62+
*
63+
* @param EnhancedBinaryFileValue $value
64+
* @param FieldDefinition $fieldDefinition
65+
*
66+
* @return string
67+
*/
68+
protected function store(EnhancedBinaryFileValue $value, FieldDefinition $fieldDefinition)
69+
{
70+
$binaryFile = $this->storeBinaryFileToPath($value);
71+
72+
$doc = new DOMDocument( '1.0', 'utf-8' );
73+
$root = $doc->createElement( 'binaryfile-info' );
74+
$binaryFileList = $doc->createElement( 'binaryfile-attributes' );
75+
76+
$fileInfo = [
77+
'Filename' => $binaryFile->uri,
78+
'OriginalFilename' => $value->fileName,
79+
'Size' => $value->fileSize,
80+
];
81+
82+
foreach($fileInfo as $key => $binaryFileItem) {
83+
$binaryFileElement = $doc->createElement($key, $binaryFileItem);
84+
$binaryFileList->appendChild( $binaryFileElement );
85+
}
86+
87+
$root->appendChild($binaryFileList);
88+
$doc->appendChild($root);
89+
90+
return $doc->saveXML();
91+
}
92+
93+
/**
94+
* Stores file to filesystem
95+
*
96+
* @param EnhancedBinaryFileValue $value
97+
* @param string $storagePrefix
98+
*
99+
* @return \eZ\Publish\Core\IO\Values\BinaryFile
100+
*/
101+
protected function storeBinaryFileToPath(EnhancedBinaryFileValue $value, $storagePrefix = '/original/collected/')
102+
{
103+
$binaryCreateStruct = $this->IOService
104+
->newBinaryCreateStructFromLocalFile($value->inputUri);
105+
$binaryCreateStruct->id = $storagePrefix . $value->fileName;
106+
107+
$binaryFile = $this->IOService->createBinaryFile($binaryCreateStruct);
108+
109+
return $binaryFile;
110+
}
111+
}
112+
113+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
ezpublish.enhancedezbinaryfile.information_collection.field_handler:
3+
class: Netgen\Bundle\EnhancedBinaryFileBundle\FieldHandler\EnhancedBinaryFileHandler
4+
arguments:
5+
- "@ezpublish.core.io.service"
6+
tags:
7+
- { name: netgen_information_collection.field_handler.custom }

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
}
1818
],
1919
"require": {
20-
"ezsystems/ezpublish-kernel": "*"
20+
"ezsystems/ezpublish-kernel": "*",
21+
"netgen/information-collection-bundle": "^1.1"
2122
},
2223
"require-dev": {
2324
"ezsystems/ezpublish-kernel": "~6.0",

0 commit comments

Comments
 (0)