Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
setdefaultvaluestepper.php
1<?php
2
4
13use Throwable;
14
15Loc::loadMessages(__FILE__);
16
22{
23 protected static $moduleId = "catalog";
24
25 public static function getTitle()
26 {
27 return Loc::getMessage('CATALOG_PRODUCT_SYSTEMFIELD_PRODUCTMAPPING_SET_DEFAULT_VALUE_STEPPER_TITLE');
28 }
29
33 public function execute(array &$option)
34 {
35 $userFieldManager = UserFieldHelper::getInstance()->getManager();
36
38 {
40 }
41 $fieldDescription = ProductMapping::getUserFieldBaseParam();
42
43 // first run
44 if (!isset($option['is_started']))
45 {
46 return $this->firstRun($option);
47 }
48
49 if (empty($option['count']))
50 {
52 }
53
54 // processing
55 $defaultValues = $this->getDefaultValues();
56 if (!$defaultValues)
57 {
59 }
60
61 $lastId = (int)($option['last_id'] ?? 0);
62 $products = $this->getProductsWithEmptyValue($lastId);
63
64 $stepCount = 0;
65
67 try
68 {
69 $db->startTransaction();
70
71 $entityId = ProductTable::getUfId();
72 $fieldId = $fieldDescription['FIELD_NAME'];
73 foreach ($products as $row)
74 {
75 $productId = (int)$row['id'];
76 $userFieldManager->Update(
77 $entityId,
78 $productId,
79 [
80 $fieldId => $defaultValues,
81 ]
82 );
83 $lastId = $productId;
84 $stepCount++;
85 }
86
87 $db->commitTransaction();
88 }
89 catch (Throwable $e)
90 {
91 $db->rollbackTransaction();
92
93 throw $e;
94 }
95
96 if ($stepCount === 0)
97 {
99 }
100
101 $option['steps'] += $stepCount;
102 $option['last_id'] = $lastId;
103
105 }
106
114 private function firstRun(array & $option): bool
115 {
116 $connection = Application::getConnection();
117 $helper = $connection->getSqlHelper();
118
119 $existUfTable = $connection->query("SHOW TABLES LIKE 'b_uts_product'")->getSelectedRowsCount() > 0;
120 if (!$existUfTable)
121 {
123 }
124
125 $existUfColumn = $connection->query(
126 "SHOW COLUMNS FROM " . $helper->quote('b_uts_product') . " LIKE 'UF_PRODUCT_MAPPING'"
127 )->getSelectedRowsCount() > 0;
128 if (!$existUfColumn)
129 {
131 }
132
133 if (!$this->isNotEmptyProducts())
134 {
136 }
137
138 $count = $this->getProductsToBeProcessedTotalCount();
139 if ($count === 0)
140 {
142 }
143
144 $option['count'] = $count;
145 $option['title'] = self::getTitle();
146 $option['is_started'] = true;
147 $option['last_id'] = null;
148
150 }
151
160 private function getProductsQuery(bool $isCountSelect = false, int $lastId = 0): string
161 {
162 $connection = Application::getConnection();
163 $helper = $connection->getSqlHelper();
164
165 $typeIds = join(',', [
169 ]);
170
171 $select = 'b_catalog_product.id as ' . $helper->quote('id');
172 if ($isCountSelect)
173 {
174 $select = 'COUNT(b_catalog_product.id)';
175 }
176
177 $where = '';
178 if ($lastId)
179 {
180 $where = new SqlExpression("AND b_catalog_product.id > ?i", $lastId);
181 }
182
183 return trim("
184 SELECT {$select}
185 FROM b_catalog_product
186 LEFT JOIN b_uts_product ON b_catalog_product.id = b_uts_product.value_id
187 WHERE b_catalog_product.TYPE in ({$typeIds})
188 AND b_uts_product.UF_PRODUCT_MAPPING IS NULL {$where}
189 " . ($isCountSelect ? "" : "ORDER BY b_catalog_product.id ASC") . "
190 ");
191 }
192
199 private function getProductsWithEmptyValue(int $lastId): Result
200 {
201 $limit = 10;
202 $sql = $this->getProductsQuery(false, $lastId);
203
204 return Application::getConnection()->query($sql, $limit);
205 }
206
212 private function getProductsToBeProcessedTotalCount(): int
213 {
214 $sql = $this->getProductsQuery(true);
215
216 return (int)Application::getConnection()->queryScalar($sql);
217 }
218
219 private function isNotEmptyProducts(): bool
220 {
221 return ProductTable::getRow([
222 'select' => [
223 'ID',
224 ]
225 ]) !== null;
226 }
227
233 private function getDefaultValues(): ?array
234 {
235 $config = ProductMapping::getConfig();
236 $tableName = $config['HIGHLOADBLOCK']['TABLE_NAME'] ?? null;
237 if (!$tableName)
238 {
239 return null;
240 }
241
242 $values = [];
243 $values[] = Application::getConnection()->queryScalar(
244 new SqlExpression("SELECT ID FROM ?# WHERE UF_XML_ID = 'LANDING'", $tableName)
245 );
246
247 return $values;
248 }
249
250 public static function clearAbandonedSteppersAgent(): string
251 {
252 $option = \Bitrix\Main\Config\Option::get(
253 'main.stepper.catalog',
254 __CLASS__,
255 null,
256 ''
257 );
258 $needRemove = false;
259 if ($option !== null)
260 {
261 if ($option === '')
262 {
263 $needRemove = true;
264 }
265 if (!CheckSerializedData($option))
266 {
267 $needRemove = true;
268 }
269 else
270 {
271 $option = unserialize($option, ['allowed_classes' => false]);
272 if (empty($option) || !is_array($option))
273 {
274 $needRemove = true;
275 }
276 else
277 {
278 if (empty($option['count']))
279 {
280 $needRemove = true;
281 }
282 }
283 }
284 }
285
286 if ($needRemove)
287 {
288 \Bitrix\Main\Config\Option::delete(
289 'main.stepper.catalog',
290 [
291 'name' => __CLASS__
292 ]
293 );
294 }
295
296 return '';
297 }
298}
static getConnection($name="")
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getRow(array $parameters)