Skip to content

Commit

Permalink
Add columns to optionset fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Uncle Cheese committed Sep 8, 2014
1 parent a95a572 commit 2a769df
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 4 deletions.
97 changes: 97 additions & 0 deletions code/BootstrapOptionsetField.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@
*/
class BootstrapOptionsetField extends BootstrapFormField {

/**
* An array of column_name => span_length pairs.
* @example
* <code>
* array (
* 'xs' => 12,
* 'sm' => 12,
* 'md' => 6,
* 'lg' => 3
* )
* </code>
* @var array
*/
protected $columnCounts = array ();


/**
* Number of columns in the options layout
* @var int
*/
protected $numberOfColumns;


/**
* Enable or disable "inline" presentation, in which
Expand All @@ -22,4 +44,79 @@ public function setInline($bool = true) {
$this->owner->Inline = $bool;
return $this->owner;
}


/**
* Sets the column layout for the options
* @param array $cols An array of column_name => span_length pairs
* @see $columnCounts
*
* @return OptionsetField
*/
public function setColumns($cols) {
if(!is_array($cols)) {
throw new Exception("BootstrapOptionsetField::setColumns must be passed an array.");
}

$allowed_keys = array('lg','md','sm','xs');
$diff = array_diff($allowed_keys, array_keys($cols));
if(!empty($diff)) {
throw new Exception("BootstrapOptionsetField::setColumns must be passed an array with keys " . implode(', ', $allowed_keys));
}

$this->columnCounts = $cols;

$maxSpan = max(array_values($this->columnCounts));
$minSpan = min(array_values($this->columnCounts));
$this->numberOfColumns = ceil($maxSpan/$minSpan);

return $this->owner;
}


/**
* Tells whether this option set is using a columnar layout
*
* @return boolean
*/
public function HasColumns() {
return !empty($this->columnCounts);
}


/**
* Number of columns in the layout
*
* @return int
*/
public function ColumnCount() {
return $this->numberOfColumns;
}


/**
* A list of classes for the column divs
*
* @return string
*/
public function ColumnClasses() {
$classes = array();
foreach($this->columnCounts as $colName => $val) {
$classes[] = "col-{$colName}-{$val}";
}

return implode(" ", $classes);
}


/**
* Number of options per column
*
* @return int
*/
public function PerColumn() {
return ceil(count($this->owner->getSource())/$this->numberOfColumns);
}


}
24 changes: 20 additions & 4 deletions templates/BootstrapCheckboxSetField.ss
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
<% if $Options.Count %>
<% if $HasColumns %>
<div class="row">
<div class="$ColumnClasses">
<% loop $Options %>
<div class="checkbox <% if $Up.Inline %>inline<% end_if %>">
<label>
<input id="$ID" class="checkbox" name="$Name" type="checkbox" value="$Value"<% if $isChecked %> checked="checked"<% end_if %><% if $isDisabled %> disabled="disabled"<% end_if %>>
$Title
</label>
</div>
<% if $Up.HasColumns && $MultipleOf($Up.PerColumn) %></div><div class="$Up.ColumnClasses"><% end_if %>
<% end_loop %>
</div>
</div>
<% else %>
<% loop $Options %>
<div class="checkbox <% if $Top.Inline %>inline<% end_if %>">
<div class="checkbox <% if $Up.Inline %>inline<% end_if %>">
<label>
<input id="$ID" class="checkbox" name="$Name" type="checkbox" value="$Value"<% if $isChecked %> checked="checked"<% end_if %><% if $isDisabled %> disabled="disabled"<% end_if %>>
$Title
</label>
</div>
</div>
<% end_loop %>
<% else %>
<li>No options available</li>
<% end_if %>
<% else %>
<li>No options available</li>
<% end_if %>

0 comments on commit 2a769df

Please sign in to comment.