mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-11 17:58:41 +00:00
prepare for form element generation
This commit is contained in:
parent
c8a5322e39
commit
00d9b3eb47
|
|
@ -1,113 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.4
|
||||
*
|
||||
* @package phpOMS\Model\Html
|
||||
* @copyright Dennis Eichhorn
|
||||
* @license OMS License 1.0
|
||||
* @version 1.0.0
|
||||
* @link https://orange-management.org
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace phpOMS\Model\Html;
|
||||
|
||||
/**
|
||||
* Form element class.
|
||||
*
|
||||
* @package phpOMS\Model\Html
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class FormElement
|
||||
{
|
||||
/**
|
||||
* Element id.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public string $id;
|
||||
|
||||
/**
|
||||
* Form id.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public string $form;
|
||||
|
||||
/**
|
||||
* Element name.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public string $name;
|
||||
|
||||
/**
|
||||
* Value is required.
|
||||
*
|
||||
* @var bool
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public bool $required;
|
||||
|
||||
/**
|
||||
* Default value.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public string $defaultValue;
|
||||
|
||||
/**
|
||||
* Required values which cannot be changed/removed.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public string $requiredValue;
|
||||
|
||||
/**
|
||||
* Autosave on change.
|
||||
*
|
||||
* @var bool
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public bool $autosave;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $id Element id
|
||||
* @param string $form Form id
|
||||
* @param string $name Element name
|
||||
* @param bool $required Value is required
|
||||
* @param string $defaultValue Default value
|
||||
* @param string $requiredValue Values which cannot be removed/changed
|
||||
* @param bool $autosave Save on change
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function __construct(
|
||||
string $id = '',
|
||||
string $form = '',
|
||||
string $name = '',
|
||||
bool $required = false,
|
||||
string $defaultValue = '',
|
||||
string $requiredValue = '',
|
||||
bool $autosave = false
|
||||
) {
|
||||
$this->id = $id;
|
||||
$this->form = $form;
|
||||
$this->name = $name;
|
||||
$this->required = $required;
|
||||
$this->defaultValue = $defaultValue;
|
||||
$this->requiredValue = $requiredValue;
|
||||
$this->autosave = $autosave;
|
||||
}
|
||||
}
|
||||
154
Model/Html/FormElementGenerator.php
Normal file
154
Model/Html/FormElementGenerator.php
Normal file
|
|
@ -0,0 +1,154 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.4
|
||||
*
|
||||
* @package phpOMS\Model\Html
|
||||
* @copyright Dennis Eichhorn
|
||||
* @license OMS License 1.0
|
||||
* @version 1.0.0
|
||||
* @link https://orange-management.org
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace phpOMS\Model\Html;
|
||||
|
||||
use phpOMS\Stdlib\Base\SmartDateTime;
|
||||
|
||||
/**
|
||||
* Form element generator class.
|
||||
*
|
||||
* @package phpOMS\Model\Html
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
final class FormElementGenerator
|
||||
{
|
||||
/**
|
||||
* Generate a form element from a json object
|
||||
*
|
||||
* @param array $json Json object representing the form element
|
||||
* @param array $lang Language array
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function generate(array $json, array $lang = []) : string
|
||||
{
|
||||
if ($json['type'] === 'select') {
|
||||
return self::generateSelect($json, $lang);
|
||||
} elseif ($json['type'] === 'input') {
|
||||
return self::generateInput($json, $lang);
|
||||
} elseif ($json['type'] === 'label') {
|
||||
return self::generateLabel($json, $lang);
|
||||
} elseif ($json['type'] === 'textarea') {
|
||||
return self::generateTextarea($json);
|
||||
}
|
||||
|
||||
return 'INVALID';
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a form element from a json object
|
||||
*
|
||||
* @param array $json Json object representing the form element
|
||||
* @param array $lang Language array
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private static function generateInput(array $json, array $lang = []) : string
|
||||
{
|
||||
$element = '<input';
|
||||
foreach ($json['attributes'] as $attribute => $value) {
|
||||
$element .= ' ' . $attribute . '="' . $value . '"';
|
||||
}
|
||||
|
||||
$element .= (isset($json['default']) ? ' value="' . ($json['subtype'] === 'datetime' ? (new SmartDateTime($json['default']['value']))->format($json['default']['format']) : $json['default']['value']) . '"' : '');
|
||||
|
||||
$element .= ($json['subtype'] === 'checkbox' || $json['subtype'] === 'radio') && $json['default']['checked'] ? ' checked' : '';
|
||||
$element .= '>';
|
||||
$element .= $json['subtype'] === 'checkbox' || $json['subtype'] === 'radio' ? '<label for="' . $json['attributes']['id'] . '">' . ($lang[$json['default']['content']] ?? $json['default']['content']) . '</label>' : '';
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a form element from a json object
|
||||
*
|
||||
* @param array $json Json object representing the form element
|
||||
* @param array $lang Language array
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private static function generateSelect(array $json, array $lang = []) : string
|
||||
{
|
||||
$element = '<select';
|
||||
foreach ($json['attributes'] as $attribute => $value) {
|
||||
$element .= ' ' . $attribute . '="' . $value . '"';
|
||||
}
|
||||
|
||||
$element .= '>';
|
||||
|
||||
foreach ($json['options'] as $value => $text) {
|
||||
$element .= '<option value="' . $value . '"' . (isset($json['default']) && $value === $json['default']['value'] ? ' selected' : '') . '>' . ($lang[$text] ?? $text) . '</option>';
|
||||
}
|
||||
|
||||
$element .= '</select>';
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a form element from a json object
|
||||
*
|
||||
* @param array $json Json object representing the form element
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private static function generateTextarea(array $json) : string
|
||||
{
|
||||
$element = '<textarea';
|
||||
foreach ($json['attributes'] as $attribute => $value) {
|
||||
$element .= ' ' . $attribute . '="' . $value . '"';
|
||||
}
|
||||
|
||||
$element .= '>';
|
||||
$element .= isset($json['default']) ? ' value="' . $json['default']['value'] . '"' : '';
|
||||
$element .= '</textarea>';
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a form element from a json object
|
||||
*
|
||||
* @param array $json Json object representing the form element
|
||||
* @param array $lang Language array
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private static function generateLabel(array $json, array $lang = []) : string
|
||||
{
|
||||
$element = '<label';
|
||||
foreach ($json['attributes'] as $attribute => $value) {
|
||||
$element .= ' ' . $attribute . '="' . $value . '"';
|
||||
}
|
||||
|
||||
$element .= '>';
|
||||
$element .= isset($json['default']) ? ' value="' . ($lang[$json['default']['value']] ?? $json['default']['value']) . '"' : '';
|
||||
$element .= '</label>';
|
||||
|
||||
return $element;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user