![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Cnc/SerialNumber/Controller/Adminhtml/SerialNumber/ |
<?php /** * Copyright (c) 2020 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved. * https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * Krzysztof Majkowski <[email protected]> */ declare(strict_types=1); namespace Cnc\SerialNumber\Controller\Adminhtml\SerialNumber; use Cnc\SerialNumber\Api\SerialNumberRepositoryInterface; use Cnc\SerialNumber\Model\ResourceModel\SerialNumber\Collection; use Cnc\SerialNumber\Model\ResourceModel\SerialNumber\CollectionFactory; use Cnc\SerialNumber\Model\SerialNumber; use Magento\Backend\App\Action; use Magento\Backend\App\Action\Context; use Magento\Framework\App\Action\HttpPostActionInterface; use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\App\ResourceConnection; use Magento\Framework\App\Response\Http\FileFactory; use Magento\Framework\App\ResponseInterface; use Magento\Framework\Controller\Result\Json; use Magento\Framework\Controller\Result\JsonFactory; use Magento\Framework\Controller\ResultInterface; use Magento\Framework\Exception\AlreadyExistsException; use Magento\Framework\Exception\FileSystemException; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Filesystem; use Magento\Ui\Component\MassAction\Filter; use Soon\DataSync\Model\Data\Type\Csv; class MassExport extends Action implements HttpPostActionInterface { /** * Authorization level of a basic admin session */ const ADMIN_RESOURCE = 'Cnc_SerialNumber::export'; const MSI_ATTRIBUTES_TO_JOIN = [ 'state' => 'cnc_availability', 'localization' => 'cnc_localization' ]; /** * @var SerialNumberRepositoryInterface */ private $serialNumberRepository; /** * @var CollectionFactory */ private $collectionFactory; /** * @var Filter */ private $filter; /** * @var Csv */ private $csv; /** * @var FileFactory */ private $fileFactory; /** * @var Filesystem */ private $filesystem; /** * @var JsonFactory */ private $jsonFactory; /** * @var ResourceConnection */ private $resourceConnection; /** * InlineEdit constructor. * @param Context $context * @param SerialNumberRepositoryInterface $serialNumberRepository * @param CollectionFactory $collectionFactory * @param Filter $filter * @param Csv $csv * @param FileFactory $fileFactory * @param Filesystem $filesystem * @param JsonFactory $jsonFactory * @param ResourceConnection $resourceConnection */ public function __construct( Context $context, SerialNumberRepositoryInterface $serialNumberRepository, CollectionFactory $collectionFactory, Filter $filter, Csv $csv, FileFactory $fileFactory, Filesystem $filesystem, JsonFactory $jsonFactory, ResourceConnection $resourceConnection ) { parent::__construct($context); $this->serialNumberRepository = $serialNumberRepository; $this->collectionFactory = $collectionFactory; $this->filter = $filter; $this->csv = $csv; $this->fileFactory = $fileFactory; $this->filesystem = $filesystem; $this->jsonFactory = $jsonFactory; $this->resourceConnection = $resourceConnection; } /** * @return ResponseInterface|Json|ResultInterface * @throws LocalizedException */ public function execute() { $resultJson = $this->jsonFactory->create(); $error = false; $messages = []; try { $collection = $this->filter->getCollection($this->collectionFactory->create()); $filename = $this->exportCollection($collection); } catch (AlreadyExistsException | FileSystemException $e) { $error = true; $messages[] = 'We couldn\'t export those data.'; } return $resultJson->setData( [ 'messages' => $messages, 'error' => $error, 'filename' => $filename, 'url' => $this->getUrl('*/*/GetSerialNumber', ['filename' => $filename]) ] ); } /** * @param Collection $collection * @return string * @throws AlreadyExistsException * @throws FileSystemException */ private function exportCollection(Collection $collection): string { $this->preExport($collection); $data = []; $conn = $this->resourceConnection->getConnection(); /** @var SerialNumber $item */ foreach ($collection as $item) { $itemData = $item->toArray(); $itemData['status'] = $item->getStatusLabel(); $itemData['qty'] = 1; foreach (self::MSI_ATTRIBUTES_TO_JOIN as $attrLabel => $attrCode) { $sql = "SELECT msi_option_values.value FROM msi_custom_attribute AS msi_attributes LEFT JOIN msi_custom_attribute_value AS msi_values ON msi_values.attribute_id = msi_attributes.attribute_id LEFT JOIN msi_custom_attribute_dropdown_option_value AS msi_option_values ON msi_option_values.value_id = msi_values.value WHERE msi_attributes.code = '" . $attrCode ."' AND msi_values.sku = '" . $itemData['sku'] ."' AND msi_values.source_code = 'default'"; $attrValue = $conn->fetchOne($sql); $itemData[$attrLabel] = $attrValue; } $attrCode = 'cnc_manufacturer'; $sql = "SELECT attribute_id FROM eav_attribute WHERE attribute_code = '".$attrCode."'"; $attrId = $conn->fetchOne($sql); $sql = "SELECT attribute_value.name FROM catalog_product_entity AS catalog_product LEFT JOIN catalog_product_entity_int AS entity_int ON entity_int.entity_id = catalog_product.entity_id LEFT JOIN cnc_manufacturer AS attribute_value ON attribute_value.entity_id = entity_int.value WHERE catalog_product.sku = '" . $itemData['sku'] ."' AND entity_int.store_id = 0 AND entity_int.attribute_id = " . $attrId ; $attrValue = $conn->fetchOne($sql); $itemData['manufacturer'] = $attrValue; unset($itemData['is_exported']); $data []= $itemData; } $this->csv->fieldDelimiter = ';'; $content = $this->csv->convert($data); $filename = 'serial_number_' . date('YmdHis'); $file = 'serialnumbers/' . $filename . '.csv'; $directory = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR); $stream = $directory->openFile($file, 'w+'); $stream->lock(); $stream->write($content); $stream->unlock(); $stream->close(); $this->postExport($collection); return $filename; } /** * Pre export actions. * * @param Collection $collection * @throws FileSystemException */ private function preExport(Collection $collection) { $directory = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR); if (!$directory->isExist('serialnumbers')) { $directory->create('serialnumbers'); } } /** * Post export actions. * * @param Collection $collection * @throws AlreadyExistsException */ private function postExport(Collection $collection) { /** @var SerialNumber $item */ foreach ($collection as $item) { $item->setIsExported(1); $this->serialNumberRepository->save($item); } } }