This commit is contained in:
the-djmaze 2022-12-03 15:18:59 +01:00
parent cb2a265373
commit 605ee88041
5 changed files with 80 additions and 53 deletions

View file

@ -614,65 +614,34 @@ class ActionsAdmin extends Actions
{
$this->IsAdminLoggined();
$mResult = false;
$sId = (string) $this->GetActionParam('Id', '');
if (!empty($sId))
{
if (!empty($sId)) {
$oPlugin = $this->Plugins()->CreatePluginByName($sId);
if ($oPlugin)
{
if ($oPlugin) {
$oConfig = $oPlugin->Config();
$aMap = $oPlugin->ConfigMap(true);
if (\is_array($aMap))
{
if (\is_array($aMap)) {
$aSettings = (array) $this->GetActionParam('Settings', []);
foreach ($aMap as $oItem)
{
foreach ($aMap as $oItem) {
$sKey = $oItem->Name();
$sValue = $aSettings[$sKey] ?? $oConfig->Get('plugin', $sKey);
if (PluginPropertyType::PASSWORD !== $oItem->Type() || static::APP_DUMMY !== $sValue)
{
$mResultValue = null;
switch ($oItem->Type()) {
case PluginPropertyType::INT:
$mResultValue = (int) $sValue;
break;
case PluginPropertyType::BOOL:
$mResultValue = '1' === (string) $sValue;
break;
case PluginPropertyType::SELECTION:
if (is_array($oItem->DefaultValue()) && in_array($sValue, $oItem->DefaultValue()))
{
$mResultValue = (string) $sValue;
}
break;
case PluginPropertyType::PASSWORD:
case PluginPropertyType::STRING:
case PluginPropertyType::STRING_TEXT:
case PluginPropertyType::URL:
$mResultValue = (string) $sValue;
break;
}
if (null !== $mResultValue)
{
if (PluginPropertyType::PASSWORD !== $oItem->Type() || static::APP_DUMMY !== $sValue) {
$oItem->SetValue($sValue);
$mResultValue = $oItem->Value();
if (null !== $mResultValue) {
$oConfig->Set('plugin', $sKey, $mResultValue);
}
}
}
}
$mResult = $oConfig->Save();
if ($oConfig->Save()) {
return $this->DefaultResponse(__FUNCTION__, true);
}
}
}
if (!$mResult)
{
throw new ClientException(Notifications::CantSavePluginSettings);
}
return $this->DefaultResponse(__FUNCTION__, true);
throw new ClientException(Notifications::CantSavePluginSettings);
}
public function DoAdminQRCode() : array

View file

@ -80,6 +80,9 @@ abstract class AbstractConfig implements \JsonSerializable
{
if (isset($this->aData[$sSectionKey][$sParamKey][0]))
{
if (!\is_scalar($mParamValue)) {
$mParamValue = null;
}
switch (\gettype($this->aData[$sSectionKey][$sParamKey][0]))
{
case 'boolean':

View file

@ -4,10 +4,7 @@ namespace RainLoop\Config;
class Plugin extends \RainLoop\Config\AbstractConfig
{
/**
* @var array
*/
private $aMap = array();
private array $aMap = array();
public function __construct(string $sPluginName, array $aMap = array())
{
@ -15,8 +12,9 @@ class Plugin extends \RainLoop\Config\AbstractConfig
$aResultMap = array();
foreach ($aMap as $oProperty) {
if ($oProperty instanceof \RainLoop\Plugins\Property) {
$mDefaultValue = $oProperty->DefaultValue();
$aResultMap[$oProperty->Name()] = array(
$oProperty->DefaultValue(),
\is_array($mDefaultValue) ? '' : $mDefaultValue,
''
);
}

View file

@ -2,6 +2,8 @@
namespace RainLoop\Plugins;
use \RainLoop\Enumerations\PluginPropertyType;
class Property implements \JsonSerializable
{
private string $sName;
@ -15,7 +17,7 @@ class Property implements \JsonSerializable
private string $sDesc = '';
private int $iType = \RainLoop\Enumerations\PluginPropertyType::STRING;
private int $iType = PluginPropertyType::STRING;
private bool $bAllowedInJs = false;
@ -24,6 +26,8 @@ class Property implements \JsonSerializable
*/
private $mDefaultValue = '';
private array $aOptions = [];
private string $sPlaceholder = '';
function __construct(string $sName)
@ -48,7 +52,35 @@ class Property implements \JsonSerializable
*/
public function SetValue($mValue) : void
{
$this->mValue = $mValue;
$this->mValue = null;
switch ($this->iType) {
case PluginPropertyType::INT:
$this->mValue = (int) $mValue;
break;
case PluginPropertyType::BOOL:
$this->mValue = !empty($mValue);
break;
case PluginPropertyType::SELECT:
foreach ($this->aOptions as $option) {
if ($mValue == $option['id']) {
$this->mValue = (string) $mValue;
}
}
break;
case PluginPropertyType::SELECTION:
if ($this->aOptions && \in_array($mValue, $oItem->Options())) {
$this->mValue = (string) $mValue;
}
break;
case PluginPropertyType::PASSWORD:
case PluginPropertyType::STRING:
case PluginPropertyType::STRING_TEXT:
case PluginPropertyType::URL:
$this->mValue = (string) $mValue;
break;
// case PluginPropertyType::GROUP:
// throw new \Exception('Not allowed to set group value');
}
}
/**
@ -56,7 +88,18 @@ class Property implements \JsonSerializable
*/
public function SetDefaultValue($mDefaultValue) : self
{
$this->mDefaultValue = $mDefaultValue;
if (\is_array($mDefaultValue)) {
$this->aOptions = $mDefaultValue;
} else {
$this->mDefaultValue = $mDefaultValue;
}
return $this;
}
public function SetOptions(array $aOptions) : self
{
$this->aOptions = $aOptions;
return $this;
}
@ -122,11 +165,24 @@ class Property implements \JsonSerializable
return $this->mDefaultValue;
}
public function Options() : array
{
return $this->aOptions;
}
public function Placeholder() : string
{
return $this->sPlaceholder;
}
/**
* @return mixed
*/
public function Value()
{
return $this->mValue;
}
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
@ -138,6 +194,7 @@ class Property implements \JsonSerializable
'Type' => $this->iType,
'Label' => $this->sLabel,
'Default' => $this->mDefaultValue,
'Options' => $this->aOptions,
'Desc' => $this->sDesc
);
}

View file

@ -16,7 +16,7 @@
data-bind="value: value, attr: {placeholder: placeholder }"></textarea>
<!-- /ko -->
<!-- ko if: 4 === Type -->
<select data-bind="options: Default, value: value"></select>
<select data-bind="options: Options, value: value"></select>
<!-- /ko -->
<!-- ko if: 5 === Type -->
<div data-bind="component: {
@ -29,7 +29,7 @@
data-bind="value: value, attr: {placeholder: placeholder}">
<!-- /ko -->
<!-- ko if: 8 === Type -->
<select data-bind="options: Default, optionsText: 'name', optionsValue: 'id', value: value"></select>
<select data-bind="options: Options, optionsText: 'name', optionsValue: 'id', value: value"></select>
<!-- /ko -->
<!-- ko if: '' !== Desc -->
<span tabindex="0" class="help-block"><span data-bind="text: Desc"></span></span>