|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Shopware\Core\System\Snippet\Subscriber; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
|
6
|
|
|
use Shopware\Core\Defaults; |
|
7
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult; |
|
8
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent; |
|
9
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent; |
|
10
|
|
|
use Shopware\Core\Framework\Uuid\Uuid; |
|
11
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
12
|
|
|
|
|
13
|
|
|
class CustomFieldSubscriber implements EventSubscriberInterface |
|
14
|
|
|
{ |
|
15
|
|
|
private const CUSTOM_FIELD_ID_FIELD = 'custom_field_id'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var Connection |
|
19
|
|
|
*/ |
|
20
|
|
|
private $connection; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(Connection $connection) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->connection = $connection; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public static function getSubscribedEvents(): array |
|
28
|
|
|
{ |
|
29
|
|
|
return [ |
|
30
|
|
|
'custom_field.written' => 'customFieldIsWritten', |
|
31
|
|
|
'custom_field.deleted' => 'customFieldIsDeleted', |
|
32
|
|
|
]; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function customFieldIsWritten(EntityWrittenEvent $event): void |
|
36
|
|
|
{ |
|
37
|
|
|
$snippets = []; |
|
38
|
|
|
$snippetSets = null; |
|
39
|
|
|
foreach ($event->getWriteResults() as $writeResult) { |
|
40
|
|
|
if (!isset($writeResult->getPayload()['config']['label']) || empty($writeResult->getPayload()['config']['label'])) { |
|
41
|
|
|
continue; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ($writeResult->getOperation() === EntityWriteResult::OPERATION_INSERT) { |
|
45
|
|
|
if ($snippetSets === null) { |
|
46
|
|
|
$snippetSets = $this->connection->fetchAll('SELECT id, iso FROM snippet_set'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (empty($snippetSets)) { |
|
50
|
|
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$this->setInsertSnippets($writeResult, $snippetSets, $snippets); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (empty($snippets)) { |
|
58
|
|
|
return; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
foreach ($snippets as $snippet) { |
|
62
|
|
|
$this->connection->executeUpdate( |
|
63
|
|
|
'INSERT INTO snippet (`id`, `snippet_set_id`, `translation_key`, `value`, `author`, `custom_fields`, `created_at`) |
|
64
|
|
|
VALUES (:id, :setId, :translationKey, :value, :author, :customFields, :createdAt) |
|
65
|
|
|
ON DUPLICATE KEY UPDATE `value` = :value', |
|
66
|
|
|
$snippet |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function customFieldIsDeleted(EntityDeletedEvent $event): void |
|
72
|
|
|
{ |
|
73
|
|
|
$this->connection->executeUpdate( |
|
74
|
|
|
'DELETE FROM `snippet` |
|
75
|
|
|
WHERE JSON_EXTRACT(`custom_fields`, "$.custom_field_id") IN (:customFieldIds)', |
|
76
|
|
|
['customFieldIds' => $event->getIds()], |
|
77
|
|
|
['customFieldIds' => Connection::PARAM_STR_ARRAY] |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function setInsertSnippets(EntityWriteResult $writeResult, array $snippetSets, array &$snippets): void |
|
82
|
|
|
{ |
|
83
|
|
|
$name = $writeResult->getPayload()['name']; |
|
84
|
|
|
$labels = $writeResult->getPayload()['config']['label']; |
|
85
|
|
|
|
|
86
|
|
|
foreach ($snippetSets as $snippetSet) { |
|
87
|
|
|
$label = $name; |
|
88
|
|
|
$iso = $snippetSet['iso']; |
|
89
|
|
|
|
|
90
|
|
|
if (isset($labels[$iso])) { |
|
91
|
|
|
$label = $labels[$iso]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$snippets[] = [ |
|
95
|
|
|
'id' => Uuid::randomBytes(), |
|
96
|
|
|
'setId' => $snippetSet['id'], |
|
97
|
|
|
'translationKey' => 'customFields.' . $name, |
|
98
|
|
|
'value' => $label, |
|
99
|
|
|
'author' => 'System', |
|
100
|
|
|
'customFields' => json_encode([ |
|
101
|
|
|
self::CUSTOM_FIELD_ID_FIELD => $writeResult->getPrimaryKey(), |
|
102
|
|
|
]), |
|
103
|
|
|
'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT), |
|
104
|
|
|
]; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|