Spamworldpro Mini Shell
Spamworldpro


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/Catalog/Setup/Patch/Data/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Cnc/Catalog/Setup/Patch/Data/CreateMSIStockAttributes.php
<?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)
 * Cnc
 * Krzysztof Majkowski <[email protected]>
 */

namespace Cnc\Catalog\Setup\Patch\Data;

use Kaliop\Core\Model\Import\AbstractImport;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\Module\Dir\Reader;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Psr\Log\LoggerInterface;
use Wyomind\MsiCustomAttributes\Model\CustomAttributeFactory;

class CreateMSIStockAttributes extends AbstractImport implements DataPatchInterface
{
    const LOCALIZATION_FILENAME = 'localization.csv';

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @var CustomAttributeFactory
     */
    private $customAttributeFactory;

    /**
     * @var ResourceConnection
     */
    private $resourceConnection;

    /**
     * CreateMSIStockAttributes constructor.
     * @param LoggerInterface $logger
     * @param CustomAttributeFactory $customAttributeFactory
     * @param ResourceConnection $resourceConnection
     * @param File $fileSystem
     * @param DirectoryList $directoryList
     * @param Reader $moduleReader
     */
    public function __construct(
        LoggerInterface $logger,
        CustomAttributeFactory $customAttributeFactory,
        ResourceConnection $resourceConnection,
        File $fileSystem,
        DirectoryList $directoryList,
        Reader $moduleReader
    ) {
        parent::__construct($fileSystem, $directoryList, $moduleReader);

        $this->logger = $logger;
        $this->customAttributeFactory = $customAttributeFactory;
        $this->resourceConnection = $resourceConnection;
    }

    /**
     * @return ConfigureMSIStocks|void
     */
    public function apply()
    {
        $conn = $this->resourceConnection->getConnection();

        $attributes = $this->getAttributes();
        foreach ($attributes as $attribute) {
            $code = $attribute['code'];

            $conn->delete('msi_custom_attribute', "code = '{$code}'");
            $conn->insert(
                'msi_custom_attribute',
                [
                    'code' => $code,
                    'label' => $attribute['label'],
                    'type' => $attribute['type'],
                    'sort_order' => $attribute['sort_order']
                ]
            );

            if (isset($attribute['options'])) {
                $sql = "SELECT attribute_id FROM msi_custom_attribute WHERE code = '{$code}'";
                $attributeId = $conn->fetchOne($sql);
                $options = $attribute['options'];
                $position = 0;
                foreach ($options as $option) {
                    $conn->insert(
                        'msi_custom_attribute_dropdown_option',
                        [
                            'attribute_id' => $attributeId,
                            'position' => $position++,
                            'is_default' => 0
                        ]
                    );

                    $sql = "SELECT option_id FROM msi_custom_attribute_dropdown_option WHERE attribute_id = {$attributeId} ORDER BY option_id DESC LIMIT 1";
                    $optionId = $conn->fetchOne($sql);
                    $conn->insert(
                        'msi_custom_attribute_dropdown_option_value',
                        [
                            'option_id' => $optionId,
                            'store_id' => 0,
                            'value' => $option['label']
                        ]
                    );

                    if (isset($option['label_fr'])) {
                        $conn->insert(
                            'msi_custom_attribute_dropdown_option_value',
                            [
                                'option_id' => $optionId,
                                'store_id' => 1,
                                'value' => $option['label_fr']
                            ]
                        );
                    }
                }
            }
        }
    }

    /**
     * @return array|string[]
     */
    public static function getDependencies(): array
    {
        return [];
    }

    /**
     * @return array|string[]
     */
    public function getAliases(): array
    {
        return [];
    }

    /**
     * @return array[]
     */
    protected function getAttributes(): array
    {
        return [
            [
                'code' => 'cnc_localization',
                'label' => 'Localization',
                'type' => 'select',
                'sort_order' => 0,
                'options' => $this->getLocalizationOptions()
            ],
            [
                'code' => 'cnc_availability',
                'label' => 'Availability',
                'type' => 'select',
                'sort_order' => 10,
                'options' => [
                    [
                        'label' => 'Immediate shipment',
                        'label_fr' => 'Envoi immédiat'
                    ],
                    [
                        'label' => 'Shipment within 3 days',
                        'label_fr' => 'Envoi sous 3 jours'
                    ],
                    [
                        'label' => 'Available in 5 to 8 days',
                        'label_fr' => 'Disponible sous 5 à 8 jours'
                    ],
                    [
                        'label' => 'Confirmation of shipping time on request',
                        'label_fr' => 'Confirmation du délai à la demande'
                    ],
                    [
                        'label' => 'Out of stock',
                        'label_fr' => 'En rupture de stock'
                    ],
                ]
            ]
        ];
    }

    /**
     * @return array
     * @throws FileSystemException
     * @throws LocalizedException
     */
    private function getLocalizationOptions(): array
    {
        $options = [];
        $handle = $this->openFile('Cnc_Localization', self::LOCALIZATION_FILENAME);

        while (($line = $this->fileSystem->fileGetCsv($handle, ',')) !== false) {
            $options []= [
                'label' => $line[0]
            ];
        }

        return $options;
    }
}

Spamworldpro Mini