Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
chatsales.php
1<?php
2
4
14
15Loc::loadMessages(__FILE__);
16Loc::loadMessages(\Bitrix\Landing\Manager::getDocRoot() . '/bitrix/components/bitrix/landing.demo/data/page/store-chats-dark/catalog/.description.php');
17
18class ChatSales extends Update
19{
23 private const ONLY_CODES = [
24 'store-chats-dark',
25 'store-chats-light',
26 'store-chats',
27 ];
28
29 private const FOOTER_LINKS_MAP = [
30 'store-chats-dark/about' => '#landing1',
31 'store-chats-dark/contacts' => '#landing2',
32 'store-chats-dark/cutaway' => '#landing3',
33 'store-chats-dark/payinfo' => '#landing8',
34 'store-chats-dark/webform' => '#landing9',
35 ];
36 private const FOOTER_BLOCK_CODE = '55.1.list_of_links';
37
43 private static function createFolder(int $siteId): ?int
44 {
45 $catalogFolderId = null;
46
47 $folders = Site::getFolders($siteId);
48 $translitCode = \CUtil::translit(
49 Loc::getMessage('LANDING_DEMO_STORE_CHATS_CATALOG-NAME'),
50 LANGUAGE_ID,
51 [
52 'replace_space' => '',
53 'replace_other' => ''
54 ]
55 );
56 foreach ($folders as $folder)
57 {
58 if (
59 $folder['CODE'] === 'catalog'
60 || $folder['CODE'] === 'katalog'
61 || $folder['CODE'] === $translitCode
62 )
63 {
64 $catalogFolderId = $folder['ID'];
65 }
66 }
67
68 if (!$catalogFolderId)
69 {
70 $res = Site::addFolder($siteId, [
71 'TITLE' => Loc::getMessage('LANDING_SITE_UPDATE_CHAT_SALE_FOLDER_NAME'),
72 'CODE' => 'catalog',
73 ]);
74 if (!$res->isSuccess())
75 {
76 return false;
77 }
78 $catalogFolderId = $res->getId();
79 }
80
81 return $catalogFolderId;
82 }
83
91 private static function createPageIfNotExists(int $siteId, int $catalogFolderId, string $code): ?int
92 {
93 // find or create
94 $res = Landing::getList([
95 'select' => [
96 'ID',
97 ],
98 'filter' => [
99 'DELETED' => 'N',
100 'SITE_ID' => $siteId,
101 '=TPL_CODE' => $code,
102 'CHECK_PERMISSIONS' => 'N',
103 ],
104 ]);
105 if ($row = $res->fetch())
106 {
107 $pageId = $row['ID'];
108
109 return $pageId;
110 }
111
112 $res = Landing::addByTemplate($siteId, $code, [
113 'FOLDER_ID' => $catalogFolderId,
114 'SITE_TYPE' => 'STORE',
115 ]);
116 $pageId = $res->getId();
117
118 if (
119 $pageId
120 && Landing::createInstance($pageId)->publication()
121 )
122 {
123 return $pageId;
124 }
125
126 return null;
127 }
128
129 private static function setTemplateToLanding(int $landingId, string $templateName, array $templateReferences): bool
130 {
131 $resTemplate = Template::getList([
132 'select' => [
133 'ID', 'XML_ID',
134 ],
135 'filter' => [
136 'XML_ID' => $templateName,
137 ],
138 ]);
139 if ($template = $resTemplate->fetch())
140 {
141 $resUpdate = Landing::update($landingId, [
142 'TPL_ID' => $template['ID'],
143 ]);
144
145 if ($resUpdate->isSuccess())
146 {
147 TemplateRef::setForLanding($landingId, $templateReferences);
148
149 return true;
150 }
151 }
152
153 return false;
154 }
155
156 private static function setIndexToFolder(int $folderId, int $indexId): void
157 {
158 $landing = Landing::createInstance($indexId);
159 if ($landing)
160 {
161 Folder::update($folderId, [
162 'INDEX_ID' => $indexId,
163 ]);
164 }
165 }
166
172 public static function update(int $siteId): bool
173 {
174 $site = self::getId($siteId);
175
176 if (!$site || !in_array($site['TPL_CODE'], self::ONLY_CODES, true))
177 {
178 return true;
179 }
180
181 $catalogFolderId = self::createFolder($siteId);
182 if (!$catalogFolderId)
183 {
184 return false;
185 }
186
187 if (
188 !($orderId = self::createPageIfNotExists($siteId, $catalogFolderId, 'store-chats-dark/catalog_order'))
189 || !($detailId = self::createPageIfNotExists($siteId, $catalogFolderId, 'store-chats-dark/catalog_detail'))
190 || !($catalogId = self::createPageIfNotExists($siteId, $catalogFolderId, 'store-chats-dark/catalog'))
191 || !($headerId = self::createPageIfNotExists($siteId, $catalogFolderId, 'store-chats-dark/catalog_header'))
192 || !($footerId = self::createPageIfNotExists($siteId, $catalogFolderId, 'store-chats-dark/catalog_footer'))
193 )
194 {
195 return false;
196 }
197
198 Syspage::set($siteId, 'catalog', $catalogId);
199 self::setIndexToFolder($catalogFolderId, $catalogId);
200 self::setTemplateToLanding($catalogId, 'header_footer', [1 => $headerId, 2 => $footerId]);
201 self::setTemplateToLanding($detailId, 'header_footer', [1 => $headerId, 2 => $footerId]);
202 self::setTemplateToLanding($orderId, 'header_footer', [1 => $headerId, 2 => $footerId]);
203
204 self::replaceFooterLinks($footerId, $siteId);
205
206 return true;
207 }
208
209 private static function replaceFooterLinks(int $footerId, int $siteId): void
210 {
211 $replaceCodes = array_keys(self::FOOTER_LINKS_MAP);
212 $replaces = [];
213
214 $landings = Landing::getList([
215 'select' => ['ID', 'TPL_CODE'],
216 'filter' => ['SITE_ID' => $siteId],
217 ]);
218 while ($landing = $landings->fetch())
219 {
220 $tpl = $landing['TPL_CODE'];
221 if (in_array($tpl, $replaceCodes, true))
222 {
223 $replaces['"' . self::FOOTER_LINKS_MAP[$tpl] . '"'] = '"#landing' . $landing['ID'] . '"';
224 }
225 }
226
227 if (!empty($replaces))
228 {
229 $blocks = BlockTable::getList([
230 'select' => ['ID', 'CONTENT'],
231 'filter' => [
232 'LID' => $footerId,
233 'CODE' => self::FOOTER_BLOCK_CODE,
234 ],
235 ]);
236 while ($block = $blocks->fetch())
237 {
238 $newContent = str_replace(
239 array_keys($replaces),
240 array_values($replaces),
241 $block['CONTENT']
242 );
243 BlockTable::update($block['ID'], [
244 'CONTENT' => $newContent
245 ]);
246 }
247 }
248 }
249}
static update($id, $fields=array())
Definition landing.php:3110
static createInstance($id, array $params=array())
Definition landing.php:534
static getId(int $siteId)
Definition update.php:18
static addFolder(int $siteId, array $fields)
Definition site.php:1151
static getFolders(int $siteId, array $filter=[])
Definition site.php:1375
static set($id, $type, $lid=false)
Definition syspage.php:30
static setForLanding($id, array $data=array())
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getList(array $parameters=array())