![]() 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/Model/ResourceModel/ |
<?php /** * Copyright © 2020 Wyomind. All rights reserved. * See LICENSE.txt for license details. */ /** * Copyright © 2020 Wyomind. All rights reserved. * See LICENSE.txt for license details. */ namespace Wyomind\MsiCustomAttributes\Model\ResourceModel; use Wyomind\MsiCustomAttributes\Model\ResourceModel\CustomAttribute\CollectionFactory; /** * Class Attributes * @package Wyomind\MsiCustomAttributes\Model\ResourceModel */ class CustomAttribute extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb { /** * */ const MSI_CUSTOM_ATTRIBUTE = "msi_custom_attribute"; /** * @var Attributes\CollectionFactory */ protected $attributesCollectionFactory; /** * Attributes constructor. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context * @param Attributes\CollectionFactory $attributesCollectionFactory * @param string|null $connectionName */ public function __construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, CollectionFactory $attributesCollectionFactory, string $connectionName = null) { $this->attributesCollectionFactory = $attributesCollectionFactory; parent::__construct($context, $connectionName); } /** * */ protected function _construct() { $this->_init(self::MSI_CUSTOM_ATTRIBUTE, \Wyomind\MsiCustomAttributes\Api\Data\CustomAttributeInterface::ATTRIBUTE_ID); } /** * @param \Magento\Framework\Model\AbstractModel $attribute * @return \Magento\Framework\Model\ResourceModel\Db\AbstractDb * @throws \Exception */ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $attribute) { $disallowed = array_keys(\Wyomind\MsiCustomAttributes\Helper\CustomAttribute::RESERVED_ATTRIBUTE_CODES); if (!$attribute->getAttributeId()) { if (empty($attribute->getCode())) { $label = $attribute->getLabel(); $code = preg_replace("/[^ a-z0-9_]+/", "", strtolower($label)); $code = str_replace(" ", "_", $code); if (in_array($code, $disallowed)) { throw new \Exception(__('The attribute code <i>%1</i> is reserved.', $code)); } $attribute->setCode($code); } $collection = $this->attributesCollectionFactory->create()->getByCode($attribute->getCode()); $collection->addFieldToFilter("attribute_id", ["neq" => $attribute->getId()]); if (count($collection) > 0) { throw new \Exception(__('An attribute with the same code already exists')); } } return parent::_beforeSave($attribute); } }