Skip to content

Commit

Permalink
replace Input->column methods with generic Element plus fixes
Browse files Browse the repository at this point in the history
Form.class - fix adding offset to submit button
Checkbox - add class to column, not input. Fix displayAsRadio return
Element - no longer abstract, used as a generic element in Input

fixes #43, refs #37
  • Loading branch information
SjonHortensius committed Mar 29, 2015
1 parent 1192840 commit 5fd8513
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 39 deletions.
10 changes: 6 additions & 4 deletions usr/local/www/classes/Form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ public function __toString()
$element = parent::__toString();
$html = implode('', $this->_sections);

if (isset($this->_submit))
foreach ($this->_global as $global)
{
$this->_submit->setWidth(12 - $this->getLabelWidth());
$this->_submit->addColumnClass('col-sm-offset-'. $this->_labelWidth);
$$html .= $this->_submit;
if (!$global instanceof Form_Button)
continue;

$global->setWidth(12 - $this->getLabelWidth());
$global->column->addClass('col-sm-offset-'. $this->_labelWidth);
}

$html .= implode('', $this->_global);
Expand Down
8 changes: 6 additions & 2 deletions usr/local/www/classes/Form/Checkbox.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
class Form_Checkbox extends Form_Input
{
protected $_attributes = array(
'class' => array('checkbox' => true),
'class' => array(),
);
protected $_description;

Expand All @@ -42,11 +42,15 @@ public function __construct($name, $title, $description, $checked, $value = 'yes

if ($checked)
$this->_attributes['checked'] = 'checked';

$this->column->addClass('checkbox');
}

public function displayAsRadio()
{
return $this->_attributes['type'] = 'radio';
$this->_attributes['type'] = 'radio';

return $this;
}

protected function _getInput()
Expand Down
15 changes: 10 additions & 5 deletions usr/local/www/classes/Form/Element.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@
POSSIBILITY OF SUCH DAMAGE.
*/

abstract class Form_Element
class Form_Element
{
protected $_tagName;
protected $_tagSelfClosing = false;
protected $_attributes = array(
'class' => array()
);
protected $_tagSelfClosing;
protected $_attributes;
protected $_parent;

public function __construct($tagName = 'div', $selfClose = false, $attributes = array('class' => array()))
{
$this->_tagName = $tagName;
$this->_tagSelfClosing = $selfClose;
$this->_attributes = $attributes;
}

public function addClass()
{
foreach (func_get_args() as $class)
Expand Down
37 changes: 9 additions & 28 deletions usr/local/www/classes/Form/Input.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/
class Form_Input extends Form_Element
{
public $column;
protected $_tagName = 'input';
protected $_tagSelfClosing = true;
protected $_attributes = array(
Expand All @@ -40,10 +41,11 @@ class Form_Input extends Form_Element
protected $_help;
protected $_helpParams = array();
protected $_columnWidth;
protected $_columnClasses = array();

public function __construct($name, $title, $type = 'text', $value = null, array $attributes = array())
{
$this->column = new Form_Element;

$this->_attributes['name'] = $name;
$this->_attributes['id'] = $name;
$this->_title = $title;
Expand Down Expand Up @@ -86,37 +88,15 @@ public function setWidth($size)
if ($size < 1 || $size > 12)
throw new Exception('Incorrect size, pass a number between 1 and 12');

$this->removeColumnClass('col-sm-'. $this->_columnWidth);
$this->column->removeClass('col-sm-'. $this->_columnWidth);

$this->_columnWidth = (int)$size;

$this->addColumnClass('col-sm-'. $this->_columnWidth);

return $this;
}

public function addColumnClass($class)
{
$this->_columnClasses[$class] = true;
$this->column->addClass('col-sm-'. $this->_columnWidth);

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)).'"';
}

public function setReadonly()
{
$this->_attributes['readonly'] = 'readonly';
Expand All @@ -141,9 +121,10 @@ protected function _getInput()
public function __toString()
{
$input = $this->_getInput();
$column = (string)$this->column;

// No classes => no element. This is useful for global inputs
if (!isset($this->_help) && empty($this->_columnClasses))
// Don't add an empty <div>, skip it instead
if (!isset($this->_help) && '<div>' == $column)
return (string)$input;

if (isset($this->_help))
Expand All @@ -158,7 +139,7 @@ public function __toString()
}

return <<<EOT
<div {$this->getColumnHtmlClass()}>
{$column}
{$input}
{$help}
Expand Down

0 comments on commit 5fd8513

Please sign in to comment.