forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFile.php
220 lines (202 loc) · 6.18 KB
/
File.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Block\Adminhtml\Form\Element;
/**
* Customer Widget Form File Element Block
*/
class File extends \Magento\Framework\Data\Form\Element\AbstractElement
{
/**
* @var \Magento\Framework\View\Asset\Repository
*/
protected $_assetRepo;
/**
* @var \Magento\Backend\Helper\Data
*/
protected $_adminhtmlData = null;
/**
* @var \Magento\Framework\Url\EncoderInterface
*/
protected $urlEncoder;
/**
* @param \Magento\Framework\Data\Form\Element\Factory $factoryElement
* @param \Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection
* @param \Magento\Framework\Escaper $escaper
* @param \Magento\Backend\Helper\Data $adminhtmlData
* @param \Magento\Framework\View\Asset\Repository $assetRepo
* @param \Magento\Framework\Url\EncoderInterface $urlEncoder
* @param array $data
*/
public function __construct(
\Magento\Framework\Data\Form\Element\Factory $factoryElement,
\Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection,
\Magento\Framework\Escaper $escaper,
\Magento\Backend\Helper\Data $adminhtmlData,
\Magento\Framework\View\Asset\Repository $assetRepo,
\Magento\Framework\Url\EncoderInterface $urlEncoder,
$data = []
) {
$this->_adminhtmlData = $adminhtmlData;
$this->_assetRepo = $assetRepo;
$this->urlEncoder = $urlEncoder;
parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
$this->setType('file');
}
/**
* Return Form Element HTML
*
* @return string
*/
public function getElementHtml()
{
$this->addClass('input-file');
if ($this->getRequired()) {
$this->removeClass('required-entry');
$this->addClass('required-file');
}
$element = sprintf(
'<input id="%s" name="%s" %s />%s%s',
$this->getHtmlId(),
$this->getName(),
$this->serialize($this->getHtmlAttributes()),
$this->getAfterElementHtml(),
$this->_getHiddenInput()
);
return $this->_getPreviewHtml() . $element . $this->_getDeleteCheckboxHtml();
}
/**
* Return Delete File CheckBox HTML
*
* @return string
*/
protected function _getDeleteCheckboxHtml()
{
$html = '';
if ($this->getValue() && !$this->getRequired() && !is_array($this->getValue())) {
$checkboxId = sprintf('%s_delete', $this->getHtmlId());
$checkbox = [
'type' => 'checkbox',
'name' => sprintf('%s[delete]', $this->getName()),
'value' => '1',
'class' => 'checkbox',
'id' => $checkboxId
];
$label = ['for' => $checkboxId];
if ($this->getDisabled()) {
$checkbox['disabled'] = 'disabled';
$label['class'] = 'disabled';
}
$html .= '<span class="' . $this->_getDeleteCheckboxSpanClass() . '">';
$html .= $this->_drawElementHtml('input', $checkbox) . ' ';
$html .= $this->_drawElementHtml('label', $label, false) . $this->_getDeleteCheckboxLabel() . '</label>';
$html .= '</span>';
}
return $html;
}
/**
* Return Delete CheckBox SPAN Class name
*
* @return string
*/
protected function _getDeleteCheckboxSpanClass()
{
return 'delete-file';
}
/**
* Return Delete CheckBox Label
*
* @return \Magento\Framework\Phrase
*/
protected function _getDeleteCheckboxLabel()
{
return __('Delete File');
}
/**
* Return File preview link HTML
*
* @return string
*/
protected function _getPreviewHtml()
{
$html = '';
if ($this->getValue() && !is_array($this->getValue())) {
$image = [
'alt' => __('Download'),
'title' => __('Download'),
'src' => $this->_assetRepo->getUrl('images/fam_bullet_disk.gif'),
'class' => 'v-middle'
];
$url = $this->_getPreviewUrl();
$html .= '<span>';
$html .= '<a href="' . $url . '">' . $this->_drawElementHtml('img', $image) . '</a> ';
$html .= '<a href="' . $url . '">' . __('Download') . '</a>';
$html .= '</span>';
}
return $html;
}
/**
* Return Hidden element with current value
*
* @return string
*/
protected function _getHiddenInput()
{
return $this->_drawElementHtml(
'input',
[
'type' => 'hidden',
'name' => sprintf('%s[value]', $this->getName()),
'id' => sprintf('%s_value', $this->getHtmlId()),
'value' => $this->getEscapedValue()
]
);
}
/**
* Return Preview/Download URL
*
* @return string
*/
protected function _getPreviewUrl()
{
return $this->_adminhtmlData->getUrl(
'customer/index/viewfile',
['file' => $this->urlEncoder->encode($this->getValue())]
);
}
/**
* Return Element HTML
*
* @param string $element
* @param array $attributes
* @param bool $closed
* @return string
*/
protected function _drawElementHtml($element, array $attributes, $closed = true)
{
$parts = [];
foreach ($attributes as $k => $v) {
$parts[] = sprintf('%s="%s"', $k, $v);
}
return sprintf('<%s %s%s>', $element, implode(' ', $parts), $closed ? ' /' : '');
}
/**
* Return escaped value
*
* @param int|null $index
* @return string|false
*/
public function getEscapedValue($index = null)
{
if (is_array($this->getValue())) {
return false;
}
$value = $this->getValue();
if (is_array($value) && $index === null) {
$index = 'value';
}
return parent::getEscapedValue($index);
}
}