![]() 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/extmag/shiplab/Block/Adminhtml/Configuration/ |
<?php /** * Copyright © Extmag. All rights reserved. */ namespace Extmag\Shiplab\Block\Adminhtml\Configuration; use Extmag\Shiplab\Helper\Config; use Extmag\Shiplab\Helper\Config\Structure\Element\Field as ExtmagField; use Extmag\Shiplab\Helper\Config\Structure\Element\Group; use Extmag\Shiplab\Helper\Config\Structure\Element\Section; use Magento\Backend\Block\Template\Context; use Magento\Backend\Block\Widget\Form\Element\Dependence; use Magento\Backend\Block\Widget\Form\Generic; use Magento\Config\App\Config\Type\System; use Magento\Config\Block\System\Config\Form\Field\File; use Magento\Config\Block\System\Config\Form\Field\Image; use Magento\Config\Block\System\Config\Form\Field\Select\Allowspecific; use Magento\Config\Block\System\Config\Form\Fieldset\Factory; use Magento\Config\Model\Config\Reader\Source\Deployed\SettingChecker; use Magento\Config\Model\Config\Structure\Element\Dependency\Field; use Magento\Config\Model\Config\Structure\ElementVisibilityInterface; use Magento\Framework\App\Config\Data\ProcessorInterface; use Magento\Framework\App\DeploymentConfig; use Magento\Framework\App\ObjectManager; use Magento\Framework\Data\Form\AbstractForm; use Magento\Framework\Data\Form\Element\Fieldset; use Magento\Framework\Data\FormFactory; use Magento\Framework\DataObject; use Magento\Framework\Exception\FileSystemException; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\RuntimeException; use Magento\Framework\Registry; use Magento\Framework\View\Element\AbstractBlock; class Form extends Generic { public const SCOPE_DEFAULT = 'default'; public const SCOPE_STORES = 'store'; public const SCOPE_DIRECTIONS = 'direction'; public const SCOPE_COUNTRIES = 'country'; /** * @var array */ protected $_configData; /** * Backend config data instance * * @var \Extmag\Shiplab\Model\Config */ protected $_configDataObject; /** * Default fieldset rendering block * * @var \Magento\Config\Block\System\Config\Form\Fieldset */ protected $_fieldsetRenderer; /** * Default field rendering block * * @var Form\Field */ protected $_fieldRenderer; /** * List of fieldset * * @var array */ protected $_fieldsets = []; /** * Translated scope labels * * @var array */ protected $_scopeLabels = []; /** * Backend Config model factory * * @var \Magento\Config\Model\Config\Factory */ protected $_configFactory; /** * Magento\Framework\Data\FormFactory * * @var FormFactory */ protected $_formFactory; /** * System config structure * * @var Config */ protected $_configStructure; /** * Form fieldset factory * * @var Factory */ protected $_fieldsetFactory; /** * Form field factory * * @var Form\Field\Factory */ protected $_fieldFactory; /** * @var SettingChecker */ protected $settingChecker; /** * @var DeploymentConfig */ protected $appConfig; /** * Checks visibility status of form elements on Stores > Settings > Configuration page in Admin Panel * by their paths in the system.xml structure. * * @var ElementVisibilityInterface */ protected $elementVisibility; /** * @param Context $context * @param Registry $registry * @param FormFactory $formFactory * @param Config $configStructure * @param Factory $fieldsetFactory * @param Form\Field\Factory $fieldFactory * @param array $data */ public function __construct( Context $context, Registry $registry, FormFactory $formFactory, Config $configStructure, Factory $fieldsetFactory, Form\Field\Factory $fieldFactory, array $data = [] ) { parent::__construct($context, $registry, $formFactory, $data); $this->_configStructure = $configStructure; $this->_fieldsetFactory = $fieldsetFactory; $this->_fieldFactory = $fieldFactory; $this->_scopeLabels = [ self::SCOPE_DEFAULT => __('[GLOBAL]'), self::SCOPE_STORES => __('[STORE]'), self::SCOPE_DIRECTIONS => __('[DIRECTION]'), self::SCOPE_COUNTRIES => __('[COUNTRY]'), ]; } /** * Gets instance of ElementVisibilityInterface. * * @return ElementVisibilityInterface * @deprecated 101.0.0 Added to not break backward compatibility of the constructor signature * by injecting the new dependency directly. * The method can be removed in a future major release, when constructor signature can be changed. * @since 101.0.0 */ public function getElementVisibility() { if (null === $this->elementVisibility) { $this->elementVisibility = ObjectManager::getInstance()->get(ElementVisibilityInterface::class); } return $this->elementVisibility; } /** * @return \Magento\Backend\Block\Widget\Form|AbstractBlock * @throws LocalizedException */ protected function _beforeToHtml() { $this->initForm(); return parent::_beforeToHtml(); } /** * Initialize form * * @return $this * @throws LocalizedException */ public function initForm() { $this->_initObjects(); $form = $this->_formFactory->create(); /** @var $section Section */ $section = $this->_configStructure->getElement($this->getSectionCode()); if ($section && $section->isVisible($this->getStoreCode(), $this->getDirectionCode(), $this->getCountryCode()) ) { foreach ($section->getChildren() as $group) { $this->_initGroup($group, $section, $form); } } $this->setForm($form); return $this; } /** * Initialize objects required to render config form * * @return $this */ protected function _initObjects() { $this->_configDataObject = ObjectManager::getInstance()->create( \Extmag\Shiplab\Model\Config::class, [ 'data' => [ 'section' => $this->getSectionCode(), 'store' => $this->getStoreCode(), 'direction' => $this->getDirectionCode(), 'country' => $this->getCountryCode(), ], ] ); $this->_configData = $this->_configDataObject->load(); $this->_fieldsetRenderer = $this->_fieldsetFactory->create(); $this->_fieldRenderer = $this->_fieldFactory->create(); return $this; } /** * Enter description here... * * @return string */ public function getSectionCode() { return $this->getRequest()->getParam('section', ''); } /** * Enter description here... * * @return string */ public function getStoreCode() { return $this->getRequest()->getParam('store', ''); } /** * Enter description here... * * @return string */ public function getDirectionCode() { return $this->getRequest()->getParam('direction', ''); } /** * Enter description here... * @return string */ public function getCountryCode() { return $this->getRequest()->getParam('country', ''); } /** * Initialize config field group * * @param Group $group * @param Section $section * @param AbstractForm $form * * @return void * @throws LocalizedException */ protected function _initGroup( Group $group, Section $section, AbstractForm $form ) { $frontendModelClass = $group->getFrontendModel(); $fieldsetRenderer = $frontendModelClass ? $this->_layout->getBlockSingleton( $frontendModelClass ) : $this->_fieldsetRenderer; $fieldsetRenderer->setForm($this); $fieldsetRenderer->setConfigData($this->_configData); $fieldsetRenderer->setGroup($group); $fieldsetConfig = [ 'legend' => $group->getLabel(), 'comment' => $group->getComment(), 'expanded' => $group->isExpanded(), 'group' => $group->getData(), ]; $fieldset = $form->addFieldset($this->_generateElementId($group->getPath()), $fieldsetConfig); $fieldset->setRenderer($fieldsetRenderer); $group->populateFieldset($fieldset); $this->_addElementTypes($fieldset); $dependencies = $group->getDependencies($this->getCountryCode()); $elementName = $this->_generateElementName($group->getPath()); $elementId = $this->_generateElementId($group->getPath()); $this->_populateDependenciesBlock($dependencies, $elementId, $elementName); if ($group->shouldCloneFields()) { $cloneModel = $group->getCloneModel(); foreach ($cloneModel->getPrefixes() as $prefix) { $this->initFields($fieldset, $group, $section, $prefix['field'], $prefix['label']); } } else { $this->initFields($fieldset, $group, $section); } $this->_fieldsets[$group->getId()] = $fieldset; } /** * Generate element id * * @param string $path * * @return string */ protected function _generateElementId($path) { return str_replace('/', '_', $path); } /** * Generate element name * * @param string $elementPath * @param string $fieldPrefix * @param string $separator * * @return string */ protected function _generateElementName($elementPath, $fieldPrefix = '', $separator = '/') { $part = explode($separator, $elementPath); array_shift($part); //shift section name $fieldId = array_pop($part); //shift filed id $groupName = implode('][groups][', $part); return 'groups[' . $groupName . '][fields][' . $fieldPrefix . $fieldId . '][value]'; } /** * Populate dependencies block * * @param array $dependencies * @param string $elementId * @param string $elementName * * @return void */ protected function _populateDependenciesBlock(array $dependencies, $elementId, $elementName) { foreach ($dependencies as $dependentField) { /** @var $dependentField Field */ $fieldNameFrom = $this->_generateElementName($dependentField->getId(), null, '_'); $this->_getDependence()->addFieldMap( $elementId, $elementName )->addFieldMap( $this->_generateElementId($dependentField->getId()), $fieldNameFrom )->addFieldDependence( $elementName, $fieldNameFrom, $dependentField ); } } /** * Return dependency block object * * @return bool|AbstractBlock */ protected function _getDependence() { if (!$this->getChildBlock('element_dependence')) { $this->addChild('element_dependence', Dependence::class); } return $this->getChildBlock('element_dependence'); } /** * Initialize config group fields * * @param Fieldset $fieldset * @param Group $group * @param Section $section * @param string $fieldPrefix * @param string $labelPrefix * * @return $this * @throws LocalizedException */ public function initFields( Fieldset $fieldset, Group $group, Section $section, $fieldPrefix = '', $labelPrefix = '' ) { if (!$this->_configDataObject) { $this->_initObjects(); } // Extends for config data $extraConfigGroups = []; /** @var $element ExtmagField */ foreach ($group->getChildren() as $element) { if ($element instanceof Group) { $this->_initGroup($element, $section, $fieldset); } else { $path = $element->getConfigPath() ?: $element->getPath($fieldPrefix); if ($element->getSectionId() != $section->getId()) { $groupPath = $element->getGroupPath(); if (!isset($extraConfigGroups[$groupPath])) { $this->_configData = $this->_configDataObject->extendConfig( $groupPath, false, $this->_configData ); $extraConfigGroups[$groupPath] = true; } } $this->_initElement($element, $fieldset, $path, $fieldPrefix, $labelPrefix); } } return $this; } /** * Initialize form element * * @param ExtmagField $field * @param Fieldset $fieldset * @param string $path * @param string $fieldPrefix * @param string $labelPrefix * * @return void * @throws FileSystemException * @throws RuntimeException */ protected function _initElement( ExtmagField $field, Fieldset $fieldset, $path, $fieldPrefix = '', $labelPrefix = '' ) { $inherit = !isset($this->_configData[$path]); $data = $this->getFieldData($field, $path); $fieldRendererClass = $field->getFrontendModel(); if ($fieldRendererClass) { $fieldRenderer = $this->_layout->getBlockSingleton($fieldRendererClass); } else { $fieldRenderer = $this->_fieldRenderer; } $fieldRenderer->setForm($this); $fieldRenderer->setConfigData($this->_configData); $elementName = $this->_generateElementName($field->getPath(), $fieldPrefix); $elementId = $this->_generateElementId($field->getPath($fieldPrefix)); $dependencies = $field->getDependencies($fieldPrefix, $this->getStoreCode()); $this->_populateDependenciesBlock($dependencies, $elementId, $elementName); $sharedClass = $this->_getSharedCssClass($field); $requiresClass = $this->_getRequiresCssClass($field, $fieldPrefix); $isReadOnly = false; /*$isReadOnly = $this->getElementVisibility()->isDisabled($field->getPath()) ?: $this->getSettingChecker()->isReadOnly($path, $this->getScope(), $this->getStringScopeCode());*/ $formField = $fieldset->addField( $elementId, $field->getType(), [ 'name' => $elementName, 'label' => $field->getLabel($labelPrefix), 'comment' => $field->getComment($data), 'tooltip' => $field->getTooltip(), 'hint' => $field->getHint(), 'value' => $data, 'inherit' => $inherit, 'class' => $field->getFrontendClass() . $sharedClass . $requiresClass, 'field_config' => $field->getData(), 'scope' => $this->getScope(), 'scope_id' => $this->getScopeId(), 'scope_label' => $this->getScopeLabel($field), 'can_use_default_value' => $this->canUseDefaultValue($field->showInDefault()), 'can_use_store_value' => $this->canUseStoreValue($field->showInStore()), 'can_use_direction_value' => $this->canUseDirectionValue($field->showInDirection()), 'can_use_country_value' => $this->canUseCountryValue($field->showInCountry()), 'can_restore_to_default' => $this->isCanRestoreToDefault($field->canRestore()), 'disabled' => $isReadOnly, 'is_disable_inheritance' => $isReadOnly, ] ); $field->populateInput($formField); if ($field->hasValidation()) { $formField->addClass($field->getValidation()); } if ($field->getType() == 'multiselect') { $formField->setCanBeEmpty($field->canBeEmpty()); } if ($field->hasOptions()) { $formField->setValues($field->getOptions()); } $formField->setRenderer($fieldRenderer); } /** * Get data of field by path * * @param ExtmagField $field * @param string $path * * @return mixed|null|string * @throws FileSystemException * @throws RuntimeException */ protected function getFieldData(ExtmagField $field, $path) { $data = $this->getAppConfigDataValue($path); $placeholderValue = $this->getSettingChecker()->getPlaceholderValue( $path, $this->getScope(), $this->getStringScopeCode() ); if ($placeholderValue) { $data = $placeholderValue; } if ($data === null) { $path = $field->getConfigPath() !== null ? $field->getConfigPath() : $path; $data = $this->getConfigValue($path); if ($field->hasBackendModel()) { $backendModel = $field->getBackendModel(); // Backend models which implement ProcessorInterface are processed by ScopeConfigInterface if (!$backendModel instanceof ProcessorInterface) { $backendModel->setPath($path) ->setValue($data) ->setStore($this->getStoreCode()) ->setDirection($this->getDirectionCode()) ->setCountry($this->getCountryCode()) ->afterLoad(); $data = $backendModel->getValue(); } } } return $data; } /** * Retrieve deployment config data value by path * * @param string $path * * @return null|string * @throws FileSystemException * @throws RuntimeException */ protected function getAppConfigDataValue($path) { $appConfig = $this->getAppConfig()->get(System::CONFIG_TYPE); $scope = $this->getScope(); $scopeCode = $this->getStringScopeCode(); if ($scope === 'default') { $data = new DataObject($appConfig[$scope] ?? []); } else { $data = new DataObject($appConfig[$scope][$scopeCode] ?? []); } return $data->getData($path); } /** * Retrieve Deployment Configuration object. * * @return DeploymentConfig * @deprecated 100.1.2 */ protected function getAppConfig() { if ($this->appConfig === null) { $this->appConfig = ObjectManager::getInstance()->get(DeploymentConfig::class); } return $this->appConfig; } /** * Retrieve current scope * * @return string */ public function getScope() { $scope = $this->getData('scope'); if ($scope === null) { if ($this->getCountryCode()) { $scope = self::SCOPE_COUNTRIES; } elseif ($this->getDirectionCode()) { $scope = self::SCOPE_DIRECTIONS; } elseif ($this->getStoreCode()) { $scope = self::SCOPE_STORES; } else { $scope = self::SCOPE_DEFAULT; } $this->setScope($scope); } return $scope; } /** * Retrieve Scope string code * * @return string */ protected function getStringScopeCode() { $scopeCode = $this->getData('scope_string_code'); if (null === $scopeCode) { if ($this->getCountryCode()) { $scopeCode = $this->getCountryCode(); } elseif ($this->getDirectionCode()) { $scopeCode = $this->getDirectionCode(); } elseif ($this->getStoreCode()) { $scopeCode = $this->getStoreCode(); } else { $scopeCode = ''; } $this->setData('scope_string_code', $scopeCode); } return $scopeCode; } /** * @return SettingChecker * @deprecated 100.1.2 */ protected function getSettingChecker() { if ($this->settingChecker === null) { $this->settingChecker = ObjectManager::getInstance()->get(SettingChecker::class); } return $this->settingChecker; } /** * Get config value * * @param string $path * * @return mixed */ public function getConfigValue($path) { return $this->_configStructure->getConfigValue($path, $this->getScope(), $this->getScopeCode()); } /** * Get current scope code * * @return string */ public function getScopeCode() { $scopeCode = $this->getData('scope_code'); if ($scopeCode === null) { if ($this->getCountryCode()) { $scopeCode = $this->getCountryCode(); } elseif ($this->getDirectionCode()) { $scopeCode = $this->getDirectionCode(); } elseif ($this->getStoreCode()) { $scopeCode = $this->getStoreCode(); } else { $scopeCode = ''; } $this->setScopeCode($scopeCode); } return $scopeCode; } /** * Get css class for "shared" functionality * * @param ExtmagField $field * * @return string */ protected function _getSharedCssClass(ExtmagField $field) { $sharedClass = ''; if ($field->getAttribute('shared') && $field->getConfigPath()) { return ' shared shared-' . str_replace('/', '-', $field->getConfigPath()); } return $sharedClass; } /** * Get css class for "requires" functionality * * @param ExtmagField $field * @param string $fieldPrefix * * @return string */ protected function _getRequiresCssClass(ExtmagField $field, $fieldPrefix) { $requiresClass = ''; $requiredPaths = array_merge($field->getRequiredFields($fieldPrefix), $field->getRequiredGroups($fieldPrefix)); if (!empty($requiredPaths)) { $requiresClass = ' requires'; foreach ($requiredPaths as $requiredPath) { $requiresClass .= ' requires-' . $this->_generateElementId($requiredPath); } return $requiresClass; } return $requiresClass; } /** * Temporary moved those $this->getRequest()->getParam('blabla') from the code across this block * to getBlala() methods to be later set from controller with setters */ /** * Get current scope code * * @return int|string */ public function getScopeId() { $scopeId = $this->getData('scope_id'); if ($scopeId === null) { if ($this->getCountryCode()) { $scopeId = $this->getCountryCode(); } elseif ($this->getDirectionCode()) { $scopeId = $this->getDirectionCode(); } elseif ($this->getStoreCode()) { $scopeId = $this->getStoreCode(); } else { $scopeId = ''; } $this->setScopeId($scopeId); } return $scopeId; } /** * Retrieve label for scope * * @param ExtmagField $field * * @return string */ public function getScopeLabel(ExtmagField $field) { $showInCountries = $field->showInCountry(); $showInDirections = $field->showInDirection(); $showInStores = $field->showInStore(); if ($showInCountries == 1) { return $this->_scopeLabels[self::SCOPE_COUNTRIES]; } elseif ($showInDirections == 1) { return $this->_scopeLabels[self::SCOPE_DIRECTIONS]; } elseif ($showInStores == 1) { return $this->_scopeLabels[self::SCOPE_STORES]; } return $this->_scopeLabels[self::SCOPE_DEFAULT]; } /** * Check if can use default value * * @param int $fieldValue * * @return boolean */ public function canUseDefaultValue($fieldValue) { if ($this->getScope() == self::SCOPE_COUNTRIES && $fieldValue) { return true; } if ($this->getScope() == self::SCOPE_DIRECTIONS && $fieldValue) { return true; } if ($this->getScope() == self::SCOPE_STORES && $fieldValue) { return true; } return false; } /** * Check if can use store value * * @param int $fieldValue * * @return boolean */ public function canUseStoreValue($fieldValue) { if ($this->getScope() == self::SCOPE_COUNTRIES && $fieldValue) { return true; } return false; } /** * Check if can use direction value * * @param int $fieldValue * * @return boolean */ public function canUseDirectionValue($fieldValue) { if ($this->getScope() == self::SCOPE_DIRECTIONS && $fieldValue) { return true; } return false; } /** * Check if can use direction value * * @param int $fieldValue * * @return boolean */ public function canUseCountryValue($fieldValue) { if ($this->getScope() == self::SCOPE_COUNTRIES && $fieldValue) { return true; } return false; } /** * Check if can use restore value * * @param int $fieldValue * * @return bool * @since 100.1.0 */ public function isCanRestoreToDefault($fieldValue) { if ($this->getScope() == self::SCOPE_DEFAULT && $fieldValue) { return true; } return false; } /** * Append dependence block at then end of form block * * @param string $html * * @return string */ protected function _afterToHtml($html) { if ($this->_getDependence()) { $html .= $this->_getDependence()->toHtml(); } return parent::_afterToHtml($html); } /** * Get additional element types * * @return array */ protected function _getAdditionalElementTypes() { return [ 'allowspecific' => Allowspecific::class, 'image' => Image::class, 'file' => File::class, ]; } }