From 157016ff35ce41bc0a9c4828577c852ab636ff9e Mon Sep 17 00:00:00 2001 From: "stephan.kasdorf" Date: Mon, 14 Jul 2025 12:20:09 +0200 Subject: [PATCH] Add TypeSwitch class and enhance attribute handling Introduced a new `TypeSwitch` class to manage input type switch fields for forms, allowing simplified creation of toggle switches or binary options. Enhanced attribute handling in `formattributes.php` to conditionally remove the `checked` attribute only if not explicitly specified. Updated framework to include the new `TypeSwitch` class. --- core/c/formattributes.php | 9 ++++++-- core/c/typeswitch.php | 44 +++++++++++++++++++++++++++++++++++++++ core/f/form.php | 18 ++++++++++++++++ core/framework.php | 1 + 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 core/c/typeswitch.php diff --git a/core/c/formattributes.php b/core/c/formattributes.php index 0ade6b1..8a52ade 100755 --- a/core/c/formattributes.php +++ b/core/c/formattributes.php @@ -59,7 +59,12 @@ class FormAttributes $this->_element = str_replace(' ID', '', $this->_element); $this->_element = str_replace(' CLASS', '', $this->_element); $this->_element = str_replace(' enctype="ENCTYPE"', '', $this->_element); - $this->_element = str_replace(' checked="CHECKED"', '', $this->_element); + // Only remove checked attribute if it's not set in the attributes + if (!isset($attributes['checked']) || empty($attributes['checked'])) + { + $this->_element = str_replace(' checked="CHECKED"', '', $this->_element); + $this->_element = str_replace(' CHECKED', '', $this->_element); + } $this->_element = str_replace(' onsubmit="ONSUBMIT"', '', $this->_element); $this->_element = str_replace(' onclick="ONCLICK"', '', $this->_element); $this->_element = str_replace(' action="ACTION"', '', $this->_element); @@ -85,8 +90,8 @@ class FormAttributes $this->_element = str_replace(' method="METHOD"', '', $this->_element); $this->_element = str_replace(' data-toggle="DATA-TOGGLE"', '', $this->_element); $this->_element = str_replace(' SELECTED', '', $this->_element); + $this->_element = str_replace(' MULTIPLE', '', $this->_element); $this->_element = str_replace(' CONTEXT', '', $this->_element); - $this->_element = str_replace(' CHECKED', '', $this->_element); $this->_element = str_replace(' VALUE', '', $this->_element); $this->_element = str_replace(' PATTERN', '', $this->_element); $this->_element = str_replace('ANY', '', $this->_element); diff --git a/core/c/typeswitch.php b/core/c/typeswitch.php new file mode 100644 index 0000000..4c4dda2 --- /dev/null +++ b/core/c/typeswitch.php @@ -0,0 +1,44 @@ + '', + self::FORM_ATTRIBUTE_ID => '', + self::FORM_ATTRIBUTE_CLASS => '', + self::FORM_NAME => '', + self::FORM_ATTRIBUTE_CHECKED + ); + + public function loadElement($attributes) + { + parent::__construct($this->_attributes); + $this->_setElement(); + $this->_setAttributes(self::loadAttributeValues($attributes)); + return $this->getElement(); + } + + /** + * @param mixed $element + */ + private function _setElement() + { + $this->_element = '
' . "\n" . + '' . "\n" . + '
'; + } + +} diff --git a/core/f/form.php b/core/f/form.php index 3ca020f..ee5bd71 100644 --- a/core/f/form.php +++ b/core/f/form.php @@ -24,6 +24,7 @@ use Nibiru\Form\TypeReset; use Nibiru\Form\TypeSearch; use Nibiru\Form\TypeSelect; use Nibiru\Form\TypeSubmit; +use Nibiru\Form\TypeSwitch; use Nibiru\Form\TypeTelefon; use Nibiru\Form\TypeTextarea; use Nibiru\Form\TypeText; @@ -240,6 +241,23 @@ class Form self::assemble( self::getElement()->loadElement( $attributes ) ); } + /** + * @desc adds an input type switch field to the form; this method is useful for + * adding toggle switches or binary options. + * @param $attributes an array of attributes, such as ("NAME", "ID", "CLASS", "ONCHANGE", "ONBLUR", "ONFOCUS", "REQUIRED", "CHECKED") + * @param $div optional parameter to specify a div wrapping element; if provided, the div will be set for the component. + * @return void + */ + public static function addInputTypeSwitch( $attributes, $div = false ) + { + if($div) + { + self::setDiv( $div ); + } + self::setElement( new TypeSwitch() ); + self::assemble( self::getElement()->loadElement( $attributes ) ); + } + /** * @desc adds a input field for password entry * @param $attributes diff --git a/core/framework.php b/core/framework.php index 367e63d..a1c4701 100644 --- a/core/framework.php +++ b/core/framework.php @@ -75,6 +75,7 @@ require_once __DIR__ . '/c/typenumber.php'; require_once __DIR__ . '/c/typereset.php'; require_once __DIR__ . '/c/typefileupload.php'; require_once __DIR__ . '/c/typecheckbox.php'; +require_once __DIR__ . '/c/typeswitch.php'; require_once __DIR__ . '/c/typecolor.php'; require_once __DIR__ . '/c/typeoption.php'; require_once __DIR__ . '/c/typeselect.php';