/** * Check if specified section allowed in ACL * * Will forward to deniedAction(), if not allowed. * * @param string $sectionId * @throws \Exception * @return bool * @throws NotFoundException */ public function isSectionAllowed($sectionId) { try { if (false == $this->_configStructure->getElement($sectionId)->isAllowed()) { throw new \Exception(''); } return true; } catch (\Zend_Acl_Exception $e) { throw new NotFoundException(); } catch (\Exception $e) { return false; } }
/** * Retrieve list of options * * @return array */ public function toOptionArray() { if (is_null($this->_options)) { $this->_options = []; /** @var $section \Magento\Backend\Model\Config\Structure\Element\Section */ $section = $this->_configStructure->getElement('trans_email'); /** @var $group \Magento\Backend\Model\Config\Structure\Element\Group */ foreach ($section->getChildren() as $group) { $this->_options[] = ['value' => preg_replace('#^ident_(.*)$#', '$1', $group->getId()), 'label' => $group->getLabel()]; } ksort($this->_options); } return $this->_options; }
/** * Check if specified section allowed in ACL * * Will forward to deniedAction(), if not allowed. * * @param string $sectionId * @throws \Exception * @return bool * @throws NotFoundException */ protected function _isSectionAllowed($sectionId) { try { if (false == $this->_configStructure->getElement($sectionId)->isAllowed()) { throw new \Exception(''); } return true; } catch (\Zend_Acl_Exception $e) { throw new NotFoundException(); } catch (\Exception $e) { $this->deniedAction(); $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true); return false; } }
/** * Prepare layout object * * @return \Magento\Framework\View\Element\AbstractBlock */ protected function _prepareLayout() { /** @var $section \Magento\Backend\Model\Config\Structure\Element\Section */ $section = $this->_configStructure->getElement($this->getRequest()->getParam('section')); $this->_formBlockName = $section->getFrontendModel(); if (empty($this->_formBlockName)) { $this->_formBlockName = self::DEFAULT_SECTION_BLOCK; } $this->setTitle($section->getLabel()); $this->setHeaderCss($section->getHeaderCss()); $this->getToolbar()->addChild('save_button', 'Magento\\Backend\\Block\\Widget\\Button', array('id' => 'save', 'label' => __('Save Config'), 'class' => 'save primary', 'data_attribute' => array('mage-init' => array('button' => array('event' => 'save', 'target' => '#config-edit-form'))))); $block = $this->getLayout()->createBlock($this->_formBlockName); $this->setChild('form', $block); return parent::_prepareLayout(); }
public function testGetElementReturnsProperElementByPathCachesObject() { $elementMock = $this->getMock('Magento\\Backend\\Model\\Config\\Structure\\Element\\Field', array(), array(), '', false); $section = $this->_structureData['config']['system']['sections']['section_1']; $fieldData = $section['children']['group_level_1']['children']['field_3']; $elementMock->expects($this->once())->method('setData')->with($fieldData, 'scope'); $this->_flyweightFactory->expects($this->once())->method('create')->with('field')->will($this->returnValue($elementMock)); $this->assertEquals($elementMock, $this->_model->getElement('section_1/group_level_1/field_3')); $this->assertEquals($elementMock, $this->_model->getElement('section_1/group_level_1/field_3')); }
/** * Convert xml config paths to decorated names * * @param array $paths * @return array * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function _getSystemConfigPathsParts($paths) { $result = $urlParams = $prefixParts = array(); $scopeLabel = __('GLOBAL'); if ($paths) { /** @var $menu \Magento\Backend\Model\Menu */ $menu = $this->_menuConfig->getMenu(); $item = $menu->get('Magento_Backend::stores'); // create prefix path parts $prefixParts[] = array('title' => __($item->getTitle())); $item = $menu->get('Magento_Backend::system_config'); $prefixParts[] = array('title' => __($item->getTitle()), 'url' => $this->getUrl('adminhtml/system_config/')); $pathParts = $prefixParts; foreach ($paths as $pathData) { $pathDataParts = explode('/', $pathData['path']); $sectionName = array_shift($pathDataParts); $urlParams = array('section' => $sectionName); if (isset($pathData['scope']) && isset($pathData['scope_id'])) { switch ($pathData['scope']) { case 'stores': $store = $this->_storeManager->getStore($pathData['scope_id']); if ($store) { $urlParams['website'] = $store->getWebsite()->getCode(); $urlParams['store'] = $store->getCode(); $scopeLabel = $store->getWebsite()->getName() . '/' . $store->getName(); } break; case 'websites': $website = $this->_storeManager->getWebsite($pathData['scope_id']); if ($website) { $urlParams['website'] = $website->getCode(); $scopeLabel = $website->getName(); } break; default: break; } } $pathParts[] = array('title' => $this->_configStructure->getElement($sectionName)->getLabel(), 'url' => $this->getUrl('adminhtml/system_config/edit', $urlParams)); $elementPathParts = array($sectionName); while (count($pathDataParts) != 1) { $elementPathParts[] = array_shift($pathDataParts); $pathParts[] = array('title' => $this->_configStructure->getElementByPathParts($elementPathParts)->getLabel()); } $elementPathParts[] = array_shift($pathDataParts); $pathParts[] = array('title' => $this->_configStructure->getElementByPathParts($elementPathParts)->getLabel(), 'scope' => $scopeLabel); $result[] = $pathParts; $pathParts = $prefixParts; } } return $result; }
/** * Initialize form * * @return $this */ public function initForm() { $this->_initObjects(); /** @var \Magento\Framework\Data\Form $form */ $form = $this->_formFactory->create(); /** @var $section \Magento\Backend\Model\Config\Structure\Element\Section */ $section = $this->_configStructure->getElement($this->getSectionCode()); if ($section && $section->isVisible($this->getWebsiteCode(), $this->getStoreCode())) { foreach ($section->getChildren() as $group) { $this->_initGroup($group, $section, $form); } } $this->setForm($form); return $this; }
/** * Process group data * * @param string $groupId * @param array $groupData * @param array $groups * @param string $sectionPath * @param array &$extraOldGroups * @param array &$oldConfig * @param \Magento\Framework\DB\Transaction $saveTransaction * @param \Magento\Framework\DB\Transaction $deleteTransaction * @return void */ protected function _processGroup($groupId, array $groupData, array $groups, $sectionPath, array &$extraOldGroups, array &$oldConfig, \Magento\Framework\DB\Transaction $saveTransaction, \Magento\Framework\DB\Transaction $deleteTransaction) { $groupPath = $sectionPath . '/' . $groupId; $scope = $this->getScope(); $scopeId = $this->getScopeId(); $scopeCode = $this->getScopeCode(); /** * * Map field names if they were cloned */ /** @var $group \Magento\Backend\Model\Config\Structure\Element\Group */ $group = $this->_configStructure->getElement($groupPath); // set value for group field entry by fieldname // use extra memory $fieldsetData = array(); if (isset($groupData['fields'])) { if ($group->shouldCloneFields()) { $cloneModel = $group->getCloneModel(); $mappedFields = array(); /** @var $field \Magento\Backend\Model\Config\Structure\Element\Field */ foreach ($group->getChildren() as $field) { foreach ($cloneModel->getPrefixes() as $prefix) { $mappedFields[$prefix['field'] . $field->getId()] = $field->getId(); } } } foreach ($groupData['fields'] as $fieldId => $fieldData) { $fieldsetData[$fieldId] = is_array($fieldData) && isset($fieldData['value']) ? $fieldData['value'] : null; } foreach ($groupData['fields'] as $fieldId => $fieldData) { $originalFieldId = $fieldId; if ($group->shouldCloneFields() && isset($mappedFields[$fieldId])) { $originalFieldId = $mappedFields[$fieldId]; } /** @var $field \Magento\Backend\Model\Config\Structure\Element\Field */ $field = $this->_configStructure->getElement($groupPath . '/' . $originalFieldId); /** @var \Magento\Framework\App\Config\ValueInterface $backendModel */ $backendModel = $field->hasBackendModel() ? $field->getBackendModel() : $this->_configValueFactory->create(); $data = array('field' => $fieldId, 'groups' => $groups, 'group_id' => $group->getId(), 'scope' => $scope, 'scope_id' => $scopeId, 'scope_code' => $scopeCode, 'field_config' => $field->getData(), 'fieldset_data' => $fieldsetData); $backendModel->addData($data); $this->_checkSingleStoreMode($field, $backendModel); if (false == isset($fieldData['value'])) { $fieldData['value'] = null; } $path = $field->getGroupPath() . '/' . $fieldId; /** * Look for custom defined field path */ if ($field && $field->getConfigPath()) { $configPath = $field->getConfigPath(); if (!empty($configPath) && strrpos($configPath, '/') > 0) { // Extend old data with specified section group $configGroupPath = substr($configPath, 0, strrpos($configPath, '/')); if (!isset($extraOldGroups[$configGroupPath])) { $oldConfig = $this->extendConfig($configGroupPath, true, $oldConfig); $extraOldGroups[$configGroupPath] = true; } $path = $configPath; } } $inherit = !empty($fieldData['inherit']); $backendModel->setPath($path)->setValue($fieldData['value']); if (isset($oldConfig[$path])) { $backendModel->setConfigId($oldConfig[$path]['config_id']); /** * Delete config data if inherit */ if (!$inherit) { $saveTransaction->addObject($backendModel); } else { $deleteTransaction->addObject($backendModel); } } elseif (!$inherit) { $backendModel->unsConfigId(); $saveTransaction->addObject($backendModel); } } } if (isset($groupData['groups'])) { foreach ($groupData['groups'] as $subGroupId => $subGroupData) { $this->_processGroup($subGroupId, $subGroupData, $groups, $groupPath, $extraOldGroups, $oldConfig, $saveTransaction, $deleteTransaction); } } }
/** * Check is allow modify system configuration * * @return bool */ protected function _isAllowed() { $sectionId = $this->_request->getParam('section'); return $this->_authorization->isAllowed('Magento_Adminhtml::config') || $this->_configStructure->getElement($sectionId)->isAllowed(); }