Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
cookies.php
1<?php
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 Manager::setPageView('AfterHeadOpen',
197 '<script data-skip-moving="true">
198 ' . $functionBody . '
199 </script>'
200 );
201 }
202 }
203
209 public static function getAgreementIdBySiteId(int $siteId): ?int
210 {
214
215 $mode = $fields['COOKIES_MODE']->getValue();
216 $informationMode = $mode == self::MODE_I || (!$mode && Manager::availableOnlyForZone('ru'));
217
218 if ($informationMode)
219 {
220 return null;
221 }
222
223 return isset($fields['COOKIES_AGREEMENT_ID'])
224 ? $fields['COOKIES_AGREEMENT_ID']->getValue()
225 : null;
226 }
227}
static getHelpUrl(string $code)
Definition help.php:268
static getAgreementIdBySiteId(int $siteId)
Definition cookies.php:209
static addCookieScript(string $cookieCode, string $functionBody)
Definition cookies.php:167
static getApplication()
Definition manager.php:71
static availableOnlyForZone(string $zone)
Definition manager.php:975
static setPageView(string $marker, string $content, bool $skipTrim=false)
Definition manager.php:465
static clearPageView($marker)
Definition manager.php:488
static getAdditionalFields($id)
Definition site.php:250
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29