Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

FormBundle/Entity/PageParts/FileUploadPagePart.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\FormBundle\Entity\PageParts;
4
5
use ArrayObject;
6
use Doctrine\ORM\Mapping as ORM;
7
use Kunstmaan\FormBundle\Entity\FormSubmissionFieldTypes\FileFormSubmissionField;
8
use Kunstmaan\FormBundle\Form\FileFormSubmissionType;
9
use Kunstmaan\FormBundle\Form\FileUploadPagePartAdminType;
10
use Symfony\Component\Form\FormBuilderInterface;
11
use Symfony\Component\Validator\Constraints\NotBlank;
12
13
/**
14
 * The file upload page part can be used to create forms with the possibility to upload files
15
 *
16
 * @ORM\Entity
17
 * @ORM\HasLifecycleCallbacks
18
 * @ORM\Table(name="kuma_file_upload_page_parts")
19
 */
20 View Code Duplication
class FileUploadPagePart extends AbstractFormPagePart
0 ignored issues
show
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
{
22
    /**
23
     * If set to true, you are obligated to fill in this page part
24
     *
25
     * @ORM\Column(type="boolean", nullable=true)
26
     */
27
    protected $required = false;
28
29
    /**
30
     * Error message shows when the page part is required and nothing is filled in
31
     *
32
     * @ORM\Column(type="string", name="error_message_required", nullable=true)
33
     */
34
    protected $errorMessageRequired;
35
36
    /**
37
     * Modify the form with the fields of the current page part
38
     *
39
     * @param FormBuilderInterface $formBuilder The form builder
40
     * @param ArrayObject          $fields      The fields
41
     * @param int                  $sequence    The sequence of the form field
42
     */
43
    public function adaptForm(FormBuilderInterface $formBuilder, ArrayObject $fields, $sequence)
44
    {
45
        $ffsf = new FileFormSubmissionField();
46
        $ffsf->setFieldName('field_' . $this->getUniqueId());
47
        $ffsf->setLabel($this->getLabel());
48
        $ffsf->setSequence($sequence);
49
50
        $data = $formBuilder->getData();
51
        $data['formwidget_' . $this->getUniqueId()] = $ffsf;
52
53
        $constraints = array();
54
        if ($this->getRequired()) {
55
            $options = array();
56
            if (!empty($this->errorMessageRequired)) {
57
                $options['message'] = $this->errorMessageRequired;
58
            }
59
            $constraints[] = new NotBlank($options);
60
        }
61
62
        $formBuilder->add(
63
            'formwidget_' . $this->getUniqueId(),
64
            FileFormSubmissionType::class,
65
            array(
66
                'label' => $this->getLabel(),
67
                'constraints' => $constraints,
68
                'required' => $this->getRequired(),
69
            )
70
        );
71
        $formBuilder->setData($data);
72
73
        $fields->append($ffsf);
74
    }
75
76
    /**
77
     * Sets the required valud of this page part
78
     *
79
     * @param bool $required
80
     *
81
     * @return FileUploadPagePart
82
     */
83
    public function setRequired($required)
84
    {
85
        $this->required = $required;
86
87
        return $this;
88
    }
89
90
    /**
91
     * Check if the page part is required
92
     *
93
     * @return bool
94
     */
95
    public function getRequired()
96
    {
97
        return $this->required;
98
    }
99
100
    /**
101
     * Sets the message shown when the page part is required and no value was entered
102
     *
103
     * @param string $errorMessageRequired
104
     *
105
     * @return FileUploadPagePart
106
     */
107
    public function setErrorMessageRequired($errorMessageRequired)
108
    {
109
        $this->errorMessageRequired = $errorMessageRequired;
110
111
        return $this;
112
    }
113
114
    /**
115
     * Get the error message that will be shown when the page part is required and no value was entered
116
     *
117
     * @return string
118
     */
119
    public function getErrorMessageRequired()
120
    {
121
        return $this->errorMessageRequired;
122
    }
123
124
    /**
125
     * Returns the view used in the frontend
126
     *
127
     * @return mixed
128
     */
129
    public function getDefaultView()
130
    {
131
        return 'KunstmaanFormBundle:FileUploadPagePart:view.html.twig';
132
    }
133
134
    /**
135
     * Returns the default backend form type for this page part
136
     *
137
     * @return string
138
     */
139
    public function getDefaultAdminType()
140
    {
141
        return FileUploadPagePartAdminType::class;
142
    }
143
}
144