Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
demos.php
1<?php
3
4use \Bitrix\Main\Localization\Loc;
5use \Bitrix\Landing\Demos as DemoCore;
6use \Bitrix\Landing\PublicActionResult;
7
8Loc::loadMessages(__FILE__);
9
10class Demos
11{
18 protected static function isItemSuitable(array $item, array $filter = [])
19 {
20 if ($filter)
21 {
22 foreach ($item as $key => $value)
23 {
24 $key = mb_strtoupper($key);
25 if (isset($filter[$key]))
26 {
27 $value = (array)$value;
28 $filter[$key] = (array)$filter[$key];
29 if (!array_intersect($value, $filter[$key]))
30 {
31 return false;
32 }
33 }
34 }
35 }
36
37 return true;
38 }
39
47 protected static function getFilesList($type, $page = false, array $filter = [])
48 {
49 $result = new PublicActionResult();
50
51 if (!is_string($type))
52 {
53 return $result;
54 }
55
56 $componentName = 'bitrix:landing.demo';
57 $className = \CBitrixComponent::includeComponentClass($componentName);
58 $demoCmp = new $className;
59 $demoCmp->initComponent($componentName);
60 $demoCmp->arParams = [
61 'TYPE' => mb_strtoupper($type),
62 'SKIP_REMOTE' => 'Y',
63 ];
64
65 if ($page)
66 {
67 $data = $demoCmp->getDemoPage();
68 }
69 else
70 {
71 $data = $demoCmp->getDemoSite();
72 }
73
74 if (is_array($data))
75 {
76 foreach ($data as $key => &$item)
77 {
78 if (
79 !is_array($item) ||
80 !self::isItemSuitable($item, $filter)
81 )
82 {
83 unset($data[$key]);
84 continue;
85 }
86 if (isset($item['DATA']['items']))
87 {
88 // always convert to UTF-8 for REST
89 $item['DATA']['encoded'] = true;
90 $item['DATA']['charset'] = 'UTF-8';
91 $item['DATA']['items'] = \Bitrix\Main\Text\Encoding::convertEncoding(
92 $item['DATA']['items'],
93 SITE_CHARSET,
94 'UTF-8'
95 );
96 }
97 }
98 unset($item);
99 }
100
101 $result->setResult($data);
102
103 return $result;
104 }
105
112 public static function getSiteList($type, array $filter = [])
113 {
114 $filter = array_change_key_case($filter, CASE_UPPER);
115 return self::getFilesList($type, false, $filter);
116 }
117
124 public static function getPageList($type, array $filter = [])
125 {
126 $filter = array_change_key_case($filter, CASE_UPPER);
127 return self::getFilesList($type, true, $filter);
128 }
129
136 public static function getUrlPreview($code, $type)
137 {
138 $result = new PublicActionResult();
139
140 if (!is_string($code) || !is_string($type))
141 {
142 return $result;
143 }
144
145 $componentName = 'bitrix:landing.demo';
146 $className = \CBitrixComponent::includeComponentClass($componentName);
147 $demoCmp = new $className;
148 $demoCmp->initComponent($componentName);
149 $demoCmp->arParams = array(
150 'TYPE' => mb_strtoupper($type)
151 );
152
153 $result->setResult($demoCmp->getUrlPreview($code));
154
155 return $result;
156 }
157
165 public static function register(array $data = array(), array $params = array())
166 {
167 $result = new PublicActionResult();
168 $error = new \Bitrix\Landing\Error;
169 $themeCode = null;
170 $themeCodeTypo = null;
171
172 // make line array from site and pages
173 if (
174 isset($data['items'])
175 )
176 {
177 if (is_array($data['items']))
178 {
179 $dataPages = $data['items'];
180 }
181 else
182 {
183 $dataPages = array();
184 }
185 unset($data['items']);
186 // set theme codes from sites to pages
187 if (isset($data['fields']['ADDITIONAL_FIELDS']['THEME_CODE']))
188 {
189 $themeCode = $data['fields']['ADDITIONAL_FIELDS']['THEME_CODE'];
190 }
191 if (isset($data['fields']['ADDITIONAL_FIELDS']['THEME_CODE_TYPO']))
192 {
193 $themeCodeTypo = $data['fields']['ADDITIONAL_FIELDS']['THEME_CODE_TYPO'];
194 }
195 foreach ($dataPages as &$page)
196 {
197 if (
198 !isset($page['fields']) ||
199 !is_array($page['fields'])
200 )
201 {
202 $page['fields'] = array();
203 }
204 if (
205 !isset($page['fields']['ADDITIONAL_FIELDS']) ||
206 !is_array($page['fields']['ADDITIONAL_FIELDS'])
207 )
208 {
209 $page['fields']['ADDITIONAL_FIELDS'] = array();
210 }
211 if (!isset($page['fields']['ADDITIONAL_FIELDS']['THEME_CODE']))
212 {
213 $page['fields']['ADDITIONAL_FIELDS']['THEME_CODE'] = $themeCode;
214 }
215 if (!isset($page['fields']['ADDITIONAL_FIELDS']['THEME_CODE_TYPO']))
216 {
217 $page['fields']['ADDITIONAL_FIELDS']['THEME_CODE_TYPO'] = $themeCodeTypo;
218 }
219 }
220 unset($page);
221
222 $data['items'] = array_keys($dataPages);
223 $data['tpl_type'] = DemoCore::TPL_TYPE_SITE;
224 $data = array_merge([$data], $dataPages);
225 }
226
227 if (empty($data) || !is_array($data))
228 {
229 $error->addError(
230 'REGISTER_ERROR_DATA',
231 Loc::getMessage('LANDING_DEMO_REGISTER_ERROR_DATA')
232 );
233 $result->setError($error);
234 return $result;
235 }
236
237 // set app code
238 if (($app = \Bitrix\Landing\PublicAction::restApplication()))
239 {
240 $appCode = $app['CODE'];
241 }
242 else
243 {
244 $appCode = null;
245 }
246
247 $deleteAdded = function(array $added)
248 {
249 foreach ($added as $id)
250 {
251 DemoCore::delete($id);
252 }
253 };
254
255 // add item separate
256 $success = $return = array();
257 $fieldCode = array(
258 'TYPE', 'TPL_TYPE', 'SHOW_IN_LIST', 'TITLE', 'DESCRIPTION',
259 'PREVIEW_URL', 'PREVIEW', 'PREVIEW2X', 'PREVIEW3X'
260 );
261 foreach ($data as $item)
262 {
263 // collect fields
264 $fields = array(
265 'XML_ID' => null,
266 'APP_CODE' => $appCode,
267 'TPL_TYPE' => DemoCore::TPL_TYPE_PAGE,
268 'LANG' => []
269 );
270 if (isset($params['site_template_id']))
271 {
272 $fields['SITE_TEMPLATE_ID'] = $params['site_template_id'];
273 }
274 else
275 {
276 $fields['SITE_TEMPLATE_ID'] = '';
277 }
278 if (isset($item['code']))
279 {
280 $fields['XML_ID'] = trim($item['code']);
281 }
282 if (isset($item['name']))
283 {
284 $fields['TITLE'] = $item['name'];
285 }
286 if (isset($params['lang']))
287 {
288 $fields['LANG']['lang'] = $params['lang'];
289 }
290 if (isset($params['lang_original']))
291 {
292 $fields['LANG']['lang_original'] = $params['lang_original'];
293 }
294 if (isset($item['items']) && !is_array($item['items']))
295 {
296 $item['items'] = [];
297 }
298 foreach ($fieldCode as $code)
299 {
300 $codel = mb_strtolower($code);
301 if (isset($item[$codel]))
302 {
303 $fields[$code] = $item[$codel];
304 }
305 }
306 // serialize and check content
307 $item = (array) $item;
308 $fields['LANG'] = (array) $fields['LANG'];
309 $fields['MANIFEST'] = serialize($item);
310 if ($fields['LANG'])
311 {
312 $fields['LANG'] = serialize($fields['LANG']);
313 }
314 else
315 {
316 unset($fields['LANG']);
317 }
318 if (isset($item['fields']['ADDITIONAL_FIELDS']))
319 {
320 unset($item['fields']['ADDITIONAL_FIELDS']);
321 }
322 \Bitrix\Landing\Manager::sanitize(
323 serialize($item),
324 $bad
325 );
326 if ($bad)
327 {
328 $error->addError(
329 'CONTENT_IS_BAD',
330 Loc::getMessage('LANDING_DEMO_CONTENT_IS_BAD') .
331 ' [code: ' . $fields['XML_ID'] . ']'
332 );
333 $result->setError($error);
334 $deleteAdded($success);
335 return $result;
336 }
337 $check = false;
338 // check unique
339 if ($fields['XML_ID'])
340 {
341 $check = DemoCore::getList(array(
342 'select' => array(
343 'ID'
344 ),
345 'filter' =>
346 isset($fields['APP_CODE'])
347 ? array(
348 '=XML_ID' => $fields['XML_ID'],
349 '=APP_CODE' => $fields['APP_CODE'],
350 '=TPL_TYPE' => $fields['TPL_TYPE']
351 )
352 : array(
353 '=XML_ID' => $fields['XML_ID'],
354 '=TPL_TYPE' => $fields['TPL_TYPE']
355 )
356 )
357 )->fetch();
358 }
359 // register (add / update)
360 if ($check)
361 {
362 $res = DemoCore::update($check['ID'], $fields);
363 }
364 else
365 {
366 $res = DemoCore::add($fields);
367 if ($res->isSuccess())
368 {
369 $success[] = $res->getId();
370 }
371 }
372 if ($res->isSuccess())
373 {
374 $return[] = (int)$res->getId();
375 }
376 else
377 {
378 $error->addFromResult($res);
379 $result->setError($error);
380 $deleteAdded($success);
381 return $result;
382 }
383 }
384
385 $result->setResult($return);
386
387 return $result;
388 }
389
395 public static function unregister($code)
396 {
397 $result = new PublicActionResult();
398 $error = new \Bitrix\Landing\Error;
399
400 $result->setResult(false);
401
402 if (!is_string($code))
403 {
404 return $result;
405 }
406
407 // search and delete
408 if ($code)
409 {
410 // set app code
411 $app = \Bitrix\Landing\PublicAction::restApplication();
412
413 $res = DemoCore::getList(array(
414 'select' => array(
415 'ID'
416 ),
417 'filter' =>
418 isset($app['CODE'])
419 ? array(
420 '=XML_ID' => $code,
421 '=APP_CODE' => $app['CODE']
422 )
423 : array(
424 '=XML_ID' => $code
425 )
426 ));
427 while ($row = $res->fetch())
428 {
429 // delete block from repo
430 $resDel = DemoCore::delete($row['ID']);
431 if ($resDel->isSuccess())
432 {
433 $result->setResult(true);
434 }
435 else
436 {
437 $error->addFromResult($resDel);
438 $result->setError($error);
439 return $result;
440 }
441 }
442 }
443
444 return $result;
445 }
446
452 public static function getList(array $params = array())
453 {
454 $result = new PublicActionResult();
455 $params = $result->sanitizeKeys($params);
456
457 if (!is_array($params))
458 {
459 $params = array();
460 }
461 if (
462 !isset($params['filter']) ||
463 !is_array($params['filter'])
464 )
465 {
466 $params['filter'] = array();
467 }
468
469 // set app code
470 if (($app = \Bitrix\Landing\PublicAction::restApplication()))
471 {
472 $params['filter']['=APP_CODE'] = $app['CODE'];
473 }
474
475 $data = array();
476 $res = DemoCore::getList($params);
477 while ($row = $res->fetch())
478 {
479 if (isset($row['DATE_CREATE']))
480 {
481 $row['DATE_CREATE'] = (string) $row['DATE_CREATE'];
482 }
483 if (isset($row['DATE_MODIFY']))
484 {
485 $row['DATE_MODIFY'] = (string) $row['DATE_MODIFY'];
486 }
487 $row['MANIFEST'] = unserialize($row['MANIFEST'], ['allowed_classes' => false]);
488 if ($row['LANG'])
489 {
490 $row['LANG'] = unserialize($row['LANG'], ['allowed_classes' => false]);
491 }
492 $data[] = $row;
493 }
494 $result->setResult($data);
495
496 return $result;
497 }
498}
const TPL_TYPE_SITE
Definition demos.php:9
static getFilesList($type, $page=false, array $filter=[])
Definition demos.php:47
static getList(array $params=array())
Definition demos.php:452
static getUrlPreview($code, $type)
Definition demos.php:136
static isItemSuitable(array $item, array $filter=[])
Definition demos.php:18
static getSiteList($type, array $filter=[])
Definition demos.php:112
static getPageList($type, array $filter=[])
Definition demos.php:124
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29