FileAjaxType::buildView()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 19
nc 4
nop 3
1
<?php
2
3
/**
4
 * Copyright 2014 Jonathan Bouzekri. All rights reserved.
5
 *
6
 * @copyright Copyright 2014 Jonathan Bouzekri <[email protected]>
7
 * @license https://github.com/jbouzekri/FileUploaderBundle/blob/master/LICENSE
8
 * @link https://github.com/jbouzekri/FileUploaderBundle
9
 */
10
11
namespace Jb\Bundle\FileUploaderBundle\Form\Type;
12
13
use Symfony\Component\Form\AbstractType;
14
use Symfony\Component\Form\Extension\Core\Type\TextType;
15
use Symfony\Component\OptionsResolver\OptionsResolver;
16
use Symfony\Component\Form\FormView;
17
use Symfony\Component\Form\FormInterface;
18
use Jb\Bundle\FileUploaderBundle\Service\FileHistoryManagerInterface;
19
20
/**
21
 * FileAjaxType
22
 */
23
class FileAjaxType extends AbstractType
24
{
25
    /**
26
     * @var FileHistoryManagerInterface
27
     */
28
    protected $fileHistoryManager;
29
    
30
    /**
31
     * Constructor
32
     *
33
     * @param FileHistoryManagerInterface $fileHistoryManager
34
     */
35
    public function __construct(FileHistoryManagerInterface $fileHistoryManager)
36
    {
37
        $this->fileHistoryManager = $fileHistoryManager;
38
    }
39
    
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function configureOptions(OptionsResolver $resolver)
44
    {
45
        // Endpoint mandatory for fileupload bundle
46
        $resolver->setRequired(array('endpoint'));
47
        
48
        $resolver->setDefined(array(
49
            'download_link',
50
            'remove_link',
51
            'loading_file',
52
            'resolver_key',
53
            'progress'
54
        ));
55
        
56
        $resolver->setDefaults(array(
57
            'download_link' => true,
58
            'remove_link' => true,
59
            'loading_file' => 'bundles/jbfileuploader/img/ajax-loader-small.gif',
60
            'resolver_key' => 'upload_resolver',
61
            'progress'=>false
62
        ));
63
    }
64
    
65
    /**
66
     * {@inheritDoc}
67
     */
68
    public function buildView(FormView $view, FormInterface $form, array $options)
69
    {
70
        $fileHistory = null;
71
        $fileHistoryUrl = null;
72
        if ($form->getData() !== null) {
73
            $fileHistory = $this->fileHistoryManager->findOneByFileName($form->getData());
74
            $fileHistoryUrl = $this->fileHistoryManager->getUrl($fileHistory, $options['resolver_key']);
75
        }
76
        
77
        $className = 'jb_result_filename';
78
        if (isset($view->vars['attr']['class'])) {
79
            $view->vars['attr']['class'] .= ' ' . $className;
80
        } else {
81
            $view->vars['attr']['class'] = $className;
82
        }
83
        
84
        $view->vars['file_history'] = $fileHistory;
85
        $view->vars['file_history_url'] = $fileHistoryUrl;
86
        $view->vars['endpoint'] = $options['endpoint'];
87
        $view->vars['download_link'] = $options['download_link'];
88
        $view->vars['remove_link'] = $options['remove_link'];
89
        $view->vars['loading_file'] = $options['loading_file'];
90
        $view->vars['progress'] = $options['progress'];
91
        $view->vars['use_crop'] = false;
92
    }
93
    
94
    /**
95
     * {@inheritDoc}
96
     */
97
    public function getParent()
98
    {
99
        return TextType::class;
100
    }
101
}
102