Skip to content

Commit

Permalink
refactor attributes from Input to Element to make it a bit more generic
Browse files Browse the repository at this point in the history
* replaced Input::setAttribute with explicit setReadonly/setDisabled
* introduced Input::toggles

fixes #37, refs #21
  • Loading branch information
SjonHortensius committed Mar 28, 2015
1 parent 6f256fe commit 1192840
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 143 deletions.
21 changes: 12 additions & 9 deletions usr/local/www/classes/Form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,23 @@

class Form extends Form_Element
{
protected $_tagName = 'form';
protected $_attributes = array(
'class' => array('form-horizontal' => true),
'method' => 'post',
// Empty is interpreted by all browsers to submit to the current URI
'action' => '',
);
protected $_sections = array();
protected $_global = array();
protected $_labelWidth = 2;

public function __construct()
{
$this->addClass('form-horizontal');
$this->setAttribute('method', 'post');

$this->addGlobal(new Form_Button(
'save',
'Save'
));

return $this;
}

public function add(Form_Section $section)
Expand All @@ -67,9 +69,9 @@ public function setLabelWidth($size)
$this->_labelWidth = (int)$size;
}

public function setAction($uri)
public function setAction($url)
{
$this->setAttribute('action', $uri);
$this->_attributes['action'] = $url;

return $this;
}
Expand All @@ -93,6 +95,7 @@ protected function _setParent()

public function __toString()
{
$element = parent::__toString();
$html = implode('', $this->_sections);

if (isset($this->_submit))
Expand All @@ -105,9 +108,9 @@ public function __toString()
$html .= implode('', $this->_global);

return <<<EOT
<form {$this->getHtmlAttribute()}>
{$element}
{$html}
</form>
EOT;
}
}
}
41 changes: 24 additions & 17 deletions usr/local/www/classes/Form/Button.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,41 @@
*/
class Form_Button extends Form_Input
{
protected $_tagSelfClosing = false;
protected $_attributes = array(
'class' => array(
'btn' => true,
),
'type' => 'submit',
);

public function __construct($name, $title, $link = null)
{
// If we have a link; we're actually an <a class='btn'>
if (isset($link)) {
$this->setAttribute('href', htmlspecialchars($link));
if (isset($link))
{
$this->_attributes['href'] = $link;
$this->_tagName = 'a';
$this->addClass('btn-default');
$type = null;
} else {
unset($this->_attributes['type']);
}
else
{
$this->_tagSelfClosing = true;
$this->_attributes['value'] = $title;
$this->addClass('btn-primary');
$this->setAttribute('value', $title);
$type = 'submit';
}

parent::__construct($name, $title, $type);

$this->removeClass('form-control')->addClass('btn');
parent::__construct($name, $title, null);
}

protected function _getInput()
{
if (empty($this->getAttribute('href'))) {
return parent::_getInput();
}
$input = parent::_getInput();

$element = preg_replace('~^<input(.*)/>$~', 'a\1', parent::_getInput());
if (!isset($this->_attributes['href']))
return $input;

return <<<EOT
<{$element}>{$this->_title}</a>
EOT;
return $input . htmlspecialchars($this->_title) .'</a>';
}
}
}
14 changes: 7 additions & 7 deletions usr/local/www/classes/Form/Checkbox.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@

class Form_Checkbox extends Form_Input
{
protected $_attributes = array(
'class' => array('checkbox' => true),
);
protected $_description;

public function __construct($name, $title, $description, $checked, $value = 'yes')
{
parent::__construct($name, $title, 'checkbox', $value);

$this->_description = $description;
$this->removeClass('form-control');
$this->addColumnClass('checkbox');

if ($checked) {
$this->setAttribute('checked', 'checked');
}
if ($checked)
$this->_attributes['checked'] = 'checked';
}

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

protected function _getInput()
Expand All @@ -58,4 +58,4 @@ protected function _getInput()

return '<label>'. $input .' '. gettext($this->_description) .'</label>';
}
}
}
68 changes: 27 additions & 41 deletions usr/local/www/classes/Form/Element.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@
POSSIBILITY OF SUCH DAMAGE.
*/

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

public function addClass()
{
foreach (func_get_args() as $class) {
foreach (func_get_args() as $class)
$this->_attributes['class'][$class] = true;
}

return $this;
}
Expand All @@ -48,50 +51,33 @@ public function removeClass($class)
return $this;
}

public function getClasses()
{
return implode(' ', array_keys($this->getAttribute('class')));
}

public function setAttribute($key, $value = null)
{
$this->_attributes[$key] = $value;

return $this;
}

public function getAttribute($name)
public function __toString()
{
return $this->_attributes[$name];
}

public function removeAttribute($name)
{
unset($this->_attributes[$name]);

return $this;
}

public function getHtmlAttribute()
{
/* Will overwright _attributes['class'] with string Therefore you cannot
* delete or add classes once getHtmlAttribute() has been called. */
if (empty($this->_attributes['class'])) {
$this->removeAttribute('class');
} else {
$this->_attributes['class'] = $this->getClasses();
}

$attributes = '';
foreach ($this->_attributes as $key => $value) {
$attributes .= ' ' . $key . (isset($value) ? '="' . htmlspecialchars($value) . '"' : '');
foreach ($this->_attributes as $key => $value)
{
if (is_array($value))
{
// Used for classes. If it's empty, we don't want the attribute at all
if (!empty($value))
$value = implode(' ', array_keys($value));
else
$value = null;
}

if ($value === null)
continue;

$attributes .= ' '. $key;
if ($value !== true)
$attributes .= '="' . htmlspecialchars($value) . '"';
}

return $attributes;
return '<'. $this->_tagName . $attributes . ($this->_tagSelfClosing ? '/' : '') .'>';
}

protected function _setParent(Form_Element $parent)
{
$this->_parent = $parent;
}
}
}
15 changes: 9 additions & 6 deletions usr/local/www/classes/Form/Group.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
*/
class Form_Group extends Form_Element
{
protected $_tagName = 'div';
protected $_attributes = array(
'class' => array('form-group' => true),
);
protected $_title;
protected $_inputs = array();
protected $_labelTarget;
Expand All @@ -36,9 +40,6 @@ class Form_Group extends Form_Element
public function __construct($title)
{
$this->_title = gettext($title);
$this->addClass('form-group');

return $this;
}

public function add(Form_Input $input)
Expand Down Expand Up @@ -73,6 +74,8 @@ public function setHelp($help)

public function __toString()
{
$element = parent::__toString();

// Automatically determine width for inputs without explicit set
$spaceLeft = 12 - $this->getLabelWidth();
$missingWidth = array();
Expand All @@ -90,12 +93,12 @@ public function __toString()
foreach ($missingWidth as $input)
$input->setWidth($spaceLeft / count($missingWidth));

$target = $this->_labelTarget->getAttribute('name');
$target = $this->_labelTarget->getName();
$inputs = implode('', $this->_inputs);
$help = isset($this->_help) ? '<div class="col-sm-'. (12 - $this->getLabelWidth()) .' col-sm-offset-'. $this->getLabelWidth() .'"><span class="help-block">'. gettext($this->_help). '</span></div>' : '';

return <<<EOT
<div {$this->getHtmlAttribute()}>
{$element}
<label for="{$target}" class="col-sm-{$this->getLabelWidth()} control-label">
{$this->_title}
</label>
Expand All @@ -104,4 +107,4 @@ public function __toString()
</div>
EOT;
}
}
}
Loading

0 comments on commit 1192840

Please sign in to comment.