Skip to content

Commit

Permalink
Added the ColumnedGroup class
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Laviale committed Nov 18, 2014
1 parent 5d750d5 commit ed01543
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions lib/ColumnedGroup.php
@@ -0,0 +1,105 @@
<?php

/*
* This file is part of the Brickrouge package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Brickrouge;

/**
* A group that lay its children into columns.
*/
class ColumnedGroup extends Group
{
/**
* Defines the number of columns.
*
* @var int
*/
const COLUMNS = '#columned-group-columns';

/**
* Defines the total number of columns.
*
* @var int
*/
const COLUMNS_TOTAL = '#columned-group-columns-total';

/**
* The instance is constructed with the following defaults:
*
* - {@link COLUMNS}: 3.
* - {@link COLUMNS_TOTAL}: 12.
*
* @param array $attribute
*/
public function __construct(array $attribute=[])
{
parent::__construct($attribute + [

self::COLUMNS => 3,
self::COLUMNS_TOTAL => 12

]);
}

/**
* Dispatch children into columns and render them.
*
* @return Element
*/
protected function render_inner_html()
{
$columns = [];
$children = $this->ordered_children;
$columns_n = $this[self::COLUMNS];
$by_column = ceil(count($children) / $columns_n);
$i = 0;

foreach ($children as $child_id => $child)
{
$column_i = $i++ % $columns_n;
$columns[$column_i][$child_id] = $child;
}

$rendered_columns = [];

foreach ($columns as $column)
{
$rendered_columns[] = $this->render_group_column($column);
}

return new Element('div', [

Element::CHILDREN => $rendered_columns,

'class' => "row"

]);
}

/**
* Render a group column.
*
* @param array $column
*
* @return Element
*/
protected function render_group_column($column)
{
$w = $this[self::COLUMNS_TOTAL] / $this[self::COLUMNS];

return new Element('div', [

Element::INNER_HTML => $this->render_children($column),

'class' => "col-md-$w span-$w"

]);
}
}

0 comments on commit ed01543

Please sign in to comment.