Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
morephotoassembler.php
1<?php
2
4
13use Bitrix\Main\Page\Asset;
15
16Loader::requireModule('iblock');
17
22{
23 private array $entities;
24
25 public function __construct(array $columnIds, ProductSettings $settings)
26 {
27 parent::__construct($columnIds, $settings);
28
29 $this->preloadResources();
30 }
31
42 private function preloadResources(): void
43 {
44 Asset::getInstance()->addJs('/bitrix/components/bitrix/ui.image.input/templates/.default/script.js');
45 Asset::getInstance()->addCss('/bitrix/components/bitrix/ui.image.input/templates/.default/style.css');
46 }
47
48 private function getIblockId(): int
49 {
50 return $this->getSettings()->getIblockId();
51 }
52
53 private function getOffersIblockId(): ?int
54 {
55 return $this->getSettings()->getOffersIblockId();
56 }
57
58 #region override
59
60 public function prepareRows(array $rowList): array
61 {
62 if (empty($this->getColumnIds()))
63 {
64 return $rowList;
65 }
66
67 $this->loadEntities($rowList);
68
69 foreach ($rowList as &$row)
70 {
71 $id = (int)($row['data']['ID'] ?? 0);
72 $type = $row['data']['ROW_TYPE'] ?? null;
76 $entity = $this->entities[$id] ?? null;
77
78 $row['columns'] ??= [];
79
80 foreach ($this->getColumnIds() as $columnId)
81 {
82 if ($id === 0 || $type !== RowType::ELEMENT || !isset($entity))
83 {
84 $row['data'][$columnId] = null;
85 $row['columns'][$columnId] = null;
86 }
87 elseif ($this->getSettings()->isExcelMode())
88 {
89 $imagesSrc = [];
90 foreach ($entity->getFrontImageCollection() as $image)
91 {
92 $uri = new Uri($image->getSource());
93 $imagesSrc[] = (string)$uri->toAbsolute();
94 }
95
96 $row['data'][$columnId] = join(', ', $imagesSrc);
97 $row['columns'][$columnId] = join(', ', $imagesSrc);
98 }
99 else
100 {
101 $imageInput = new ImageInput($entity);
102 //$imageInput->disableAutoSaving();
103 $html = $imageInput->getFormattedField();
104
105 $row['data']['~' . $columnId] = $html['input'];
106 $row['columns'][$columnId] = $html['preview'];
107 }
108 }
109 }
110
111 return $rowList;
112 }
113
114 protected function prepareColumn($value)
115 {
116 return $value;
117 }
118
119 #endregion override
120
121 private function loadEntities(array $rowList): void
122 {
123 $this->entities = [];
124
125 $productToOfferId = $this->getSettings()->getSelectedProductOfferIds();
126 $offerToProductId = array_flip($productToOfferId);
127
128 $ids = [];
129 foreach ($rowList as $row)
130 {
131 $id = (int)($row['data']['ID'] ?? 0);
132 if ($id === 0)
133 {
134 continue;
135 }
136
137 $type = $row['data']['ROW_TYPE'] ?? null;
138 if ($type !== RowType::ELEMENT)
139 {
140 continue;
141 }
142
143 // replace `product id` to `offer id` to display correct offer image.
144 if (isset($productToOfferId[$id]))
145 {
146 $id = $productToOfferId[$id];
147 }
148
149 $ids[] = $id;
150 }
151
152 if (empty($ids))
153 {
154 return;
155 }
156
157 $repository = ServiceContainer::getProductRepository($this->getIblockId());
158 if (isset($repository))
159 {
160 $items = $repository->getEntitiesBy([
161 'filter' => [
162 'ID' => $ids,
163 ],
164 ]);
165 foreach ($items as $item)
166 {
171 $this->entities[$item->getId()] = $item;
172 }
173 }
174
175 $offersIblockId = $this->getOffersIblockId();
176 $repository =
177 isset($offersIblockId)
178 ? ServiceContainer::getSkuRepository($offersIblockId)
179 : null
180 ;
181 if (isset($repository))
182 {
183 $items = $repository->getEntitiesBy([
184 'filter' => [
185 'ID' => $ids,
186 ],
187 ]);
188 foreach ($items as $item)
189 {
194 $productId = $offerToProductId[$item->getId()];
195 $this->entities[$productId] = $item;
196 }
197 }
198 }
199}
__construct(array $columnIds, ProductSettings $settings)