Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
restorer.php
1<?php
3
7
12{
13 private $archiveId = null;
14 private $version = null;
15 private $dateArchived = null;
17 private $builder = null;
18 private $firstVersion = 1;
19
20 const EVENT_ON_ARCHIVE_ORDER_BEFORE_RESTORED = "OnSaleArchiveOrderBeforeRestored";
21
22 protected function __construct($id)
23 {
24 $this->archiveId = (int)$id;
25 }
26
35 public static function load($id)
36 {
37 $id = (int)$id;
38 if ($id <= 0)
39 {
40 return null;
41 }
42
43 $archivedOrder = Internals\OrderArchiveTable::getList(
44 array(
45 "select" => array("*", "ORDER_FULL" => "ORDER_PACKED.ORDER_DATA"),
46 "filter" => array("=ID" => $id),
47 "limit" => 1
48 )
49 );
50 $orderFields = $archivedOrder->fetch();
51
52 if (!$orderFields)
53 return null;
54
55 $restorer = new self($id);
56 if (!$restorer->checkVersion($orderFields['VERSION']))
57 {
58 return null;
59 }
60
61 $restorer->version = (int)$orderFields['VERSION'];
62 $restorer->dateArchived = $orderFields['DATE_ARCHIVED'];
63 $builder = $restorer->loadBuilder();
64 $builder->setPackedOrder($restorer->packField($orderFields['ORDER_FULL']));
65 $orderFields['ID'] = $orderFields['ORDER_ID'];
66 $archiveFields = ['ORDER_DATA', 'ORDER_FULL', 'ORDER_ID', 'VERSION', 'DATE_ARCHIVED'];
67 foreach ($archiveFields as $fieldName)
68 {
69 unset($orderFields[$fieldName]);
70 }
71
72 $builder->setEntityFields('ORDER', $orderFields);
73
74 $basketArchivedItems = Internals\BasketArchiveTable::getList(
75 array(
76 "select" => array("*", "BASKET_FULL" => "BASKET_PACKED.BASKET_DATA"),
77 "filter" => array("ARCHIVE_ID" => $id)
78 )
79 );
80
81 $basketItems = [];
82 while ($item = $basketArchivedItems->fetch())
83 {
84 $builder->addPackedBasketItem(
85 $item['ID'],
86 $restorer->packField($item['BASKET_FULL'])
87 );
88 unset($item['BASKET_DATA'], $item['BASKET_FULL'], $item['ARCHIVE_ID']);
89 $basketItems[$item['ID']] = $item;
90 }
91 $builder->setEntityFields('BASKET', $basketItems);
92 $restorer->setBuilder($builder);
93 return $restorer;
94 }
95
101 private function checkVersion($version)
102 {
103 return ($this->firstVersion <= (int)$version && (int)$version <= Archive\Manager::SALE_ARCHIVE_VERSION);
104 }
105
109 private function loadBuilder()
110 {
111 switch ($this->version)
112 {
113 case 1:
114 case 2:
115 return $this->builder = new FirstSchemeBuilder();
116 }
117
118 return null;
119 }
120
124 private function setBuilder(Buildable $builder)
125 {
126 $this->builder = $builder;
127 }
128
134 public function packField($field = '')
135 {
136 if ($this->version === 1)
137 {
138 return new SerializedField($field);
139 }
140 else
141 {
142 return new JsonField($field);
143 }
144 }
145
149 public function getBuilder()
150 {
151 return $this->builder;
152 }
153
157 public function getArchiveVersion()
158 {
159 return $this->version;
160 }
161
165 public function getArchiveId()
166 {
167 return $this->archiveId;
168 }
169
173 public function restoreOrder()
174 {
175 $eventManager = Main\EventManager::getInstance();
176 if ($eventsList = $eventManager->findEventHandlers('sale', self::EVENT_ON_ARCHIVE_ORDER_BEFORE_RESTORED))
177 {
178 $event = new Main\Event('sale', self::EVENT_ON_ARCHIVE_ORDER_BEFORE_RESTORED, array(
179 'ENTITY' => $this
180 ));
181 $event->send();
182 }
183
184 $archivedOrder = $this->builder->buildOrder();
185 $archivedOrder->setVersion($this->version);
186 $archivedOrder->setDateArchived($this->dateArchived);
187 return $archivedOrder;
188 }
189}