Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
platform.php
1<?php
2
4
6use Bitrix\Main\Entity\EventResult;
7use Bitrix\Main\Entity\Result;
12
18abstract class Platform
19{
20 public const LINK_TYPE_PUBLIC_DETAIL_ORDER = 'PUBLIC_DETAIL_ORDER';
21 public const LINK_TYPE_PUBLIC_FEEDBACK = 'PUBLIC_FEEDBACK';
22
23 protected $logger;
25
26 protected $code;
27 protected $isActive = false;
28 protected $settings = array();
29
30 protected $isInstalled = false;
31 protected $isNeedCatalogSectionsTab = false;
32
33 protected $id;
34 protected $fields = [];
35
36 protected static $instances = array();
37
39
44 protected function __construct($code)
45 {
46 $this->code = $code;
47
48 $dbRes = TradingPlatformTable::getList([
49 'filter' => [
50 '=CODE' => $this->code,
51 ],
52 ]);
53
54 if ($platform = $dbRes->fetch())
55 {
56 $this->isActive = $platform["ACTIVE"] == "Y";
57 $this->isNeedCatalogSectionsTab = $platform["CATALOG_SECTION_TAB_CLASS_NAME"] <> '';
58
59 if (is_array($platform["SETTINGS"]))
60 {
61 $this->settings = $platform["SETTINGS"];
62 }
63
64 $this->isInstalled = true;
65 $this->id = $platform["ID"];
66 $this->fields = $platform;
67 }
68
69 $this->logger = new Logger($this->logLevel);
70 }
71
72 protected function __clone() {}
73
79 public static function getInstanceByCode($code)
80 {
81 if ($code === '')
82 {
83 throw new ArgumentNullException("code");
84 }
85
86 if (!isset(self::$instances[$code]))
87 {
88 self::$instances[$code] = new static($code);
89 }
90
91 return self::$instances[$code];
92 }
93
97 public function getId()
98 {
99 return $this->id;
100 }
101
109 public function addLogRecord($level, $type, $itemId, $description)
110 {
111 return $this->logger->addRecord($level, $type, $itemId, $description);
112 }
113
114 public function getField($fieldName)
115 {
116 if(!isset($this->fields[$fieldName]))
117 {
118 return '';
119 }
120
121 return $this->fields[$fieldName];
122 }
123
124 public function getRealName()
125 {
126 return $this->getField('NAME');
127 }
128
132 public function isActive()
133 {
134 return $this->isActive;
135 }
136
141 public function setActive()
142 {
143 if ($this->isActive())
144 {
145 return true;
146 }
147
148 $this->isActive = true;
149
150 if ($this->isNeedCatalogSectionsTab && !$this->isSomebodyUseCatalogSectionsTab())
152
153 // if we are the first, let's switch on the event to notify about the track numbers changings
154 if (!$this->isActiveItemsExist())
156
157 $res = TradingPlatformTable::update($this->id, array("ACTIVE" => "Y"));
158
159 return $res->isSuccess();
160 }
161
166 public function unsetActive()
167 {
168 $this->isActive = false;
169
170 if ($this->isNeedCatalogSectionsTab && !$this->isSomebodyUseCatalogSectionsTab())
172
173 $res = TradingPlatformTable::update($this->id, array("ACTIVE" => "N"));
174
175 //If we are last let's switch off unused event about track numbers changing
176 if (!$this->isActiveItemsExist())
177 {
179 }
180
181 return $res->isSuccess();
182 }
183
184 protected static function isActiveItemsExist()
185 {
186 $dbRes = TradingPlatformTable::getList([
187 'filter' => [
188 '=ACTIVE' => 'Y',
189 ],
190 'select' => ['ID'],
191 ]);
192
193 return (bool)$dbRes->fetch();
194 }
195
196 public static function setShipmentTableOnAfterUpdateEvent()
197 {
198 $eventManager = EventManager::getInstance();
199 $eventManager->registerEventHandler(
200 'sale',
201 'ShipmentOnAfterUpdate',
202 'sale',
203 '\Bitrix\Sale\TradingPlatform\Helper',
204 'onAfterUpdateShipment'
205 );
206 }
207
208 protected static function unSetShipmentTableOnAfterUpdateEvent()
209 {
210 $eventManager = EventManager::getInstance();
211 $eventManager->unRegisterEventHandler(
212 'sale',
213 'ShipmentOnAfterUpdate',
214 'sale',
215 '\Bitrix\Sale\TradingPlatform\Helper',
216 'onAfterUpdateShipment'
217 );
218 }
219
225 {
226 $result = false;
227
228 $res = TradingPlatformTable::getList(array(
229 'select' => array("ID", "CATALOG_SECTION_TAB_CLASS_NAME"),
230 'filter' => array(
231 '!=CODE' => $this->code,
232 '=ACTIVE' => 'Y',
233 ),
234 ));
235
236 while ($arRes = $res->fetch())
237 {
238 if ($arRes["CATALOG_SECTIONS_TAB_CLASS_NAME"] <> '')
239 {
240 $result = true;
241 break;
242 }
243 }
244
245 return $result;
246 }
247
248 protected function setCatalogSectionsTabEvent()
249 {
250 $eventManager = EventManager::getInstance();
251 $eventManager->registerEventHandlerCompatible("main", "OnAdminIBlockSectionEdit", "sale", "\\Bitrix\\Sale\\TradingPlatform\\CatalogSectionTab", "OnInit");
252 }
253
254 protected function unSetCatalogSectionsTabEvent()
255 {
256 $eventManager = EventManager::getInstance();
257 $eventManager->unRegisterEventHandler("main", "OnAdminIBlockSectionEdit", "sale", "\\Bitrix\\Sale\\TradingPlatform\\CatalogSectionTab", "OnInit");
258 }
259
263 public function getSettings()
264 {
265 return $this->settings;
266 }
267
272 public function saveSettings(array $settings)
273 {
274 $this->settings = $settings;
275 $result = TradingPlatformTable::update($this->id, array("SETTINGS" => $settings));
276
277 return $result->isSuccess() && $result->getAffectedRowsCount();
278 }
279
280
281 public function resetSettings($siteId)
282 {
283 $settings = $this->getSettings();
284 if (isset($settings[$siteId]) && is_array($settings[$siteId]))
285 {
286 unset($settings[$siteId]);
287 }
288
289 if (empty($settings))
290 $this->unsetActive();
291
292 return $this->saveSettings($settings);
293 }
294
298 public function isInstalled()
299 {
300 return $this->isInstalled;
301 }
302
307 public function install()
308 {
309 $res = TradingPlatformTable::add(array(
310 "CODE" => self::TRADING_PLATFORM_CODE,
311 "ACTIVE" => "N",
312 ));
313
314 self::$instances[$this->getCode()] = new static($this->getCode());
315
316 return $res->getId();
317 }
318
322 public function uninstall()
323 {
324 if ($this->isInstalled())
325 {
326 $this->unsetActive();
327 $res = TradingPlatformTable::delete($this->getId());
328 }
329 else
330 {
331 $res = new Result();
332 }
333
334 unset(self::$instances[$this->getCode()]);
335 $this->isInstalled = false;
336
337 return $res->isSuccess();
338 }
339
343 public function getCode()
344 {
345 return $this->code;
346 }
347
351 public function getAnalyticCode()
352 {
353 return static::TRADING_PLATFORM_CODE;
354 }
355
356 public static function onAfterUpdateShipment(\Bitrix\Main\Event $event, array $additional)
357 {
358 return new EventResult();
359 }
360
364 public function getInfo()
365 {
366 return [];
367 }
368
373 public function isOfType(string $type): bool
374 {
375 return false;
376 }
377
383 public function getExternalLink($type, Sale\Order $order)
384 {
385 return '';
386 }
387}
388
getExternalLink($type, Sale\Order $order)
Definition platform.php:383
static onAfterUpdateShipment(\Bitrix\Main\Event $event, array $additional)
Definition platform.php:356
addLogRecord($level, $type, $itemId, $description)
Definition platform.php:109