Bitrix-D7  20.5.0
platform.php
См. документацию.
1 <?php
2 
4 
7 use Bitrix\Main\Entity\EventResult;
8 use Bitrix\Main\Entity\Result;
10 use Bitrix\Sale;
13 
14 /**
15  * Class Platform
16  * Base class for trading platforms.
17  * @package Bitrix\Sale\TradingPlatform
18  */
19 abstract class Platform
20 {
21  const LINK_TYPE_PUBLIC_DETAIL_ORDER = 'PUBLIC_DETAIL_ORDER';
22 
23  protected $logger;
24  protected $logLevel = Logger::LOG_LEVEL_ERROR;
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 
40  /**
41  * Constructor
42  * @param $code
43  */
44  protected function __construct($code)
45  {
46  $this->code = $code;
47 
48  $resPltf = TradingPlatformTable::getList(array(
49  'filter' => array(
50  '=CODE' => $this->code,
51  ),
52  ));
53 
54  if ($platform = $resPltf->fetch())
55  {
56  $this->isActive = $platform["ACTIVE"] == "Y" ? true : false;
57  $this->isNeedCatalogSectionsTab = $platform["CATALOG_SECTION_TAB_CLASS_NAME"] <> '' ? true : false;
58 
59  if (is_array($platform["SETTINGS"]))
60  $this->settings = $platform["SETTINGS"];
61 
62  $this->isInstalled = true;
63  $this->id = $platform["ID"];
64  $this->fields = $platform;
65  }
66 
67  $this->logger = new Logger($this->logLevel);
68  }
69 
70  protected function __clone()
71  {
72  }
73 
74  /**
75  * @param $code
76  * @return \Bitrix\Sale\TradingPlatform\Platform
77  * @throws ArgumentNullException
78  */
79  public static function getInstanceByCode($code)
80  {
81  if ($code == '')
82  throw new ArgumentNullException("code");
83 
84  if (!isset(self::$instances[$code]))
85  self::$instances[$code] = new static($code);
86 
87  return self::$instances[$code];
88  }
89 
90  /**
91  * @return mixed Id of the current trading platform.
92  */
93  public function getId()
94  {
95  return $this->id;
96  }
97 
98  /**
99  * @param int $level The level of event.
100  * @param string $type Type of event.
101  * @param string $itemId Item idenifyer.
102  * @param string $description Description of event.
103  * @return bool Success or not.
104  */
105  public function addLogRecord($level, $type, $itemId, $description)
106  {
107  return $this->logger->addRecord($level, $type, $itemId, $description);
108  }
109 
110  public function getField($fieldName)
111  {
112  if(!isset($this->fields[$fieldName]))
113  {
114  return '';
115  }
116 
117  return $this->fields[$fieldName];
118  }
119 
120  public function getRealName()
121  {
122  return $this->getField('NAME');
123  }
124 
125  /**
126  * @return bool Is the platfor active?.
127  */
128  public function isActive()
129  {
130  return $this->isActive;
131  }
132 
133  /**
134  * Sets the platform active.
135  * @return bool
136  */
137  public function setActive()
138  {
139  if ($this->isActive())
140  return true;
141 
142  $this->isActive = true;
143 
144  if ($this->isNeedCatalogSectionsTab && !$this->isSomebodyUseCatalogSectionsTab())
146 
147  // if we are the first, let's switch on the event to notify about the track numbers changings
148  if (!$this->isActiveItemsExist())
150 
151  $res = TradingPlatformTable::update($this->id, array("ACTIVE" => "Y"));
152 
153  return $res->isSuccess();
154  }
155 
156  /**
157  * Sets the platform inactive.
158  * @return bool
159  */
160  public function unsetActive()
161  {
162  $this->isActive = false;
163 
164  if ($this->isNeedCatalogSectionsTab && !$this->isSomebodyUseCatalogSectionsTab())
166 
167  $res = TradingPlatformTable::update($this->id, array("ACTIVE" => "N"));
168 
169  //If we are last let's switch off unused event about track numbers changing
170  if (!$this->isActiveItemsExist())
172 
173  return $res->isSuccess();
174  }
175 
176  protected static function isActiveItemsExist()
177  {
178  $dbRes = TradingPlatformTable::getList(array(
179  'filter' => array(
180  'ACTIVE' => 'Y',
181  ),
182  'select' => array('ID'),
183  ));
184 
185  if ($platform = $dbRes->fetch())
186  $result = true;
187  else
188  $result = false;
189 
190  return $result;
191  }
192 
193  public static function setShipmentTableOnAfterUpdateEvent()
194  {
195  $eventManager = EventManager::getInstance();
196  $eventManager->registerEventHandler(
197  'sale',
198  'ShipmentOnAfterUpdate',
199  'sale',
200  '\Bitrix\Sale\TradingPlatform\Helper',
201  'onAfterUpdateShipment'
202  );
203  }
204 
205  protected static function unSetShipmentTableOnAfterUpdateEvent()
206  {
207  $eventManager = EventManager::getInstance();
208  $eventManager->unRegisterEventHandler(
209  'sale',
210  'ShipmentOnAfterUpdate',
211  'sale',
212  '\Bitrix\Sale\TradingPlatform\Helper',
213  'onAfterUpdateShipment'
214  );
215  }
216 
217  /**
218  * Shows is another platforms using the iblock section edit page, "trading platforms" tab.
219  * @return bool
220  */
221  protected function isSomebodyUseCatalogSectionsTab()
222  {
223  $result = false;
224 
225  $res = TradingPlatformTable::getList(array(
226  'select' => array("ID", "CATALOG_SECTION_TAB_CLASS_NAME"),
227  'filter' => array(
228  '!=CODE' => $this->code,
229  '=ACTIVE' => 'Y',
230  ),
231  ));
232 
233  while ($arRes = $res->fetch())
234  {
235  if ($arRes["CATALOG_SECTIONS_TAB_CLASS_NAME"] <> '')
236  {
237  $result = true;
238  break;
239  }
240  }
241 
242  return $result;
243  }
244 
245  protected function setCatalogSectionsTabEvent()
246  {
247  $eventManager = EventManager::getInstance();
248  $eventManager->registerEventHandlerCompatible("main", "OnAdminIBlockSectionEdit", "sale", "\\Bitrix\\Sale\\TradingPlatform\\CatalogSectionTab", "OnInit");
249  }
250 
251  protected function unSetCatalogSectionsTabEvent()
252  {
253  $eventManager = EventManager::getInstance();
254  $eventManager->unRegisterEventHandler("main", "OnAdminIBlockSectionEdit", "sale", "\\Bitrix\\Sale\\TradingPlatform\\CatalogSectionTab", "OnInit");
255  }
256 
257  /**
258  * @return array Platform settings.
259  */
260  public function getSettings()
261  {
262  return $this->settings;
263  }
264 
265  /**
266  * @param array $settings Platform settings.
267  * @return bool Is success?.
268  */
269  public function saveSettings(array $settings)
270  {
271  $this->settings = $settings;
272  $result = TradingPlatformTable::update($this->id, array("SETTINGS" => $settings));
273 
274  return $result->isSuccess() && $result->getAffectedRowsCount();
275  }
276 
277 
278  public function resetSettings($siteId)
279  {
280  $settings = $this->getSettings();
281  if (isset($settings[$siteId]) && is_array($settings[$siteId]))
282  {
283  unset($settings[$siteId]);
284  }
285 
286  if (empty($settings))
287  $this->unsetActive();
288 
289  return $this->saveSettings($settings);
290  }
291 
292  /**
293  * @return bool Is platfom installed?.
294  */
295  public function isInstalled()
296  {
297  return $this->isInstalled;
298  }
299 
300  /**
301  * Installs platform
302  * @return int Platform Id.
303  */
304  public function install()
305  {
306  $res = TradingPlatformTable::add(array(
307  "CODE" => self::TRADING_PLATFORM_CODE,
308  "ACTIVE" => "N",
309  ));
310 
311  self::$instances[$this->getCode()] = new static($this->getCode());
312 
313  return $res->getId();
314  }
315 
316  /**
317  * @return bool Is deletion successful?.
318  */
319  public function uninstall()
320  {
321  if ($this->isInstalled())
322  {
323  $this->unsetActive();
324  $res = TradingPlatformTable::delete($this->getId());
325  }
326  else
327  {
328  $res = new Result();
329  }
330 
331  unset(self::$instances[$this->getCode()]);
332  $this->isInstalled = false;
333 
334  return $res->isSuccess();
335  }
336 
337  /**
338  * @return string Platform code.
339  */
340  public function getCode()
341  {
342  return $this->code;
343  }
344 
345  public static function onAfterUpdateShipment(\Bitrix\Main\Event $event, array $additional)
346  {
347  return new EventResult();
348  }
349 
350  /**
351  * @return array
352  */
353  public function getInfo()
354  {
355  return [];
356  }
357 
358  /**
359  * @param $type
360  * @param Sale\Order $order
361  * @return string
362  */
363  public function getExternalLink($type, Sale\Order $order)
364  {
365  return '';
366  }
367 }
368 
Exception is thrown when "empty" value is passed to a function that does not accept it as a valid arg...
Exception is thrown when the value of an argument is outside the allowable range of values.
Base class for fatal exceptions.
getExternalLink($type, Sale\Order $order)
Definition: platform.php:363
static onAfterUpdateShipment(\Bitrix\Main\Event $event, array $additional)
Definition: platform.php:345
isSomebodyUseCatalogSectionsTab()
Shows is another platforms using the iblock section edit page, "trading platforms" tab.
Definition: platform.php:221
unsetActive()
Sets the platform inactive.
Definition: platform.php:160
setActive()
Sets the platform active.
Definition: platform.php:137
__construct($code)
Constructor.
Definition: platform.php:44
addLogRecord($level, $type, $itemId, $description)
Definition: platform.php:105
Class RestHandler \Handlers\PaySystem.