AttachFileType::getBlockPrefix()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace ItBlaster\AttachFileBundle\Form\Type;
4
5
6
use Symfony\Component\Form\Extension\Core\Type\FileType;
7
use Symfony\Component\Form\FormBuilderInterface;
8
use Symfony\Component\Form\FormInterface;
9
use Symfony\Component\Form\FormView;
10
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
11
12
/**
13
 * Виджет "Файл с информацией для объектов i18n"
14
 *
15
 * Class AttachFileType
16
 * @package ItBlaster\AttachFileBundle\Form\Type
17
 */
18
class AttachFileType extends FileType
19
{
20
    /**
21
     * @param OptionsResolverInterface $resolver
22
     */
23
    public function setDefaultOptions(OptionsResolverInterface $resolver)
24
    {
25
        $resolver->setDefaults(array(
26
            'compound'    => false,
27
            'data_class'  => 'Symfony\Component\HttpFoundation\File\File',
28
            'empty_data'  => null,
29
            'multiple'    => false,
30
            'object'      => false,
31
            'sonata_help' => 'Допустимые типы файлов: pdf, doc, docx, zip, jpg, gif, png',
32
            'constraints' => [
33
                new \Symfony\Component\Validator\Constraints\File([
34
                    'mimeTypes' => [
35
                        'application/pdf',
36
                        'application/msword',
37
                        'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
38
                        'application/vnd.oasis.opendocument.text',
39
                        'application/zip',
40
                        'image/gif',
41
                        'image/jpeg',
42
                        'image/pjpeg',
43
                        'image/png'
44
                    ]
45
                ])
46
            ]
47
        ));
48
    }
49
50
51
    /**
52
     * @param FormView      $view
53
     * @param FormInterface $form
54
     * @param array         $options
55
     */
56
    public function buildView(FormView $view, FormInterface $form, array $options)
57
    {
58
        if ($options['multiple']) {
59
            $view->vars['full_name'] .= '[]';
60
            $view->vars['attr']['multiple'] = 'multiple';
61
        }
62
63
        $view->vars = array_replace($view->vars, array(
64
            'type'  => 'file',
65
            'value' => '',
66
        ));
67
68
        $field = substr($view->vars['name'], 0, -5);
69
        $object_i18n = $view->parent->vars['data'];
70
        $attach_file = $object_i18n->getFileObject($field);
71
        $view->vars['options'] = $options;
72
        if (!$attach_file->isNew() && $attach_file->issetFile()) {
73
            $view->vars['object'] = $object_i18n;
74
            $view->vars['attach_file'] = $attach_file;
75
        } else {
76
            $view->vars['object'] = false;
77
            $view->vars['attach_file'] = false;
78
        }
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getName()
85
    {
86
        return 'attach_file';
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function getBlockPrefix()
93
    {
94
        return 'attach_file';
95
    }
96
}