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/vendor/wyomind/msicustomattributes/Controller/Adminhtml/CustomAttribute/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/vendor/wyomind/msicustomattributes/Controller/Adminhtml/CustomAttribute/Save.php
<?php

/**
 * Copyright © 2020 Wyomind. All rights reserved.
 * See LICENSE.txt for license details.
 */

namespace Wyomind\MsiCustomAttributes\Controller\Adminhtml\CustomAttribute;

/**
 * Class Save
 * @package Wyomind\MsiCustomAttributes\Controller\Adminhtml\CustomAttribute
 */
class Save extends \Wyomind\MsiCustomAttributes\Controller\Adminhtml\CustomAttribute
{
    public $dataPersistor;
    /**
     * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void
     */
    public function execute()
    {
        $data = $this->getRequest()->getPostValue();

        if ($data) {
            $attribute = $this->customattributeModelFactory->create();
            $id = $this->getRequest()->getParam('attribute_id');
            if (empty($id)) {
                $id = null;
            }


            if (empty($data['attribute_id'])) {
                $data['attribute_id'] = null;
            }
            $params = $this->getRequest()->getParams();


            try {
                $attribute->setData([
                    "attribute_id" => $id,
                    "code" => $params["code"],
                    "label" => $params["label"],
                    "type" => $params["type"],
                    "sort_order" => $params["sort_order"],
                    "default" => $params["default"],
                    "is_required" => $params["is_required"],
                ]);


                $this->dataPersistor->set('custom_attribute', $attribute);
                $optionsObject = [];
                if (isset($params["dynamic-rows"])) {
                    $options = $params["dynamic-rows"];
                    foreach ($options as $option) {
                        $optionObject = new \Magento\Framework\DataObject();
                        $optionsObject[] = $optionObject->setItem($option);
                    }
                    $this->dataPersistor->set('custom_attribute_options', $optionsObject);
                }
                $attribute->save();


                if (isset($params["dynamic-rows"])) {
                    $records = $params['dynamic-rows'];


                    if (is_array($records) && !empty($records)) {
                        $optionResource = $this->customAttributeDropdownOptionResource->create();
                        $optionResource->deleteValues($attribute->getAttributeId());
                        foreach ($records as $i => $record) {
                            $isDefault = false;
                            if ($i == $params["is_default"]) {
                                $isDefault = true;
                                $attribute->setData("default", $i);
                                $attribute->save();
                            }

                            $options = [

                                "option_id" => null,
                                "attribute_id" => $attribute->getAttributeId(),
                                "position" => $record["position"],
                                "is_default" => $isDefault


                            ];


                            $modelDropdownOption = $this->customAttributeDropdownOption->create();
                            $modelDropdownOption->addData($options);
                            $modelDropdownOption->save();


                            $optionValuesResource = $this->customAttributeDropdownOptionValueResource->create();
                            $optionValuesResource->deleteValues($modelDropdownOption->getOptionId());

                            foreach ($record as $key => $value) {
                                if (preg_match("#storeview_([0-9]+)_value#", $key, $matches)) {
                                    if (!empty($value)) {
                                        $values = [

                                            "value_id" => null,
                                            "option_id" => $modelDropdownOption->getOptionId(),
                                            "store_id" => $matches[1],
                                            "value" => $value

                                        ];

                                        $modelDropdownOptionValue = $this->customAttributeDropdownOptionValue->create();
                                        $modelDropdownOptionValue->addData($values);


                                        $modelDropdownOptionValue->save();
                                    }
                                }
                            }
                        }
                    }
                }

                $this->dataPersistor->clear('custom_attribute');
                $this->dataPersistor->clear('custom_attribute_options');
                $this->messageManager->addSuccess(__('The attribute has been saved.'));

                if ($this->getRequest()->getParam('back')) {
                    $this->_redirect("msicustomattributes/customattribute/edit", ['attribute_id' => $attribute->getId()]);
                    return;
                }
            } catch (\Exception $e) {
                $this->messageManager->addError(__('Unable to save the attribute. ' . $e->getMessage()));
                $this->_redirect("msicustomattributes/customattribute/edit", ['attribute_id' => $attribute->getId()]);
                return;
            }
        }

        $this->_redirect("msicustomattributes/customattribute/index", ['attribute_id' => $attribute->getId()]);
    }
}

Spamworldpro Mini