From f3c0f9d890facba2f616bb3cabe6e8aefe9a665c Mon Sep 17 00:00:00 2001 From: Sasha Rybkin Date: Wed, 20 Nov 2024 15:59:13 +0300 Subject: [PATCH] init --- Settings.php | 89 +++++++++++++++++++++++++++++++ composer.json | 21 ++++++++ schema/SettingsMutationType.php | 34 ++++++++++++ schema/SettingsPaginationType.php | 48 +++++++++++++++++ schema/SettingsType.php | 34 ++++++++++++ schema/Types.php | 26 +++++++++ 6 files changed, 252 insertions(+) create mode 100644 Settings.php create mode 100644 composer.json create mode 100644 schema/SettingsMutationType.php create mode 100644 schema/SettingsPaginationType.php create mode 100644 schema/SettingsType.php create mode 100644 schema/Types.php diff --git a/Settings.php b/Settings.php new file mode 100644 index 0000000..2737851 --- /dev/null +++ b/Settings.php @@ -0,0 +1,89 @@ + 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(); + } +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..6452ab7 --- /dev/null +++ b/composer.json @@ -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\\": "" + } + } +} \ No newline at end of file diff --git a/schema/SettingsMutationType.php b/schema/SettingsMutationType.php new file mode 100644 index 0000000..0ead209 --- /dev/null +++ b/schema/SettingsMutationType.php @@ -0,0 +1,34 @@ + 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); + } +} \ No newline at end of file diff --git a/schema/SettingsPaginationType.php b/schema/SettingsPaginationType.php new file mode 100644 index 0000000..b528c62 --- /dev/null +++ b/schema/SettingsPaginationType.php @@ -0,0 +1,48 @@ + 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); + } + +} diff --git a/schema/SettingsType.php b/schema/SettingsType.php new file mode 100644 index 0000000..46718f6 --- /dev/null +++ b/schema/SettingsType.php @@ -0,0 +1,34 @@ + function() { + return [ + 'id' => [ + 'type' => Type::int(), + 'description' => 'Id', + ], + 'code' => [ + 'type' => Type::string(), + 'description' => 'code', + ], + 'value' => [ + 'type' => Type::string(), + 'description' => 'value', + ], + ]; + } + ]; + + parent::__construct($config); + } + +} \ No newline at end of file diff --git a/schema/Types.php b/schema/Types.php new file mode 100644 index 0000000..47f0195 --- /dev/null +++ b/schema/Types.php @@ -0,0 +1,26 @@ +