improve/inc/core/action.php

506 lines
12 KiB
PHP
Raw Normal View History

2021-09-01 23:35:23 +00:00
<?php
/**
* @brief improve, a plugin for Dotclear 2
2021-11-01 21:17:44 +00:00
*
2021-09-01 23:35:23 +00:00
* @package Dotclear
2021-09-06 21:36:50 +00:00
* @subpackage Plugin
2021-11-01 21:17:44 +00:00
*
2021-09-01 23:35:23 +00:00
* @author Jean-Christian Denis and contributors
2021-11-01 21:17:44 +00:00
*
2021-09-01 23:35:23 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace plugins\improve;
/* dotclear */
use dcCore;
use dcPage;
/* clearbricks */
use http;
/* php */
use arrayObject;
2021-09-01 23:35:23 +00:00
/**
* Improve action class helper
*
* Action class must extends class action.
* If your class signature is myActionClass extends plugins\improve\class\action,
* do dcCore::app()->addBehavior('ImproveAddAction'), ['myClass', 'create']);
* your action class is automatically created,
2021-09-01 23:35:23 +00:00
* then function init() of your class wil be called.
* One class must manage only one action.
*/
abstract class action
2021-09-01 23:35:23 +00:00
{
2021-11-07 23:59:52 +00:00
/** @var array<string> Current module */
protected $module = [];
/** @var string Current full path */
protected $path_full = '';
/** @var string Current file extension */
protected $path_extension = '';
2021-09-01 23:35:23 +00:00
2021-11-07 23:59:52 +00:00
/** @var boolean Current path is directory */
protected $path_is_dir = null;
/** @var string The child class name */
private $class_name = '';
/** @var array<string, array> Messages logs */
private $logs = ['success' => [], 'warning' => [], 'error' => []];
/** @var array<string> Action module settings */
private $settings = [];
/** @var array List of allowed properties */
protected static $allowed_properties = ['id', 'name', 'description', 'priority', 'configurator', 'types'];
/** @var string Module id */
private $id = '';
/** @var string Module name */
private $name = '';
/** @var string Module description */
private $description = '';
/** @var integer Module id */
private $priority = 500;
/** @var boolean Module has config page */
private $configurator = false;
/** @var array Module supported types */
private $types = ['plugin'];
2021-09-01 23:35:23 +00:00
/**
* Action constructor inits properties and settings of a child class.
*/
final public function __construct()
2021-09-01 23:35:23 +00:00
{
$this->class_name = str_replace(prepend::getActionsNS(), '', get_called_class());
2021-09-01 23:35:23 +00:00
$settings = dcCore::app()->blog->settings->improve->get('settings_' . $this->class_name);
if (null != $settings) {
$settings = unserialize($settings);
}
$this->settings = is_array($settings) ? $settings : [];
2021-09-01 23:35:23 +00:00
$this->init();
// can overload priority by settings
if (1 < ($p = (int) dcCore::app()->blog->settings->improve->get('priority_' . $this->class_name))) {
$this->priority = $p;
2021-09-01 23:35:23 +00:00
}
}
/**
* Helper to create an instance of a ImproveAction child class.
*
2021-11-07 23:59:52 +00:00
* @param ArrayObject $list ArrayObject of actions list
*/
final public static function create(arrayObject $list): void
2021-09-01 23:35:23 +00:00
{
2021-11-07 23:59:52 +00:00
$child = static::class;
$class = new $child();
2021-11-07 23:59:52 +00:00
$list->append($class);
2021-09-01 23:35:23 +00:00
}
/**
* Action initialisation function.
2021-11-01 21:17:44 +00:00
*
* It's called when an instance of ImproveAction child class is created.
* Usefull to setup action class.
2021-11-01 21:17:44 +00:00
*
* @return bool True if initialisation is ok.
*/
abstract protected function init(): bool;
2021-09-01 23:35:23 +00:00
/// @name Properties methods
//@{
/**
2021-11-07 23:59:52 +00:00
* Get a definition property of action class
*
* @param string $key a property or setting id
*
* @return mixed Value of property or setting of action.
*/
2021-11-07 23:59:52 +00:00
final public function get(string $key)
2021-09-01 23:35:23 +00:00
{
if (isset($this->settings[$key])) {
2021-11-07 23:59:52 +00:00
return $this->settings[$key];
}
return null;
2021-09-01 23:35:23 +00:00
}
/** Get action module id */
final public function id(): string
{
return $this->id;
}
/** Get action module name */
final public function name(): string
2021-09-01 23:35:23 +00:00
{
return $this->name;
}
/** Get action module description */
final public function description(): string
{
return $this->description;
}
/** Get action module priority */
final public function priority(): int
{
return $this->priority;
}
/** Get action module configuration url if any */
final public function configurator(): bool
{
return $this->configurator;
}
/** Get action module supported types */
final public function types(): array
{
return $this->types;
2021-09-01 23:35:23 +00:00
}
/**
* Set properties of action class
2021-11-01 21:17:44 +00:00
*
* @param array $properties Properties
2021-11-01 21:17:44 +00:00
*
2021-11-07 23:59:52 +00:00
* @return boolean Success
*/
final protected function setProperties(array $properties): bool
2021-09-01 23:35:23 +00:00
{
foreach ($properties as $key => $value) {
if (in_array($key, self::$allowed_properties)) {
$this->{$key} = $value;
2021-09-01 23:35:23 +00:00
}
}
2021-11-01 21:17:44 +00:00
2021-09-01 23:35:23 +00:00
return true;
}
//@}
2021-09-01 23:35:23 +00:00
/// @name Settings methods
//@{
/**
* Get a settings of action class
2021-11-01 21:17:44 +00:00
*
* @param string $setting a settings id
2021-11-01 21:17:44 +00:00
*
* @return mixed A setting of action.
*/
final protected function getSetting(string $setting)
2021-09-01 23:35:23 +00:00
{
return $this->settings[$setting] ?? null;
2021-09-01 23:35:23 +00:00
}
/**
* Set one or more setting of action class
2021-11-01 21:17:44 +00:00
*
* @param mixed $settings one or more settings
2021-11-07 23:59:52 +00:00
* @param mixed $value value for a single setting
2021-11-01 21:17:44 +00:00
*
* @return mixed A setting of action.
*/
final protected function setSettings($settings, $value = null)
2021-09-01 23:35:23 +00:00
{
$settings = is_array($settings) ? $settings : [$settings => $value];
2021-11-01 21:17:44 +00:00
foreach ($settings as $k => $v) {
$this->settings[$k] = $v;
2021-09-01 23:35:23 +00:00
}
2021-11-01 21:17:44 +00:00
2021-09-06 21:29:56 +00:00
return true;
2021-09-01 23:35:23 +00:00
}
/**
* Redirection after settings update
2021-11-01 21:17:44 +00:00
*
* This save settings update before redirect.
2021-11-01 21:17:44 +00:00
*
* @param string $url redirect url after settings update
*/
2021-11-07 23:59:52 +00:00
final protected function redirect(string $url): bool
2021-09-01 23:35:23 +00:00
{
dcCore::app()->blog->settings->improve->put(
2021-11-07 23:59:52 +00:00
'settings_' . $this->class_name,
2021-11-01 21:17:44 +00:00
serialize($this->settings),
'string',
null,
true,
2021-09-01 23:35:23 +00:00
true
);
dcCore::app()->blog->triggerBlog();
dcPage::addSuccessNotice(__('Configuration successfully updated'));
2021-09-01 23:35:23 +00:00
http::redirect($url);
2021-11-07 23:59:52 +00:00
return true;
2021-09-01 23:35:23 +00:00
}
/**
* Check if action class is well configured
2021-11-01 21:17:44 +00:00
*
* @return boolean True if class action is well configured
*/
2021-09-01 23:35:23 +00:00
abstract public function isConfigured(): bool;
/**
* Get action configuration page header
*
* @return string Headers
*/
public function header(): ?string
{
return null;
}
/**
* Get configuraton gui
2021-11-01 21:17:44 +00:00
*
* If action class uses internal configuration,
* it must share here html form content of its settings.
* It must not use enclose bloc "form" nor button "save".
2021-11-01 21:17:44 +00:00
* This function is also called to redirect form
* after validation with $this->redirect($url);
2021-11-01 21:17:44 +00:00
*
2021-11-07 23:59:52 +00:00
* @param string $url post form redirect url
2021-11-01 21:17:44 +00:00
*
2021-11-07 23:59:52 +00:00
* @return string|null A setting of action.
*/
public function configure(string $url): ?string
2021-09-01 23:35:23 +00:00
{
return null;
2021-09-01 23:35:23 +00:00
}
//@}
2021-09-01 23:35:23 +00:00
/**
* Set in class var current module definitions.
2021-11-01 21:17:44 +00:00
*
* @see Improve::sanitizeModule()
2021-11-01 21:17:44 +00:00
*
2021-11-07 23:59:52 +00:00
* @param array<string> $module Full array of module definitons
*/
2021-11-07 23:59:52 +00:00
final public function setModule(array $module): bool
2021-09-01 23:35:23 +00:00
{
$this->module = $module;
2021-11-07 23:59:52 +00:00
return true;
2021-09-01 23:35:23 +00:00
}
/**
* Set in class var current path definitons.
2021-11-01 21:17:44 +00:00
*
2021-11-07 23:59:52 +00:00
* @param string $path_full Full path
* @param string $path_extension Path extension (if it is a file)
* @param boolean $path_is_dir True if path is a directory
*/
2021-11-07 23:59:52 +00:00
final public function setPath(string $path_full, string $path_extension, bool $path_is_dir): bool
2021-09-01 23:35:23 +00:00
{
2021-11-01 21:17:44 +00:00
$this->path_full = $path_full;
$this->path_extension = $path_extension;
2021-11-01 21:17:44 +00:00
$this->path_is_dir = $path_is_dir;
2021-11-07 23:59:52 +00:00
return true;
}
2021-09-01 23:35:23 +00:00
/// @name Fix methods
//@{
/**
* Called when starting to fix module.
*/
public function openModule(): ?bool
{
2021-09-01 23:35:23 +00:00
return null;
}
/**
* Called when open a directory to fix.
*/
public function openDirectory(): ?bool
2021-09-01 23:35:23 +00:00
{
return null;
}
/**
* Called when open a file to fix.
*/
public function openFile(): ?bool
2021-09-01 23:35:23 +00:00
{
return null;
}
/**
* Called when read content of a file to fix.
2021-11-01 21:17:44 +00:00
*
* Content is shared from action to another.
* If an action erase content, fix is stopped.
2021-11-01 21:17:44 +00:00
* If you want to erase a content you must erase
* the file on action openDirectory.
2021-11-01 21:17:44 +00:00
*
* @param string $content File content
*/
public function readFile(string &$content): ?bool
2021-09-01 23:35:23 +00:00
{
return null;
}
/**
* Called when close a file to fix.
*/
public function closeFile(): ?bool
2021-09-01 23:35:23 +00:00
{
return null;
}
/**
* Called when close a module to fix.
*/
public function closeModule(): ?bool
2021-09-01 23:35:23 +00:00
{
return null;
}
//@}
/// @name Logs methods
//@{
/**
* Set an action log.
2021-11-01 21:17:44 +00:00
*
* Log must be use every time an action something happen.
2021-11-01 21:17:44 +00:00
*
* @param string $type type of message, can be error, warning, succes
* @param string $message message to log
2021-11-01 21:17:44 +00:00
*
* @return boolean True if message is logged.
*/
final public function setLog(string $type, string $message): bool
{
if (empty($this->path_full) || !array_key_exists($type, $this->logs)) {
return false;
}
$this->logs[$type][$this->path_full][] = $message;
2021-11-01 21:17:44 +00:00
return true;
}
/**
* Check if action class has log of given type.
2021-11-01 21:17:44 +00:00
*
* @param string $type type of message, can be error, warning, succes
2021-11-01 21:17:44 +00:00
*
* @return boolean True if messages exist.
*/
final public function hasLog(string $type): bool
{
return array_key_exists($type, $this->logs) && !empty($this->logs[$type]);
}
/**
* Get action logs.
2021-11-01 21:17:44 +00:00
*
* @param string|null $type type of message, can be error, warning, succes
2021-11-01 21:17:44 +00:00
*
* @return array Arry of given type of log or all if type is null
*/
final public function getLogs($type = null): array
{
if (null === $type) {
return $this->logs;
}
2021-11-01 21:17:44 +00:00
if (empty($this->path_full)
|| !array_key_exists($type, $this->logs)
|| !array_key_exists($this->path_full, $this->logs[$type])
) {
return [];
}
2021-11-01 21:17:44 +00:00
return $this->logs[$type][$this->path_full];
}
/**
* Set a log of type error.
*/
2021-11-07 23:59:52 +00:00
final public function setError(string $message): bool
{
2021-11-07 23:59:52 +00:00
return $this->setLog('error', $message);
}
/**
* Check logs of type error exists.
*/
final public function hasError(): bool
{
return !empty($this->getLogs('error'));
}
/**
* Get logs of type error.
*/
final public function getErrors(): array
{
return $this->getLogs('error');
}
/**
* Set a log of type warning.
*/
2021-11-07 23:59:52 +00:00
final public function setWarning(string $message): bool
{
2021-11-07 23:59:52 +00:00
return $this->setLog('warning', $message);
}
/**
* Check logs of type error warnings.
*/
final public function hasWarning(): bool
{
return !empty($this->getLogs('warning'));
}
/**
* Get logs of type warning.
*/
final public function getWarnings(): array
{
return $this->getLogs('warning');
}
/**
* Set a log of type success.
*/
2021-11-07 23:59:52 +00:00
final public function setSuccess(string $message): bool
{
2021-11-07 23:59:52 +00:00
return $this->setLog('success', $message);
}
/**
* Check logs of type error success.
*/
final public function hasSuccess(): bool
{
return !empty($this->getLogs('success'));
}
/**
* Get logs of type success.
*/
final public function getSuccess(): array
{
return $this->getLogs('success');
}
//@}
2021-11-01 21:17:44 +00:00
}