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
|
|
|
|
*
|
2021-02-04 11:57:01 -05:00
|
|
|
* PHP version 8
|
2018-10-05 21:32:15 -04:00
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2021-01-13 01:52:03 -05:00
|
|
|
* @copyright 2015 - 2021 Timothy J. Warren
|
2018-10-05 21:32:15 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-12-10 17:06:50 -05:00
|
|
|
* @version 5.2
|
2018-10-05 21:32:15 -04:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\Model;
|
|
|
|
|
2020-04-22 11:39:44 -04:00
|
|
|
use function is_array;
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Model for handling settings control panel
|
|
|
|
*/
|
|
|
|
final class Settings {
|
|
|
|
use ContainerAware;
|
|
|
|
|
2020-04-10 16:35:01 -04:00
|
|
|
private ConfigInterface $config;
|
2018-10-05 21:32:15 -04:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2020-04-22 11:39:44 -04:00
|
|
|
$cfg = Config::check($settings);
|
2018-10-08 15:45:46 -04:00
|
|
|
|
|
|
|
$looseConfig = [];
|
|
|
|
$keyedConfig = [];
|
|
|
|
|
|
|
|
// Convert 'boolean' values to true and false
|
|
|
|
// Also order keys so they can be saved properly
|
2020-04-22 11:39:44 -04:00
|
|
|
foreach ($cfg as $key => $val)
|
2018-10-08 15:45:46 -04:00
|
|
|
{
|
|
|
|
if (is_scalar($val))
|
|
|
|
{
|
|
|
|
if ($val === '1')
|
|
|
|
{
|
|
|
|
$looseConfig[$key] = TRUE;
|
|
|
|
}
|
|
|
|
elseif ($val === '0')
|
|
|
|
{
|
|
|
|
$looseConfig[$key] = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$looseConfig[$key] = $val;
|
|
|
|
}
|
|
|
|
}
|
2020-04-22 11:39:44 -04: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;
|
|
|
|
}
|
|
|
|
|
2021-02-03 09:46:36 -05:00
|
|
|
$savePath = _dir(dirname(__DIR__, 3), 'app', 'config');
|
2018-10-08 15:45:46 -04:00
|
|
|
$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
|
|
|
}
|
|
|
|
}
|