You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					72 lines
				
				2.1 KiB
			
		
		
			
		
	
	
					72 lines
				
				2.1 KiB
			| 
											7 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * Created by Error202
 | ||
|  |  * Date: 13.09.2018
 | ||
|  |  */
 | ||
|  | 
 | ||
|  | namespace console\controllers;
 | ||
|  | 
 | ||
|  | use core\entities\Settings;
 | ||
|  | use core\forms\SettingsForm;
 | ||
|  | use core\services\SettingsService;
 | ||
|  | use yii\console\Controller;
 | ||
|  | 
 | ||
|  | class SettingsController extends Controller
 | ||
|  | {
 | ||
|  |     private $_settings_service;
 | ||
|  | 
 | ||
|  |     public function __construct(string $id, $module, SettingsService $settings_service, array $config = [])
 | ||
|  |     {
 | ||
|  |         parent::__construct($id, $module, $config);
 | ||
|  |         $this->_settings_service = $settings_service;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     public function actionSet($language, $section, $key, $value)
 | ||
|  |     {
 | ||
|  |         $setting = $this->findModel($section, $key);
 | ||
|  |         if ($setting) {
 | ||
|  |             $translate = $setting->findTranslation($language);
 | ||
|  | 
 | ||
|  |             if (!$translate) {
 | ||
|  |                 $translate = new $setting->virtualClassName();
 | ||
|  |                 $translate->section = $section;
 | ||
|  |                 $translate->key = $key;
 | ||
|  |                 $translate->language = $language;
 | ||
|  |             }
 | ||
|  | 
 | ||
|  |             $translate->value = $value;
 | ||
|  |             $translate->save();
 | ||
|  |         } else {
 | ||
|  |             $settingForm = new SettingsForm();
 | ||
|  |             $settingForm->type = 'string';
 | ||
|  |             $settingForm->section = $section;
 | ||
|  |             $settingForm->key = $key;
 | ||
|  |             $settingForm->active = 1;
 | ||
|  |             $setting = $this->_settings_service->create($settingForm);
 | ||
|  | 
 | ||
|  |             $translate = $setting->findTranslation($language);
 | ||
|  | 
 | ||
|  |             if (!$translate) {
 | ||
|  |                 $translate = new $setting->virtualClassName();
 | ||
|  |                 $translate->section = $section;
 | ||
|  |                 $translate->key = $key;
 | ||
|  |                 $translate->language = $language;
 | ||
|  |             }
 | ||
|  | 
 | ||
|  |             $translate->value = $value;
 | ||
|  |             $translate->save();
 | ||
|  |         }
 | ||
|  |         $this->stdout('Settings updated!' . PHP_EOL);
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     protected function findModel($section, $key)
 | ||
|  |     {
 | ||
|  |         if (($model = Settings::find()->andWhere(['section' => $section])->andWhere(['key' => $key])->one()) !== null) {
 | ||
|  |             return $model;
 | ||
|  |         } else {
 | ||
|  |             //throw new NotFoundException('The requested setting does not exist.');
 | ||
|  |             return null;
 | ||
|  |         }
 | ||
|  |     }
 | ||
|  | }
 |