Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
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
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 1
    public function adaptForm(FormBuilderInterface $formBuilder, ArrayObject $fields, $sequence)
44
    {
45 1
        $ffsf = new FileFormSubmissionField();
46 1
        $ffsf->setFieldName('field_' . $this->getUniqueId());
47 1
        $ffsf->setLabel($this->getLabel());
48 1
        $ffsf->setSequence($sequence);
49
50 1
        $data = $formBuilder->getData();
51 1
        $data['formwidget_' . $this->getUniqueId()] = $ffsf;
52
53 1
        $constraints = [];
54 1
        if ($this->getRequired()) {
55 1
            $options = [];
56 1
            if (!empty($this->errorMessageRequired)) {
57 1
                $options['message'] = $this->errorMessageRequired;
58
            }
59 1
            $constraints[] = new NotBlank($options);
60
        }
61
62 1
        $formBuilder->add(
63 1
            'formwidget_' . $this->getUniqueId(),
64 1
            FileFormSubmissionType::class,
65
            [
66 1
                'label' => $this->getLabel(),
67 1
                'value_constraints' => $constraints,
68 1
                'required' => $this->getRequired(),
69
            ]
70
        );
71 1
        $formBuilder->setData($data);
72
73 1
        $fields->append($ffsf);
74 1
    }
75
76
    /**
77
     * Sets the required valud of this page part
78
     *
79
     * @param bool $required
80
     *
81
     * @return FileUploadPagePart
82
     */
83 3
    public function setRequired($required)
84
    {
85 3
        $this->required = $required;
86
87 3
        return $this;
88
    }
89
90
    /**
91
     * Check if the page part is required
92
     *
93
     * @return bool
94
     */
95 3
    public function getRequired()
96
    {
97 3
        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 2
    public function setErrorMessageRequired($errorMessageRequired)
108
    {
109 2
        $this->errorMessageRequired = $errorMessageRequired;
110
111 2
        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 2
    public function getErrorMessageRequired()
120
    {
121 2
        return $this->errorMessageRequired;
122
    }
123
124
    /**
125
     * Returns the view used in the frontend
126
     *
127
     * @return mixed
0 ignored issues
show
Consider making the return type a bit more specific; maybe use string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
128
     */
129 1
    public function getDefaultView()
130
    {
131 1
        return '@KunstmaanForm/FileUploadPagePart/view.html.twig';
132
    }
133
134
    /**
135
     * Returns the default backend form type for this page part
136
     *
137
     * @return string
138
     */
139 1
    public function getDefaultAdminType()
140
    {
141 1
        return FileUploadPagePartAdminType::class;
142
    }
143
}
144