diff --git a/composer.json b/composer.json index 0c98ee6..662aa6d 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Definition/Column.php b/src/Definition/Column.php index daa9698..230d7cc 100644 --- a/src/Definition/Column.php +++ b/src/Definition/Column.php @@ -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. * @@ -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) { diff --git a/src/GridBuilder.php b/src/GridBuilder.php index 51e2746..f2f600c 100644 --- a/src/GridBuilder.php +++ b/src/GridBuilder.php @@ -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']); @@ -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']); + } + } + } } diff --git a/src/Listener/Dca/GridListener.php b/src/Listener/Dca/GridListener.php index 2d5317a..a949a50 100644 --- a/src/Listener/Dca/GridListener.php +++ b/src/Listener/Dca/GridListener.php @@ -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); diff --git a/src/Migration/MigrateAutoGridWidths.php b/src/Migration/MigrateAutoGridWidths.php new file mode 100644 index 0000000..6cdaf21 --- /dev/null +++ b/src/Migration/MigrateAutoGridWidths.php @@ -0,0 +1,109 @@ + + * @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); + } +} diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 207301d..b2b38dd 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -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' diff --git a/src/Resources/contao/config/runonce.php b/src/Resources/contao/config/runonce.php new file mode 100644 index 0000000..caae191 --- /dev/null +++ b/src/Resources/contao/config/runonce.php @@ -0,0 +1,18 @@ + + * @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)(); +})();