From 69f9ff4034e4de69f4b375fbf51bc4822f9038db Mon Sep 17 00:00:00 2001 From: Sjon Hortensius Date: Tue, 27 Jan 2015 21:28:17 +0100 Subject: [PATCH] final touches on Forms system - removed trailing unused form-html Form - set proper width for submit-button column Element - support returning raw-list for Inputs Group,Input - renamed getColumnWidth to getWidth Input - make setWidth consistent with Form->setWidth, directly add proper class to column --- usr/local/www/classes/Form.class.php | 12 ++++-- usr/local/www/classes/Form/Checkbox.class.php | 2 +- usr/local/www/classes/Form/Element.class.php | 11 +++-- usr/local/www/classes/Form/Group.class.php | 6 +-- usr/local/www/classes/Form/Input.class.php | 42 ++++++++++++++----- usr/local/www/classes/Form/Section.class.php | 2 +- usr/local/www/classes/Form/Select.class.php | 4 +- usr/local/www/system.php | 10 +---- 8 files changed, 57 insertions(+), 32 deletions(-) diff --git a/usr/local/www/classes/Form.class.php b/usr/local/www/classes/Form.class.php index 0bc715a0b3..2a0eb44e37 100644 --- a/usr/local/www/classes/Form.class.php +++ b/usr/local/www/classes/Form.class.php @@ -52,14 +52,18 @@ class Form extends Form_Element public function __toString() { $sections = implode('', $this->_sections); - $submit = isset($this->_submit) ? '
'. $this->_submit .'
' : ''; + + if (isset($this->_submit)) + { + $this->_submit->setWidth(12 - $this->getLabelWidth()); + $this->_submit->addColumnClass('col-sm-offset-'. $this->_labelWidth); + $sections .= $this->_submit; + } return << {$sections} - - {$submit} EOT; } -} +} \ No newline at end of file diff --git a/usr/local/www/classes/Form/Checkbox.class.php b/usr/local/www/classes/Form/Checkbox.class.php index 4893ce5d08..ccdf42d796 100644 --- a/usr/local/www/classes/Form/Checkbox.class.php +++ b/usr/local/www/classes/Form/Checkbox.class.php @@ -25,4 +25,4 @@ class Form_Checkbox extends Form_Input return ''; } -} +} \ No newline at end of file diff --git a/usr/local/www/classes/Form/Element.class.php b/usr/local/www/classes/Form/Element.class.php index d2960332e3..4b57be22f3 100644 --- a/usr/local/www/classes/Form/Element.class.php +++ b/usr/local/www/classes/Form/Element.class.php @@ -19,16 +19,21 @@ class Form_Element return $this; } - public function getHtmlClass() + public function getHtmlClass($wrapped = true) { if (empty($this->_classes)) return ''; - return ' class="'. implode(' ', array_keys($this->_classes)).'" '; + $list = implode(' ', array_keys($this->_classes)); + + if (!$wrapped) + return $list; + + return 'class="'. $list .'"'; } protected function _setParent(Form_Element $parent) { $this->_parent = $parent; } -} +} \ No newline at end of file diff --git a/usr/local/www/classes/Form/Group.class.php b/usr/local/www/classes/Form/Group.class.php index b9bb0a424d..10397340e5 100644 --- a/usr/local/www/classes/Form/Group.class.php +++ b/usr/local/www/classes/Form/Group.class.php @@ -29,7 +29,7 @@ class Form_Group extends Form_Element $this->_labelTarget = $input; } - // Potentially allow overloading + // Potentially allow overloading public function getLabelWidth() { return $this->_parent->getLabelWidth(); @@ -50,7 +50,7 @@ class Form_Group extends Form_Element foreach ($this->_inputs as $input) { - $width = $input->getColumnWidth(); + $width = $input->getWidth(); if (isset($width)) $spaceLeft -= $width; @@ -76,4 +76,4 @@ class Form_Group extends Form_Element EOT; } -} +} \ No newline at end of file diff --git a/usr/local/www/classes/Form/Input.class.php b/usr/local/www/classes/Form/Input.class.php index 57bdd9adf1..4a06f842ac 100644 --- a/usr/local/www/classes/Form/Input.class.php +++ b/usr/local/www/classes/Form/Input.class.php @@ -51,27 +51,48 @@ class Form_Input extends Form_Element return $this; } - public function getColumnWidth() + public function getWidth() { return $this->_columnWidth; } - public function setWidth($count, $soft = false) + public function setWidth($size) { - if ($soft && isset($this->_columnWidth)) - return; + if ($size < 1 || $size > 12) + throw new Exception('Incorrect size, pass a number between 1 and 12'); - $this->_columnWidth = (int)$count; + $this->removeColumnClass('col-sm-'. $this->_columnWidth); + + $this->_columnWidth = (int)$size; + + $this->addColumnClass('col-sm-'. $this->_columnWidth); } public function addColumnClass($class) { - array_push($this->_columnClasses, $class); + $this->_columnClasses[$class] = true; + + return $this; + } + + public function removeColumnClass($class) + { + unset($this->_columnClasses[$class]); + + return $this; + } + + public function getColumnHtmlClass() + { + if (empty($this->_columnClasses)) + return ''; + + return 'class="'. implode(' ', array_keys($this->_columnClasses)).'"'; } protected function _getInput() { - $html = 'getHtmlClass(); + $html = '_attributes; if (isset($this->_name)) @@ -85,16 +106,17 @@ class Form_Input extends Form_Element public function __toString() { + $this->_attributes['class'] = $this->getHtmlClass(false); + $input = $this->_getInput(); - $columnClasses = implode(' ', $this->_columnClasses); $help = isset($this->_help) ? ''. gettext($this->_help). '' : ''; return << +
getColumnHtmlClass()}> {$input} {$help}
EOT; } -} +} \ No newline at end of file diff --git a/usr/local/www/classes/Form/Section.class.php b/usr/local/www/classes/Form/Section.class.php index e62437f1e7..c620561ccc 100644 --- a/usr/local/www/classes/Form/Section.class.php +++ b/usr/local/www/classes/Form/Section.class.php @@ -50,4 +50,4 @@ class Form_Section extends Form_Element EOT; } -} +} \ No newline at end of file diff --git a/usr/local/www/classes/Form/Select.class.php b/usr/local/www/classes/Form/Select.class.php index b4632910cb..3660739737 100644 --- a/usr/local/www/classes/Form/Select.class.php +++ b/usr/local/www/classes/Form/Select.class.php @@ -16,7 +16,7 @@ class Form_Select extends Form_Input protected function _getInput() { - $element = 'select'. $this->getHtmlClass(); + $element = 'select'; foreach ($this->_attributes as $key => $value) $element .= ' '.$key.'="'. htmlspecialchars($value).'"'; @@ -30,4 +30,4 @@ class Form_Select extends Form_Input EOT; } -} +} \ No newline at end of file diff --git a/usr/local/www/system.php b/usr/local/www/system.php index a5e1ecff29..f862bc6641 100644 --- a/usr/local/www/system.php +++ b/usr/local/www/system.php @@ -395,11 +395,5 @@ $section->addInput(new Form_Select( $form->add($section); print $form; -return; -?> -
- -
- - - + +include("foot.inc"); \ No newline at end of file