Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from contao-bootstrap/feature/variable-width
Support auto grid size
- Loading branch information
Showing
7 changed files
with
180 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)(); | ||
})(); |