1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
cookies.php
См. документацию.
1<?php
2namespace Bitrix\Landing\Hook\Page;
3
4use \Bitrix\Landing\Help;
5use \Bitrix\Landing\Rights;
6use \Bitrix\Landing\Site;
7use \Bitrix\Landing\Field;
8use \Bitrix\Landing\Manager;
9use \Bitrix\Main\Localization\Loc;
10
12{
16 const MODE_A = 'A';
17 const MODE_I = 'I';
18
23 public static $enabled = false;
24
29 protected function getMap(): array
30 {
31 $helpUrl = Help::getHelpUrl('COOKIES_EDIT');
32
33 return [
34 'USE' => new Field\Checkbox('USE', [
35 'title' => Loc::getMessage('LANDING_HOOK_COOKIES_USE')
36 ]),
37 'AGREEMENT_ID' => new Field\Checkbox('AGREEMENT_ID', [
38 'title' => Loc::getMessage('LANDING_HOOK_COOKIES_AGREEMENT_ID')
39 ]),
40 'COLOR_BG' => new Field\Text('COLOR_BG', [
41 'title' => Loc::getMessage('LANDING_HOOK_COOKIES_COLOR_BG'),
42 'default' => '#03c1fe'
43 ]),
44 'COLOR_TEXT' => new Field\Text('COLOR_TEXT', [
45 'title' => Loc::getMessage('LANDING_HOOK_COOKIES_COLOR_TEXT'),
46 'default' => '#fff'
47 ]),
48 'POSITION' => new Field\Select('POSITION', [
49 'title' => Loc::getMessage('LANDING_HOOK_COOKIES_POSITION'),
50 'default' => 'bottom_left',
51 'options' => [
52 'bottom_left' => Loc::getMessage('LANDING_HOOK_COOKIES_POSITION_BL'),
53 'bottom_right' => Loc::getMessage('LANDING_HOOK_COOKIES_POSITION_BR'),
54 ]
55 ]),
56 'MODE' => new Field\Select('MODE', [
57 'title' => Loc::getMessage('LANDING_HOOK_COOKIES_MODE'),
58 'default' => 'A',
59 'options' => [
60 self::MODE_A => Loc::getMessage('LANDING_HOOK_COOKIES_MODE_A'),
61 self::MODE_I => Loc::getMessage('LANDING_HOOK_COOKIES_MODE_I'),
62 ],
63 'help' => $helpUrl
64 ? '<a href="' . $helpUrl . '" target="_blank">' .
65 Loc::getMessage('LANDING_HOOK_COOKIES_MODE_HELP') .
66 '</a>'
67 : ''
68 ])
69 ];
70 }
71
76 public function getTitle(): string
77 {
78 return Loc::getMessage('LANDING_HOOK_COOKIES_TITLE');
79 }
80
85 public function enabledInEditMode(): bool
86 {
87 return false;
88 }
89
94 public function getSort()
95 {
96 return 50;
97 }
98
103 public function enabled(): bool
104 {
105 // this hook are enabled always
106 return !$this->isPage();
107 }
108
113 public function isInformationMode(): bool
114 {
115 $mode = $this->fields['MODE']->getValue();
116 return $mode == self::MODE_I || (!$mode && Manager::availableOnlyForZone('ru'));
117 }
118
123 public function exec(): void
124 {
125 if ($this->execCustom())
126 {
127 return;
128 }
129
130 if ($this->fields['USE']->getValue() == 'Y')
131 {
132 $infoMode = $this->isInformationMode();
133
134 if (!$infoMode)
135 {
136 self::$enabled = true;
137 Manager::clearPageView('Noscript');
138 }
139
140 ob_start();
141 Manager::getApplication()->includeComponent(
142 'bitrix:landing.cookies',
143 '',
144 [
145 'USE' => $this->fields['USE']->getValue(),
146 'POSITION' => $this->fields['POSITION']->getValue(),
147 'COLOR_BG' => $this->fields['COLOR_BG']->getValue(),
148 'COLOR_TEXT' => $this->fields['COLOR_TEXT']->getValue(),
149 'AGREEMENT_ID' => $this->fields['AGREEMENT_ID']->getValue(),
150 'INFORMATION' => $infoMode ? 'Y' : 'N'
151 ],
152 false
153 );
154 $hookContent = ob_get_contents();
155 ob_end_clean();
156
157 echo $hookContent;
158 }
159 }
160
167 public static function addCookieScript(string $cookieCode, string $functionBody): void
168 {
169 if (self::$enabled)
170 {
171 Manager::setPageView('AfterHeadOpen',
172 '<script data-skip-moving="true">
173 window["bxCookies"] = window["bxCookies"] || {};
174 window["bxCookies"]["' . $cookieCode . '"] = false;
175 window.addEventListener("load", function() {
176 BX.addCustomEvent(
177 "BX.Landing.Cookies:onAccept",
178 function(acceptedCookies)
179 {
180 if (
181 !window["bxCookies"]["' . $cookieCode . '"] &&
182 BX.util.in_array("' . $cookieCode . '", acceptedCookies)
183 )
184 {
185 window["bxCookies"]["' . $cookieCode . '"] = true;
186 ' . $functionBody . '
187 }
188 }
189 );
190 });
191 </script>'
192 );
193 }
194 else
195 {
196 {
197 $scriptContent = '<script data-skip-moving="true">' . $functionBody . '</script>';
198
199 if ($cookieCode === 'ym')
200 {
201 $scriptContent = '<!-- Yandex.Metrika counter -->' . "\n" . $scriptContent . "\n" . '<!-- /Yandex.Metrika counter -->';
202 }
203
204 Manager::setPageView('AfterHeadOpen', $scriptContent);
205 }
206 }
207 }
208
214 public static function getAgreementIdBySiteId(int $siteId): ?int
215 {
217 $fields = Site::getAdditionalFields($siteId);
219
220 $mode = $fields['COOKIES_MODE']->getValue();
221 $informationMode = $mode == self::MODE_I || (!$mode && Manager::availableOnlyForZone('ru'));
222
223 if ($informationMode)
224 {
225 return null;
226 }
227
228 return isset($fields['COOKIES_AGREEMENT_ID'])
229 ? $fields['COOKIES_AGREEMENT_ID']->getValue()
230 : null;
231 }
232}
static getHelpUrl(string $code)
Определения help.php:174
static getAgreementIdBySiteId(int $siteId)
Определения cookies.php:214
static $enabled
Определения cookies.php:23
static addCookieScript(string $cookieCode, string $functionBody)
Определения cookies.php:167
isPage()
Определения page.php:66
$fields
Определения page.php:18
execCustom()
Определения page.php:262
static getApplication()
Определения manager.php:71
static availableOnlyForZone(string $zone)
Определения manager.php:980
static setPageView(string $marker, string $content, bool $skipTrim=false)
Определения manager.php:470
static clearPageView($marker)
Определения manager.php:493
static setOff()
Определения rights.php:89
static setOn()
Определения rights.php:98
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$siteId
Определения ajax.php:8