Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
urlpreview.php
1<?php
3
4use \Bitrix\Landing\Landing;
5use \Bitrix\Landing\Site;
6use \Bitrix\Landing\Hook;
7use \Bitrix\Landing\File;
8use \Bitrix\Landing\Manager;
9use \Bitrix\Main\Loader;
10
12{
18 public static function getPreview(int $landingId): array
19 {
20 static $cached = [];
21
22 if (array_key_exists($landingId, $cached))
23 {
24 return $cached[$landingId];
25 }
26
27 $result = [];
28
29 $res = Landing::getList([
30 'select' => [
31 'ID',
32 'TITLE',
33 'SITE_TYPE' => 'SITE.TYPE'
34 ],
35 'filter' => [
36 'ID' => $landingId
37 ]
38 ]);
39 if ($row = $res->fetch())
40 {
41 $landing = Landing::createInstance(0);
42 $row['URL'] = $landing->getPublicUrl($landingId);
43 $row['DESCRIPTION'] = '';
44 $row['PICTURE'] = '';
45
46 // gets title and description with hight priority
47 $hookData = Hook::getForLandingRow($row['ID']);
48 if (isset($hookData['METAOG']['TITLE']))
49 {
50 $row['TITLE'] = $hookData['METAOG']['TITLE'];
51 if (isset($hookData['METAOG']['DESCRIPTION']))
52 {
53 $row['DESCRIPTION'] = $hookData['METAOG']['DESCRIPTION'];
54 }
55 }
56 else if (isset($hookData['METAMAIN']['TITLE']))
57 {
58 $row['TITLE'] = $hookData['METAMAIN']['TITLE'];
59 if (isset($hookData['METAMAIN']['DESCRIPTION']))
60 {
61 $row['DESCRIPTION'] = $hookData['METAMAIN']['DESCRIPTION'];
62 }
63 }
64 if (isset($hookData['METAOG']['IMAGE']))
65 {
66 $row['PICTURE'] = $hookData['METAOG']['IMAGE'];
67 if (intval($row['PICTURE']) > 0)
68 {
69 $row['PICTURE'] = File::getFilePath($row['PICTURE']);
70 }
71 }
72
73 $result = $row;
74 }
75
76 $cached[$landingId] = $result;
77
78 return $cached[$landingId];
79 }
80
86 public static function buildPreview(array $params): ?string
87 {
88 if (isset($params['scope']))
89 {
90 \Bitrix\Landing\Site\Type::setScope(
91 $params['scope']
92 );
93 }
94
95 if (!isset($params['URL']))
96 {
97 $params['URL'] = Manager::getPublicationPath() . $params['URL'];
98 }
99
100 $landingId = self::resolveLandingId($params['URL']);
101 if ($landingId)
102 {
103 ob_start();
104 Manager::getApplication()->includeComponent(
105 'bitrix:landing.socialnetwork.preview',
106 '',
107 [
108 'LANDING_ID' => $landingId
109 ]
110 );
111 return ob_get_clean();
112 }
113
114 return null;
115 }
116
122 public static function getImAttach(array $params)
123 {
124 if (!Loader::includeModule('im'))
125 {
126 return false;
127 }
128
129 if (isset($params['scope']))
130 {
131 \Bitrix\Landing\Site\Type::setScope(
132 $params['scope']
133 );
134 }
135
136 if (!isset($params['URL']))
137 {
138 $params['URL'] = Manager::getPublicationPath() . ($params['knowledgeCode'] ?? '');
139 }
140
141 $landingId = self::resolveLandingId($params['URL']);
142
143 if ($landingId)
144 {
145 $preview = self::getPreview($landingId);
146 if ($preview)
147 {
148 $attach = new \CIMMessageParamAttach(1, '#E30000');
149 $attach->addLink([
150 'NAME' => $preview['TITLE'],
151 'DESC' => $preview['DESCRIPTION'],
152 'LINK' => $preview['URL'],
153 'PREVIEW' => $preview['PICTURE']
154 ]);
155 return $attach;
156 }
157 }
158
159 return false;
160 }
161
162 public static function getImRich(array $params)
163 {
164 if (!Loader::includeModule('im'))
165 {
166 return false;
167 }
168
169 if (!class_exists('\Bitrix\Im\V2\Entity\Url\RichData'))
170 {
171 return false;
172 }
173
174 if (isset($params['scope']))
175 {
176 \Bitrix\Landing\Site\Type::setScope(
177 $params['scope']
178 );
179 }
180
181 if (!isset($params['URL']))
182 {
183 $params['URL'] = Manager::getPublicationPath() . ($params['knowledgeCode'] ?? '');
184 }
185
186 $landingId = self::resolveLandingId($params['URL']);
187
188 if ($landingId)
189 {
190 $preview = self::getPreview($landingId);
191 if ($preview)
192 {
193 $rich = new \Bitrix\Im\V2\Entity\Url\RichData();
194 $rich
195 ->setName($preview['TITLE'])
196 ->setDescription($preview['DESCRIPTION'])
197 ->setLink($preview['URL'])
198 ->setPreviewUrl($preview['PICTURE'])
199 ->setType(\Bitrix\Im\V2\Entity\Url\RichData::LANDING_TYPE)
200 ;
201 return $rich;
202 }
203 }
204
205 return false;
206 }
207
214 public static function checkUserReadAccess(array $params, int $userId): bool
215 {
216 if (isset($params['scope']))
217 {
218 \Bitrix\Landing\Site\Type::setScope(
219 $params['scope']
220 );
221 }
222
223 if (!isset($params['URL']))
224 {
225 $params['URL'] = Manager::getPublicationPath() . ($params['knowledgeCode'] ?? '');
226 }
227
228 return self::resolveLandingId($params['URL']) !== null;
229 }
230
236 protected static function resolveSiteId(string $landingPath): ?int
237 {
238 $publicPath = Manager::getPublicationPath();
239
240 if ($landingPath[0] !== '/')
241 {
242 $urlParts = parse_url($landingPath);
243 $landingPath = $urlParts['path'] ?? '';
244 }
245
246 if (mb_strpos($landingPath, $publicPath) === 0)
247 {
248 $landingPath = mb_substr($landingPath, mb_strlen($publicPath));
249 $pathChunks = explode('/', $landingPath);
250 if (!empty($pathChunks[0]))
251 {
252 $res = Site::getList([
253 'select' => [
254 'ID'
255 ],
256 'filter' => [
257 '=CODE' => '/' . $pathChunks[0] . '/'
258 ]
259 ]);
260 if ($row = $res->fetch())
261 {
262 return $row['ID'];
263 }
264 }
265 }
266
267 return null;
268 }
269
275 public static function resolveLandingId(string $landingPath): ?int
276 {
277 $landingId = null;
278
279 if ($landingPath[0] !== '/')
280 {
281 $urlParts = parse_url($landingPath);
282 $landingPath = $urlParts['path'] ?? '';
283 }
284
285 if ($landingPath)
286 {
287 $siteId = self::resolveSiteId($landingPath);
288 if ($siteId)
289 {
290 $landingId = Landing::resolveIdByPublicUrl($landingPath, $siteId);
291 }
292 }
293
294 return $landingId;
295 }
296}
static getList(array $params=array())
Definition site.php:1348
static getPreview(int $landingId)
static resolveLandingId(string $landingPath)
static resolveSiteId(string $landingPath)
static buildPreview(array $params)
static getImRich(array $params)
static getImAttach(array $params)
static checkUserReadAccess(array $params, int $userId)
static includeModule($moduleName)
Definition loader.php:69