noodles/inc/class.noodles.php

221 lines
4.3 KiB
PHP
Raw Normal View History

2021-09-08 22:53:13 +00:00
<?php
/**
2021-09-08 23:05:05 +00:00
* @brief noodles, a plugin for Dotclear 2
2022-11-17 20:00:42 +00:00
*
* @package Dotclear
2021-09-08 23:05:05 +00:00
* @subpackage Plugin
2022-11-17 20:00:42 +00:00
*
2021-09-08 23:05:05 +00:00
* @author Jean-Christian Denis and contributors
2022-11-17 20:00:42 +00:00
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2021-09-09 07:12:31 +00:00
if (!defined('DC_RC_PATH')) {
return null;
}
2021-09-08 22:53:13 +00:00
class noodles
{
2021-09-09 07:12:31 +00:00
private $noodles = [];
public static function decode($s)
{
$o = @unserialize(base64_decode($s));
if ($o instanceof noodles) {
2021-09-09 07:12:31 +00:00
return $o;
}
2021-11-01 09:28:22 +00:00
return new self();
}
public function encode()
{
return base64_encode(serialize($this));
}
2021-09-09 07:12:31 +00:00
public function add($id, $name, $js_callback, $php_callback = null)
{
2021-09-09 07:12:31 +00:00
$this->noodles[$id] = new noodle($id, $name, $js_callback, $php_callback);
2021-11-01 09:28:22 +00:00
2021-09-09 07:12:31 +00:00
return $this->noodles[$id];
}
public function get($id)
{
2021-11-01 09:28:22 +00:00
return $this->noodles[$id] ?? null;
}
public function __get($id)
{
return $this->get($id);
}
2021-10-30 19:17:50 +00:00
public function set($id, $noodle)
{
return $this->noodles[$id] = $noodle;
}
public function __set($id, $noodle)
{
return $this->set($id, $noodle);
}
public function exists($id)
{
return isset($this->noodles[$id]);
}
public function isEmpty()
{
return !count($this->noodles);
}
public function noodles()
{
return $this->noodles;
}
2021-09-08 22:53:13 +00:00
}
class noodle
{
private $id;
private $name;
private $js_callback;
private $php_callback;
2021-09-09 07:12:31 +00:00
private $settings = [
'active' => 0,
'rating' => 'g',
2021-11-01 09:28:22 +00:00
'size' => 16,
'target' => '',
2022-11-17 20:00:42 +00:00
'place' => 'prepend',
2021-09-09 07:12:31 +00:00
];
2021-09-09 07:12:31 +00:00
public function __construct($id, $name, $js_callback, $php_callback = null)
{
2021-11-01 09:28:22 +00:00
$this->id = $id;
$this->name = $name;
$this->js_callback = $js_callback;
$this->php_callback = $php_callback;
}
public function id()
{
return $this->id;
}
public function name()
{
return $this->name;
}
2021-09-09 07:12:31 +00:00
public function jsCallback($g, $content = '')
{
2021-09-09 07:12:31 +00:00
if (!is_callable($this->js_callback)) {
return null;
}
2021-11-01 09:28:22 +00:00
2021-09-09 07:12:31 +00:00
return call_user_func($this->js_callback, $g, $content);
}
public function hasJsCallback()
{
return !empty($this->js_callback);
}
2022-11-17 20:00:42 +00:00
public function phpCallback()
{
2021-09-09 07:12:31 +00:00
if (!is_callable($this->php_callback)) {
return null;
}
2021-11-01 09:28:22 +00:00
2022-11-17 20:00:42 +00:00
return call_user_func($this->php_callback, $this);
}
public function hasPhpCallback()
{
return !empty($this->php_callback);
}
2021-09-09 07:12:31 +00:00
public function set($type, $value)
{
2021-09-09 07:12:31 +00:00
switch ($type) {
case 'active':
2021-11-01 09:28:22 +00:00
$this->settings['active'] = abs((int) $value);
2022-11-17 20:00:42 +00:00
break;
case 'rating':
2021-09-09 07:12:31 +00:00
$this->settings['rating'] = in_array($value, ['g', 'pg', 'r', 'x']) ? $value : 'g';
2021-11-01 09:28:22 +00:00
2022-11-17 20:00:42 +00:00
break;
case 'size':
2021-09-09 07:12:31 +00:00
$this->settings['size'] = in_array($value, [16, 24, 32, 48, 56, 64, 92, 128, 256]) ? $value : 16;
2021-11-01 09:28:22 +00:00
2022-11-17 20:00:42 +00:00
break;
case 'css':
2021-09-09 07:12:31 +00:00
$this->settings['css'] = (string) $value;
2021-11-01 09:28:22 +00:00
2022-11-17 20:00:42 +00:00
break;
case 'target':
2021-09-09 07:12:31 +00:00
$this->settings['target'] = (string) $value;
2021-11-01 09:28:22 +00:00
2022-11-17 20:00:42 +00:00
break;
case 'place':
2021-09-09 07:12:31 +00:00
$this->settings['place'] = in_array($value, ['append', 'prepend', 'before', 'after']) ? $value : 'prepend';
2021-11-01 09:28:22 +00:00
2022-11-17 20:00:42 +00:00
break;
}
2021-11-01 09:28:22 +00:00
2021-09-09 07:12:31 +00:00
return $this;
}
public function active($value)
{
return $this->set('active', $value);
}
public function rating($value)
{
return $this->set('rating', $value);
}
public function size($value)
{
return $this->set('size', $value);
}
public function css($value)
{
return $this->set('css', $value);
}
public function target($value)
{
return $this->set('target', $value);
}
public function place($value)
{
return $this->set('place', $value);
}
2021-09-09 07:12:31 +00:00
public function __set($type, $value)
{
2021-09-09 07:12:31 +00:00
$this->set($type, $value);
}
public function get($type)
{
2021-11-01 09:28:22 +00:00
return $this->settings[$type] ?? null;
}
public function __get($type)
{
return $this->get($type);
}
2022-11-17 20:00:42 +00:00
}