1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
repowidget.php
См. документацию.
1<?php
2namespace Bitrix\Landing\PublicAction;
3
4use Bitrix\Landing;
5use Bitrix\Landing\Error;
6use Bitrix\Landing\PublicAction;
7use Bitrix\Landing\Subtype;
8use Bitrix\Landing\Manager;
9use Bitrix\Landing\PublicActionResult;
10use Bitrix\Landing\Site\Scope;
11use Bitrix\Rest;
12use Bitrix\Main\Loader;
13use Bitrix\Main\Localization\Loc;
14use Bitrix\Main\Web\HttpClient;
15use Bitrix\Rest\UsageStatTable;
16
20class RepoWidget extends Repo
21{
22 private const SUBTYPE_WIDGET = 'widgetvue';
23
31 protected static function onRegisterBefore(array &$fields, array &$manifest, Error $error): void
32 {
33 if (!isset($fields['WIDGET_PARAMS']) && !is_array($fields['WIDGET_PARAMS']))
34 {
35 $error->addError(
36 'REQUIRED_FIELD_NO_EXISTS',
37 Loc::getMessage('LANDING_WIDGET_FIELD_NO_EXISTS', ['#field#' => 'WIDGET_PARAMS'])
38 );
39
40 return;
41 }
42
43 $requiredParams = [
44 'rootNode',
45 'handler',
46 'demoData',
47 ];
48 foreach ($requiredParams as $param)
49 {
50 if (!isset($fields['WIDGET_PARAMS'][$param]))
51 {
52 $error->addError(
53 'REQUIRED_PARAM_NO_EXISTS',
54 Loc::getMessage('LANDING_WIDGET_PARAM_NO_EXISTS', ['#param#' => $param])
55 );
56
57 return;
58 }
59 }
60
61 // Can set only available fields to manifest. Security!
62 $manifest = [];
63
64 $manifest['block']['type'] = mb_strtolower(Landing\Site\Type::SCOPE_CODE_MAINPAGE);
65 $manifest['block']['subtype'] = self::SUBTYPE_WIDGET;
66 $manifest['block']['subtype_params'] = [
67 'rootNode' => $fields['WIDGET_PARAMS']['rootNode'] ?? null,
68 'demoData' => $fields['WIDGET_PARAMS']['demoData'] ?? null,
69 'handler' => $fields['WIDGET_PARAMS']['handler'] ?? null,
70 'style' => $fields['WIDGET_PARAMS']['style'] ?? null,
71 'lang' => $fields['WIDGET_PARAMS']['lang'] ?? null,
72 ];
73
74 $manifest = Scope\Mainpage::prepareBlockManifest($manifest);
75 }
76
77 // todo: move to non-rest namespace?
83 public static function fetchData(int $blockId, array $params = []): PublicActionResult
84 {
86 $result->setResult(false);
88
89 $block = new Landing\Block($blockId);
90 if (!$block->getId())
91 {
92 $error->addError(
93 'BLOCK_NOT_FOUND',
94 Loc::getMessage('LANDING_WIDGET_BLOCK_NOT_FOUND')
95 );
96 $result->setError($error);
97
98 return $result;
99 }
100
101 if (!Loader::includeModule('rest'))
102 {
103 $error->addError(
104 'REST_NOT_FOUND',
105 Loc::getMessage('LANDING_WIDGET_REST_NOT_FOUND')
106 );
107 $result->setError($error);
108
109 return $result;
110 }
111
112 // check app
113 $repoId = $block->getRepoId();
115 if (
116 !$repoId
117 || empty($app)
118 || !isset($app['CLIENT_ID'])
119 )
120 {
121 $error->addError(
122 'APP_NOT_FOUND',
123 Loc::getMessage('LANDING_WIDGET_APP_NOT_FOUND')
124 );
125 $result->setError($error);
126
127 return $result;
128 }
129
130 // check subtype
131 $manifest = $block->getManifest();
132 if (
133 !in_array(self::SUBTYPE_WIDGET, (array)$manifest['block']['subtype'], true)
134 || !is_array($manifest['block']['subtype_params'])
135 || !isset($manifest['block']['subtype_params']['handler'])
136 )
137 {
138 $error->addError(
139 'HANDLER_NOT_FOUND',
140 Loc::getMessage('LANDING_WIDGET_HANDLER_NOT_FOUND_2')
141 );
142 $result->setError($error);
143
144 return $result;
145 }
146
147 // get auth
149 $app['CLIENT_ID'],
150 'landing',
151 [],
153 );
154 if (isset($auth['error']))
155 {
156 $error->addError(
157 'APP_AUTH_ERROR__' . $auth['error'],
158 $auth['error_description'] ?? ''
159 );
160 $result->setError($error);
161
162 return $result;
163 }
164 $params['auth'] = $auth;
165
166 // request
167 $url = (string)$manifest['block']['subtype_params']['handler'];
168 $http = new HttpClient();
169 $data = $http->post(
170 $url,
171 $params
172 );
173
174 if ($http->getStatus() !== 200)
175 {
176 $error->addError(
177 'HANDLER_NOT_ALLOW',
178 Loc::getMessage('LANDING_WIDGET_HANDLER_NOT_ALLOW')
179 );
180 $result->setError($error);
181
182 return $result;
183 }
184
185 $type = empty($params) ? 'fetch' : 'fetch_params';
186 UsageStatTable::logLandingWidget($app['CLIENT_ID'], $type);
187 UsageStatTable::finalize();
188
189 if (isset($data['error']))
190 {
191 $error->addError(
192 $data['error'],
193 $data['error_description'] ?? ''
194 );
195 $result->setError($error);
196
197 return $result;
198 }
199
200 $result->setResult($data);
201
202 return $result;
203 }
204
211 public static function debug(bool $enable): PublicActionResult
212 {
214 $error = new \Bitrix\Landing\Error;
215 $result->setResult(false);
216
217 $app = PublicAction::restApplication();
218 if (
219 $app
220 && isset($app['CODE'])
221 && !empty(Landing\Repo::getAppByCode($app['CODE']))
222 )
223 {
224 Subtype\WidgetVue::setAppDebug($app['CODE'], $enable);
225 $result->setResult(true);
226 }
227 else
228 {
229 $error->addError(
230 'APP_NOT_FOUND',
231 Loc::getMessage('LANDING_WIDGET_APP_NOT_FOUND')
232 );
233 $result->setError($error);
234 }
235
236 return $result;
237 }
238
244 public static function getAppInfo($code)
245 {
247 $error = new \Bitrix\Landing\Error;
248 $error->addError(
249 'ILLEGAL_METHOD',
250 Loc::getMessage('LANDING_WIDGET_METHOD_NOT_AVAILABLE')
251 );
252 $result->setError($error);
253
254 return $result;
255 }
256
262 public static function bind(array $fields)
263 {
265 $error = new \Bitrix\Landing\Error;
266 $error->addError(
267 'ILLEGAL_METHOD',
268 Loc::getMessage('LANDING_WIDGET_METHOD_NOT_AVAILABLE')
269 );
270 $result->setError($error);
271
272 return $result;
273 }
274
281 public static function unbind($code, $handler = null)
282 {
284 $error = new \Bitrix\Landing\Error;
285 $error->addError(
286 'ILLEGAL_METHOD',
287 Loc::getMessage('LANDING_WIDGET_METHOD_NOT_AVAILABLE')
288 );
289 $result->setError($error);
290
291 return $result;
292 }
293
298 public static function getList(array $params = array()): PublicActionResult
299 {
301
302 $listRes = parent::getList($params);
303 if ($listRes->isSuccess())
304 {
305 $result->setResult([]);
306
307 $listAll = $listRes->getResult();
308 if (is_array($listAll) && !empty($listAll))
309 {
310 $listAll = array_filter($listAll, function ($item) {
311 $isType = isset($item['MANIFEST']['block']['type'])
312 && in_array(
313 mb_strtolower(Landing\Site\Type::SCOPE_CODE_MAINPAGE),
314 (array)$item['MANIFEST']['block']['type'],
315 true
316 )
317 ;
318
319 $isSubtype =
320 isset($item['MANIFEST']['block']['subtype'])
321 && in_array(
322 self::SUBTYPE_WIDGET,
323 (array)$item['MANIFEST']['block']['subtype'],
324 true
325 )
326 ;
327
328 return $isType && $isSubtype;
329 });
330
331 $result->setResult($listAll);
332 }
333
334 return $result;
335 }
336
337 return $listRes;
338 }
339}
$type
Определения options.php:106
static getUserId()
Определения manager.php:107
static fetchData(int $blockId, array $params=[])
Определения repowidget.php:83
static getList(array $params=array())
Определения repowidget.php:298
static debug(bool $enable)
Определения repowidget.php:211
static onRegisterBefore(array &$fields, array &$manifest, Error $error)
Определения repowidget.php:31
static getAppInfo($code)
Определения repowidget.php:244
static bind(array $fields)
Определения repowidget.php:262
static unbind($code, $handler=null)
Определения repowidget.php:281
static getAppByCode($code)
Определения repo.php:302
static getAppInfo(array|int $id)
Определения repo.php:245
static prepareBlockManifest(array $manifest)
Определения mainpage.php:122
static setAppDebug(string $appCode, bool $enable)
Определения widgetvue.php:304
Определения error.php:15
static getAuthProvider()
Определения application.php:19
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$auth
Определения get_user.php:29
$app
Определения proxy.php:8
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
Определения cookies.php:2
Определения agent.php:3
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$error
Определения subscription_card_product.php:20
$url
Определения iframe.php:7
$fields
Определения yandex_run.php:501