Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
domain.php
1<?php
3
4use \Bitrix\Landing\Manager;
5use \Bitrix\Landing\Domain as DomainCore;
6use \Bitrix\Main\Localization\Loc;
7use \Bitrix\Main\Entity;
8
9Loc::loadMessages(__FILE__);
10
27class DomainTable extends Entity\DataManager
28{
32 const PROTOCOL_HTTPS = 'https';
33
37 const PROTOCOL_HTTP = 'http';
38
43 public static function getTableName()
44 {
45 return 'b_landing_domain';
46 }
47
52 public static function getMap()
53 {
54 return array(
55 'ID' => new Entity\IntegerField('ID', array(
56 'primary' => true,
57 'autocomplete' => true,
58 'title' => 'ID'
59 )),
60 'ACTIVE' => new Entity\StringField('ACTIVE', array(
61 'title' => Loc::getMessage('LANDING_TABLE_FIELD_ACTIVE'),
62 'default_value' => 'Y'
63 )),
64 'DOMAIN' => new Entity\StringField('DOMAIN', array(
65 'title' => Loc::getMessage('LANDING_TABLE_FIELD_DOMAIN'),
66 'required' => true
67 )),
68 'PREV_DOMAIN' => new Entity\StringField('PREV_DOMAIN', array(
69 'title' => Loc::getMessage('LANDING_TABLE_FIELD_PREV_DOMAIN')
70 )),
71 'XML_ID' => new Entity\StringField('XML_ID', array(
72 'title' => Loc::getMessage('LANDING_TABLE_FIELD_XML_ID')
73 )),
74 'PROTOCOL' => new Entity\StringField('PROTOCOL', array(
75 'title' => Loc::getMessage('LANDING_TABLE_FIELD_PROTOCOL'),
76 'required' => true,
77 'default_value' => self::PROTOCOL_HTTPS
78 )),
79 'PROVIDER' => new Entity\StringField('PROVIDER', array(
80 'title' => Loc::getMessage('LANDING_TABLE_FIELD_PROVIDER')
81 )),
82 'FAIL_COUNT' => new Entity\IntegerField('FAIL_COUNT', array(
83 'title' => Loc::getMessage('LANDING_TABLE_FIELD_FAIL_COUNT')
84 )),
85 'CREATED_BY_ID' => new Entity\IntegerField('CREATED_BY_ID', array(
86 'title' => Loc::getMessage('LANDING_TABLE_FIELD_CREATED_BY_ID'),
87 'required' => true
88 )),
89 'CREATED_BY' => new Entity\ReferenceField(
90 'CREATED_BY',
91 'Bitrix\Main\UserTable',
92 array('=this.CREATED_BY_ID' => 'ref.ID')
93 ),
94 'MODIFIED_BY_ID' => new Entity\IntegerField('MODIFIED_BY_ID', array(
95 'title' => Loc::getMessage('LANDING_TABLE_FIELD_MODIFIED_BY_ID'),
96 'required' => true
97 )),
98 'MODIFIED_BY' => new Entity\ReferenceField(
99 'MODIFIED_BY',
100 'Bitrix\Main\UserTable',
101 array('=this.MODIFIED_BY_ID' => 'ref.ID')
102 ),
103 'DATE_CREATE' => new Entity\DatetimeField('DATE_CREATE', array(
104 'title' => Loc::getMessage('LANDING_TABLE_FIELD_DATE_CREATE'),
105 'required' => true
106 )),
107 'DATE_MODIFY' => new Entity\DatetimeField('DATE_MODIFY', array(
108 'title' => Loc::getMessage('LANDING_TABLE_FIELD_DATE_MODIFY'),
109 'required' => true
110 ))
111 );
112 }
113
118 public static function getProtocolList()
119 {
120 return array(
121 self::PROTOCOL_HTTPS => 'https',
122 self::PROTOCOL_HTTP => 'http'
123 );
124 }
125
131 protected static function isValidProtocol($protocol)
132 {
133 $list = self::getProtocolList();
134 return isset($list[$protocol]);
135 }
136
142 protected static function prepareChange(Entity\Event $event): Entity\EventResult
143 {
144 $result = new Entity\EventResult();
145 $fields = $event->getParameter('fields');
146 $primary = $event->getParameter('primary');
147 $update = array();
148
149 if ($fields['DOMAIN'] ?? null)
150 {
151 if (
152 Manager::isB24() &&
154 mb_strtolower($fields['DOMAIN']) !== Manager::getHttpHost() &&
155 !DomainCore::getBitrix24Subdomain($fields['DOMAIN'])
156 )
157 {
158 \Bitrix\Landing\Agent::addUniqueAgent('removeBadDomain', [], 86400);
159 }
160 }
161
162 // prepare CODE - base part of URL
163 if (array_key_exists('DOMAIN', $fields))
164 {
165 $url = parse_url($fields['DOMAIN']);
166 if (isset($url['host']))
167 {
168 $fields['DOMAIN'] = $url['host'];
169 }
170 else
171 {
172 $fields['DOMAIN'] = trim($fields['DOMAIN']);
173 }
174 $prevDomain = null;
175 $res = self::getList(array(
176 'select' => array(
177 '*'
178 ),
179 'filter' => array(
180 'LOGIC' => 'OR',
181 'ID' => $primary['ID'] ?? 0,
182 '=DOMAIN' => $fields['DOMAIN']
183 )
184 ));
185 while ($rowDomain = $res->fetch())
186 {
187 if ($rowDomain['ID'] == ($primary['ID'] ?? 0))
188 {
189 $prevDomain = $rowDomain['DOMAIN'];
190 continue;
191 }
192 $result->setErrors(array(
194 Loc::getMessage('LANDING_TABLE_ERROR_DOMAIN_IS_NOT_UNIQUE'),
195 'DOMAIN_IS_NOT_UNIQUE'
196 )
197 ));
198 return $result;
199 }
200 $update['DOMAIN'] = $fields['DOMAIN'];
201 if ($prevDomain !== $fields['DOMAIN'])
202 {
203 $update['PREV_DOMAIN'] = $prevDomain;
204 }
205 }
206
207 // force set protocol
208 $fields['PROTOCOL'] = Manager::isHttps()
211 $update['PROTOCOL'] = $fields['PROTOCOL'];
212
213 // modify fields
214 if (!empty($update))
215 {
216 $result->modifyFields($update);
217 }
218
219 return $result;
220 }
221
227 public static function OnBeforeAdd(Entity\Event $event)
228 {
229 return self::prepareChange($event);
230 }
231
237 public static function OnBeforeUpdate(Entity\Event $event)
238 {
239 return self::prepareChange($event);
240 }
241
247 public static function OnBeforeDelete(Entity\Event $event)
248 {
249 $result = new Entity\EventResult();
250 $primary = $event->getParameter('primary');
251 // check if domain is not empty
252 if ($primary)
253 {
254 $res = SiteTable::getList(array(
255 'select' => array(
256 'ID'
257 ),
258 'filter' => array(
259 'DOMAIN_ID' => $primary['ID'],
260 'CHECK_PERMISSIONS' => 'N'
261 )
262 ));
263 if ($res->fetch())
264 {
265 $result->setErrors(array(
267 Loc::getMessage('LANDING_TABLE_ERROR_DOMAIN_IS_NOT_EMPTY'),
268 'DOMAIN_IS_NOT_EMPTY'
269 )
270 ));
271 return $result;
272 }
273 }
274 return $result;
275 }
276}
static OnBeforeDelete(Entity\Event $event)
Definition domain.php:247
static prepareChange(Entity\Event $event)
Definition domain.php:142
static isValidProtocol($protocol)
Definition domain.php:131
static OnBeforeAdd(Entity\Event $event)
Definition domain.php:227
static OnBeforeUpdate(Entity\Event $event)
Definition domain.php:237
static getList(array $params=array())
Definition site.php:1348
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29