|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* File: Attachments.php |
|
7
|
|
|
* |
|
8
|
|
|
* @author Bartosz Kubicki [email protected]> |
|
9
|
|
|
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace LizardMedia\ProductAttachment\Ui\DataProvider\Product\Form\Modifier\Data; |
|
13
|
|
|
|
|
14
|
|
|
use LizardMedia\ProductAttachment\Api\AttachmentRepositoryInterface; |
|
15
|
|
|
use LizardMedia\ProductAttachment\Api\Data\AttachmentInterface; |
|
16
|
|
|
use LizardMedia\ProductAttachment\Api\SettingsInterface; |
|
17
|
|
|
use LizardMedia\ProductAttachment\Model\Attachment as AttachmentModel; |
|
18
|
|
|
use Magento\Catalog\Model\Locator\LocatorInterface; |
|
19
|
|
|
use Magento\Downloadable\Helper\File as DownloadableFile; |
|
20
|
|
|
use Magento\Framework\Escaper; |
|
21
|
|
|
use Magento\Framework\UrlInterface; |
|
22
|
|
|
use Magento\Store\Model\ScopeInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class Attachments |
|
26
|
|
|
* @package LizardMedia\ProductAttachment\Ui\DataProvider\Product\Form\Modifier\Data |
|
27
|
|
|
*/ |
|
28
|
|
|
class Attachments |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var AttachmentRepositoryInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $attachmentRepository; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var SettingsInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $settings; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var AttachmentModel |
|
42
|
|
|
*/ |
|
43
|
|
|
private $attachmentModel; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var LocatorInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
private $locator; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var DownloadableFile |
|
52
|
|
|
*/ |
|
53
|
|
|
private $downloadableFile; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var Escaper |
|
57
|
|
|
*/ |
|
58
|
|
|
private $escaper; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var UrlInterface |
|
62
|
|
|
*/ |
|
63
|
|
|
private $urlBuilder; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param AttachmentRepositoryInterface $attachmentRepository |
|
67
|
|
|
* @param SettingsInterface $settings |
|
68
|
|
|
* @param AttachmentModel $attachmentModel |
|
69
|
|
|
* @param LocatorInterface $locator |
|
70
|
|
|
* @param DownloadableFile $downloadableFile |
|
71
|
|
|
* @param Escaper $escaper |
|
72
|
|
|
* @param UrlInterface $urlBuilder |
|
73
|
|
|
*/ |
|
74
|
|
|
public function __construct( |
|
75
|
|
|
AttachmentRepositoryInterface $attachmentRepository, |
|
76
|
|
|
SettingsInterface $settings, |
|
77
|
|
|
AttachmentModel $attachmentModel, |
|
78
|
|
|
LocatorInterface $locator, |
|
79
|
|
|
DownloadableFile $downloadableFile, |
|
80
|
|
|
Escaper $escaper, |
|
81
|
|
|
UrlInterface $urlBuilder |
|
82
|
|
|
) { |
|
83
|
|
|
$this->attachmentRepository = $attachmentRepository; |
|
84
|
|
|
$this->settings = $settings; |
|
85
|
|
|
$this->attachmentModel = $attachmentModel; |
|
86
|
|
|
$this->locator = $locator; |
|
87
|
|
|
$this->downloadableFile = $downloadableFile; |
|
88
|
|
|
$this->escaper = $escaper; |
|
89
|
|
|
$this->urlBuilder = $urlBuilder; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getAttachmentsTitle() : string |
|
96
|
|
|
{ |
|
97
|
|
|
$product = $this->locator->getProduct(); |
|
98
|
|
|
return $product->getAttachmentsTitle() |
|
99
|
|
|
? $product->getAttachmentsTitle() |
|
100
|
|
|
: $this->settings->getAttachmentDefaultTitle(ScopeInterface::SCOPE_STORE); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getAttachmentsData() : array |
|
107
|
|
|
{ |
|
108
|
|
|
$attachmentsData = []; |
|
109
|
|
|
|
|
110
|
|
|
$product = $this->locator->getProduct(); |
|
111
|
|
|
|
|
112
|
|
|
if ($product) { |
|
113
|
|
|
$attachments = $this->attachmentRepository->getAttachmentsByProduct($product); |
|
114
|
|
|
/** @var AttachmentInterface $attachment */ |
|
115
|
|
|
foreach ($attachments as $attachment) { |
|
116
|
|
|
$attachmentData = []; |
|
117
|
|
|
$attachmentData[AttachmentModel::ID] = (int) $attachment->getId() ?: 0; |
|
118
|
|
|
$attachmentData[AttachmentModel::TITLE] = $this->escaper->escapeHtml($attachment->getTitle()); |
|
119
|
|
|
$attachmentData[AttachmentModel::ATTACHMENT_URL] = $attachment->getAttachmentUrl(); |
|
120
|
|
|
$attachmentData[AttachmentModel::ATTACHMENT_TYPE] = $attachment->getAttachmentType(); |
|
121
|
|
|
$attachmentData[AttachmentModel::SORT_ORDER] = $attachment->getSortOrder(); |
|
122
|
|
|
|
|
123
|
|
|
$attachmentData = $this->addAttachmentFile($attachmentData, $attachment); |
|
124
|
|
|
|
|
125
|
|
|
$attachmentsData[] = $attachmentData; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $attachmentsData; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param array $attachmentData |
|
134
|
|
|
* @param AttachmentInterface $attachment |
|
135
|
|
|
* @return array |
|
136
|
|
|
*/ |
|
137
|
|
|
private function addAttachmentFile(array $attachmentData, AttachmentInterface $attachment) : array |
|
138
|
|
|
{ |
|
139
|
|
|
$attachmentFile = $attachment->getAttachmentFile(); |
|
140
|
|
|
|
|
141
|
|
|
if ($attachmentFile) { |
|
|
|
|
|
|
142
|
|
|
$file = $this->downloadableFile->getFilePath($this->attachmentModel->getBasePath(), $attachmentFile); |
|
143
|
|
|
|
|
144
|
|
|
if ($this->downloadableFile->ensureFileInFilesystem($file)) { |
|
145
|
|
|
$attachmentData['file'][0] = [ |
|
146
|
|
|
'file' => $attachmentFile, |
|
147
|
|
|
'name' => $this->downloadableFile->getFileFromPathFile($attachmentFile), |
|
148
|
|
|
'size' => $this->downloadableFile->getFileSize($file), |
|
149
|
|
|
'status' => 'old', |
|
150
|
|
|
'url' => $this->urlBuilder->getUrl( |
|
151
|
|
|
'downloadable/attachment_file/preview', |
|
152
|
|
|
[AttachmentModel::ID => $attachment->getId(), '_secure' => true] |
|
153
|
|
|
), |
|
154
|
|
|
]; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $attachmentData; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: