From 2a769df46d1f1fd93be4a8b35e78124cb59e75b5 Mon Sep 17 00:00:00 2001 From: Uncle Cheese Date: Mon, 8 Sep 2014 12:20:37 +1200 Subject: [PATCH] Add columns to optionset fields --- code/BootstrapOptionsetField.php | 97 ++++++++++++++++++++++++++ templates/BootstrapCheckboxSetField.ss | 24 +++++-- 2 files changed, 117 insertions(+), 4 deletions(-) diff --git a/code/BootstrapOptionsetField.php b/code/BootstrapOptionsetField.php index 938ed1c..f16eac4 100755 --- a/code/BootstrapOptionsetField.php +++ b/code/BootstrapOptionsetField.php @@ -10,6 +10,28 @@ */ class BootstrapOptionsetField extends BootstrapFormField { + /** + * An array of column_name => span_length pairs. + * @example + * + * array ( + * 'xs' => 12, + * 'sm' => 12, + * 'md' => 6, + * 'lg' => 3 + * ) + * + * @var array + */ + protected $columnCounts = array (); + + + /** + * Number of columns in the options layout + * @var int + */ + protected $numberOfColumns; + /** * Enable or disable "inline" presentation, in which @@ -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); + } + + } \ No newline at end of file diff --git a/templates/BootstrapCheckboxSetField.ss b/templates/BootstrapCheckboxSetField.ss index 25338d8..978edbb 100755 --- a/templates/BootstrapCheckboxSetField.ss +++ b/templates/BootstrapCheckboxSetField.ss @@ -1,12 +1,28 @@ <% if $Options.Count %> + <% if $HasColumns %> +
+
+ <% loop $Options %> +
+ +
+ <% if $Up.HasColumns && $MultipleOf($Up.PerColumn) %>
<% end_if %> + <% end_loop %> +
+
+ <% else %> <% loop $Options %> -
+
-
+
<% end_loop %> - <% else %> -
  • No options available
  • + <% end_if %> + <% else %> +
  • No options available
  • <% end_if %>