entityPool->get($entityType); $enabledProviders = array_column($this->providerConfig->getEnabledOptions(), 'code'); if (!in_array($provider, $enabledProviders, true)) { throw new LocalizedException(__('The selected translation provider is not enabled.')); } $validStoreIds = array_column($this->storeViewProvider->getStoreViewOptions(), 'store_id'); $entityIds = array_values(array_unique(array_filter(array_map('intval', $entityIds)))); $targetStoreIds = array_values(array_unique(array_filter(array_map('intval', $targetStoreIds)))); $targetStoreIds = array_values(array_intersect($targetStoreIds, $validStoreIds)); if ($sourceStoreId > 0) { $targetStoreIds = array_values(array_diff($targetStoreIds, [$sourceStoreId])); } if ($entityIds === [] || $targetStoreIds === []) { throw new LocalizedException(__('Select at least one entity and one target Store View.')); } $connection = $this->resourceConnection->getConnection(); $table = $this->resourceConnection->getTableName(self::TABLE_NAME); $batchId = bin2hex(random_bytes(16)); $rows = []; $skipped = 0; foreach ($entityIds as $entityId) { foreach ($targetStoreIds as $targetStoreId) { $existing = (int)$connection->fetchOne( $connection->select() ->from($table, 'COUNT(*)') ->where('entity_type = ?', $entityType) ->where('entity_id = ?', $entityId) ->where('source_store_id = ?', max(0, $sourceStoreId)) ->where('target_store_id = ?', $targetStoreId) ->where('provider = ?', $provider) ->where('status IN (?)', ['pending', 'processing']) ); if ($existing > 0) { $skipped++; continue; } $rows[] = [ 'batch_id' => $batchId, 'provider' => $provider, 'entity_type' => $entityType, 'entity_id' => $entityId, 'source_store_id' => max(0, $sourceStoreId), 'target_store_id' => $targetStoreId, 'status' => 'pending', ]; } } if ($rows !== []) { $connection->insertMultiple($table, $rows); } return ['batch_id' => $batchId, 'created' => count($rows), 'skipped' => $skipped]; } /** @return array{deleted:int,processing:int} */ public function clearAll(): array { $connection = $this->resourceConnection->getConnection(); $table = $this->resourceConnection->getTableName(self::TABLE_NAME); $processing = (int)$connection->fetchOne( $connection->select() ->from($table, 'COUNT(*)') ->where('status = ?', 'processing') ); return [ 'deleted' => $connection->delete($table), 'processing' => $processing, ]; } }