1 | <?php |
||
27 | class RemoteFileFormFactory implements FormFactory |
||
28 | { |
||
29 | use Extensible; |
||
30 | use Configurable; |
||
31 | |||
32 | private static $fileurl_scheme_whitelist = ['http', 'https']; |
||
|
|||
33 | |||
34 | private static $fileurl_domain_whitelist = []; |
||
35 | |||
36 | /** |
||
37 | * @param Controller $controller |
||
38 | * @param string $name |
||
39 | * @param array $context |
||
40 | * @return Form |
||
41 | */ |
||
42 | public function getForm(Controller $controller, $name = self::DEFAULT_NAME, $context = []) |
||
43 | { |
||
44 | // Validate context |
||
45 | foreach ($this->getRequiredContext() as $required) { |
||
46 | if (!isset($context[$required])) { |
||
47 | throw new InvalidArgumentException("Missing required context $required"); |
||
48 | } |
||
49 | } |
||
50 | |||
51 | $fields = $this->getFormFields($controller, $name, $context); |
||
52 | $actions = $this->getFormActions($controller, $name, $context); |
||
53 | |||
54 | $validator = new RequiredFields(); |
||
55 | $form = Form::create($controller, $name, $fields, $actions, $validator); |
||
56 | $form->addExtraClass('form--fill-height'); |
||
57 | $form->addExtraClass('form--no-dividers'); |
||
58 | $form->addExtraClass('insert-embed-modal--'. strtolower($context['type'])); |
||
59 | |||
60 | // Extend form |
||
61 | $this->invokeWithExtensions('updateForm', $form, $controller, $name, $context); |
||
62 | |||
63 | return $form; |
||
64 | } |
||
65 | |||
66 | public function getRequiredContext() |
||
67 | { |
||
68 | return ['type']; |
||
69 | } |
||
70 | |||
71 | protected function getFormFields($controller, $name, $context) |
||
72 | { |
||
73 | $fields = []; |
||
74 | $url = (isset($context['url'])) ? $context['url'] : null; |
||
75 | |||
76 | if ($context['type'] === 'create') { |
||
77 | $fields = [ |
||
78 | TextField::create( |
||
79 | 'Url', |
||
80 | _t( |
||
81 | 'RemoteFileForm.UrlDescription', |
||
82 | 'Embed Youtube and Vimeo videos, images and other media directly from the web.' |
||
83 | ) |
||
84 | ) |
||
85 | ->addExtraClass('insert-embed-modal__url-create'), |
||
86 | ]; |
||
87 | } |
||
88 | |||
89 | if ($context['type'] === 'edit' && $url && $this->validateUrl($url)) { |
||
90 | $embed = $this->getEmbed($url); |
||
91 | $alignments = array( |
||
92 | 'leftAlone' => _t('AssetAdmin.AlignmentLeftAlone', 'Left'), |
||
93 | 'center' => _t('AssetAdmin.AlignmentCenter', 'Center'), |
||
94 | 'rightAlone' => _t('AssetAdmin.AlignmentRightAlone', 'Right'), |
||
95 | 'left' => _t('AssetAdmin.AlignmentLeft', 'Left wrap'), |
||
96 | 'right' => _t('AssetAdmin.AlignmentRight', 'Right wrap'), |
||
97 | ); |
||
98 | |||
99 | $fields = CompositeField::create([ |
||
100 | LiteralField::create( |
||
101 | 'Preview', |
||
102 | sprintf( |
||
103 | '<img src="%s" class="%s" />', |
||
104 | $embed->getPreviewURL(), |
||
105 | 'insert-embed-modal__preview' |
||
106 | ) |
||
107 | )->addExtraClass('insert-embed-modal__preview-container'), |
||
108 | HiddenField::create('PreviewUrl', 'PreviewUrl', $embed->getPreviewURL()), |
||
109 | CompositeField::create([ |
||
110 | TextField::create('UrlPreview', $embed->getName(), $url) |
||
111 | ->setReadonly(true), |
||
112 | HiddenField::create('Url', false, $url), |
||
113 | TextField::create('CaptionText', _t('AssetAdmin.Caption', 'Caption')), |
||
114 | OptionsetField::create( |
||
115 | 'Placement', |
||
116 | _t('AssetAdmin.Placement', 'Placement'), |
||
117 | $alignments |
||
118 | ) |
||
119 | ->addExtraClass('insert-embed-modal__placement'), |
||
120 | FieldGroup::create( |
||
121 | _t('AssetAdmin.ImageSpecs', 'Dimensions'), |
||
122 | TextField::create('Width', '', $embed->getWidth()) |
||
123 | ->setRightTitle(_t('AssetAdmin.ImageWidth', 'Width')) |
||
124 | ->setMaxLength(5) |
||
125 | ->addExtraClass('flexbox-area-grow'), |
||
126 | TextField::create('Height', '', $embed->getHeight()) |
||
127 | ->setRightTitle(_t('AssetAdmin.ImageHeight', 'Height')) |
||
128 | ->setMaxLength(5) |
||
129 | ->addExtraClass('flexbox-area-grow') |
||
130 | )->addExtraClass('fieldgroup--fill-width') |
||
131 | ])->addExtraClass('flexbox-area-grow'), |
||
132 | ])->addExtraClass('insert-embed-modal__fields--fill-width'); |
||
133 | } |
||
134 | |||
135 | return FieldList::create($fields); |
||
136 | } |
||
137 | |||
138 | protected function getFormActions($controller, $name, $context) |
||
159 | |||
160 | /** |
||
161 | * @param $url |
||
162 | * @return bool |
||
163 | * @throws InvalidUrlException |
||
164 | */ |
||
165 | protected function validateUrl($url) |
||
191 | |||
192 | protected function getEmbed($url) |
||
198 | } |
||
199 |
This check marks private properties in classes that are never used. Those properties can be removed.