Skip to content

Commit

Permalink
Added command to convert old accordion blocks for new Thelia Blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Neox63 authored and Lucanis committed Sep 5, 2022
1 parent 47b8de5 commit 3cdddfa
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions Command/ValidateJsonContent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace TheliaBlocks\Command;

use Propel\Runtime\ActiveQuery\ModelCriteria;
use Propel\Runtime\ActiveRecord\ActiveRecordInterface;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\File;
use Thelia\Command\ContainerAwareCommand;
use Thelia\Model\ConfigQuery;
use Thelia\Model\FolderImage;
use Thelia\Model\LangQuery;
use TheliaBlocks\Model\BlockGroupI18nQuery;

class ValidateJsonContent extends ContainerAwareCommand
{
public function __construct()
{
parent::__construct();
}

protected function configure(): void
{
$this
->setName('thelia_blocks:blocks:validate')
->setDescription('Validate & update json_content structure');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$jsonContents = BlockGroupI18nQuery::create()->find();


$output->writeln('<bg=blue>============================================================= </>');
$output->writeln("<fg=blue>Thelia blocks json_content validation started</>");
$progressBar = new ProgressBar($output, $jsonContents->count());
$progressBar->start();

foreach ($jsonContents as $jsonContent) {
$content = $jsonContent->getJsonContent();

$decodedJson = json_decode($content, true);

foreach ($decodedJson as $key => $value) {
$output->writeln("-");
$output->writeln($value["type"]["id"]);

if ($value["type"]["id"] === "blockAccordion") {
$oldData = $decodedJson[$key]["data"];

$group = array_map(function ($item) {
if (isset($item["group"])) {
$item["content"] = [];
$item["content"][] = $item["group"];

unset($item["group"]);

$item["content"][0]["data"] = ["content" => [$item["content"][0]["data"]]];
}

return $item;
}, $oldData);

$decodedJson[$key]["data"] = [
"title" => "",
"group" => $group
];
}
}


$jsonContent->setJsonContent(json_encode($decodedJson));

$jsonContent->save();

$progressBar->advance();
}

$progressBar->finish();
$output->writeln("");
$output->writeln("<info>Thelia blocks json_content validation ended</info>");
$output->writeln('<bg=blue>============================================================= </>');

return 1;
}
}

0 comments on commit 3cdddfa

Please sign in to comment.