Skip to content

Commit 193d8a3

Browse files
Add VC for CMS pages and blocks
1 parent a3947f6 commit 193d8a3

14 files changed

+430
-5
lines changed

Api/ConfigRepositoryInterface.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CtiDigital\Configurator\Api;
6+
7+
use CtiDigital\Configurator\Api\Data\ConfigInterface;
8+
9+
interface ConfigRepositoryInterface
10+
{
11+
/**
12+
* Get config
13+
*
14+
* @param string $name
15+
* @return ConfigInterface|null
16+
*/
17+
public function getConfig(string $name): ?ConfigInterface;
18+
19+
/**
20+
* Save config
21+
*
22+
* @param ConfigInterface $config
23+
* @return int
24+
*/
25+
public function save(ConfigInterface $config): int;
26+
27+
/**
28+
* Delete COnfig
29+
*
30+
* @param ConfigInterface $config
31+
* @return void
32+
*/
33+
public function delete(ConfigInterface $config): void;
34+
}

Api/Data/ConfigInterface.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* This file is part of the CtiDigital_Configurator package.
4+
*
5+
* DISCLAIMER
6+
*
7+
* Do not edit or add to this file if you wish to upgrade CtiDigital_Configurator
8+
* to newer versions in the future.
9+
*
10+
* @copyright Copyright (c) 2023 Magebit, Ltd. (https://magebit.com/)
11+
* @author Magebit <info@magebit.com>
12+
* @license MIT
13+
*/
14+
15+
declare(strict_types=1);
16+
17+
namespace CtiDigital\Configurator\Api\Data;
18+
19+
interface ConfigInterface
20+
{
21+
public const ID = 'config_id';
22+
public const NAME = 'name';
23+
public const VALUE = 'value';
24+
25+
public function getName(): string;
26+
27+
public function setName(string $name): self;
28+
29+
public function getValue(): string;
30+
31+
public function setValue(string $value): self;
32+
}

Api/VersionManagementInterface.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* This file is part of the CtiDigital_Configurator package.
4+
*
5+
* DISCLAIMER
6+
*
7+
* Do not edit or add to this file if you wish to upgrade CtiDigital_Configurator
8+
* to newer versions in the future.
9+
*
10+
* @copyright Copyright (c) 2023 Magebit, Ltd. (https://magebit.com/)
11+
* @author Magebit <info@magebit.com>
12+
* @license MIT
13+
*/
14+
15+
declare(strict_types=1);
16+
17+
namespace CtiDigital\Configurator\Api;
18+
19+
interface VersionManagementInterface
20+
{
21+
/**
22+
* @return int
23+
*/
24+
public function getCurrentVersion(string $id): int;
25+
26+
/**
27+
* @param int $version
28+
* @return void
29+
*/
30+
public function setVersion(string $id, int $version): void;
31+
32+
/**
33+
* @param int $version
34+
* @return bool
35+
*/
36+
public function isNewVersion(string $id, int $version): bool;
37+
}

Component/Blocks.php

+21-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CtiDigital\Configurator\Component;
44

55
use CtiDigital\Configurator\Api\ComponentInterface;
6+
use CtiDigital\Configurator\Api\VersionManagementInterface;
67
use CtiDigital\Configurator\Exception\ComponentException;
78
use CtiDigital\Configurator\Api\LoggerInterface;
89
use Exception;
@@ -38,7 +39,8 @@ public function __construct(
3839
private readonly LoggerInterface $log,
3940
private readonly Filesystem $filesystem,
4041
private readonly ViewModelRegistry $viewModelRegistry,
41-
private readonly Escaper $escaper
42+
private readonly Escaper $escaper,
43+
private readonly VersionManagementInterface $versionManagement
4244
) {
4345
}
4446

@@ -79,6 +81,19 @@ private function processBlock(string $identifier, array $blockData, string $mode
7981
$canSave = false;
8082
$block = null;
8183

84+
$version = $data['version'] ?? null;
85+
$versionId = $this->alias . '_' . $identifier;
86+
87+
if (isset($data['stores'])) {
88+
$versionId .= implode('_', $data['stores']);
89+
}
90+
91+
if ($version) {
92+
unset($data['version']);
93+
}
94+
95+
$isNewVersion = isset($version) && $this->versionManagement->isNewVersion($versionId, (int) $version);
96+
8297
// Check if there are existing blocks
8398
if ($blocks->count()) {
8499
$stores = [];
@@ -97,7 +112,7 @@ private function processBlock(string $identifier, array $blockData, string $mode
97112
$block = $this->blockFactory->create();
98113
$block->setIdentifier($identifier);
99114
$canSave = true;
100-
} elseif ($mode === Processor::MODE_CREATE) {
115+
} elseif ($mode === Processor::MODE_CREATE && !$isNewVersion) {
101116
// In create mode we skip modifying block
102117
$this->log->logComment(sprintf("'%s' Block exists, skip modifying it (create mode)", $identifier));
103118
continue;
@@ -184,6 +199,10 @@ private function processBlock(string $identifier, array $blockData, string $mode
184199
$identifier . ' (' . $block->getId() . ')'
185200
));
186201
}
202+
203+
if ($version) {
204+
$this->versionManagement->setVersion($versionId, (int) $version);
205+
}
187206
}
188207
} catch (ComponentException $e) {
189208
$this->log->logError($e->getMessage());

Component/Pages.php

+22-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use CtiDigital\Configurator\Api\ComponentInterface;
66
use CtiDigital\Configurator\Api\LoggerInterface;
7+
use CtiDigital\Configurator\Api\VersionManagementInterface;
78
use CtiDigital\Configurator\Exception\ComponentException;
89
use Exception;
910
use Hyva\Theme\Model\ViewModelRegistry;
@@ -44,7 +45,8 @@ public function __construct(
4445
private readonly LoggerInterface $log,
4546
private readonly Filesystem $filesystem,
4647
private readonly ViewModelRegistry $viewModelRegistry,
47-
private readonly Escaper $escaper
48+
private readonly Escaper $escaper,
49+
private readonly VersionManagementInterface $versionManagement
4850
) {
4951
}
5052

@@ -89,9 +91,22 @@ protected function processPage(string $identifier, array $data, string $mode): v
8991
} else {
9092
$pageId = $this->pageFactory->create()->checkIdentifier($identifier, 0);
9193
}
94+
95+
$version = $pageData['version'] ?? null;
96+
$versionId = $this->alias . '_' . $identifier;
97+
98+
if (isset($pageData['stores'])) {
99+
$versionId .= implode('_', $pageData['stores']);
100+
}
101+
102+
if ($version) {
103+
unset($pageData['version']);
104+
}
105+
92106
/** @var PageInterface $page */
93107
if ($pageId) {
94-
if ($mode === 'create') {
108+
$isNewVersion = $version && $this->versionManagement->isNewVersion($versionId, (int) $version);
109+
if ($mode === 'create' && !$isNewVersion) {
95110
continue;
96111
}
97112
$page = $this->pageRepository->getById($pageId);
@@ -182,6 +197,11 @@ protected function processPage(string $identifier, array $data, string $mode): v
182197
"Save page %s",
183198
$identifier . ' (' . $page->getId() . ')'
184199
));
200+
201+
}
202+
203+
if ($version) {
204+
$this->versionManagement->setVersion($versionId, (int) $version);
185205
}
186206
}
187207
} catch (NoSuchEntityException $e) {

Model/Config.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CtiDigital\Configurator\Model;
6+
7+
use CtiDigital\Configurator\Api\Data\ConfigInterface;
8+
use Magento\Framework\Model\AbstractModel;
9+
use CtiDigital\Configurator\Model\ResourceModel\ConfigResource;
10+
11+
class Config extends AbstractModel implements ConfigInterface
12+
{
13+
protected $_eventPrefix = 'ctidigital_configurator_config_model';
14+
15+
protected function _construct()
16+
{
17+
$this->_init(ConfigResource::class);
18+
}
19+
20+
public function getName(): string
21+
{
22+
return $this->getData(self::NAME);
23+
}
24+
25+
public function setName(string $name): ConfigInterface
26+
{
27+
return $this->setData(self::NAME, $name);
28+
}
29+
30+
public function getValue(): string
31+
{
32+
return $this->getData(self::VALUE);
33+
}
34+
35+
public function setValue(string $value): ConfigInterface
36+
{
37+
return $this->setData(self::VALUE, $value);
38+
}
39+
}

Model/ConfigRepository.php

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CtiDigital\Configurator\Model;
6+
7+
use CtiDigital\Configurator\Api\ConfigRepositoryInterface;
8+
use CtiDigital\Configurator\Api\Data\ConfigInterface;
9+
use CtiDigital\Configurator\Model\ResourceModel\Config\ConfigCollection;
10+
use CtiDigital\Configurator\Model\ResourceModel\Config\ConfigCollectionFactory;
11+
use CtiDigital\Configurator\Model\ResourceModel\ConfigResource;
12+
use Magento\Framework\Api\SearchCriteria\CollectionProcessor;
13+
use Magento\Framework\Api\SearchCriteriaBuilder;
14+
use Magento\Framework\Api\SearchCriteriaInterface;
15+
16+
class ConfigRepository implements ConfigRepositoryInterface
17+
{
18+
/**
19+
* @param SearchCriteriaBuilder $searchCriteriaBuilder
20+
* @param ConfigCollectionFactory $configCollectionFactory
21+
* @param ConfigResource $configResource
22+
* @param CollectionProcessor $collectionProcessor
23+
*/
24+
public function __construct(
25+
private readonly SearchCriteriaBuilder $searchCriteriaBuilder,
26+
private readonly ConfigCollectionFactory $configCollectionFactory,
27+
private readonly ConfigResource $configResource,
28+
private readonly CollectionProcessor $collectionProcessor
29+
) {
30+
}
31+
32+
/**
33+
* @param SearchCriteriaInterface $searchCriteria
34+
* @return ConfigCollection
35+
*/
36+
public function getCollection(SearchCriteriaInterface $searchCriteria): ConfigCollection
37+
{
38+
$collection = $this->configCollectionFactory->create();
39+
$this->collectionProcessor->process($searchCriteria, $collection);
40+
41+
return $collection;
42+
}
43+
44+
/**
45+
* @param string $name
46+
* @return ConfigInterface|null
47+
*/
48+
public function getConfig(string $name): ?ConfigInterface
49+
{
50+
$collection = $this->getCollection($this->searchCriteriaBuilder
51+
->addFilter(ConfigInterface::NAME, $name)->create());
52+
53+
return $collection->getFirstItem();
54+
}
55+
56+
/**
57+
* @param ConfigInterface $config
58+
* @return int
59+
* @throws \Magento\Framework\Exception\AlreadyExistsException
60+
*/
61+
public function save(ConfigInterface $config): int
62+
{
63+
$this->configResource->save($config);
64+
65+
return (int) $config->getId();
66+
}
67+
68+
/**
69+
* @param ConfigInterface $config
70+
* @return void
71+
* @throws \Exception
72+
*/
73+
public function delete(ConfigInterface $config): void
74+
{
75+
$this->configResource->delete($config);
76+
}
77+
}

Model/Processor.php

100755100644
File mode changed.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CtiDigital\Configurator\Model\ResourceModel\Config;
6+
7+
use CtiDigital\Configurator\Model\Config;
8+
use CtiDigital\Configurator\Model\ResourceModel\ConfigResource;
9+
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
10+
11+
class ConfigCollection extends AbstractCollection
12+
{
13+
/**
14+
* @var string
15+
*/
16+
protected $_eventPrefix = 'ctidigital_configurator_config_collection';
17+
18+
/**
19+
* Initialize collection model.
20+
*/
21+
protected function _construct()
22+
{
23+
$this->_init(Config::class, ConfigResource::class);
24+
}
25+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CtiDigital\Configurator\Model\ResourceModel;
6+
7+
use CtiDigital\Configurator\Api\Data\ConfigInterface;
8+
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
9+
10+
class ConfigResource extends AbstractDb
11+
{
12+
/**
13+
* @var string
14+
*/
15+
protected string $_eventPrefix = 'ctidigital_configurator_config_resource_model';
16+
17+
/**
18+
* Initialize resource model.
19+
*/
20+
protected function _construct()
21+
{
22+
$this->_init('configurator_config', ConfigInterface::ID);
23+
$this->_useIsObjectNew = true;
24+
}
25+
}

0 commit comments

Comments
 (0)