2023-04-28 22:34:01 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @brief improve, a plugin for Dotclear 2
|
|
|
|
*
|
|
|
|
* @package Dotclear
|
|
|
|
* @subpackage Plugin
|
|
|
|
*
|
|
|
|
* @author Jean-Christian Denis and contributors
|
|
|
|
*
|
|
|
|
* @copyright Jean-Christian Denis
|
|
|
|
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
|
|
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Dotclear\Plugin\improve;
|
|
|
|
|
|
|
|
use dcCore;
|
2023-04-29 00:20:43 +00:00
|
|
|
use Exception;
|
2023-04-28 22:34:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Task settings management.
|
|
|
|
*/
|
|
|
|
class TaskSettings
|
|
|
|
{
|
|
|
|
/** @var string The setting prefix */
|
|
|
|
public const PREFIX = 'settings_';
|
|
|
|
|
|
|
|
/** @ var array<string,mixed> $stack The settings stack */
|
|
|
|
private array $stack = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor sets settings suffix.
|
|
|
|
*
|
2023-04-29 00:20:43 +00:00
|
|
|
* @param string $suffix The settings suffix (ie taks id)
|
2023-04-28 22:34:01 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
private string $suffix
|
|
|
|
) {
|
|
|
|
if (is_null(dcCore::app()->blog)) {
|
|
|
|
throw new Exception(__('Blog is not set'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null !== ($settings = dcCore::app()->blog->settings->get(My::id())->get(self::PREFIX . $this->suffix))) {
|
|
|
|
$settings = json_decode($settings, true);
|
|
|
|
$this->stack = is_array($settings) ? $settings : [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a task setting.
|
|
|
|
*
|
|
|
|
* @param string $key The setting ID
|
|
|
|
*
|
|
|
|
* @return mixed The setting value
|
|
|
|
*/
|
|
|
|
public function get(string $key)
|
|
|
|
{
|
|
|
|
return $this->stack[$key] ?? null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set one or more setting(s).
|
|
|
|
*
|
|
|
|
* @param mixed $settings one or more settings
|
|
|
|
* @param mixed $value value for a single setting
|
|
|
|
*/
|
|
|
|
public function set($settings, $value = null): void
|
|
|
|
{
|
|
|
|
foreach (is_array($settings) ? $settings : [$settings => $value] as $k => $v) {
|
|
|
|
$this->stack[$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save settings.
|
|
|
|
*/
|
2023-04-29 00:20:43 +00:00
|
|
|
public function save(): void
|
2023-04-28 22:34:01 +00:00
|
|
|
{
|
|
|
|
if (!is_null(dcCore::app()->blog)) {
|
|
|
|
dcCore::app()->blog->settings->get(My::id())->put(
|
|
|
|
self::PREFIX . $this->suffix,
|
|
|
|
json_encode($this->stack),
|
|
|
|
'string',
|
|
|
|
null,
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
dcCore::app()->blog->triggerBlog();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|