init
This commit is contained in:
		
							
								
								
									
										89
									
								
								Settings.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										89
									
								
								Settings.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,89 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace dominion\settings;
 | 
			
		||||
 | 
			
		||||
use Yii;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * This is the model class for table "settings".
 | 
			
		||||
 *
 | 
			
		||||
 * @property int $id
 | 
			
		||||
 * @property string $code
 | 
			
		||||
 * @property string $value
 | 
			
		||||
 */
 | 
			
		||||
class Settings extends \yii\db\ActiveRecord
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * {@inheritdoc}
 | 
			
		||||
     */
 | 
			
		||||
    public static function tableName()
 | 
			
		||||
    {
 | 
			
		||||
        return 'settings';
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * {@inheritdoc}
 | 
			
		||||
     */
 | 
			
		||||
    public function rules()
 | 
			
		||||
    {
 | 
			
		||||
        return [
 | 
			
		||||
            [['code'], 'required'],
 | 
			
		||||
            [['code'], 'string', 'max' => 500],
 | 
			
		||||
            [['value'], 'string', 'max' => 5000],
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * {@inheritdoc}
 | 
			
		||||
     */
 | 
			
		||||
    public function attributeLabels()
 | 
			
		||||
    {
 | 
			
		||||
        return [
 | 
			
		||||
            'id' => 'ID',
 | 
			
		||||
            'code' => 'Code',
 | 
			
		||||
            'value' => 'Value',
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static function getData($params, $root)
 | 
			
		||||
    {
 | 
			
		||||
        $query = self::find();
 | 
			
		||||
        $root->andWhere($query, $params, ['id', 'code']);
 | 
			
		||||
        $root->andWhereLike($query, $params, ['value']);
 | 
			
		||||
 | 
			
		||||
        $root->setMetaByQuery($query);
 | 
			
		||||
        $root->orderBy($query, $params, ['id', 'code', 'value']);
 | 
			
		||||
        return $root->getItemsByPage($query);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Получить настройки
 | 
			
		||||
     *
 | 
			
		||||
     * @param string $code символьный код уникальный
 | 
			
		||||
     * @param string $defaulValue значение по умолчанию
 | 
			
		||||
     * @return string
 | 
			
		||||
     */
 | 
			
		||||
    public static function getValue($code, $defaulValue = false)
 | 
			
		||||
    {
 | 
			
		||||
        $model = Settings::find()->andWhere(['code' => $code])->one();
 | 
			
		||||
        return empty($model) ? $defaulValue : $model->value  ;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Сохранить настройк
 | 
			
		||||
     *
 | 
			
		||||
     * @param type $code
 | 
			
		||||
     * @param type $value
 | 
			
		||||
     */
 | 
			
		||||
    public static function setValue($code, $value)
 | 
			
		||||
    {
 | 
			
		||||
        $model = Settings::find()->andWhere(['code' => $code])->one();
 | 
			
		||||
        if(empty($model))
 | 
			
		||||
        {
 | 
			
		||||
            $model = new Settings;
 | 
			
		||||
            $model->code = $code;
 | 
			
		||||
        }
 | 
			
		||||
        $model->value = (string)$value;
 | 
			
		||||
        return $model->save();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										21
									
								
								composer.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								composer.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
{
 | 
			
		||||
    "name": "dominion/settings",
 | 
			
		||||
    "description": "Функционал для работы с settings",
 | 
			
		||||
    "type": "yii2-extension",
 | 
			
		||||
    "keywords": ["yii2","extension"],
 | 
			
		||||
    "license": "MIT",
 | 
			
		||||
    "authors": [
 | 
			
		||||
        {
 | 
			
		||||
            "name": "Rybkin Sasha",
 | 
			
		||||
            "email": "ribkin@dominion.ru"
 | 
			
		||||
        }
 | 
			
		||||
    ],
 | 
			
		||||
    "require": {
 | 
			
		||||
        "yiisoft/yii2": "~2.0.0"
 | 
			
		||||
    },
 | 
			
		||||
    "autoload": {
 | 
			
		||||
        "psr-4": {
 | 
			
		||||
            "dominion\\settings\\": ""
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										34
									
								
								schema/SettingsMutationType.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								schema/SettingsMutationType.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,34 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
namespace dominion\settings\schema;
 | 
			
		||||
 | 
			
		||||
use GraphQL\Type\Definition\ObjectType;
 | 
			
		||||
use GraphQL\Type\Definition\Type;
 | 
			
		||||
use dominion\settings\Settings;
 | 
			
		||||
 | 
			
		||||
class SettingsMutationType extends ObjectType
 | 
			
		||||
{
 | 
			
		||||
    public function __construct()
 | 
			
		||||
    {
 | 
			
		||||
        $config = [
 | 
			
		||||
            'fields' => function() {
 | 
			
		||||
                return [
 | 
			
		||||
                    'save' => [
 | 
			
		||||
                        'type' => Type::int(),
 | 
			
		||||
                        'description' => 'Сохранение наклейки',
 | 
			
		||||
                        'args' => [
 | 
			
		||||
                            'code' => Type::nonNull(Type::string()),
 | 
			
		||||
                            'value' => Type::nonNull(Type::string()),
 | 
			
		||||
                        ],
 | 
			
		||||
                        'resolve' => function($root, $args) {
 | 
			
		||||
                            return Settings::setValue($args['code'], $args['value']);
 | 
			
		||||
                        },
 | 
			
		||||
                    ],
 | 
			
		||||
                ];
 | 
			
		||||
            }
 | 
			
		||||
        ];
 | 
			
		||||
 | 
			
		||||
        parent::__construct($config);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										48
									
								
								schema/SettingsPaginationType.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								schema/SettingsPaginationType.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,48 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace dominion\settings\schema;
 | 
			
		||||
 | 
			
		||||
use GraphQL\Type\Definition\Type;
 | 
			
		||||
use dominion\api\GraphQLSchemaPagination;
 | 
			
		||||
use dominion\settings\Settings;
 | 
			
		||||
use dominion\api\GraphQLPagination;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class SettingsPaginationType extends GraphQLSchemaPagination
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    public function __construct()
 | 
			
		||||
    {
 | 
			
		||||
        $config = [
 | 
			
		||||
            'fields' => function(){
 | 
			
		||||
                $output = $this->getCustomFields();
 | 
			
		||||
                $output['data'] = [
 | 
			
		||||
                    'type' => Type::listOf(Types::settings()),
 | 
			
		||||
                    'description' => 'Настройки',
 | 
			
		||||
                    'args' => GraphQLPagination::argumentModify([
 | 
			
		||||
                        'id' => [
 | 
			
		||||
                            'type' => Type::int(),
 | 
			
		||||
                            'description' => 'Id',
 | 
			
		||||
                        ],
 | 
			
		||||
                        'code' => [
 | 
			
		||||
                            'type' => Type::string(),
 | 
			
		||||
                            'description' => 'символьный код',
 | 
			
		||||
                            'modify' => true,
 | 
			
		||||
                            'modifyOption' => ['in'],
 | 
			
		||||
                        ],
 | 
			
		||||
                        'sort' => [
 | 
			
		||||
                            'type' => Type::string(),
 | 
			
		||||
                            'description' => 'Сортировка (пример "isNew_asc" или "isVisible_desc")',
 | 
			
		||||
                        ],
 | 
			
		||||
                    ]),
 | 
			
		||||
                    'resolve' => function($root, $args){
 | 
			
		||||
                        return Settings::getData($args, $root);
 | 
			
		||||
                    }
 | 
			
		||||
                ];
 | 
			
		||||
                return $output;
 | 
			
		||||
            }
 | 
			
		||||
        ];
 | 
			
		||||
        parent::__construct($config);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										34
									
								
								schema/SettingsType.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								schema/SettingsType.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,34 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace dominion\settings\schema;
 | 
			
		||||
 | 
			
		||||
use GraphQL\Type\Definition\ObjectType;
 | 
			
		||||
use GraphQL\Type\Definition\Type;
 | 
			
		||||
 | 
			
		||||
class SettingsType extends ObjectType
 | 
			
		||||
{
 | 
			
		||||
    public function __construct()
 | 
			
		||||
    {
 | 
			
		||||
        $config = [
 | 
			
		||||
            'fields' => function() {
 | 
			
		||||
                return [
 | 
			
		||||
                    'id' => [
 | 
			
		||||
                        'type' => Type::int(),
 | 
			
		||||
                        'description' => 'Id',
 | 
			
		||||
                    ],
 | 
			
		||||
                    'code' => [
 | 
			
		||||
                        'type' => Type::string(),
 | 
			
		||||
                        'description' => 'code',
 | 
			
		||||
                    ],
 | 
			
		||||
                    'value' => [
 | 
			
		||||
                        'type' => Type::string(),
 | 
			
		||||
                        'description' => 'value',
 | 
			
		||||
                    ],
 | 
			
		||||
                ];
 | 
			
		||||
            }
 | 
			
		||||
        ];
 | 
			
		||||
 | 
			
		||||
        parent::__construct($config);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										26
									
								
								schema/Types.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								schema/Types.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace dominion\settings\schema;
 | 
			
		||||
 | 
			
		||||
trait Types
 | 
			
		||||
{
 | 
			
		||||
    private static $settings;
 | 
			
		||||
    private static $settingsPagination;
 | 
			
		||||
    //
 | 
			
		||||
    private static $settingsMutation;
 | 
			
		||||
 | 
			
		||||
    public static function settings()
 | 
			
		||||
    {
 | 
			
		||||
        return self::$settings ?: (self::$settings = new SettingsType());
 | 
			
		||||
    }
 | 
			
		||||
    public static function settingsPagination()
 | 
			
		||||
    {
 | 
			
		||||
        return self::$settingsPagination ?: (self::$settingsPagination = new SettingsPaginationType());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //
 | 
			
		||||
    public static function settingsMutation()
 | 
			
		||||
    {
 | 
			
		||||
        return self::$settingsMutation ?: (self::$settingsMutation = new SettingsMutationType());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user