/** * Cron settings after save * * @return void * @throws \Magento\Framework\Exception\LocalizedException */ public function afterSave() { $enabled = $this->getData('groups/log/fields/enabled/value'); $time = $this->getData('groups/log/fields/time/value'); $frequency = $this->getData('groups/log/fields/frequency/value'); $frequencyWeekly = \Magento\Cron\Model\Config\Source\Frequency::CRON_WEEKLY; $frequencyMonthly = \Magento\Cron\Model\Config\Source\Frequency::CRON_MONTHLY; if ($enabled) { $cronExprArray = [intval($time[1]), intval($time[0]), $frequency == $frequencyMonthly ? '1' : '*', '*', $frequency == $frequencyWeekly ? '1' : '*']; $cronExprString = join(' ', $cronExprArray); } else { $cronExprString = ''; } try { /** @var $configValue \Magento\Framework\App\Config\ValueInterface */ $configValue = $this->_configValueFactory->create(); $configValue->load(self::CRON_STRING_PATH, 'path'); $configValue->setValue($cronExprString)->setPath(self::CRON_STRING_PATH)->save(); /** @var $configValue \Magento\Framework\App\Config\ValueInterface */ $configValue = $this->_configValueFactory->create(); $configValue->load(self::CRON_MODEL_PATH, 'path'); $configValue->setValue($this->_runModelPath)->setPath(self::CRON_MODEL_PATH)->save(); } catch (\Exception $e) { throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t save the Cron expression.')); } }
/** * Get url for config settings where base url option can be changed * * @return string */ protected function _getConfigUrl() { $output = ''; $defaultUnsecure = $this->_config->getValue(\Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_URL, 'default'); $defaultSecure = $this->_config->getValue(\Magento\Store\Model\Store::XML_PATH_SECURE_BASE_URL, 'default'); if ($defaultSecure == \Magento\Store\Model\Store::BASE_URL_PLACEHOLDER || $defaultUnsecure == \Magento\Store\Model\Store::BASE_URL_PLACEHOLDER) { $output = $this->_urlBuilder->getUrl('adminhtml/system_config/edit', array('section' => 'web')); } else { /** @var $dataCollection \Magento\Core\Model\Resource\Config\Data\Collection */ $dataCollection = $this->_configValueFactory->create()->getCollection(); $dataCollection->addValueFilter(\Magento\Store\Model\Store::BASE_URL_PLACEHOLDER); /** @var $data \Magento\Framework\App\Config\ValueInterface */ foreach ($dataCollection as $data) { if ($data->getScope() == 'stores') { $code = $this->_storeManager->getStore($data->getScopeId())->getCode(); $output = $this->_urlBuilder->getUrl('adminhtml/system_config/edit', array('section' => 'web', 'store' => $code)); break; } elseif ($data->getScope() == 'websites') { $code = $this->_storeManager->getWebsite($data->getScopeId())->getCode(); $output = $this->_urlBuilder->getUrl('adminhtml/system_config/edit', array('section' => 'web', 'website' => $code)); break; } } } return $output; }
/** * Cron settings after save * * @return $this */ public function afterSave() { $cronExprString = ''; $time = explode(',', $this->_configValueFactory->create()->load('paypal/fetch_reports/time', 'path')->getValue()); if ($this->_configValueFactory->create()->load('paypal/fetch_reports/active', 'path')->getValue()) { $interval = $this->_configValueFactory->create()->load(self::CRON_MODEL_PATH_INTERVAL, 'path')->getValue(); $cronExprString = "{$time[1]} {$time[0]} */{$interval} * *"; } $this->_configValueFactory->create()->load(self::CRON_STRING_PATH, 'path')->setValue($cronExprString)->setPath(self::CRON_STRING_PATH)->save(); return parent::afterSave(); }
/** * @return void * @throws \Exception */ public function afterSave() { $time = $this->getData('groups/productalert_cron/fields/time/value'); $frequency = $this->getData('groups/productalert_cron/fields/frequency/value'); $cronExprArray = [intval($time[1]), intval($time[0]), $frequency == \Magento\Cron\Model\Config\Source\Frequency::CRON_MONTHLY ? '1' : '*', '*', $frequency == \Magento\Cron\Model\Config\Source\Frequency::CRON_WEEKLY ? '1' : '*']; $cronExprString = join(' ', $cronExprArray); try { $this->_configValueFactory->create()->load(self::CRON_STRING_PATH, 'path')->setValue($cronExprString)->setPath(self::CRON_STRING_PATH)->save(); $this->_configValueFactory->create()->load(self::CRON_MODEL_PATH, 'path')->setValue($this->_runModelPath)->setPath(self::CRON_MODEL_PATH)->save(); } catch (\Exception $e) { throw new \Exception(__('We can\'t save the cron expression.')); } }
/** * Get configuration value by path * * @param string $path * @param string $scope * @param string $scopeId * @param bool $full * @return array */ public function getConfigByPath($path, $scope, $scopeId, $full = true) { $configDataCollection = $this->_configValueFactory->create(); $configDataCollection = $configDataCollection->getCollection()->addScopeFilter($scope, $scopeId, $path); $config = []; $configDataCollection->load(); foreach ($configDataCollection->getItems() as $data) { if ($full) { $config[$data->getPath()] = ['path' => $data->getPath(), 'value' => $data->getValue(), 'config_id' => $data->getConfigId()]; } else { $config[$data->getPath()] = $data->getValue(); } } return $config; }
/** * @return void * @throws \Exception */ protected function _afterSave() { $time = $this->getData('groups/import/fields/time/value'); $frequency = $this->getData('groups/import/fields/frequency/value'); $frequencyWeekly = \Magento\Cron\Model\Config\Source\Frequency::CRON_WEEKLY; $frequencyMonthly = \Magento\Cron\Model\Config\Source\Frequency::CRON_MONTHLY; $cronExprArray = array(intval($time[1]), intval($time[0]), $frequency == $frequencyMonthly ? '1' : '*', '*', $frequency == $frequencyWeekly ? '1' : '*'); $cronExprString = join(' ', $cronExprArray); try { /** @var $configValue \Magento\Framework\App\Config\ValueInterface */ $configValue = $this->_configValueFactory->create(); $configValue->load(self::CRON_STRING_PATH, 'path'); $configValue->setValue($cronExprString)->setPath(self::CRON_STRING_PATH)->save(); } catch (\Exception $e) { throw new \Exception(__('We can\'t save the Cron expression.')); } }
/** * Cron settings after save * * @return void * @throws \Magento\Framework\Exception\LocalizedException */ public function afterSave() { $enabled = $this->getData(self::XML_PATH_BACKUP_ENABLED); $time = $this->getData(self::XML_PATH_BACKUP_TIME); $frequency = $this->getData(self::XML_PATH_BACKUP_FREQUENCY); $frequencyWeekly = \Magento\Cron\Model\Config\Source\Frequency::CRON_WEEKLY; $frequencyMonthly = \Magento\Cron\Model\Config\Source\Frequency::CRON_MONTHLY; if ($enabled) { $cronExprArray = [intval($time[1]), intval($time[0]), $frequency == $frequencyMonthly ? '1' : '*', '*', $frequency == $frequencyWeekly ? '1' : '*']; $cronExprString = join(' ', $cronExprArray); } else { $cronExprString = ''; } try { $this->_configValueFactory->create()->load(self::CRON_STRING_PATH, 'path')->setValue($cronExprString)->setPath(self::CRON_STRING_PATH)->save(); $this->_configValueFactory->create()->load(self::CRON_MODEL_PATH, 'path')->setValue($this->_runModelPath)->setPath(self::CRON_MODEL_PATH)->save(); } catch (\Exception $e) { throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t save the Cron expression.')); } }
/** * Cron settings after save * * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function afterSave() { $active = $this->getData(self::XML_PATH_BAIDUPING_ACTIVE); $frequency = (int) $this->getData(self::XML_PATH_BAIDUPING_FREQUENCY); if ($frequency >= 60 || $frequency <= 0) { $frequency = 15; } if ($active) { $cronExprArray = ['*/' . $frequency, '*', '*', '*', '*']; $cronExprString = join(' ', $cronExprArray); } else { $cronExprString = ''; } try { $this->_configValueFactory->create()->load(self::CRON_STRING_PATH, 'path')->setValue($cronExprString)->setPath(self::CRON_STRING_PATH)->save(); } catch (\Exception $e) { throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t save the Cron expression.')); } return parent::afterSave(); }
/** * 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); } } }
/** * @param ObjectManagerInterface $objectManager * @param MetadataProvider $metadataProvider * @param CollectionFactory $collectionFactory */ public function __construct(ObjectManagerInterface $objectManager, MetadataProvider $metadataProvider, CollectionFactory $collectionFactory) { $this->metadataProvider = $metadataProvider; $this->collectionFactory = $collectionFactory; parent::__construct($objectManager); }