Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
filefieldassembler.php
1<?php
2
4
9use CFile;
10use CFileInput;
11
13{
14 private int $iblockId;
15 private array $files;
16
17 public function __construct(int $iblockId)
18 {
19 $this->iblockId = $iblockId;
20
21 parent::__construct(
22 $this->getPropertyColumnsIds()
23 );
24 }
25
26 private function getPropertyColumnsIds(): array
27 {
28 $result = [];
29
31 'select' => [
32 'ID',
33 ],
34 'filter' => [
35 '=IBLOCK_ID' => $this->iblockId,
36 '=PROPERTY_TYPE' => PropertyTable::TYPE_FILE,
37 'USER_TYPE' => null,
38 ],
39 ]);
40 foreach ($rows as $row)
41 {
42 $result[] = ElementPropertyProvider::getColumnIdByPropertyId((int)$row['ID']);
43 }
44
45 return $result;
46 }
47
48 public function prepareRows(array $rowList): array
49 {
50 if (empty($this->getColumnIds()))
51 {
52 return $rowList;
53 }
54
55 $fileIds = [];
56 foreach ($rowList as $row)
57 {
58 foreach ($this->getColumnIds() as $columnId)
59 {
60 $value = $row['data'][$columnId] ?? null;
61 if (is_array($value))
62 {
63 foreach ($value as $valueItem)
64 {
65 $fileIds[] = (int)$valueItem;
66 }
67 }
68 elseif (isset($value))
69 {
70 $fileIds[] = (int)$value;
71 }
72 }
73 }
74
75 if (empty($fileIds))
76 {
77 return $rowList;
78 }
79
80 $this->preloadFiles($fileIds);
81
82 return parent::prepareRows($rowList);
83 }
84
85 protected function prepareRow(array $row): array
86 {
87 $row['columns'] ??= [];
88
89 foreach ($this->getColumnIds() as $columnId)
90 {
91 $value = $row['data'][$columnId] ?? null;
92 if ($value !== null || is_array($value))
93 {
94 // edit
95 $row['data']['~' . $columnId] ??= $this->getDataForEdit($value);
96
97 // view
98 $row['columns'][$columnId] ??= $this->getImageHtml($columnId, $value);
99 }
100 }
101
102 return $row;
103 }
104
113 private function getImageHtml(string $columnId, $value): string
114 {
115 if (Loader::includeModule('fileman'))
116 {
117 return CFileInput::Show(
118 '',
119 $value,
120 [
121 'IMAGE' => 'Y',
122 'IMAGE_POPUP' => 'N',
123 'PATH' => 'N',
124 'FILE_SIZE' => 'N',
125 'DIMENSIONS' => 'N',
126 'MAX_SIZE' => [
127 'W' => 50,
128 'H' => 50,
129 ],
130 'MIN_SIZE' => [
131 'W' => 1,
132 'H' => 1,
133 ],
134 ],
135 [
136 'upload' => false,
137 'medialib' => false,
138 'file_dialog' => false,
139 'cloud' => false,
140 'del' => false,
141 'description' => false,
142 ]
143 );
144 }
145
146 global $APPLICATION;
147
152 try
153 {
154 ob_start();
155
156 $APPLICATION->IncludeComponent('bitrix:main.file.input', '', [
157 'MODULE_ID' => 'catalog',
158 'MULTIPLE'=> 'Y',
159 'ALLOW_UPLOAD' => 'N',
160 'INPUT_NAME' => $columnId,
161 'INPUT_VALUE' => $value,
162 ]);
163
164 return ob_get_contents();
165 }
166 finally
167 {
168 ob_end_clean();
169 }
170 }
171
172 private function getFileSrc(int $fileId): ?string
173 {
174 $file = $this->files[$fileId] ?? null;
175 if ($file)
176 {
177 return CFile::GetFileSRC($file);
178 }
179
180 return null;
181 }
182
183 private function getDataForEdit($value)
184 {
185 if (is_array($value))
186 {
187 $result = [];
188
189 foreach ($value as $valueItem)
190 {
191 $result[] = $this->getFileSrc((int)$valueItem);
192 }
193
194 return $result;
195 }
196 elseif (isset($value))
197 {
198 return $this->getFileSrc((int)$value);
199 }
200
201 return null;
202 }
203
204 private function preloadFiles(array $fileIds): void
205 {
206 $this->files = [];
207
208 if (empty($fileIds))
209 {
210 return;
211 }
212
213 $rows = CFile::GetList([], [
214 '@ID' => $fileIds,
215 ]);
216 while ($row = $rows->Fetch())
217 {
218 $this->files[$row['ID']] = $row;
219 }
220 }
221}
static getList(array $parameters=array())