PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/components/bitrix/landing.start/component.php

https://gitlab.com/alexprowars/bitrix
PHP | 542 lines | 481 code | 34 blank | 27 comment | 56 complexity | 7ede2df30fdbb2338ef5c0208b7cf3d3 MD5 | raw file
  1. <?php
  2. if (!defined('B_PROLOG_INCLUDED') || B_PROLOG_INCLUDED!==true)
  3. {
  4. die();
  5. }
  6. use \Bitrix\Crm\WebForm\Preset;
  7. use \Bitrix\Landing\Rights;
  8. use \Bitrix\Landing\Block;
  9. use \Bitrix\Landing\Manager;
  10. use \Bitrix\Main\Localization\Loc;
  11. use \Bitrix\Main\Application;
  12. use \Bitrix\Main\Web\Uri;
  13. use \Bitrix\Main\Loader;
  14. use \Bitrix\Main\SiteTemplateTable;
  15. use \Bitrix\Main\UserConsent\Consent;
  16. use \Bitrix\Main\UserConsent\Agreement;
  17. use \Bitrix\Main\UserConsent\Internals\AgreementTable;
  18. use \Bitrix\Main\UserConsent\Internals\ConsentTable;
  19. Loc::loadMessages(__FILE__);
  20. if (!Loader::includeModule('landing'))
  21. {
  22. Showerror(Loc::getMessage('LANDING_CMP_MODULE_NOT_INSTALLED'));
  23. return;
  24. }
  25. // something about crm
  26. if (Loader::includeModule('crm'))
  27. {
  28. // set webform presets
  29. if (Preset::checkVersion())
  30. {
  31. $preset = new Preset();
  32. $preset->install();
  33. }
  34. // install demo data for crm
  35. if (!CAllCrmInvoice::installExternalEntities())
  36. {
  37. Showerror(Loc::getMessage('LANDING_CMP_MODULE_NOT_INSTALLED_CRM'));
  38. return;
  39. }
  40. }
  41. // refresh block repo
  42. Block::getRepository();
  43. $arParams['TYPE'] = isset($arParams['TYPE']) ? $arParams['TYPE'] : '';
  44. $arParams['STRICT_TYPE'] = isset($arParams['STRICT_TYPE']) ? $arParams['STRICT_TYPE'] : 'N';
  45. Manager::setPageTitle(
  46. Loc::getMessage('LANDING_CMP_TITLE')
  47. );
  48. if (!\Bitrix\Landing\Site\Type::isEnabled($arParams['TYPE']))
  49. {
  50. Showerror(Loc::getMessage('LANDING_CMP_TYPE_IS_NOT_ENABLED'));
  51. return;
  52. }
  53. \Bitrix\Landing\Site\Type::setScope(
  54. $arParams['TYPE']
  55. );
  56. // check rights
  57. \Bitrix\Landing\Role::checkRequiredRoles();
  58. if (Loader::includeModule('bitrix24'))
  59. {
  60. if (
  61. Manager::getOption('temp_permission_admin_only')
  62. && !\CBitrix24::isPortalAdmin(Manager::getUserId())
  63. )
  64. {
  65. Manager::getApplication()->showAuthForm(
  66. Loc::getMessage('LANDING_CMP_ACCESS_DENIED2')
  67. );
  68. return;
  69. }
  70. }
  71. if (!Rights::hasAdditionalRight(Rights::ADDITIONAL_RIGHTS['menu24'], null, true))
  72. {
  73. Manager::getApplication()->showAuthForm(
  74. Loc::getMessage('LANDING_CMP_ACCESS_DENIED2')
  75. );
  76. return;
  77. }
  78. // preset paths and sef (.parameters.php)
  79. $defaultUrlTemplates404 = array(
  80. 'sites' => '',
  81. 'site_show' => 'site/#site_show#/',
  82. 'site_edit' => 'site/edit/#site_edit#/',
  83. 'site_design' => 'site/design/#site_edit#/',
  84. 'site_master' => 'site/master/#site_edit#/',
  85. 'site_contacts' => 'site/contacts/#site_edit#/',
  86. 'site_domain' => 'site/domain/#site_edit#/',
  87. 'site_domain_switch' => 'site/domain_switch/#site_edit#/',
  88. 'site_cookies' => 'site/cookies/#site_edit#/',
  89. 'landing_edit' => 'site/#site_show#/edit/#landing_edit#/',
  90. 'landing_design' => 'site/#site_show#/design/#landing_edit#/',
  91. 'landing_view' => 'site/#site_show#/view/#landing_edit#/',
  92. 'domains' => 'domains/',
  93. 'domain_edit' => 'domain/edit/#domain_edit#/',
  94. 'roles' => 'roles/',
  95. 'notes' => 'notes/',
  96. 'role_edit' => 'role/edit/#role_edit#/',
  97. 'folder_edit' => 'folder/edit/#folder_edit#/'
  98. );
  99. $urlTpls = array(
  100. 'sites' => array(),
  101. 'site_show' => array('site_show'),
  102. 'site_edit' => array('site_edit'),
  103. 'site_design' => array('site_edit'),
  104. 'site_master' => array('site_edit'),
  105. 'site_contacts' => array('site_edit'),
  106. 'site_domain' => array('site_edit'),
  107. 'site_domain_switch' => array('site_edit'),
  108. 'site_cookies' => array('site_edit'),
  109. 'landing_edit' => array('landing_edit', 'site_show'),
  110. 'landing_design' => array('landing_edit', 'site_show'),
  111. 'landing_view' => array('landing_edit', 'site_show'),
  112. 'domains' => array(),
  113. 'domain_edit' => array('domain_edit'),
  114. 'roles' => array(),
  115. 'notes' => array(),
  116. 'role_edit' => array('role_edit'),
  117. 'folder_edit' => array('folder_edit')
  118. );
  119. // init vars
  120. $variables = array();
  121. $componentPage = '';
  122. $curPage = '';
  123. $request = Application::getInstance()->getContext()->getRequest();
  124. $uriString = $request->getRequestUri();
  125. $uriPage = $request->getRequestedPage();
  126. $landingTypes = \Bitrix\Landing\Site::getTypes();
  127. // template vars
  128. $arResult['AGREEMENT'] = array();
  129. $arResult['AGREEMENT_ACCEPTED'] = false;
  130. $arResult['CHECK_FEATURE_PERM'] = \Bitrix\Landing\Restriction\Manager::isAllowed('limit_sites_access_permissions');
  131. $arParams['ACTION_FOLDER'] = isset($arParams['ACTION_FOLDER']) ? $arParams['ACTION_FOLDER'] : 'folderId';
  132. $arParams['SEF_MODE'] = isset($arParams['SEF_MODE']) ? $arParams['SEF_MODE'] : 'Y';
  133. $arParams['SEF_FOLDER'] = isset($arParams['SEF_FOLDER']) ? $arParams['SEF_FOLDER'] : '/';
  134. $arParams['SEF_URL_TEMPLATES'] = isset($arParams['SEF_URL_TEMPLATES']) ? $arParams['SEF_URL_TEMPLATES'] : array();
  135. $arParams['VARIABLE_ALIASES'] = isset($arParams['VARIABLE_ALIASES']) ? $arParams['VARIABLE_ALIASES'] : array();
  136. $arParams['TILE_LANDING_MODE'] = isset($arParams['TILE_LANDING_MODE']) ? $arParams['TILE_LANDING_MODE'] : 'edit';
  137. $arParams['TILE_SITE_MODE'] = isset($arParams['TILE_SITE_MODE']) ? $arParams['TILE_SITE_MODE'] : 'list';
  138. $arParams['EDIT_FULL_PUBLICATION'] = isset($arParams['EDIT_FULL_PUBLICATION']) ? $arParams['EDIT_FULL_PUBLICATION'] : 'N';
  139. $arParams['EDIT_PANEL_LIGHT_MODE'] = isset($arParams['EDIT_PANEL_LIGHT_MODE']) ? $arParams['EDIT_PANEL_LIGHT_MODE'] : 'N';
  140. $arParams['EDIT_DONT_LEAVE_FRAME'] = isset($arParams['EDIT_DONT_LEAVE_FRAME']) ? $arParams['EDIT_DONT_LEAVE_FRAME'] : 'N';
  141. $arParams['REOPEN_LOCATION_IN_SLIDER'] = isset($arParams['REOPEN_LOCATION_IN_SLIDER']) ? $arParams['REOPEN_LOCATION_IN_SLIDER'] : 'N';
  142. $arParams['DRAFT_MODE'] = isset($arParams['DRAFT_MODE']) ? $arParams['DRAFT_MODE'] : 'N';
  143. foreach ($defaultUrlTemplates404 as $pageCode => $pagePath)
  144. {
  145. if (!isset($arParams['SEF_URL_TEMPLATES'][$pageCode]))
  146. {
  147. $arParams['SEF_URL_TEMPLATES'][$pageCode] = $pagePath;
  148. }
  149. }
  150. if (!$arParams['TYPE'] || !isset($landingTypes[$arParams['TYPE']]))
  151. {
  152. $arParams['TYPE'] = \Bitrix\Landing\Site::getDefaultType();
  153. }
  154. if (!isset($arParams['SHOW_MENU']))
  155. {
  156. $arParams['SHOW_MENU'] = ($arParams['TYPE'] == 'STORE') ? 'N' : 'Y';
  157. }
  158. // sef / not sef modes
  159. if ($arParams['SEF_MODE'] == 'Y')
  160. {
  161. $defaultVariableAliases404 = [];
  162. $componentVariables = [];
  163. // resolve variables, values and template page
  164. $urlTemplates = \CComponentEngine::makeComponentUrlTemplates(
  165. $defaultUrlTemplates404,
  166. $arParams['SEF_URL_TEMPLATES']
  167. );
  168. $variableAliases = \CComponentEngine::makeComponentVariableAliases(
  169. $defaultVariableAliases404,
  170. $arParams['VARIABLE_ALIASES']
  171. );
  172. $componentPage = \CComponentEngine::parseComponentPath(
  173. $arParams['SEF_FOLDER'],
  174. $urlTemplates,
  175. $variables
  176. );
  177. \CComponentEngine::initComponentVariables(
  178. $componentPage,
  179. $componentVariables,
  180. $variableAliases,
  181. $variables
  182. );
  183. // build urls by rules
  184. foreach ($urlTpls as $code => $var)
  185. {
  186. $arParams['PAGE_URL_'.mb_strtoupper($code)] = $arParams['SEF_FOLDER'] . $urlTemplates[$code];
  187. }
  188. }
  189. else
  190. {
  191. // default variable aliases
  192. $defaultVariableAliases = [
  193. 'page' => 'page'
  194. ];
  195. foreach ($urlTpls as $key => $vars)
  196. {
  197. foreach ($vars as $var)
  198. {
  199. $defaultVariableAliases[$var] = $var;
  200. }
  201. }
  202. // resolve variables and values
  203. $variableAliases = \CComponentEngine::makeComponentVariableAliases(
  204. $defaultVariableAliases,
  205. $arParams['VARIABLE_ALIASES']
  206. );
  207. \CComponentEngine::initComponentVariables(
  208. false,
  209. $defaultVariableAliases,
  210. $variableAliases,
  211. $variables
  212. );
  213. // resolve template page
  214. if (isset($variables['page']) && isset($urlTpls[$variables['page']]))
  215. {
  216. $componentPage = $variables['page'];
  217. if (!$defaultUrlTemplates404[$componentPage])
  218. {
  219. $componentPage = '';
  220. }
  221. }
  222. // build urls by rules
  223. foreach ($urlTpls as $code => $vars)
  224. {
  225. $paramCode = 'PAGE_URL_' . mb_strtoupper($code);
  226. $uri = new Uri($uriPage);
  227. $uri->addParams(['page' => $code]);
  228. foreach ($vars as $var)
  229. {
  230. if (isset($defaultVariableAliases[$var]))
  231. {
  232. $uri->addParams([$var => '#' . $var . '#']);
  233. }
  234. }
  235. $arParams[$paramCode] = urldecode($uri->getUri());
  236. }
  237. }
  238. $arResult['VARS'] = $variables;
  239. // check rules for templates
  240. if (
  241. $arParams['SEF_MODE'] == 'Y' &&
  242. isset($arParams['PAGE_URL_LANDING_VIEW'])
  243. )
  244. {
  245. $condition = $arParams['PAGE_URL_LANDING_VIEW'];
  246. $condition = str_replace(
  247. array('#site_show#', '#landing_edit#'),
  248. '[\\d]+',
  249. $condition
  250. );
  251. $condition = 'preg_match(\'#' . $condition . '#\', ' .
  252. '$GLOBALS[\'APPLICATION\']->GetCurPage(0))';
  253. $res = SiteTemplateTable::getList(array(
  254. 'select' => array(
  255. 'ID'
  256. ),
  257. 'filter' => array(
  258. '=SITE_ID' => SITE_ID,
  259. '=CONDITION' => $condition
  260. )
  261. ));
  262. if (!$res->fetch())
  263. {
  264. SiteTemplateTable::add(array(
  265. 'TEMPLATE' => Manager::getTemplateId(SITE_ID),
  266. 'SITE_ID' => SITE_ID,
  267. 'SORT' => 500,
  268. 'CONDITION' => $condition
  269. ));
  270. Manager::getCacheManager()->clean('b_site_template');
  271. if ($componentPage == 'landing_view')
  272. {
  273. \localRedirect(Manager::getApplication()->getCurPage());
  274. }
  275. }
  276. }
  277. // rights
  278. $arResult['ACCESS_PAGE_NEW'] = 'N';
  279. $arResult['ACCESS_SITE_NEW'] = 'N';
  280. $arResult['ACCESS_SITE_SETTINGS'] = 'N';
  281. $rights = Rights::getOperationsForSite(0);
  282. if (
  283. isset($arResult['VARS']['site_show']) &&
  284. $arResult['VARS']['site_show']
  285. )
  286. {
  287. $arResult['ACCESS_PAGE_NEW'] = Rights::hasAccessForSite(
  288. $arResult['VARS']['site_show'],
  289. Rights::ACCESS_TYPES['edit']
  290. )
  291. ? 'Y' : 'N';
  292. $arResult['ACCESS_SITE_SETTINGS'] = in_array(Rights::ACCESS_TYPES['sett'], $rights)
  293. ? 'Y' : 'N';
  294. }
  295. else
  296. {
  297. $arResult['ACCESS_SITE_NEW'] = (
  298. Rights::hasAdditionalRight(Rights::ADDITIONAL_RIGHTS['create']) &&
  299. in_array(Rights::ACCESS_TYPES['edit'], $rights)
  300. )
  301. ? 'Y' : 'N';
  302. }
  303. // disable domain's pages in the cloud
  304. if ($componentPage == 'domains' || $componentPage == 'domain_edit')
  305. {
  306. $componentPage = '';
  307. }
  308. // only AGREEMENTS below
  309. if (
  310. $request->get('landing_mode') ||
  311. !Manager::isB24()
  312. )
  313. {
  314. $this->IncludeComponentTemplate($componentPage);
  315. return;
  316. }
  317. $currentLang = LANGUAGE_ID;
  318. $currentZone = Manager::getZone();
  319. $agreementCode = 'landing_agreement';
  320. $agreementsId = array();
  321. $agreements = array(
  322. 'en' => array(),
  323. $currentLang => array()
  324. );
  325. $virtualLangs = array(
  326. 'ua' => 'ru',
  327. 'by' => 'ru',
  328. 'kz' => 'ru'
  329. );
  330. if (isset($agreements['es']))
  331. {
  332. $virtualLangs['la'] = 'es';
  333. }
  334. // lang zone is in CIS
  335. $cis = $currentZone == 'by' || $currentZone == 'kz';
  336. // actual from lang-file
  337. foreach ($agreements as $lng => $item)
  338. {
  339. if ($cis)
  340. {
  341. $mess = Loc::loadLanguageFile(
  342. __DIR__ . '/component_' . $currentZone . '.php',
  343. 'ru'
  344. );
  345. }
  346. else
  347. {
  348. $mess = Loc::loadLanguageFile(__FILE__, $lng);
  349. }
  350. if ($mess)
  351. {
  352. $agreements[$lng] = array(
  353. 'ID' => 0,
  354. 'NAME' => isset($mess['LANDING_CMP_AGREEMENT_NAME'])
  355. ? $mess['LANDING_CMP_AGREEMENT_NAME']
  356. : '',
  357. 'TEXT' => isset($mess['LANDING_CMP_AGREEMENT_TEXT3'])
  358. ? $mess['LANDING_CMP_AGREEMENT_TEXT3']
  359. : '',
  360. 'LANGUAGE_ID' => $lng
  361. );
  362. }
  363. }
  364. // check actual agreements
  365. $needToUpdate = Manager::getOption('user_agreement_version') <
  366. Manager::USER_AGREEMENT_VERSION;
  367. if ($needToUpdate)
  368. {
  369. Manager::setOption(
  370. 'user_agreement_version',
  371. Manager::USER_AGREEMENT_VERSION
  372. );
  373. }
  374. // current from database (actualize in db)
  375. $res = AgreementTable::getList(array(
  376. 'select' => array(
  377. 'ID',
  378. 'NAME',
  379. 'TEXT' => 'AGREEMENT_TEXT',
  380. 'LANGUAGE_ID',
  381. 'LABEL_TEXT'
  382. ),
  383. 'filter' => array(
  384. '=ACTIVE' => 'Y',
  385. '=CODE' => $agreementCode,
  386. '=LANGUAGE_ID' => array_keys($agreements)
  387. )
  388. ));
  389. while ($row = $res->fetch())
  390. {
  391. if ($needToUpdate)
  392. {
  393. AgreementTable::delete($row['ID']);
  394. continue;
  395. }
  396. $agreementsId[] = $row['ID'];
  397. $actual = $agreements[$row['LANGUAGE_ID']];
  398. if (
  399. $row['NAME'] != $actual['NAME'] ||
  400. $row['TEXT'] != $actual['TEXT']
  401. )
  402. {
  403. AgreementTable::update($row['ID'], [
  404. 'NAME' => $actual['NAME'],
  405. 'AGREEMENT_TEXT' => $actual['TEXT'],
  406. 'LABEL_TEXT' => Loc::getMessage('LANDING_CMP_AGREEMENT_LABEL'),
  407. 'IS_AGREEMENT_TEXT_HTML' => 'Y'
  408. ]);
  409. }
  410. else if (!$row['LABEL_TEXT'])
  411. {
  412. AgreementTable::update($row['ID'], [
  413. 'LABEL_TEXT' => Loc::getMessage('LANDING_CMP_AGREEMENT_LABEL'),
  414. 'IS_AGREEMENT_TEXT_HTML' => 'Y'
  415. ]);
  416. }
  417. $agreements[$row['LANGUAGE_ID']]['ID'] = $row['ID'];
  418. }
  419. // add new to db
  420. foreach ($agreements as $lng => $agreement)
  421. {
  422. if (!$agreement['ID'])
  423. {
  424. $res = AgreementTable::add(array(
  425. 'CODE' => $agreementCode,
  426. 'LANGUAGE_ID' => $lng,
  427. 'TYPE' => Agreement::TYPE_CUSTOM,
  428. 'NAME' => $agreement['NAME'],
  429. 'AGREEMENT_TEXT' => $agreement['TEXT'],
  430. 'LABEL_TEXT' => Loc::getMessage('LANDING_CMP_AGREEMENT_LABEL'),
  431. 'IS_AGREEMENT_TEXT_HTML' => 'Y'
  432. ));
  433. if ($res->isSuccess())
  434. {
  435. $agreements[$lng]['ID'] = $res->getId();
  436. }
  437. }
  438. }
  439. if (
  440. !empty($agreements[$currentLang]) &&
  441. $agreements[$currentLang]['ID']
  442. )
  443. {
  444. $arResult['AGREEMENT'] = $agreements[$currentLang];
  445. }
  446. elseif (
  447. isset($virtualLangs[$currentLang]) &&
  448. !empty($agreements[$virtualLangs[$currentLang]]) &&
  449. $agreements[$virtualLangs[$currentLang]]['ID']
  450. )
  451. {
  452. $arResult['AGREEMENT'] = $agreements[$virtualLangs[$currentLang]];
  453. }
  454. elseif (
  455. !empty($agreements['en']) &&
  456. $agreements['en']['ID']
  457. )
  458. {
  459. $arResult['AGREEMENT'] = $agreements['en'];
  460. }
  461. else
  462. {
  463. $redirectIfUnAccept = true;
  464. }
  465. // check accepted
  466. $res = ConsentTable::getList(array(
  467. 'filter' => array(
  468. 'USER_ID' => Manager::getUserId(),
  469. 'AGREEMENT_ID' => $agreementsId
  470. )
  471. ));
  472. if ($res->fetch())
  473. {
  474. $redirectIfUnAccept = false;
  475. $arResult['AGREEMENT_ACCEPTED'] = true;
  476. }
  477. // accept
  478. if (
  479. $request->get('action') == 'accept_agreement' &&
  480. !empty($arResult['AGREEMENT']) &&
  481. check_bitrix_sessid()
  482. )
  483. {
  484. Consent::addByContext(
  485. $arResult['AGREEMENT']['ID']
  486. );
  487. LocalRedirect($uriString);
  488. }
  489. // if not accept and don't exist agreement
  490. if (
  491. isset($redirectIfUnAccept) &&
  492. $redirectIfUnAccept === true
  493. )
  494. {
  495. LocalRedirect(SITE_DIR, true);
  496. }
  497. if (!empty($arParams['PAGE_URL_SITE_SHOW']))
  498. {
  499. $url = Manager::getOption('tmp_last_show_url', '');
  500. if ($url !== $arParams['PAGE_URL_SITE_SHOW'])
  501. {
  502. Manager::setOption(
  503. 'tmp_last_show_url',
  504. $arParams['PAGE_URL_SITE_SHOW']
  505. );
  506. }
  507. }
  508. $this->IncludeComponentTemplate($componentPage);