This repository has been archived by the owner on Jun 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BucoPartDocuments.php
126 lines (105 loc) · 3.85 KB
/
BucoPartDocuments.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace BucoPartDocuments;
use Shopware\Bundle\AttributeBundle\Service\TypeMapping;
use Shopware\Components\Plugin;
use Shopware\Components\Plugin\Context\ActivateContext;
use Shopware\Components\Plugin\Context\DeactivateContext;
use Shopware\Components\Plugin\Context\InstallContext;
use Shopware\Components\Plugin\Context\UninstallContext;
use Shopware\Models\Attribute\Document;
class BucoPartDocuments extends Plugin {
const ATTRIBUTES = [
Document::class => ['buco_is_part_document', 'buco_part_document_legacy_id'],
];
const CACHE_LIST = [
InstallContext::CACHE_TAG_TEMPLATE,
InstallContext::CACHE_TAG_PROXY,
InstallContext::CACHE_TAG_CONFIG,
InstallContext::CACHE_TAG_ROUTER
];
public function install(InstallContext $context)
{
$this->createAttributes();
$this->addAcl();
$context->scheduleClearCache(self::CACHE_LIST);
}
public function activate(ActivateContext $context)
{
$context->scheduleClearCache([InstallContext::CACHE_TAG_TEMPLATE]);
}
public function deactivate(DeactivateContext $context)
{
$context->scheduleClearCache([InstallContext::CACHE_TAG_TEMPLATE]);
}
public function uninstall(UninstallContext $context)
{
if(!$context->keepUserData()) {
$this->removeAttributes();
$this->removeAcl();
}
$context->scheduleClearCache(self::CACHE_LIST);
}
private function createAttributes()
{
/** @var \Shopware\Bundle\AttributeBundle\Service\CrudService $crudService */
$crudService = $this->container->get('shopware_attribute.crud_service');
$em = $this->container->get('models');
$crudService->update(
$em->getClassMetadata(Document::class)->getTableName(),
'buco_is_part_document',
TypeMapping::TYPE_BOOLEAN,
[
'label' => 'Ist Teildokument?',
'helpText' => 'Dieses Dokument enthält nur ausgewählte Positionen der Bestellung.',
'displayInBackend' => true,
]);
$crudService->update(
$em->getClassMetadata(Document::class)->getTableName(),
'buco_part_document_legacy_id',
TypeMapping::TYPE_INTEGER,
[
'label' => 'Teildokumente Legacy ID',
'helpText' => 'ID dieses Teildokumentes vor der Datenmigration aus dem mbdus Plugin.',
'displayInBackend' => false,
]);
}
private function removeAttributes()
{
$crudService = $this->container->get('shopware_attribute.crud_service');
$em = $this->container->get('models');
foreach (self::ATTRIBUTES as $modelClass => $attributes) {
foreach ($attributes as $attribute) {
try {
$crudService->delete($em->getClassMetadata($modelClass)->getTableName(), $attribute);
}
catch (\Exception $e) {
// ignore
}
}
}
}
private function addAcl()
{
$aclService = $this->container->get('acl');
$em = $this->container->get('models');
/** @var \Shopware\Models\Plugin\Plugin $pluginModel */
$pluginModel = $em->getRepository(\Shopware\Models\Plugin\Plugin::class)->findOneBy(['name' => $this->getName()]);
try {
$aclService->createResource(
'bucopartdocumentsmigration',
['migrate'],
null,
$pluginModel ? $pluginModel->getId() : null
);
}
catch (\Exception $e) {}
}
private function removeAcl()
{
$aclService = $this->container->get('acl');
try {
$aclService->deleteResource('bucopartdocumentsmigration');
}
catch (\Exception $e) {}
}
}