Skip to content

Commit

Permalink
Make block options multi-shop compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-combet committed Aug 2, 2023
1 parent 66f9a8d commit 567d4f0
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 70 deletions.
4 changes: 3 additions & 1 deletion config/services/admin/domain/block.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ services:
- name: tactician.handler
command: Kaudaj\Module\Blocks\Domain\Block\Query\GetBlocks

kaudaj.module.blocks.block.query_handler.get_block_for_editing:
kaudaj.module.blocks.block.query_handler.get_for_editing:
public: true
parent: kaudaj.module.blocks.block.query_handler.abstract
class: Kaudaj\Module\Blocks\Domain\Block\QueryHandler\GetBlockForEditingHandler
arguments:
- '@kaudaj.module.blocks.block_context'
tags:
- name: tactician.handler
command: Kaudaj\Module\Blocks\Domain\Block\Query\GetBlockForEditing
Expand Down
1 change: 1 addition & 0 deletions config/services/common/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:
class: Kaudaj\Module\Blocks\BlockTypeProviderFactory
arguments:
- '@=service("prestashop.adapter.legacy.context").getContext().language.id'
- '@kaudaj.module.blocks.block_context'

kaudaj.module.blocks.block_type_provider:
class: Kaudaj\Module\Blocks\BlockTypeProvider
Expand Down
9 changes: 7 additions & 2 deletions kjblocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ private function installTables()
$sql[] = "
CREATE TABLE IF NOT EXISTS `$dbPrefix" . BlockRepository::TABLE_NAME . "` (
`id_block` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`type` VARCHAR(255) NOT NULL,
`options` JSON
`type` VARCHAR(255) NOT NULL
) ENGINE=$dbEngine COLLATE=utf8mb4_general_ci;
";

Expand All @@ -157,6 +156,7 @@ private function installTables()
`id_shop` INT,
`id_shop_group` INT,
`active` BOOLEAN NOT NULL,
`options` JSON,
FOREIGN KEY (`id_block`)
REFERENCES `$dbPrefix" . BlockRepository::TABLE_NAME . "` (`id_block`)
ON DELETE CASCADE,
Expand Down Expand Up @@ -389,4 +389,9 @@ private function getService(string $backService, string $frontService)

return $object;
}

public function getContextCacheId(): string
{
return str_replace($this->name . '|', '', $this->getCacheId());
}
}
24 changes: 7 additions & 17 deletions src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,16 @@ public function render(): string
throw new \RuntimeException("Module {$this->getModuleName()} not found");
}

/** @var \KJBlocks|false */
$kjblocks = \Module::getInstanceByName('kjblocks');
if (!$kjblocks) {
throw new \RuntimeException('Module kjblocks not found');
}

$smarty = $this->legacyContext->getSmarty();

$template = $this->getTemplate();
$cacheId = $this->getCacheId() . $this->getContextCacheId();
$cacheId = $this->getCacheId() . $kjblocks->getContextCacheId();
$isCached = $module->isCached($template, $cacheId);

if (!$isCached) {
Expand Down Expand Up @@ -133,22 +139,6 @@ protected function getCacheId(): string
return "{$this->getModuleName()}|{$this->id}";
}

public static function getContextCacheId(): string
{
$context = \Context::getContext();
if (!$context || !$context->language) {
return '';
}

$cacheId = "|{$context->language->id}";

if ($context->currency) {
$cacheId .= "|{$context->currency->id}";
}

return $cacheId;
}

public function setOptions(array $options = []): void
{
if (key_exists(self::OPTION_ID, $options)) {
Expand Down
11 changes: 9 additions & 2 deletions src/BlockTypeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ class BlockTypeProvider
*/
protected $contextLangId;

public function __construct(int $contextLangId, string $hookName)
/**
* @var BlockContext
*/
protected $blockContext;

public function __construct(int $contextLangId, BlockContext $blockContext, string $hookName)
{
$this->contextLangId = $contextLangId;
$this->blockContext = $blockContext;
$this->hookName = $hookName;
}

Expand Down Expand Up @@ -206,9 +212,10 @@ public function configureOptions($blockType, array $options = []): array
public function getBlockTypeFromEntity(Block $block)
{
$id = $block->getId();
$blockShop = $this->blockContext->getBlockShop($block);

if (!key_exists($id, $this->blocks)) {
$options = json_decode($block->getOptions() ?: '', true) ?: [];
$options = $blockShop ? json_decode($blockShop->getOptions() ?: '', true) : [];
$options = is_array($options) ? $options : [];

$this->blocks[$id] = $this->getBlockType($block->getType(), $options + [
Expand Down
10 changes: 8 additions & 2 deletions src/BlockTypeProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,20 @@ class BlockTypeProviderFactory
*/
protected $contextLangId;

public function __construct(int $contextLangId)
/**
* @var BlockContext
*/
protected $blockContext;

public function __construct(int $contextLangId, BlockContext $blockContext)
{
$this->contextLangId = $contextLangId;
$this->blockContext = $blockContext;
}

// @phpstan-ignore-next-line
public function create(string $hookName): BlockTypeProvider
{
return new BlockTypeProvider($this->contextLangId, $hookName);
return new BlockTypeProvider($this->contextLangId, $this->blockContext, $hookName);
}
}
12 changes: 6 additions & 6 deletions src/Domain/Block/CommandHandler/AddBlockHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public function handle(AddBlockCommand $command): BlockId
$block = new Block();

$block->setType($command->getType());
$block->setOptions($command->getOptions() !== null ? $command->getOptions()->getValue() : null);

$blockGroupRepository = $this->entityManager->getRepository(BlockGroup::class);

Expand Down Expand Up @@ -73,13 +72,14 @@ public function handle(AddBlockCommand $command): BlockId
$block->addBlockLang($configuratorBlockLang);
}

// $shopConstraint = $command->getShopConstraint();
// $shopId = $shopConstraint->getShopId() !== null ? $shopConstraint->getShopId()->getValue() : null;
// $shopGroupId = $shopConstraint->getShopGroupId() !== null ? $shopConstraint->getShopGroupId()->getValue() : null;
$shopConstraint = $command->getShopConstraint();
$shopId = $shopConstraint->getShopId() !== null ? $shopConstraint->getShopId()->getValue() : null;
$shopGroupId = $shopConstraint->getShopGroupId() !== null ? $shopConstraint->getShopGroupId()->getValue() : null;

// $blockShop = $this->createBlockShop($shopId, $shopGroupId);
$blockShop = $this->createBlockShop($shopId, $shopGroupId);
$blockShop->setOptions($command->getOptions() !== null ? $command->getOptions()->getValue() : null);

// $block->addBlockShop($blockShop);
$block->addBlockShop($blockShop);

$this->entityManager->persist($block);
$this->entityManager->flush();
Expand Down
29 changes: 16 additions & 13 deletions src/Domain/Block/CommandHandler/EditBlockHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ public function handle(EditBlockCommand $command): void
$block->setType($command->getType());
}

if ($command->getOptions() !== null) {
$block->setOptions($command->getOptions()->getValue());
}

$blockGroupsIds = $command->getBlockGroupsIds();
if ($blockGroupsIds) {
$blockGroupsIds = array_map(function (BlockGroupId $blockGroupId): int {
Expand All @@ -68,10 +64,10 @@ public function handle(EditBlockCommand $command): void
$localizedNames = $command->getLocalizedNames();

if (null !== $localizedNames) {
foreach ($block->getBlockLangs() as $blockLangs) {
$block->removeBlockLang($blockLangs);
foreach ($block->getBlockLangs() as $blockLang) {
$block->removeBlockLang($blockLang);

$this->entityManager->remove($blockLangs);
$this->entityManager->remove($blockLang);
}

$this->entityManager->flush();
Expand All @@ -84,15 +80,22 @@ public function handle(EditBlockCommand $command): void
}
}

// $shopConstraint = $command->getShopConstraint();
// $shopId = $shopConstraint->getShopId() !== null ? $shopConstraint->getShopId()->getValue() : null;
// $shopGroupId = $shopConstraint->getShopGroupId() !== null ? $shopConstraint->getShopGroupId()->getValue() : null;
$shopConstraint = $command->getShopConstraint();
$shopId = $shopConstraint->getShopId() !== null ? $shopConstraint->getShopId()->getValue() : null;
$shopGroupId = $shopConstraint->getShopGroupId() !== null ? $shopConstraint->getShopGroupId()->getValue() : null;

// $block->removeBlockShop($block->getBlockShop($shopId, $shopGroupId));
$blockShop = $block->getBlockShop($shopId, $shopGroupId);
if ($blockShop) {
$block->removeBlockShop($blockShop);
$this->entityManager->remove($blockShop);
}

$this->entityManager->flush();

// $blockShop = $this->createBlockShop($shopId, $shopGroupId);
$blockShop = $this->createBlockShop($shopId, $shopGroupId);
$blockShop->setOptions($command->getOptions() !== null ? $command->getOptions()->getValue() : null);

// $block->addBlockShop($blockShop);
$block->addBlockShop($blockShop);

$this->entityManager->persist($block);
$this->entityManager->flush();
Expand Down
29 changes: 21 additions & 8 deletions src/Domain/Block/QueryHandler/GetBlockForEditingHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

namespace Kaudaj\Module\Blocks\Domain\Block\QueryHandler;

use Doctrine\ORM\EntityManager;
use Kaudaj\Module\Blocks\BlockContext;
use Kaudaj\Module\Blocks\Domain\Block\Exception\BlockException;
use Kaudaj\Module\Blocks\Domain\Block\Query\GetBlockForEditing;
use Kaudaj\Module\Blocks\Domain\Block\QueryResult\EditableBlock;
Expand All @@ -32,12 +34,24 @@
*/
final class GetBlockForEditingHandler extends AbstractBlockQueryHandler
{
/**
* @var BlockContext
*/
protected $blockContext;

public function __construct(EntityManager $entityManager, BlockContext $blockContext)
{
parent::__construct($entityManager);

$this->blockContext = $blockContext;
}

public function handle(GetBlockForEditing $query): EditableBlock
{
try {
$block = $this->getBlockEntity(
$query->getBlockId()->getValue()
);
$blockId = $query->getBlockId()->getValue();
$block = $this->getBlockEntity($blockId);
$blockShop = $this->blockContext->getBlockShop($block);

$localizedNames = [];
foreach ($block->getBlockLangs() as $blockLang) {
Expand All @@ -50,22 +64,21 @@ public function handle(GetBlockForEditing $query): EditableBlock
}

$editableBlock = new EditableBlock(
$block->getId(),
$blockId,
$block->getType(),
$block->getOptions(),
$blockShop ? $blockShop->getOptions() : null,
$blockGroupsIds,
$localizedNames
);
} catch (\Exception $e) {
$message = sprintf(
'An unexpected error occurred when retrieving block with id %s',
var_export($query->getBlockId()->getValue(), true)
var_export($blockId, true)
);

throw new BlockException($message, 0, $e);
}

return $editableBlock
;
return $editableBlock;
}
}
18 changes: 2 additions & 16 deletions src/Entity/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,10 @@ class Block
*/
private $type;

/**
* @var string|null
*
* @ORM\Column(type="json", nullable=true)
*/
private $options;

public function __construct()
{
$this->blockLangs = new ArrayCollection();
$this->blockShops = new ArrayCollection();
$this->blockGroups = new ArrayCollection();
}

Expand Down Expand Up @@ -209,15 +203,7 @@ public function setType(string $type): self
return $this;
}

public function getOptions(): ?string
public function getOptions(): void
{
return $this->options;
}

public function setOptions(?string $options): self
{
$this->options = $options;

return $this;
}
}
19 changes: 19 additions & 0 deletions src/Entity/BlockShop.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ class BlockShop
*/
protected $active = true;

/**
* @var string|null
*
* @ORM\Column(type="json", nullable=true)
*/
private $options;

public function getId(): int
{
return $this->id;
Expand Down Expand Up @@ -122,4 +129,16 @@ public function setActive(bool $active): self

return $this;
}

public function getOptions(): ?string
{
return $this->options;
}

public function setOptions(?string $options): self
{
$this->options = $options;

return $this;
}
}
3 changes: 0 additions & 3 deletions tests/php/phpstan_1.7.8.9.dist.neon
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
parameters:
ignoreErrors:
-
message: '#^Negated boolean expression is always false.$#'
path: %currentWorkingDirectory%/src/Block.php
-
message: '#^Parameter \#1 \$characterCleaner of class PrestaShop\\PrestaShop\\Core\\ConstraintValidator\\TypedRegexValidator constructor expects PrestaShop\\PrestaShop\\Core\\String\\CharacterCleaner, PrestaShop\\PrestaShop\\Adapter\\Configuration given\.$#'
path: %currentWorkingDirectory%/src/Constraint/ConstraintValidatorFactory.php
Expand Down

0 comments on commit 567d4f0

Please sign in to comment.