Skip to content

Commit

Permalink
Merge pull request #28 from contao-bootstrap/feature/variable-width
Browse files Browse the repository at this point in the history
Support auto grid size
  • Loading branch information
dmolineus committed Apr 18, 2019
2 parents 83715a4 + 5d48053 commit a680e40
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 13 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -20,6 +20,7 @@
"require": {
"php": ">=7.1",
"ext-dom": "*",
"ext-pdo": "*",
"contao-bootstrap/core": "^2.0",
"contao-community-alliance/meta-palettes": "^2.0 || ^1.11",
"contao/core-bundle": "^4.4",
Expand Down
18 changes: 17 additions & 1 deletion src/Definition/Column.php
Expand Up @@ -88,6 +88,20 @@ public function width(int $width): self
return $this;
}

/**
* Force variable width of column.
*
* @return Column
*
* @see https://getbootstrap.com/docs/4.3/layout/grid/#variable-width-content
*/
public function variableWidth(): self
{
$this->width = 'auto';

return $this;
}

/**
* Set the flex order.
*
Expand Down Expand Up @@ -168,7 +182,9 @@ public function build(array $classes, string $size = ''): array
{
$sizeSuffix = $size ? '-' . $size : $size;

if ($this->width === null || $this->width > 0) {
if ($this->width === 'auto') {
$classes[] = 'col' . $sizeSuffix . '-auto';
} elseif ($this->width === null || $this->width > 0) {
$widthSuffix = ($this->width > 0) ? '-' . $this->width : '';
$classes[] = 'col' . $sizeSuffix . $widthSuffix;
} elseif ($size) {
Expand Down
40 changes: 29 additions & 11 deletions src/GridBuilder.php
Expand Up @@ -152,17 +152,8 @@ private function buildSize(string $size, array $definition): void
private function buildColumn(array $definition): Column
{
$column = new Column();
if ($definition['width']) {
switch ($definition['width']) {
case 'auto':
break;
case 'null':
$column->width(0);
break;
default:
$column->width((int) $definition['width']);
}
}

$this->buildColumnWidth($definition, $column);

if ($definition['order']) {
$column->order((int) $definition['order']);
Expand Down Expand Up @@ -219,4 +210,31 @@ private function parseOffset($offset)

return $offset;
}

/**
* Build the column width.
*
* @param array $definition The grid column definition.
* @param Column $column The column.
*
* @return void
*/
private function buildColumnWidth(array $definition, Column $column): void
{
if ($definition['width']) {
switch ($definition['width']) {
case 'variable':
$column->variableWidth();
break;
case 'auto':
case 'equal':
break;
case 'null':
$column->width(0);
break;
default:
$column->width((int) $definition['width']);
}
}
}
}
2 changes: 1 addition & 1 deletion src/Listener/Dca/GridListener.php
Expand Up @@ -108,7 +108,7 @@ public function generateLabel(array $row): string
public function getWidths(): array
{
$columns = $this->getColumns();
$values = ['auto', 'null'];
$values = ['equal', 'variable', 'null'];
$values = array_merge($values, range(1, $columns));

return array_combine($values, $values);
Expand Down
109 changes: 109 additions & 0 deletions src/Migration/MigrateAutoGridWidths.php
@@ -0,0 +1,109 @@
<?php

/**
* Contao Bootstrap grid.
*
* @package contao-bootstrap
* @subpackage Grid
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2017-2019 netzmacht David Molineus. All rights reserved.
* @license https://github.com/contao-bootstrap/grid/blob/master/LICENSE LGPL 3.0-or-later
* @filesource
*/

declare(strict_types=1);

namespace ContaoBootstrap\Grid\Migration;

use Contao\StringUtil;
use Doctrine\DBAL\Connection;
use PDO;
use function array_map;
use function serialize;
use function time;

/**
* Migrate the auto grid widths to equal.
*/
final class MigrateAutoGridWidths
{
private const SIZES = ['xs', 'sm', 'md', 'lg', 'xl'];

/**
* Database connection.
*
* @var Connection
*/
private $connection;

/**
* MigrateAutoGridWidths constructor.
*
* @param Connection $connection Database connection.
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
}

/**
* Invoke the migration script.
*
* @return void
*/
public function __invoke(): void
{
$statement = $this->connection->executeQuery('SELECT * FROM tl_bs_grid');

while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
$this->migrateRow($row);
}
}

/**
* Migrate a grid definition row.
*
* @param array $row The grid definition row.
*
* @return void
*/
private function migrateRow(array $row): void
{
$data = ['tstamp' => time()];

foreach (self::SIZES as $size) {
$size .= 'Size';
$data[$size] = $this->migrateSize($row[$size]);
}

$this->connection->update('tl_bs_grid', $data, ['id' => $row['id']]);
}

/**
* Migrate a grid size.
*
* @param string|null $size The grid size definition.
*
* @return string|null
*/
private function migrateSize(?string $size): ?string
{
if ($size === null) {
return null;
}


$columns = array_map(
function (array $column) {
if ($column['width'] === 'auto') {
$column['width'] = 'equal';
}

return $column;
},
StringUtil::deserialize($size, true)
);

return serialize($columns);
}
}
5 changes: 5 additions & 0 deletions src/Resources/config/services.yml
Expand Up @@ -53,3 +53,8 @@ services:
contao_bootstrap.grid.response_tagger:
alias: netzmacht.contao_toolkit.response_tagger
public: true

ContaoBootstrap\Grid\Migration\MigrateAutoGridWidths:
public: true
arguments:
- '@database_connection'
18 changes: 18 additions & 0 deletions src/Resources/contao/config/runonce.php
@@ -0,0 +1,18 @@
<?php

/**
* Contao Bootstrap grid.
*
* @package contao-bootstrap
* @subpackage Grid
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2017-2019 netzmacht David Molineus. All rights reserved.
* @license https://github.com/contao-bootstrap/grid/blob/master/LICENSE LGPL 3.0-or-later
* @filesource
*/

use ContaoBootstrap\Grid\Migration\MigrateAutoGridWidths;

(function () {
\Contao\System::getContainer()->get(MigrateAutoGridWidths::class)();
})();

0 comments on commit a680e40

Please sign in to comment.