forked from ezsystems/repository-forms
/
BinaryBaseFieldType.php
84 lines (75 loc) · 2.44 KB
/
BinaryBaseFieldType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\RepositoryForms\Form\Type\FieldType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Parent Form Type for binary file based field types.
*/
class BinaryBaseFieldType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'remove',
CheckboxType::class,
[
'label' => /** @Desc("Remove") */ 'content.field_type.binary_base.remove',
]
)
->add(
'file',
FileType::class,
[
'label' => /** @Desc("File") */ 'content.field_type.binary_base.file',
'required' => $options['required'],
'constraints' => [
new Assert\File([
'maxSize' => $this->getMaxUploadSize(),
]),
],
]
);
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['max_upload_size'] = $this->getMaxUploadSize();
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(['translation_domain' => 'ezrepoforms_fieldtype']);
}
private function getMaxUploadSize()
{
static $value = null;
if ($value === null) {
$value = $this->str2bytes(ini_get('upload_max_filesize'));
}
return $value;
}
private function str2bytes($str)
{
$str = strtoupper(trim($str));
$value = substr($str, 0, -1);
$unit = substr($str, -1);
switch ($unit) {
case 'G':
$value *= 1024;
case 'M':
$value *= 1024;
case 'K':
$value *= 1024;
}
return (int) $value;
}
}