Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
duplicateimages.php
1<?php
2
4
12use Bitrix\Main\EO_File;
13
20{
21 protected const IMG_TYPES = ['img', 'styleimg'];
22
23 protected Block $block;
24 protected array $manifest;
25 protected string $content;
26
31 public function __construct(?int $blockId, array $params = [])
32 {
33 if (
34 !empty($params)
35 && $params['block']
36 && $params['content']
37 )
38 {
39 $this->block = $params['block'];
40 $this->content = $params['content'];
41 $this->manifest = $this->block->getManifest();
42 }
43 elseif ($blockId)
44 {
45 $this->block = new Block($blockId);
46 $this->content = $this->block->getContent();
47 $this->manifest = $this->block->getManifest();
48 }
49 // todo: error, if has no params or blockId
50 }
51
57 public function update(bool $needSave = false): string
58 {
59 $srcForReplace = [];
60 if (isset($this->manifest['nodes']) && is_array($this->manifest['nodes']))
61 {
62 foreach ($this->manifest['nodes'] as $selector => $node)
63 {
64 if (in_array($node['type'], self::IMG_TYPES, true))
65 {
66 $nodeClass = Node\Type::getClassName($node['type']);
67 $previousValues = call_user_func([$nodeClass, 'getNode'], $this->block, $selector);
68
69 foreach ($previousValues as $previousValue)
70 {
71 if (!($previousValue['id'] ?? null) && !($previousValue['id2x'] ?? null))
72 {
73 continue;
74 }
75
76 if (
77 ($previousValue['id'] && !File::getFilePath($previousValue['id']))
78 || ($previousValue['id2x'] && !File::getFilePath($previousValue['id2x']))
79 )
80 {
81 continue;
82 }
83
84 $hostUrl = '//' . Manager::getHttpHost();
85
86 $newSrc = File::getFilePath($previousValue['id']);
87 $newSrc = Manager::getUrlFromFile($newSrc);
88 $previousSrc =
89 $previousValue['isLazy'] === 'Y'
90 ? $previousValue['lazyOrigSrc']
91 : $previousValue['src']
92 ;
93 if ($previousSrc !== $newSrc)
94 {
95 $srcForReplace[$previousSrc] = $newSrc;
96
97 // add urls w/o host url too
98 if (strpos($newSrc, $hostUrl) === 0)
99 {
100 $srcForReplace[str_replace($hostUrl, '', $previousSrc)] =
101 str_replace($hostUrl, '', $newSrc);
102 }
103 }
104
105 if ($previousValue['id2x'])
106 {
107 $newSrc2x = File::getFilePath($previousValue['id2x']);
108 $newSrc2x = Manager::getUrlFromFile($newSrc2x);
109 $previousSrc2x =
110 $previousValue['isLazy'] === 'Y'
111 ? $previousValue['lazyOrigSrc2x']
112 : $previousValue['src2x']
113 ;
114 if ($previousSrc2x !== $newSrc2x)
115 {
116 $srcForReplace[$previousSrc2x] = $newSrc2x;
117
118 // add urls w/o host url too
119 if (strpos($newSrc2x, $hostUrl) === 0)
120 {
121 $srcForReplace[str_replace($hostUrl, '', $previousSrc2x)] =
122 str_replace($hostUrl, '', $newSrc2x);
123 }
124 }
125 }
126 }
127 }
128 }
129 }
130
131 if (!empty($srcForReplace))
132 {
133 $this->content = str_replace(
134 array_keys($srcForReplace),
135 array_values($srcForReplace),
136 $this->content
137 );
138
139 if ($needSave)
140 {
141 $this->block->saveContent($this->content);
142 $this->block->save();
143 }
144 }
145
146 return $this->content;
147 }
148
149 public static function updateLanding(int $lid): void
150 {
151 $res = BlockTable::getList(
152 [
153 'select' => [
154 'ID', 'CONTENT',
155 ],
156 'filter' => [
157 'LID' => $lid,
158 ],
159 'order' => 'SORT',
160 ]
161 );
162 while ($row = $res->fetch())
163 {
164 $block = new self($row['ID']);
165 $block->update(true);
166 }
167 }
168
169 public static function onAfterFileDeleteDuplicate(EO_File $original, EO_File $duplicate)
170 {
171 // BLOCKS
172 $sitesToUpdate = [];
173 $oldPath = '/' . $duplicate->getSubdir() . '/' . $duplicate->getFileName();
174 $newPath = '/' . $original->getSubdir() . '/' . $original->getFileName();
175
176 $resFiles = FileTable::query()
177 ->addSelect('ENTITY_ID')
178 ->where('ENTITY_TYPE', '=', File::ENTITY_TYPE_BLOCK)
179 ->where('FILE_ID', $duplicate->getId())
180 ->exec()
181 ;
182 while($blockFile = $resFiles->fetch())
183 {
184 $resBlocks = BlockTable::query()
185 ->addSelect('ID')
186 ->addSelect('CONTENT')
187 ->addSelect('LANDING.SITE_ID')
188 ->where('ID', $blockFile['ENTITY_ID'])
189 ->exec()
190 ;
191 while($block = $resBlocks->fetch())
192 {
193 BlockTable::update($block['ID'], [
194 'CONTENT' => str_replace($oldPath, $newPath, $block['CONTENT'])
195 ]);
196 $siteId = (int)$block['LANDING_INTERNALS_BLOCK_LANDING_SITE_ID'];
197 if ($siteId && $siteId > 0)
198 {
199 $sitesToUpdate[] = $siteId;
200 }
201 }
202 }
203
204 $sitesToUpdate = array_unique($sitesToUpdate);
205 foreach($sitesToUpdate as $siteId)
206 {
208 }
209
210 // ASSETS
211 $landingsForUpdate = [];
212 $resFiles = FileTable::query()
213 ->addSelect('ENTITY_ID')
214 ->where('ENTITY_TYPE', '=', File::ENTITY_TYPE_ASSET)
215 ->where('FILE_ID', $duplicate->getId())
216 ->exec()
217 ;
218 while($assetFile = $resFiles->fetch())
219 {
220 $landingsForUpdate[] = abs((int)$assetFile['ENTITY_ID']);
221 }
222 $landingsForUpdate = array_unique($landingsForUpdate);
223 foreach($landingsForUpdate as $landingId)
224 {
226 }
227 }
228}
static update($id, $fields=array())
Definition block.php:5598
const ENTITY_TYPE_ASSET
Definition file.php:26
const ENTITY_TYPE_BLOCK
Definition file.php:21
static getFilePath($fileId)
Definition file.php:600
static clearCacheForSite(int $siteId)
Definition manager.php:1402
static getUrlFromFile($file)
Definition manager.php:1067
static clearCacheForLanding(int $lid)
Definition manager.php:1415
__construct(?int $blockId, array $params=[])
static onAfterFileDeleteDuplicate(EO_File $original, EO_File $duplicate)