2018-10-05 21:32:15 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Hummingbird Anime List Client
|
|
|
|
*
|
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
|
|
|
*
|
|
|
|
* PHP version 7.1
|
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
|
|
|
* @copyright 2015 - 2018 Timothy J. Warren
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @version 4.1
|
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\Model;
|
|
|
|
|
2018-10-09 18:10:20 -04:00
|
|
|
use const Aviat\AnimeClient\SETTINGS_MAP;
|
|
|
|
|
2018-10-08 15:45:46 -04:00
|
|
|
use function Aviat\AnimeClient\arrayToToml;
|
|
|
|
use function Aviat\Ion\_dir;
|
|
|
|
|
2018-10-05 21:32:15 -04:00
|
|
|
use Aviat\AnimeClient\Types\{Config, UndefinedPropertyException};
|
|
|
|
|
|
|
|
use Aviat\Ion\ConfigInterface;
|
|
|
|
use Aviat\Ion\Di\ContainerAware;
|
|
|
|
use Aviat\Ion\StringWrapper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Model for handling settings control panel
|
|
|
|
*/
|
|
|
|
final class Settings {
|
|
|
|
use ContainerAware;
|
|
|
|
use StringWrapper;
|
|
|
|
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
public function __construct(ConfigInterface $config)
|
|
|
|
{
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
|
|
|
|
2018-11-09 10:38:35 -05:00
|
|
|
public function getSettings(): array
|
2018-10-05 21:32:15 -04:00
|
|
|
{
|
|
|
|
$settings = [
|
|
|
|
'config' => [],
|
|
|
|
];
|
|
|
|
|
2018-10-09 18:10:20 -04:00
|
|
|
foreach(SETTINGS_MAP as $file => $values)
|
2018-10-05 21:32:15 -04:00
|
|
|
{
|
|
|
|
if ($file === 'config')
|
|
|
|
{
|
|
|
|
$keys = array_keys($values);
|
|
|
|
foreach($keys as $key)
|
|
|
|
{
|
|
|
|
$settings['config'][$key] = $this->config->get($key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$settings[$file] = $this->config->get($file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $settings;
|
|
|
|
}
|
|
|
|
|
2018-11-09 10:38:35 -05:00
|
|
|
public function getSettingsForm(): array
|
2018-10-05 21:32:15 -04:00
|
|
|
{
|
|
|
|
$output = [];
|
|
|
|
|
|
|
|
$settings = $this->getSettings();
|
|
|
|
|
|
|
|
foreach($settings as $file => $values)
|
|
|
|
{
|
2018-10-09 18:10:20 -04:00
|
|
|
$values = $values ?? [];
|
|
|
|
|
|
|
|
foreach(SETTINGS_MAP[$file] as $key => $value)
|
2018-10-05 21:32:15 -04:00
|
|
|
{
|
|
|
|
if ($value['type'] === 'subfield')
|
|
|
|
{
|
|
|
|
foreach($value['fields'] as $k => $field)
|
|
|
|
{
|
2018-10-08 16:47:40 -04:00
|
|
|
if (empty($values[$key][$k]))
|
|
|
|
{
|
|
|
|
unset($value['fields'][$k]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-08 16:38:08 -04:00
|
|
|
$value['fields'][$k]['disabled'] = FALSE;
|
2018-10-05 21:32:15 -04:00
|
|
|
$value['fields'][$k]['display'] = TRUE;
|
|
|
|
$value['fields'][$k]['readonly'] = FALSE;
|
2018-10-08 16:38:08 -04:00
|
|
|
$value['fields'][$k]['value'] = $values[$key][$k] ?? '';
|
2018-10-05 21:32:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-08 16:38:08 -04:00
|
|
|
if (array_key_exists($key, $values) && is_scalar($values[$key]))
|
2018-10-05 21:32:15 -04:00
|
|
|
{
|
|
|
|
$value['value'] = $values[$key];
|
|
|
|
}
|
2018-10-08 16:38:08 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$value['value'] = $value['default'] ?? '';
|
|
|
|
}
|
2018-10-05 21:32:15 -04:00
|
|
|
|
|
|
|
foreach (['readonly', 'disabled'] as $flag)
|
|
|
|
{
|
|
|
|
if ( ! array_key_exists($flag, $value))
|
|
|
|
{
|
|
|
|
$value[$flag] = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! array_key_exists('display', $value))
|
|
|
|
{
|
|
|
|
$value['display'] = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
$output[$file][$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2018-11-09 10:38:35 -05:00
|
|
|
public function validateSettings(array $settings): array
|
2018-10-08 15:45:46 -04:00
|
|
|
{
|
|
|
|
$config = (new Config($settings))->toArray();
|
|
|
|
|
|
|
|
$looseConfig = [];
|
|
|
|
$keyedConfig = [];
|
|
|
|
|
|
|
|
// Convert 'boolean' values to true and false
|
|
|
|
// Also order keys so they can be saved properly
|
|
|
|
foreach ($config as $key => $val)
|
|
|
|
{
|
|
|
|
if (is_scalar($val))
|
|
|
|
{
|
|
|
|
if ($val === '1')
|
|
|
|
{
|
|
|
|
$looseConfig[$key] = TRUE;
|
|
|
|
}
|
|
|
|
elseif ($val === '0')
|
|
|
|
{
|
|
|
|
$looseConfig[$key] = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$looseConfig[$key] = $val;
|
|
|
|
}
|
|
|
|
}
|
2018-11-09 10:38:35 -05:00
|
|
|
elseif (\is_array($val) && ! empty($val))
|
2018-10-08 15:45:46 -04:00
|
|
|
{
|
|
|
|
foreach($val as $k => $v)
|
|
|
|
{
|
|
|
|
if ($v === '1')
|
|
|
|
{
|
|
|
|
$keyedConfig[$key][$k] = TRUE;
|
|
|
|
}
|
|
|
|
elseif($v === '0')
|
|
|
|
{
|
|
|
|
$keyedConfig[$key][$k] = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$keyedConfig[$key][$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ksort($looseConfig);
|
|
|
|
ksort($keyedConfig);
|
|
|
|
|
|
|
|
$output = [];
|
|
|
|
|
|
|
|
foreach($looseConfig as $k => $v)
|
|
|
|
{
|
|
|
|
$output[$k] = $v;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($keyedConfig as $k => $v)
|
|
|
|
{
|
|
|
|
$output[$k] = $v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function saveSettingsFile(array $settings): bool
|
2018-10-05 21:32:15 -04:00
|
|
|
{
|
2018-10-09 18:10:20 -04:00
|
|
|
$configWrapped = (count(array_keys($settings)) === 1 && array_key_exists('config', $settings));
|
|
|
|
if ($configWrapped)
|
|
|
|
{
|
|
|
|
$settings = $settings['config'];
|
|
|
|
}
|
2018-10-08 15:45:46 -04:00
|
|
|
|
2018-10-05 21:32:15 -04:00
|
|
|
try
|
|
|
|
{
|
2018-10-08 15:45:46 -04:00
|
|
|
$settings = $this->validateSettings($settings);
|
2018-10-05 21:32:15 -04:00
|
|
|
}
|
|
|
|
catch (UndefinedPropertyException $e)
|
|
|
|
{
|
2018-10-09 18:10:20 -04:00
|
|
|
dump($e);
|
|
|
|
dump($settings);
|
2018-10-05 21:32:15 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-10-08 15:45:46 -04:00
|
|
|
$savePath = realpath(_dir(__DIR__, '..', '..', 'app', 'config'));
|
|
|
|
$saveFile = _dir($savePath, 'admin-override.toml');
|
2018-10-05 21:32:15 -04:00
|
|
|
|
2018-10-08 15:45:46 -04:00
|
|
|
$saved = file_put_contents($saveFile, arrayToToml($settings));
|
2018-10-05 21:32:15 -04:00
|
|
|
|
2018-10-08 15:45:46 -04:00
|
|
|
return $saved !== FALSE;
|
2018-10-05 21:32:15 -04:00
|
|
|
}
|
|
|
|
}
|