Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
File.php
Go to the documentation of this file.
1 <?php
7 
10 
17 class File extends \Magento\Eav\Model\Attribute\Data\AbstractData
18 {
25 
29  protected $urlEncoder;
30 
34  protected $_fileValidator;
35 
39  protected $_directory;
40 
50  public function __construct(
51  \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
52  \Psr\Log\LoggerInterface $logger,
53  \Magento\Framework\Locale\ResolverInterface $localeResolver,
54  \Magento\Framework\Url\EncoderInterface $urlEncoder,
55  \Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $fileValidator,
56  \Magento\Framework\Filesystem $filesystem
57  ) {
58  parent::__construct($localeDate, $logger, $localeResolver);
59  $this->urlEncoder = $urlEncoder;
60  $this->_fileValidator = $fileValidator;
61  $this->_directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
62  }
63 
72  {
73  if ($this->getIsAjaxRequest()) {
74  return false;
75  }
76 
77  $extend = $this->_getRequestValue($request);
78 
79  $attrCode = $this->getAttribute()->getAttributeCode();
80  if ($this->_requestScope) {
81  $value = [];
82  if (strpos($this->_requestScope, '/') !== false) {
83  $scopes = explode('/', $this->_requestScope);
84  $mainScope = array_shift($scopes);
85  } else {
86  $mainScope = $this->_requestScope;
87  $scopes = [];
88  }
89 
90  if (!empty($_FILES[$mainScope])) {
91  foreach ($_FILES[$mainScope] as $fileKey => $scopeData) {
92  foreach ($scopes as $scopeName) {
93  if (isset($scopeData[$scopeName])) {
94  $scopeData = $scopeData[$scopeName];
95  } else {
96  $scopeData[$scopeName] = [];
97  }
98  }
99 
100  if (isset($scopeData[$attrCode])) {
101  $value[$fileKey] = $scopeData[$attrCode];
102  }
103  }
104  } else {
105  $value = [];
106  }
107  } else {
108  if (isset($_FILES[$attrCode])) {
109  $value = $_FILES[$attrCode];
110  } else {
111  $value = [];
112  }
113  }
114 
115  if (!empty($extend['delete'])) {
116  $value['delete'] = true;
117  }
118 
119  return $value;
120  }
121 
128  protected function _validateByRules($value)
129  {
130  $label = $this->getAttribute()->getStoreLabel();
131  $rules = $this->getAttribute()->getValidateRules();
132  $extension = pathinfo($value['name'], PATHINFO_EXTENSION);
133 
134  if (!empty($rules['file_extensions'])) {
135  $extensions = explode(',', $rules['file_extensions']);
136  $extensions = array_map('trim', $extensions);
137  if (!in_array($extension, $extensions)) {
138  return [__('"%1" is not a valid file extension.', $label)];
139  }
140  }
141 
145  if (!$this->_fileValidator->isValid($extension)) {
146  return $this->_fileValidator->getMessages();
147  }
148 
149  if (!empty($value['tmp_name']) && !is_uploaded_file($value['tmp_name'])) {
150  return [__('"%1" is not a valid file.', $label)];
151  }
152 
153  if (!empty($rules['max_file_size'])) {
154  $size = $value['size'];
155  if ($rules['max_file_size'] < $size) {
156  return [__('"%1" exceeds the allowed file size.', $label)];
157  }
158  }
159 
160  return [];
161  }
162 
171  public function validateValue($value)
172  {
173  if ($this->getIsAjaxRequest()) {
174  return true;
175  }
176  $fileData = $value;
177 
178  if (is_string($value) && !empty($value)) {
179  $dir = $this->_directory->getAbsolutePath($this->getAttribute()->getEntityType()->getEntityTypeCode());
180  $fileData = [
181  'size' => filesize($dir . $value),
182  'name' => $value,
183  'tmp_name' => $dir . $value
184  ];
185  }
186 
187  $errors = [];
188  $attribute = $this->getAttribute();
189 
190  $toDelete = !empty($value['delete']) ? true : false;
191  $toUpload = !empty($value['tmp_name']) || is_string($value) && !empty($value) ? true : false;
192 
193  if (!$toUpload && !$toDelete && $this->getEntity()->getData($attribute->getAttributeCode())) {
194  return true;
195  }
196 
197  if (!$attribute->getIsRequired() && !$toUpload) {
198  return true;
199  }
200 
201  if ($attribute->getIsRequired() && !$toUpload) {
202  $label = __($attribute->getStoreLabel());
203  $errors[] = __('"%1" is a required value.', $label);
204  }
205 
206  if ($toUpload) {
207  $errors = array_merge($errors, $this->_validateByRules($fileData));
208  }
209 
210  if (count($errors) == 0) {
211  return true;
212  } elseif (is_string($value) && !empty($value)) {
213  $this->_directory->delete($dir . $value);
214  }
215 
216  return $errors;
217  }
218 
226  public function compactValue($value)
227  {
228  if ($this->getIsAjaxRequest()) {
229  return $this;
230  }
231 
232  $attribute = $this->getAttribute();
233  $original = $this->getEntity()->getData($attribute->getAttributeCode());
234  $toDelete = false;
235  if ($original) {
236  if (!$attribute->getIsRequired() && !empty($value['delete'])) {
237  $toDelete = true;
238  }
239  if (!empty($value['tmp_name'])) {
240  $toDelete = true;
241  }
242  }
243 
244  $destinationFolder = $attribute->getEntityType()->getEntityTypeCode();
245 
246  // unlink entity file
247  if ($toDelete) {
248  $this->getEntity()->setData($attribute->getAttributeCode(), '');
249  $file = $destinationFolder . $original;
250  if ($this->_directory->isExist($file)) {
251  $this->_directory->delete($file);
252  }
253  }
254 
255  if (!empty($value['tmp_name'])) {
256  try {
257  $uploader = new \Magento\Framework\File\Uploader($value);
258  $uploader->setFilesDispersion(true);
259  $uploader->setFilenamesCaseSensitivity(false);
260  $uploader->setAllowRenameFiles(true);
261  $uploader->save($this->_directory->getAbsolutePath($destinationFolder), $value['name']);
262  $fileName = $uploader->getUploadedFileName();
263  $this->getEntity()->setData($attribute->getAttributeCode(), $fileName);
264  } catch (\Exception $e) {
265  $this->_logger->critical($e);
266  }
267  }
268 
269  return $this;
270  }
271 
281  public function restoreValue($value)
282  {
283  return $this;
284  }
285 
293  {
294  $output = '';
295  $value = $this->getEntity()->getData($this->getAttribute()->getAttributeCode());
296  if ($value) {
297  switch ($format) {
298  case \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_JSON:
299  $output = ['value' => $value, 'url_key' => $this->urlEncoder->encode($value)];
300  break;
301  }
302  }
303 
304  return $output;
305  }
306 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
_getRequestValue(RequestInterface $request)
outputValue($format=\Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_TEXT)
Definition: File.php:292
__()
Definition: __.php:13
$logger
extractValue(RequestInterface $request)
Definition: File.php:71
$fileName
Definition: translate.phtml:15
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
__construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Locale\ResolverInterface $localeResolver, \Magento\Framework\Url\EncoderInterface $urlEncoder, \Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $fileValidator, \Magento\Framework\Filesystem $filesystem)
Definition: File.php:50
$filesystem
$errors
Definition: overview.phtml:9