Skip to content

Commit

Permalink
Merge pull request #1 from phemellc/master
Browse files Browse the repository at this point in the history
Merging delta from phemelic
  • Loading branch information
cinghie authored Oct 22, 2018
2 parents 6489031 + 81ebf49 commit dc38952
Show file tree
Hide file tree
Showing 26 changed files with 926 additions and 21 deletions.
10 changes: 10 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
imports:
- php

tools:
external_code_coverage:
timeout: 1800 # Timeout in seconds.
# disable copy paste detector and similarity analyzer as they have no real value
# and a huge bunch of false-positives
php_sim: false
php_cpd: false
1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
preset: psr2
34 changes: 34 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
language: php

matrix:
fast_finish: true

php:
- 5.6
#- 7.1

# cache vendor dirs
cache:
directories:
- $HOME/.composer/cache
- vendor

services:
- mysql

install:
- travis_retry composer self-update
- composer config -g github-oauth.github.com dcb6b0049723eb6f56039b2d6389ac76eb29e352
- travis_retry composer install --prefer-dist --no-interaction
- travis_retry mysql -e 'CREATE DATABASE test;'

before_script:
- travis_retry composer self-update
- travis_retry composer install --no-interaction --prefer-source --dev

script:
- ./vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
2 changes: 1 addition & 1 deletion Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Module extends \yii\base\Module

/**
*
* @var string source language for translation
* @var string source language for translation
*/
public $sourceLanguage = 'en-US';

Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[![StyleCI](https://styleci.io/repos/99540308/shield?branch=master)](https://styleci.io/repos/99540308)
[![Build Status](https://travis-ci.org/monster-hunter/yii2-settings.svg?branch=master)](https://travis-ci.org/monster-hunter/yii2-settings)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/monster-hunter/yii2-settings/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/monster-hunter/yii2-settings/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/monster-hunter/yii2-settings/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/monster-hunter/yii2-settings/?branch=master)


Yii2 Settings
=============
Yii2 Database settings
Expand Down Expand Up @@ -87,7 +93,7 @@ SettingsAction
To use a custom settings form, you can use the included `SettingsAction`.

1. Create a model class with your validation rules.
2. Create an associated view with an `ActiveForm` contain all the settings you need.
2. Create an associated view with an `ActiveForm` containing all the settings you need.
3. Add `pheme\settings\SettingsAction` to the controller's actions.

The settings will be stored in section taken from the form name, with the key being the field name.
Expand Down
67 changes: 57 additions & 10 deletions components/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use yii\base\Component;
use yii\caching\Cache;
use Yii;
use yii\helpers\Json;

/**
* @author Aris Karageorgos <[email protected]>
Expand Down Expand Up @@ -55,6 +56,12 @@ class Settings extends Component
*/
public $cacheKey = 'pheme/settings';

/**
* @var bool Whether to convert objects stored as JSON into an PHP array
* @since 0.6
*/
public $autoDecodeJson = false;

/**
* Holds a cached copy of the data for the current request
*
Expand Down Expand Up @@ -90,8 +97,8 @@ public function init()
* are equivalent
*
* @param $key
* @param null $section
* @param null $default
* @param string|null $section
* @param string|null $default
* @return mixed
*/
public function get($key, $section = null, $default = null)
Expand All @@ -109,11 +116,36 @@ public function get($key, $section = null, $default = null)
$data = $this->getRawConfig();

if (isset($data[$section][$key][0])) {
settype($data[$section][$key][0], $data[$section][$key][1]);
if (in_array($data[$section][$key][1], ['object', 'boolean', 'bool', 'integer', 'int', 'float', 'string', 'array'])) {
if ($this->autoDecodeJson && $data[$section][$key][1] === 'object') {
$value = Json::decode($data[$section][$key][0]);
} else {
$value = $data[$section][$key][0];
settype($value, $data[$section][$key][1]);
}
}
} else {
$value = $default;
}
return $value;
}

/**
* Checks to see if a setting exists.
* If $searchDisabled is set to true, calling this function will result in an additional query.
* @param $key
* @param string|null $section
* @param boolean $searchDisabled
* @return boolean
*/
public function has($key, $section = null, $searchDisabled = false)
{
if ($searchDisabled) {
$setting = $this->model->findSetting($key, $section);
} else {
$data[$section][$key][0] = $default;
$setting = $this->get($key, $section);
}
return $data[$section][$key][0];
return is_null($setting) ? false : true;
}

/**
Expand All @@ -132,13 +164,30 @@ public function set($key, $value, $section = null, $type = null)
}

if ($this->model->setSetting($section, $key, $value, $type)) {
if ($this->clearCache()) {
return true;
}
return true;
}
return false;
}

/**
* Returns the specified key or sets the key with the supplied (default) value
*
* @param $key
* @param $value
* @param null $section
* @param null $type
*
* @return bool|mixed
*/
public function getOrSet($key, $value, $section = null, $type = null)
{
if ($this->has($key, $section, true)) {
return $this->get($key, $section);
} else {
return $this->set($key, $value, $section, $type);
}
}

/**
* Deletes a setting
*
Expand Down Expand Up @@ -227,9 +276,7 @@ public function getRawConfig()
{
if ($this->_data === null) {
if ($this->cache instanceof Cache) {

$data = $this->cache->get($this->cacheKey);

if ($data === false) {
$data = $this->model->getSettings();
$this->cache->set($this->cacheKey, $data);
Expand Down
11 changes: 10 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@
"yiisoft/yii2": ">=2.0.6",
"pheme/yii2-toggle-column": "*"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"autoload": {
"psr-4": {
"pheme\\settings\\": ""
}
}
},
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
]
}
51 changes: 51 additions & 0 deletions messages/es/settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Message translations.
*
* This file is automatically generated by 'yii message' command.
* It contains the localizable messages extracted from source code.
* You may modify this file by translating the extracted messages.
*
* Each array element represents the translation (value) of a message (key).
* If the value is empty, the message is considered as not translated.
* Messages that no longer need translation will have their translations
* enclosed between a pair of '@@' marks.
*
* Message string can be used with plural forms format. Check i18n section
* of the guide for details.
*
* NOTE: this file must be saved in UTF-8 encoding.
*/
return [
'Active' => 'Activo',
'Are you sure you want to delete this item?' => '¿Estás seguro que quieres eliminar esa configuración?',
'Change at your own risk' => 'Cámbialo bajo tu propio riesgo',
'Create' => 'Crear',
'Create {modelClass}' => 'Crear {modelClass}',
'Created' => 'Creado',
'Delete' => 'Eliminar',
'ID' => 'ID',
'Key' => 'Clave',
'Modified' => 'Modificado',
'Please select correct type' => 'Por favor selecciona el tipo correcto',
'Reset' => 'Restablecer',
'Search' => 'Buscar',
'Section' => 'Sección',
'Setting' => 'Configuración',
'Settings' => 'Configuraciones',
'Successfully saved settings on {section}' => 'Configuración guardada correctamente en la sección {section}',
'Type' => 'Tipo',
'Update' => 'Actualizar',
'Update {modelClass}: ' => 'Actualizar {modelClass}: ',
'Value' => 'Valor',
'{attribute} "{value}" already exists for this section.' => '{attribute} "{value}" ya existe en esta sección.',
'"{attribute}" must be a valid JSON object' => '"{attribute}" debe ser un objeto JSON válido',
'string' => 'Cadena de texto',
'integer' => 'Entero',
'boolean' => 'Booleano',
'float' => 'Decimal',
'email' => 'Correo electrónico',
'ip' => 'Dirección IP',
'url' => 'URL',
'object' => 'Objeto',
];
2 changes: 1 addition & 1 deletion messages/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'sourcePath' => __DIR__ . DIRECTORY_SEPARATOR . '..',
// array, required, list of language codes that the extracted messages
// should be translated to. For example, ['zh-CN', 'de'].
'languages' => ['de', 'fa-IR', 'gr', 'ru', 'uk', 'vn'],
'languages' => ['de', 'fa-IR', 'gr', 'it', 'pl','ru', 'uk', 'vn', 'th', 'zh-CN', 'zh-TW'],
// string, the name of the function for translating messages.
// Defaults to 'Yii::t'. This is used as a mark to find the messages to be
// translated. You may use a string for single function name or an array for
Expand Down
1 change: 1 addition & 0 deletions messages/ru/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@
'ip' => 'IP',
'url' => 'URL',
'object' => 'JSON',
'Successfully saved settings on {section}' => 'Настройки удачно сохранены для {section}'
];
50 changes: 50 additions & 0 deletions messages/th/settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Message translations.
*
* This file is automatically generated by 'yii message' command.
* It contains the localizable messages extracted from source code.
* You may modify this file by translating the extracted messages.
*
* Each array element represents the translation (value) of a message (key).
* If the value is empty, the message is considered as not translated.
* Messages that no longer need translation will have their translations
* enclosed between a pair of '@@' marks.
*
* Message string can be used with plural forms format. Check i18n section
* of the guide for details.
*
* NOTE: this file must be saved in UTF-8 encoding.
*/
return [
'Active' => 'พร้อมทำงาน',
'Are you sure you want to delete this item?' => 'คุณแน่ใจใช่ไหมว่าต้องการที่จะลบข้อมูลนี้?',
'Change at your own risk' => 'เปลี่ยนข้อมูลมีความเสี่ยง',
'Create' => 'สร้าง',
'Create {modelClass}' => 'สร้าง {modelClass}',
'Created' => 'สร้าง',
'Delete' => 'ลบ',
'ID' => 'ไอดี',
'Key' => 'คีย์',
'Modified' => 'แก้ไข',
'Reset' => 'คืนค่า',
'Search' => 'ค้นหา',
'Section' => 'ส่วน',
'Setting' => 'การตั้งค่า',
'Settings' => 'การตั้งค่า',
'Type' => 'ชนิด',
'Update' => 'ปรับปรุง',
'Update {modelClass}: ' => 'ปรับปรุง {modelClass}: ',
'Value' => 'ค่า',
'{attribute} "{value}" already exists for this section.' => '{attribute} "{value}" มีแล้วในะระบบ',
'"{attribute}" must be a valid JSON object' => '"{attribute}" ต้องอยู่ในรูปแบบ JSON',
'Please select correct type' => 'เลือกให้ตรงชนิด',
'string' => 'ตัวอักษร',
'integer' => 'ตัวเลข',
'boolean' => '0 หรือ 1',
'float' => 'ค่าทศนิยม',
'email' => 'E-mail',
'ip' => 'IP',
'url' => 'URL',
'object' => 'JSON',
];
6 changes: 3 additions & 3 deletions messages/uk/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
'Update' => 'Редагувати',
'Update {modelClass}: ' => 'Редагування {modelClass}: ',
'Value' => 'Значення',
'{attribute} "{value}" already exists for this section.' => '',
'"{attribute}" must be a valid JSON object' => '"{attribute}" повинен містити корректний JSON объект',
'{attribute} "{value}" already exists for this section.' => '{attribute} "{value}" уже існує в даній секції.',
'"{attribute}" must be a valid JSON object' => '"{attribute}" повинен містити корректний JSON об’єкт',
'Please select correct type' => 'Оберіть корректний тип',
'string' => 'Строка',
'string' => 'Рядок',
'integer' => 'Ціле число',
'boolean' => '0 або 1',
'float' => 'Число',
Expand Down
52 changes: 52 additions & 0 deletions messages/yii2mod.settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Message translations.
*
* This file is automatically generated by 'yii message/extract' command.
* It contains the localizable messages extracted from source code.
* You may modify this file by translating the extracted messages.
*
* Each array element represents the translation (value) of a message (key).
* If the value is empty, the message is considered as not translated.
* Messages that no longer need translation will have their translations
* enclosed between a pair of '@@' marks.
*
* Message string can be used with plural forms format. Check i18n section
* of the guide for details.
*
* NOTE: this file must be saved in UTF-8 encoding.
*/
return [
'ID' => 'ID',
'Type' => 'Typ',
'Section' => 'Grupa',
'Key' => 'Klucz',
'Value' => 'Wartość',
'Status' => 'Status',
'Description' => 'Opis',
'Created Date' => 'Data utworzenia',
'Updated Date' => 'Data aktualizacji',
'Settings' => 'Konfiguracja',
'Create Setting' => 'Dodaj konfigurację',
'Select Type' => 'Wybierz typ',
'Select Section' => 'Wybierz grupę',
'Select Status' => 'Wybierz status',
'Actions' => 'Akcje',
'Active' => 'Aktywne',
'Inactive' => 'Nieaktywne',
'Update Setting: {0} -> {1}' => 'Edytuj konfigurację: {0} -> {1}',
'Update Setting' => 'Edytuj konfigurację',
'Update' => 'Aktualizuj',
'Create' => 'Dodaj',
'Delete' => 'Usuń',
'Go Back' => 'Wstecz',
'String' => 'Tekst',
'Integer' => 'Liczba całkowita',
'Boolean' => 'Prawda/Fałsz',
'Float' => 'Liczba zmiennoprzecinkowa',
'Null' => 'Null',
'Setting has been created.' => 'Konfiguracja została dodana.',
'Setting has been deleted.' => 'Konfiguracja została usunięta.',
'Setting has been updated.' => 'Konfiguracja została zaktualizowana.',
'The requested page does not exist.' => 'Szukana strona nie istnieje',
];
Loading

0 comments on commit dc38952

Please sign in to comment.