Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ImageRepository.php
1<?php
2
4
9
19{
21 protected $factory;
22
24 {
25 $this->factory = $factory;
26 }
27
28 public function getEntityById(int $id, string $type = null): ?BaseEntity
29 {
30 if ($id <= 0)
31 {
32 throw new \OutOfRangeException($id);
33 }
34
35 $entities = $this->getEntitiesBy([
36 'filter' => [
37 '=ID' => $id,
38 ],
39 'limit' => 1,
40 ], $type);
41
42 return reset($entities) ?: null;
43 }
44
45 public function getEntitiesBy($params, string $type = null): array
46 {
47 $entities = [];
48
49 foreach ($this->getList((array)$params) as $item)
50 {
51 $entities[] = $this->createEntity($item, $type);
52 }
53
54 return $entities;
55 }
56
57 public function save(BaseEntity ...$entities): Result
58 {
59 return new Result();
60 }
61
62 public function delete(BaseEntity ...$entities): Result
63 {
64 foreach ($entities as $entity)
65 {
66 \CFile::Delete($entity->getId());
67 }
68
69 return new Result();
70 }
71
73 {
74 $collection = $this->factory->createCollection();
75 if ($element->isNew())
76 {
77 return $collection;
78 }
79
80 $items = [];
81
82 $previewValue = (int)$element->getField(PreviewImage::CODE);
83 if ($previewValue > 0)
84 {
85 $previewEntity = $this->getEntityById($previewValue, ImageFactory::PREVIEW_IMAGE);
86 if ($previewEntity)
87 {
88 $items[] = $previewEntity;
89 }
90 }
91
92 $detailValue = (int)$element->getField(DetailImage::CODE);
93 if ($detailValue > 0)
94 {
95 $detailEntity = $this->getEntityById($detailValue, ImageFactory::DETAIL_IMAGE);
96 if ($detailEntity)
97 {
98 $items[] = $detailEntity;
99 }
100 }
101
102 foreach ($this->getMorePhotoEntities($element) as $item)
103 {
104 $items[] = $item;
105 }
106
107 if (!empty($items))
108 {
109 $collection->add(...$items);
110 }
111
112 return $collection;
113 }
114
115 private function getMorePhotoEntities(BaseIblockElementEntity $element): array
116 {
117 $morePhotos = [];
118 $property = $element->getPropertyCollection()->findByCode(MorePhotoImage::CODE);
119 if (!$property)
120 {
121 return [];
122 }
123
124 $morePhotoValueCollection = $property->getPropertyValueCollection();
125 $morePhotoIds = $morePhotoValueCollection->getValues();
126 if (empty($morePhotoIds))
127 {
128 return [];
129 }
130 $fields = $this->getList([
131 'filter' => [
132 '=ID' => $morePhotoIds,
133 ],
134 ]);
135 if (empty($fields))
136 {
137 return [];
138 }
139
140
141 $fields = array_combine(array_column($fields, 'ID'), $fields);
143 foreach ($morePhotoValueCollection as $value)
144 {
145 $fileId = (int)$value->getValue();
146 if ($fileId > 0 && isset($fields[$fileId]))
147 {
148 $fileFields = $fields[$fileId];
149 if (empty($fileFields))
150 {
151 continue;
152 }
153 $fileFields['PROPERTY_VALUE_ID'] = $value->getId();
154 $morePhotos[] = $this->createEntity($fileFields);
155 }
156 }
157
158 return $morePhotos;
159 }
160
161 protected function getList(array $params): array
162 {
163 $files = [];
164 $filesRaw = FileTable::getList($params);
165 while ($file = $filesRaw->fetch())
166 {
167 $file['SRC'] = \CFile::getFileSRC($file);
168 $file['FILE_STRUCTURE'] = $file;
169 $files[] = $file;
170 }
171
172 return $files;
173 }
174
175 protected function createEntity(array $fields = [], string $type = null): BaseImage
176 {
177 $entity = $this->factory->createEntity($type);
178
179 $entity->initFields($fields);
180
181 return $entity;
182 }
183}
createEntity(array $fields=[], string $type=null)
getEntityById(int $id, string $type=null)
getCollectionByParent(BaseIblockElementEntity $element)
getEntitiesBy($params, string $type=null)