Skip to content

Commit

Permalink
Merge branch 'psophis-form-element' into bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
SjonHortensius committed Mar 28, 2015
2 parents 214a9c3 + df3b00b commit 6f256fe
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 84 deletions.
15 changes: 10 additions & 5 deletions usr/local/www/classes/Form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,18 @@ class Form extends Form_Element
protected $_sections = array();
protected $_global = array();
protected $_labelWidth = 2;
// Empty is interpreted by all browsers to submit to the current URI
protected $_action;

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 @@ -66,7 +69,9 @@ public function setLabelWidth($size)

public function setAction($uri)
{
$this->_action = $uri;
$this->setAttribute('action', $uri);

return $this;
}

public function getLabelWidth()
Expand Down Expand Up @@ -100,9 +105,9 @@ public function __toString()
$html .= implode('', $this->_global);

return <<<EOT
<form class="form-horizontal" action="{$this->_action}" method="post">
<form {$this->getHtmlAttribute()}>
{$html}
</form>
EOT;
}
}
}
20 changes: 6 additions & 14 deletions usr/local/www/classes/Form/Button.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,14 @@
*/
class Form_Button extends Form_Input
{
protected $_link;

public function __construct($name, $title, $link = null)
{
// If we have a link; we're actually an <a class='btn'>
if (isset($link))
{
$this->_link = $link;
if (isset($link)) {
$this->setAttribute('href', htmlspecialchars($link));
$this->addClass('btn-default');
$type = null;
}
else
{
} else {
$this->addClass('btn-primary');
$this->setAttribute('value', $title);
$type = 'submit';
Expand All @@ -53,17 +48,14 @@ public function __construct($name, $title, $link = null)

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

$element = preg_replace('~^<input(.*)/>$~', 'a\1', parent::_getInput());
$link = htmlspecialchars($this->_link);
$title = htmlspecialchars($this->_title);

return <<<EOT
<{$element} href="{$link}">
{$title}
</a>
<{$element}>{$this->_title}</a>
EOT;
}
}
7 changes: 4 additions & 3 deletions usr/local/www/classes/Form/Checkbox.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ public function __construct($name, $title, $description, $checked, $value = 'yes
$this->removeClass('form-control');
$this->addColumnClass('checkbox');

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

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

return '<label>'. $input .' '. gettext($this->_description) .'</label>';
}
}
}
49 changes: 39 additions & 10 deletions usr/local/www/classes/Form/Element.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,65 @@

class Form_Element
{
protected $_classes = array();
protected $_attributes = array('class' => array());
protected $_parent;

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

return $this;
}

public function removeClass($class)
{
unset($this->_classes[$class]);
unset($this->_attributes['class'][$class]);

return $this;
}

public function getHtmlClass($wrapped = true)
public function getClasses()
{
if (empty($this->_classes))
return '';
return implode(' ', array_keys($this->getAttribute('class')));
}

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

$list = implode(' ', array_keys($this->_classes));
return $this;
}

if (!$wrapped)
return $list;
public function getAttribute($name)
{
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) . '"' : '');
}

return 'class="'. $list .'"';
return $attributes;
}

protected function _setParent(Form_Element $parent)
Expand Down
5 changes: 2 additions & 3 deletions usr/local/www/classes/Form/Group.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,15 @@ public function __toString()
foreach ($missingWidth as $input)
$input->setWidth($spaceLeft / count($missingWidth));

$target = $this->_labelTarget->getName();
$target = $this->_labelTarget->getAttribute('name');
$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->getHtmlClass()}>
<div {$this->getHtmlAttribute()}>
<label for="{$target}" class="col-sm-{$this->getLabelWidth()} control-label">
{$this->_title}
</label>
{$inputs}
{$help}
</div>
Expand Down
49 changes: 17 additions & 32 deletions usr/local/www/classes/Form/Input.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,29 @@
class Form_Input extends Form_Element
{
protected $_title;
protected $_attributes = array();
protected $_help;
protected $_helpParams = array();
protected $_columnWidth;
protected $_columnClasses = array();

public function __construct($name, $title, $type = 'text', $value = null, array $attributes = array())
{
$this->_attributes['name'] = $name;
$this->_attributes['id'] = $name;
$this->_title = $title;
$this->setAttribute('name', $name);
$this->setAttribute('id', $name);
$this->_title = htmlspecialchars($title);
$this->addClass('form-control');

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

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

foreach($attributes as $name => $value) {
$this->setAttribute($name, $value);
}

return $this;
}
Expand All @@ -56,11 +61,6 @@ public function getTitle()
return $this->_title;
}

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

public function setHelp($help, array $params = array())
{
$this->_help = $help;
Expand All @@ -76,8 +76,9 @@ public function getWidth()

public function setWidth($size)
{
if ($size < 1 || $size > 12)
if ($size < 1 || $size > 12) {
throw new Exception('Incorrect size, pass a number between 1 and 12');
}

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

Expand All @@ -88,13 +89,6 @@ public function setWidth($size)
return $this;
}

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

return $this;
}

public function addColumnClass($class)
{
$this->_columnClasses[$class] = true;
Expand All @@ -119,22 +113,14 @@ public function getColumnHtmlClass()

protected function _getInput()
{
$html = '<input';

foreach ($this->_attributes as $key => $value)
$html .= ' '.$key. (isset($value) ? '="'. htmlspecialchars($value).'"' : '');

return $html .'/>';
return "<input{$this->getHtmlAttribute()}/>";
}

public function __toString()
{
$this->_attributes['class'] = $this->getHtmlClass(false);

$input = $this->_getInput();

if (isset($this->_help))
{
if (isset($this->_help)) {
$help = gettext($this->_help);

if (!empty($this->_helpParams))
Expand All @@ -153,7 +139,6 @@ public function __toString()
return <<<EOT
<div {$this->getColumnHtmlClass()}>
{$input}
{$help}
</div>
EOT;
Expand Down
7 changes: 4 additions & 3 deletions usr/local/www/classes/Form/Section.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ public function __toString()
$body = implode('', $this->_groups);

return <<<EOT
<div {$this->getHtmlClass()}>
<div {$this->getHtmlAttribute()}>
<div class="panel-heading">
<h2 class="panel-title">{$title}</h2>
</div>
<div class="panel-body">{$body}</div>
<div class="panel-body">
{$body}
</div>
</div>
EOT;
}
Expand Down
22 changes: 8 additions & 14 deletions usr/local/www/classes/Form/Select.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ class Form_Select extends Form_Input

public function __construct($name, $title, $value, array $values, $allowMultiple = false)
{
parent::__construct($name, $title, null);

if ($allowMultiple)
{
$this->_attributes['multiple'] = 'multiple';
$this->_attributes['name'] .= '[]';
if ($allowMultiple) {
$this->setAttribute('multiple', 'multiple');
$name = $name . '[]';
}

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

$this->_value = $value;
$this->_values = $values;
}
Expand All @@ -51,13 +50,8 @@ protected function _getInput()
$element = preg_replace('~^<input(.*)/>$~', 'select\1', parent::_getInput());

$options = '';
foreach ($this->_values as $value => $name)
{
if (isset($this->_attributes['multiple']))
$selected = in_array($value, (array)$this->_value);
else
$selected = ($this->_value == $value);

foreach ($this->_values as $value => $name) {
$selected = (is_array($this->_value) && array_key_exists($value, $this->_value) || $this->_value == $value);
$options .= '<option value="'. htmlspecialchars($value) .'"'.($selected ? ' selected' : '').'>'. gettext($name) .'</option>';
}

Expand All @@ -67,4 +61,4 @@ protected function _getInput()
</select>
EOT;
}
}
}

0 comments on commit 6f256fe

Please sign in to comment.